Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathapp.cpp
More file actions
1336 lines (1157 loc) · 45 KB
/
Copy pathapp.cpp
File metadata and controls
1336 lines (1157 loc) · 45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
* http://www.gnu.org/licenses/gpl-3.0.html
*
* $Revision$
* $Id$
* $HeadURL$
*/
#include <sdk.h>
#include "app.h"
#include <wx/arrstr.h>
#include <wx/fs_zip.h>
#include <wx/fs_mem.h>
#include <wx/xrc/xmlres.h>
#include <wx/cmdline.h>
#include <wx/regex.h>
#include <wx/filefn.h>
#include <wx/log.h> // for wxSafeShowMessage()
#include <wx/msgdlg.h>
#include <wx/choicdlg.h>
#include <wx/notebook.h>
#include <wx/clipbrd.h>
#include <wx/debugrpt.h>
#include <wx/ipc.h>
#include <wx/msgout.h>
#include <cbexception.h>
#include <configmanager.h>
#include <editormanager.h>
#include <globals.h>
#include <loggers.h>
#include <logmanager.h>
#include <manager.h>
#include <personalitymanager.h>
#include <pluginmanager.h>
#include <projectmanager.h>
#include <scriptingmanager.h>
#include <sdk_events.h>
#include <sqplus.h>
#include "appglobals.h"
#include "associations.h"
#include "cbauibook.h"
#include "cbstyledtextctrl.h"
#include "crashhandler.h"
#include "projectmanagerui.h"
#include "splashscreen.h"
#ifndef __WXMSW__
#include "prefix.h" // binreloc
#endif
#if defined(__APPLE__) && defined(__MACH__)
#include <sys/param.h>
#include <mach-o/dyld.h>
#endif
#ifndef CB_PRECOMP
#include <wx/dir.h>
#include "xtra_res.h"
#include "filemanager.h" // LoaderBase
#endif
#ifndef APP_PREFIX
#define APP_PREFIX ""
#endif
#ifndef __WXMAC__
inline wxString GetResourcesDir(){ return wxEmptyString; }
#endif
namespace
{
bool s_Loading = false;
class DDEServer : public wxServer
{
public:
DDEServer(MainFrame* frame) : m_Frame(frame) { ; }
wxConnectionBase *OnAcceptConnection(const wxString& topic);
MainFrame* GetFrame() { return m_Frame; }
void SetFrame(MainFrame* frame) { m_Frame = frame; }
private:
MainFrame* m_Frame;
};
class DDEConnection : public wxConnection
{
public:
DDEConnection(MainFrame* frame) : m_Frame(frame) { ; }
#if wxCHECK_VERSION(2, 9, 5)
bool OnExecute(const wxString& topic, const void *data, size_t size, wxIPCFormat format);
#else
bool OnExecute(const wxString& topic, wxChar *data, int size, wxIPCFormat format);
#endif
bool OnDisconnect();
private:
MainFrame* m_Frame;
};
wxConnectionBase* DDEServer::OnAcceptConnection(const wxString& topic)
{
return topic == DDE_TOPIC ? new DDEConnection(m_Frame) : nullptr;
}
#if wxCHECK_VERSION(2, 9, 5)
bool DDEConnection::OnExecute(cb_unused const wxString& topic, const void *data, cb_unused size_t size, cb_unused wxIPCFormat format)
#else
bool DDEConnection::OnExecute(cb_unused const wxString& topic, wxChar *data, cb_unused int size, cb_unused wxIPCFormat format)
#endif
{
wxString strData((wxChar*)data);
if (strData.StartsWith(_T("[IfExec_Open(\"")))
return false; // let Shell Open handle the request as we *know* that we have registered the Shell Open command, too
if (strData.StartsWith(_T("[Open(\"")))
{
wxRegEx reCmd(_T("\"(.*)\""));
if (reCmd.Matches(strData))
{
const wxString file = reCmd.GetMatch(strData, 1);
// always put files in the delayed queue, the will either be loaded in OnDisconnect, or after creating of MainFrame
// if we open the files directly it can lead to an applicaton hang (at least when opening C::B's project-file on linux)
CodeBlocksApp* cb = (CodeBlocksApp*)wxTheApp;
if (cb)
cb->AddFileToOpenDelayed(file);
}
return true;
}
else if (strData.StartsWith(_T("[OpenLine(\"")))
{
wxRegEx reCmd(_T("\"(.*)\""));
if (reCmd.Matches(strData))
{
wxString file = reCmd.GetMatch(strData, 1);
CodeBlocksApp* cb = (CodeBlocksApp*)wxTheApp;
cb->SetAutoFile(file);
}
return true;
}
else if (strData.StartsWith(_T("[Raise]")))
{
if (m_Frame)
{
if (m_Frame->IsIconized())
m_Frame->Iconize(false);
m_Frame->Raise();
}
return true;
}
else if (strData.StartsWith(_T("[CmdLine({")))
{
int pos = strData.Find(_T("})]"));
if (pos!=wxNOT_FOUND)
{
wxString line = strData.Mid(10, pos-10);
line.Replace(_T("\\)"), _T(")"));
line.Replace(_T("\\("), _T("("));
CodeBlocksApp* cb = (CodeBlocksApp*)wxTheApp;
if (m_Frame && !line.empty())
{
// Manager::Get()->GetLogManager()->Log(wxString::Format(_T("DDEConnection::OnExecute line = ") + line));
cb->ParseCmdLine(m_Frame, line);
CodeBlocksEvent event(cbEVT_APP_CMDLINE);
Manager::Get()->ProcessEvent(event);
}
}
return true;
}
wxSafeShowMessage(wxT("Warning"),wxString::Format(wxT("DDE topic %s not handled."),strData.wx_str()));
return false;
}
bool DDEConnection::OnDisconnect()
{
// delayed files will be loaded automatically if MainFrame already exists,
// otherwise it happens automatically in OnInit after MainFrame is created
if (!s_Loading && m_Frame)
{
CodeBlocksApp* cb = (CodeBlocksApp*)wxTheApp;
cb->LoadDelayedFiles(m_Frame);
}
return true;
}
DDEServer* g_DDEServer = nullptr;
class DDEClient: public wxClient {
public:
DDEClient(void) {}
wxConnectionBase *OnMakeConnection(void) { return new DDEConnection(nullptr); }
};
#if wxUSE_CMDLINE_PARSER
#if wxCHECK_VERSION(2, 9, 0)
#define CMD_ENTRY(X) X
#else
#define CMD_ENTRY(X) _T(X)
#endif
const wxCmdLineEntryDesc cmdLineDesc[] =
{
{ wxCMD_LINE_SWITCH, CMD_ENTRY("h"), CMD_ENTRY("help"), CMD_ENTRY("show this help message"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
{ wxCMD_LINE_SWITCH, CMD_ENTRY("?"), CMD_ENTRY("?"), CMD_ENTRY("show this help message (alias for help)"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
{ wxCMD_LINE_SWITCH, CMD_ENTRY(""), CMD_ENTRY("safe-mode"), CMD_ENTRY("load in safe mode (all plugins will be disabled)"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
#ifdef __WXMSW__
{ wxCMD_LINE_SWITCH, CMD_ENTRY("na"), CMD_ENTRY("no-check-associations"), CMD_ENTRY("don't perform any association checks"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_SWITCH, CMD_ENTRY("nd"), CMD_ENTRY("no-dde"), CMD_ENTRY("don't start a DDE server"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
#endif
#ifndef __WXMSW__
{ wxCMD_LINE_SWITCH, CMD_ENTRY("ni"), CMD_ENTRY("no-ipc"), CMD_ENTRY("don't start an IPC server"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
#endif
{ wxCMD_LINE_SWITCH, CMD_ENTRY("ns"), CMD_ENTRY("no-splash-screen"), CMD_ENTRY("don't display a splash screen while loading"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_SWITCH, CMD_ENTRY(""), CMD_ENTRY("multiple-instance"), CMD_ENTRY("allow running multiple instances"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_SWITCH, CMD_ENTRY("d"), CMD_ENTRY("debug-log"), CMD_ENTRY("display application's debug log"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_SWITCH, CMD_ENTRY("nc"), CMD_ENTRY("no-crash-handler"), CMD_ENTRY("don't use the crash handler (useful for debugging C::B)"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_SWITCH, CMD_ENTRY("v"), CMD_ENTRY("verbose"), CMD_ENTRY("show more debugging messages"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_OPTION, CMD_ENTRY(""), CMD_ENTRY("prefix"), CMD_ENTRY("the shared data dir prefix"),
wxCMD_LINE_VAL_STRING, wxCMD_LINE_NEEDS_SEPARATOR },
{ wxCMD_LINE_OPTION, CMD_ENTRY(""), CMD_ENTRY("user-data-dir"), CMD_ENTRY("set a custom location for user settings and plugins"),
wxCMD_LINE_VAL_STRING, wxCMD_LINE_NEEDS_SEPARATOR },
{ wxCMD_LINE_OPTION, CMD_ENTRY("p"), CMD_ENTRY("personality"), CMD_ENTRY("the personality to use: \"ask\" or <personality-name>"),
wxCMD_LINE_VAL_STRING, wxCMD_LINE_NEEDS_SEPARATOR },
{ wxCMD_LINE_SWITCH, CMD_ENTRY(""), CMD_ENTRY("no-log"), CMD_ENTRY("turn off the application log"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_SWITCH, CMD_ENTRY(""), CMD_ENTRY("log-to-file"), CMD_ENTRY("redirect application log to a file"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_SWITCH, CMD_ENTRY(""), CMD_ENTRY("debug-log-to-file"), CMD_ENTRY("redirect application debug log to a file"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_OPTION, CMD_ENTRY(""), CMD_ENTRY("profile"), CMD_ENTRY("synonym to personality"),
wxCMD_LINE_VAL_STRING, wxCMD_LINE_NEEDS_SEPARATOR },
{ wxCMD_LINE_SWITCH, CMD_ENTRY(""), CMD_ENTRY("rebuild"), CMD_ENTRY("clean and then build the project/workspace"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_SWITCH, CMD_ENTRY(""), CMD_ENTRY("build"), CMD_ENTRY("just build the project/workspace"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_SWITCH, CMD_ENTRY(""), CMD_ENTRY("clean"), CMD_ENTRY("clean the project/workspace"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_OPTION, CMD_ENTRY(""), CMD_ENTRY("target"), CMD_ENTRY("the target for the batch build"),
wxCMD_LINE_VAL_STRING, wxCMD_LINE_NEEDS_SEPARATOR },
{ wxCMD_LINE_SWITCH, CMD_ENTRY(""), CMD_ENTRY("no-batch-window-close"), CMD_ENTRY("do not auto-close log window when batch build is done"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_SWITCH, CMD_ENTRY(""), CMD_ENTRY("batch-build-notify"), CMD_ENTRY("show message when batch build is done"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_OPTION, CMD_ENTRY(""), CMD_ENTRY("script"), CMD_ENTRY("execute script file"),
wxCMD_LINE_VAL_STRING, wxCMD_LINE_NEEDS_SEPARATOR },
{ wxCMD_LINE_OPTION, CMD_ENTRY(""), CMD_ENTRY("file"), CMD_ENTRY("open file and optionally jump to specific line (file[:line])"),
wxCMD_LINE_VAL_STRING, wxCMD_LINE_NEEDS_SEPARATOR },
{ wxCMD_LINE_PARAM, CMD_ENTRY(""), CMD_ENTRY(""), CMD_ENTRY("filename(s)"),
wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
{ wxCMD_LINE_NONE }
};
#endif // wxUSE_CMDLINE_PARSER
class Splash
{
public:
Splash(const bool show) : m_pSplash(nullptr)
{
if (show)
{
wxBitmap bmp = cbLoadBitmap(ConfigManager::ReadDataPath() + _T("/images/splash_1312.png"));
m_pSplash = new cbSplashScreen(bmp, -1, nullptr, -1, wxNO_BORDER | wxFRAME_NO_TASKBAR | wxFRAME_SHAPED);
Manager::Yield();
}
}
~Splash()
{
Hide();
}
void Hide()
{
if (m_pSplash)
{
m_pSplash->Destroy();
m_pSplash = nullptr;
}
}
private:
cbSplashScreen* m_pSplash;
};
class cbMessageOutputNull : public wxMessageOutput
{
public:
#if wxCHECK_VERSION(3,0,0)
virtual void Output(const wxString &str);
#else
#ifdef WX_ATTRIBUTE_PRINTF
virtual void Printf(const wxChar* format, ...) WX_ATTRIBUTE_PRINTF_2;
#else
virtual void Printf(const wxChar* format, ...) ATTRIBUTE_PRINTF_2;
#endif
#endif // wxCHECK_VERSION
};
#if wxCHECK_VERSION(3,0,0)
void cbMessageOutputNull::Output(cb_unused const wxString &str){}
#else
void cbMessageOutputNull::Printf(cb_unused const wxChar* format, ...){}
#endif
} // namespace
IMPLEMENT_APP(CodeBlocksApp) // TODO: This gives a "redundant declaration" warning, though I think it's false. Dig through macro and check.
BEGIN_EVENT_TABLE(CodeBlocksApp, wxApp)
EVT_ACTIVATE_APP(CodeBlocksApp::OnAppActivate)
EVT_TASKBAR_LEFT_DOWN(CodeBlocksApp::OnTBIconLeftDown)
END_EVENT_TABLE()
#ifdef __WXMAC__
#if wxCHECK_VERSION(2,9,0)
#include "wx/osx/core/cfstring.h"
#else
#include "wx/mac/corefoundation/cfstring.h"
#endif
#include "wx/intl.h"
#include <CoreFoundation/CFBundle.h>
#include <CoreFoundation/CFURL.h>
// returns e.g. "/Applications/appname.app/Contents/Resources" if application is bundled,
// or the directory of the binary, e.g. "/usr/local/bin/appname", if it is *not* bundled.
static wxString GetResourcesDir()
{
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
CFURLRef absoluteURL = CFURLCopyAbsoluteURL(resourcesURL); // relative -> absolute
CFRelease(resourcesURL);
CFStringRef cfStrPath = CFURLCopyFileSystemPath(absoluteURL,kCFURLPOSIXPathStyle);
CFRelease(absoluteURL);
#if wxCHECK_VERSION(2,9,0)
return wxCFStringRef(cfStrPath).AsString(wxLocale::GetSystemEncoding());
#else
return wxMacCFStringHolder(cfStrPath).AsString(wxLocale::GetSystemEncoding());
#endif
}
#endif
bool CodeBlocksApp::LoadConfig()
{
if (m_UserDataDir!=wxEmptyString)
{
// if --user-data-dir=path was specified we tell
//ConfigManager (and CfgMgrBldr) about it, which will propagate
//it through the app and plugins
if ( !ConfigManager::SetUserDataFolder(m_UserDataDir) )
return false;
}
ConfigManager *cfg = Manager::Get()->GetConfigManager(_T("app"));
wxString data(wxT(APP_PREFIX));
if (platform::windows)
data.assign(GetAppPath());
else if (platform::macosx)
{
data.assign(GetResourcesDir()); // CodeBlocks.app/Contents/Resources
if (!data.Contains(wxString(_T("/Resources")))) // not a bundle, use relative path
data = GetAppPath() + _T("/..");
}
if (data.IsEmpty())
{
data.assign(GetAppPath()); // fallback
data.Replace(_T("/bin"),_T(""));
}
if (!m_Prefix.IsEmpty()) // --prefix command line switch overrides builtin value
data = m_Prefix;
else // also, check for environment
{
wxString env;
wxGetEnv(_T("CODEBLOCKS_DATA_DIR"), &env);
if (!env.IsEmpty())
data = env;
}
data.append(_T("/share/codeblocks"));
cfg->Write(_T("data_path"), data);
//m_HasDebugLog = Manager::Get()->GetConfigManager(_T("message_manager"))->ReadBool(_T("/has_debug_log"), false) || m_HasDebugLog;
//Manager::Get()->GetConfigManager(_T("message_manager"))->Write(_T("/has_debug_log"), m_HasDebugLog);
return true;
}
void CodeBlocksApp::InitAssociations()
{
#ifdef __WXMSW__
ConfigManager *cfg = Manager::Get()->GetConfigManager(_T("app"));
if (m_Assocs && cfg->ReadBool(_T("/environment/check_associations"), true))
{
if (!Associations::Check())
{
AskAssocDialog dlg(Manager::Get()->GetAppWindow());
PlaceWindow(&dlg);
switch(dlg.ShowModal())
{
case ASC_ASSOC_DLG_NO_DONT_ASK:
cfg->Write(_T("/environment/check_associations"), false);
break;
case ASC_ASSOC_DLG_NO_ONLY_NOW:
break;
case ASC_ASSOC_DLG_YES_C_FILES:
Associations::SetCore();
break;
case ASC_ASSOC_DLG_YES_ALL_FILES:
Associations::SetAll();
break;
default:
break;
};
}
}
#endif
}
void CodeBlocksApp::InitDebugConsole()
{
#ifdef __WXMSW__
#ifdef __CBDEBUG__
// Remember to compile as a console application!
AllocConsole();
HANDLE myhandle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD co = {80,2000};
SetConsoleScreenBufferSize(myhandle, co);
fprintf(stdout,"CONSOLE DEBUG ACTIVATED\n");
// wxLogWindow *myerr = new wxLogWindow(NULL,"debug");
#endif
#endif
}
void CodeBlocksApp::InitExceptionHandler()
{
#ifdef __WXMSW__
m_ExceptionHandlerLib = LoadLibrary(_T("exchndl.dll"));
#endif
}
bool CodeBlocksApp::InitXRCStuff()
{
if ( !Manager::LoadResource(_T("resources.zip")) )
{
wxString msg;
msg.Printf(_T("Cannot find resources...\n"
"%s was configured to be installed in '%s'.\n"
"Please use the command-line switch '--prefix' or "
"set the CODEBLOCKS_DATA_DIR environment variable "
"to point where %s is installed,\n"
"or try re-installing the application..."),
appglobals::AppName.wx_str(),
ConfigManager::ReadDataPath().wx_str(),
appglobals::AppName.wx_str());
cbMessageBox(msg);
return false;
}
return true;
}
MainFrame* CodeBlocksApp::InitFrame()
{
CompileTimeAssertion<wxMinimumVersion<2,8,9>::eval>::Assert();
MainFrame *frame = new MainFrame();
wxUpdateUIEvent::SetUpdateInterval(100);
SetTopWindow(nullptr);
if (g_DDEServer && m_DDE)
g_DDEServer->SetFrame(frame); // Set m_Frame in DDE-Server
return frame;
}
void CodeBlocksApp::CheckVersion()
{
// This is a remnant from early 2006 (Windows only), but keep the revision tag for possible future use
ConfigManager *cfg = Manager::Get()->GetConfigManager(_T("app"));
if (cfg->Read(_T("version")) != appglobals::AppActualVersion)
cfg->Write(_T("version"), appglobals::AppActualVersion);
}
void CodeBlocksApp::InitLocale()
{
ConfigManager* cfg = Manager::Get()->GetConfigManager(_T("app"));
wxString path(ConfigManager::GetDataFolder() + _T("/locale"));
if (cfg->ReadBool(_T("/locale/enable"), false) == false)
return;
wxString lang(cfg->Read(_T("/locale/language")));
wxLocale::AddCatalogLookupPathPrefix(path);
const wxLanguageInfo *info;
if (!lang.IsEmpty()) // Note: You can also write this line of code as !(!lang) from wx-2.9 onwards
info = wxLocale::FindLanguageInfo(lang);
else
info = wxLocale::GetLanguageInfo(wxLANGUAGE_DEFAULT);
if (info == nullptr) // should never happen, but who knows...
return;
m_locale.Init(info->Language);
path.Alloc(path.length() + 10);
path.Append(_T('/'));
path.Append(info->CanonicalName);
if ( !wxDirExists(path) )
return;
wxDir dir(path);
if (!dir.IsOpened())
return;
wxString moName;
if (dir.GetFirst(&moName, _T("*.mo"), wxDIR_FILES))
{
do
{
m_locale.AddCatalog(moName);
} while (dir.GetNext(&moName));
}
}
bool CodeBlocksApp::OnInit()
{
#ifdef __WXMSW__
InitCommonControls();
#endif
wxLog::EnableLogging(true);
SetAppName(_T("codeblocks"));
s_Loading = true;
m_pBatchBuildDialog = nullptr;
m_BatchExitCode = 0;
m_Batch = false;
m_BatchNotify = false;
m_Build = false;
m_ReBuild = false;
m_Clean = false;
m_HasProject = false;
m_HasWorkSpace = false;
m_SafeMode = false;
m_BatchWindowAutoClose = true;
wxTheClipboard->Flush();
wxCmdLineParser& parser = *Manager::GetCmdLineParser();
parser.SetDesc(cmdLineDesc);
// NOTE: crash handler explicitly disabled because it causes problems
// with plugins loading/unloading...
//
// static CrashHandler crash_handler(!m_CrashHandler);
// we'll do this once and for all at startup
wxFileSystem::AddHandler(new wxZipFSHandler);
wxFileSystem::AddHandler(new wxMemoryFSHandler);
wxXmlResource::Get()->InsertHandler(new wxToolBarAddOnXmlHandler);
wxXmlResource::Get()->InsertHandler(new wxScrollingDialogXmlHandler);
wxInitAllImageHandlers();
wxXmlResource::Get()->InitAllHandlers();
Manager::Get()->GetLogManager()->Log(F(wxT("Starting ") + appglobals::AppName + wxT(" ") +
appglobals::AppActualVersionVerb + wxT(" ") +
appglobals::AppBuildTimestamp));
try
{
#if (wxUSE_ON_FATAL_EXCEPTION == 1)
wxHandleFatalExceptions(true);
#endif
InitExceptionHandler();
delete wxMessageOutput::Set(new cbMessageOutputNull); // No output. (suppress warnings about unknown options from plugins)
if (ParseCmdLine(nullptr) == -1) // only abort if '--help' was passed in the command line
{
delete wxMessageOutput::Set(new wxMessageOutputMessageBox);
parser.Usage();
return false;
}
if ( !LoadConfig() )
return false;
// set safe-mode appropriately
PluginManager::SetSafeMode(m_SafeMode);
// If not in batch mode, and no startup-script defined, initialise XRC
if(!m_Batch && m_Script.IsEmpty() && !InitXRCStuff())
return false;
InitLocale();
if (m_DDE && !m_Batch && Manager::Get()->GetConfigManager(_T("app"))->ReadBool(_T("/environment/use_ipc"), true))
{
// Create a new client
DDEClient *client = new DDEClient;
DDEConnection* connection = nullptr;
wxLogNull ln; // own error checking implemented -> avoid debug warnings
connection = (DDEConnection *)client->MakeConnection(_T("localhost"), F(DDE_SERVICE, wxGetUserId().wx_str()), DDE_TOPIC);
if (connection)
{
// don't eval here just forward the whole command line to the other instance
wxString cmdLine;
for (int i = 1 ; i < argc; ++i)
cmdLine += wxString(argv[i]) + _T(' ');
if ( !cmdLine.IsEmpty() )
{
// escape openings and closings so it is easily possible to find the end on the rx side
cmdLine.Replace(_T("("), _T("\\("));
cmdLine.Replace(_T(")"), _T("\\)"));
connection->Execute(_T("[CmdLine({") + cmdLine + _T("})]"));
}
// On Linux, C::B has to be raised explicitely if it's wanted
if (Manager::Get()->GetConfigManager(_T("app"))->ReadBool(_T("/environment/raise_via_ipc"), true))
connection->Execute(_T("[Raise]"));
connection->Disconnect();
delete connection;
delete client;
// return false to end the application
return false;
}
// free memory DDE-/IPC-clients, if we are here connection could not be established and there is no need to free it
delete client;
}
// Now we can start the DDE-/IPC-Server, if we did it earlier we would connect to ourselves
if (m_DDE && !m_Batch)
{
g_DDEServer = new DDEServer(nullptr);
g_DDEServer->Create(F(DDE_SERVICE, wxGetUserId().wx_str()));
}
m_pSingleInstance = nullptr;
if ( Manager::Get()->GetConfigManager(_T("app"))->ReadBool(_T("/environment/single_instance"), true)
&& !parser.Found(_T("multiple-instance")) )
{
const wxString name = wxString::Format(_T("Code::Blocks-%s"), wxGetUserId().wx_str());
m_pSingleInstance = new wxSingleInstanceChecker(name, ConfigManager::GetTempFolder());
if (m_pSingleInstance->IsAnotherRunning())
{
/* NOTE: Due to a recent change in logging code, this visual warning got disabled.
So the wxLogError() has been changed to a cbMessageBox(). */
cbMessageBox(_("Another program instance is already running.\nCode::Blocks is currently configured to only allow one running instance.\n\nYou can access this Setting under the menu item 'Environment'."),
_T("Code::Blocks"), wxOK | wxICON_ERROR);
return false;
}
}
// Splash screen moved to this place, otherwise it would be short visible, even if we only pass filenames via DDE/IPC
// we also don't need it, if only a single instance is allowed
Splash splash(!m_Batch && m_Script.IsEmpty() && m_Splash &&
Manager::Get()->GetConfigManager(_T("app"))->ReadBool(_T("/environment/show_splash"), true));
InitDebugConsole();
Manager::SetBatchBuild(m_Batch || !m_Script.IsEmpty());
Manager::Get()->GetScriptingManager();
MainFrame* frame = nullptr;
frame = InitFrame();
m_Frame = frame;
// plugins loaded -> check command line arguments again
delete wxMessageOutput::Set(new wxMessageOutputBest); // warn about unknown options
if ( ParseCmdLine(m_Frame) == 0 )
{
if (Manager::Get()->GetConfigManager(_T("app"))->ReadBool(_T("/environment/blank_workspace"), true) == false)
Manager::Get()->GetProjectManager()->LoadWorkspace();
}
if (m_SafeMode) wxLog::EnableLogging(true); // re-enable logging in safe-mode
if (m_Batch)
{
Manager::SetAppStartedUp(true);
// the compiler plugin might be waiting for this
CodeBlocksEvent event(cbEVT_APP_STARTUP_DONE);
Manager::Get()->ProcessEvent(event);
Manager::Get()->RegisterEventSink(cbEVT_COMPILER_FINISHED, new cbEventFunctor<CodeBlocksApp, CodeBlocksEvent>(this, &CodeBlocksApp::OnBatchBuildDone));
s_Loading = false;
LoadDelayedFiles(frame);
BatchJob();
frame->Close();
return true;
}
if (!m_Script.IsEmpty())
{
s_Loading = false;
LoaderBase* loader = Manager::Get()->GetFileManager()->Load(m_Script);
if (loader->GetData())
Manager::Get()->GetScriptingManager()->LoadBuffer(cbC2U(loader->GetData()));
delete loader;
frame->Close();
return true;
}
CheckVersion();
// run startup script
try
{
wxString startup = ConfigManager::LocateDataFile(_T("startup.script"), sdScriptsUser | sdScriptsGlobal);
if (!startup.IsEmpty())
Manager::Get()->GetScriptingManager()->LoadScript(startup);
}
catch (SquirrelError& exception)
{
Manager::Get()->GetScriptingManager()->DisplayErrors(&exception);
}
Manager::ProcessPendingEvents();
// finally, show the app
splash.Hide();
SetTopWindow(frame);
frame->Show();
frame->StartupDone();
frame->ShowTips(); // this func checks if the user wants tips, so no need to check here
if (platform::windows)
InitAssociations();
s_Loading = false;
LoadDelayedFiles(frame);
Manager::Get()->GetProjectManager()->WorkspaceChanged();
// all done
Manager::SetAppStartedUp(true);
CodeBlocksEvent event(cbEVT_APP_STARTUP_DONE);
Manager::Get()->ProcessEvent(event);
return true;
}
catch (cbException& exception)
{
exception.ShowErrorMessage();
}
catch (SquirrelError& exception)
{
Manager::Get()->GetScriptingManager()->DisplayErrors(&exception);
}
catch (const char* message)
{
wxSafeShowMessage(_T("Exception"), cbC2U(message));
}
catch (...)
{
wxSafeShowMessage(_T("Exception"), _T("Unknown exception was raised. The application will terminate immediately..."));
}
// if we reached here, return error
return false;
}
int CodeBlocksApp::OnExit()
{
wxTheClipboard->Flush();
if (g_DDEServer) delete g_DDEServer;
#ifdef __WXMSW__
if (m_ExceptionHandlerLib)
FreeLibrary(m_ExceptionHandlerLib);
#endif
if (m_pSingleInstance)
delete m_pSingleInstance;
// ultimate shutdown...
Manager::Free();
// WX docs say that this function's return value is ignored,
// but we return our value anyway. It might not be ignored at some point...
return m_Batch ? m_BatchExitCode : 0;
}
#ifdef __WXMSW__
inline void EnableLFH()
{
typedef BOOL (WINAPI *HeapSetInformation_t)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T);
typedef DWORD (WINAPI *GetProcessHeaps_t)(DWORD, PHANDLE);
HINSTANCE kh = GetModuleHandle(TEXT("kernel32.dll"));
HeapSetInformation_t HeapSetInformation_func = (HeapSetInformation_t) GetProcAddress(kh, "HeapSetInformation");
GetProcessHeaps_t GetProcessHeaps_func = (GetProcessHeaps_t) GetProcAddress(kh, "GetProcessHeaps");
if (GetProcessHeaps_func && HeapSetInformation_func)
{
ULONG HeapFragValue = 2;
int n = GetProcessHeaps_func(0, 0);
HANDLE *h = new HANDLE[n];
GetProcessHeaps_func(n, h);
for (int i = 0; i < n; ++i)
HeapSetInformation_func(h[i], HeapCompatibilityInformation, &HeapFragValue, sizeof(HeapFragValue));
delete[] h;
}
}
#else
inline void EnableLFH() {}
#endif
int CodeBlocksApp::OnRun()
{
EnableLFH();
try
{
int retval = wxApp::OnRun();
// wx 2.6.3 docs says that OnRun() function's return value is used as exit code
return m_Batch ? m_BatchExitCode : retval;
}
catch (cbException& exception)
{
exception.ShowErrorMessage();
}
catch (SquirrelError& exception)
{
Manager::Get()->GetScriptingManager()->DisplayErrors(&exception);
}
catch (const char* message)
{
wxSafeShowMessage(_("Exception"), cbC2U(message));
}
catch (...)
{
wxSafeShowMessage(_("Exception"), _("Unknown exception was raised. The application will terminate immediately..."));
}
// if we reached here, return error
return -1;
}
bool CodeBlocksApp::OnCmdLineParsed(wxCmdLineParser& parser)
{
return wxApp::OnCmdLineParsed(parser);
}
void CodeBlocksApp::OnFatalException()
{
#if wxUSE_DEBUGREPORT && wxUSE_XML && wxUSE_ON_FATAL_EXCEPTION
wxDebugReport report;
wxDebugReportPreviewStd preview;
report.AddAll();
if ( preview.Show(report) )
report.Process();
#else
cbMessageBox(wxString::Format(_("Something has gone wrong inside %s and it will terminate immediately.\n"
"We are sorry for the inconvenience..."), appglobals::AppName.wx_str()));
#endif
}
int CodeBlocksApp::BatchJob()
{
if (!m_Batch)
return -1;
// find compiler plugin
PluginsArray arr = Manager::Get()->GetPluginManager()->GetCompilerOffers();
if (arr.GetCount() == 0)
return -2;
cbCompilerPlugin* compiler = static_cast<cbCompilerPlugin*>(arr[0]);
if (!compiler)
return -3;
if (!m_Clean && m_BatchTarget.Lower() == _T("ask"))
{
m_BatchTarget.Clear();
cbProject* prj = Manager::Get()->GetProjectManager()->GetActiveProject();
if (prj)
{
int idx = -1;
wxString defTarget = prj->GetActiveBuildTarget();
// find active target's index
// TODO: make this easier in the SDK
for (int i = 0; i < prj->GetBuildTargetsCount(); ++i)
{
ProjectBuildTarget* target = prj->GetBuildTarget(i);
if (target->GetTitle().Matches(defTarget))
{
idx = i;
break;
}
}
idx = prj->SelectTarget(idx, false);
if (idx == -1)
return 0; // no target selected: just abort
m_BatchTarget = prj->GetBuildTarget(idx)->GetTitle();
}
}
m_pBatchBuildDialog = m_Frame->GetBatchBuildDialog();
PlaceWindow(m_pBatchBuildDialog);
wxTaskBarIcon* tbIcon = new wxTaskBarIcon();
tbIcon->SetIcon(
#ifdef __WXMSW__
wxICON(A_MAIN_ICON),
#else
wxIcon(app),
#endif // __WXMSW__
_("Building ") + wxFileNameFromPath(wxString(argv[argc-1])));
m_pBatchBuildDialog->Show();
if (m_ReBuild)
{
if (m_HasProject)
compiler->Rebuild(m_BatchTarget);
else if (m_HasWorkSpace)
compiler->RebuildWorkspace(m_BatchTarget);
}
else if (m_Build)
{
if (m_HasProject)
compiler->Build(m_BatchTarget);
else if (m_HasWorkSpace)
compiler->BuildWorkspace(m_BatchTarget);
}
else if (m_Clean)
{
if (m_HasProject)
compiler->Clean(m_BatchTarget);
else if (m_HasWorkSpace)
compiler->CleanWorkspace(m_BatchTarget);
}
// The batch build log might have been deleted in
// CodeBlocksApp::OnBatchBuildDone().
// If it has not, it's still compiling.
if (m_pBatchBuildDialog)
{
// If operation is "--clean", there is no need to display the dialog
// as the operation is synchronous and it already has finished by the
// time the call to Clean() returned.
if (!m_Clean)
m_pBatchBuildDialog->ShowModal();
if ( m_pBatchBuildDialog->IsModal() )
m_pBatchBuildDialog->EndModal(wxID_OK);
else
{
m_pBatchBuildDialog->Destroy();
m_pBatchBuildDialog = nullptr;
}
}
if (tbIcon)
{
tbIcon->RemoveIcon();
delete tbIcon;
}
return 0;
}
void CodeBlocksApp::OnBatchBuildDone(CodeBlocksEvent& event)
{
event.Skip();
// the event comes more than once. deal with it...
static bool one_time_only = false;
You can’t perform that action at this time.
