]> git.saurik.com Git - wxWidgets.git/blob - src/msw/app.cpp
Removed wxProp source files
[wxWidgets.git] / src / msw / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Purpose: wxApp
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "app.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #if defined(__BORLANDC__)
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/frame.h"
25 #include "wx/app.h"
26 #include "wx/utils.h"
27 #include "wx/gdicmn.h"
28 #include "wx/pen.h"
29 #include "wx/brush.h"
30 #include "wx/cursor.h"
31 #include "wx/icon.h"
32 #include "wx/palette.h"
33 #include "wx/dc.h"
34 #include "wx/dialog.h"
35 #include "wx/msgdlg.h"
36 #endif
37
38 #include "wx/msw/private.h"
39 #include "wx/log.h"
40 #include "wx/module.h"
41
42 #if wxUSE_WX_RESOURCES
43 #include "wx/resource.h"
44 #endif
45
46 // To UG: there's no point in putting this #if here
47 // if you don't do the same for the Ole calls further down.
48 // Also, OLE is used not just for drag and drop (it's used by automatn.cpp).
49 // #if wxUSE_DRAG_AND_DROP
50 #ifndef __GNUWIN32__
51 #include <ole2.h>
52 #endif
53 // #endif
54
55 #include <string.h>
56 #include <ctype.h>
57
58 #if defined(__WIN95__) && !defined(__GNUWIN32__)
59 #include <commctrl.h>
60 #endif
61
62 // use debug CRT functions for memory leak detections in VC++ if we're not
63 // using wxWindows own methods
64 #if defined(__WXDEBUG__) && defined(_MSC_VER) && !wxUSE_GLOBAL_MEMORY_OPERATORS && !defined(__NO_VC_CRTDBG__)
65 #define wxUSE_VC_CRTDBG
66 #else
67 #undef wxUSE_VC_CRTDBG
68 #endif
69
70 #ifdef wxUSE_VC_CRTDBG
71 // VC++ uses this macro as debug/release mode indicator
72 #ifndef _DEBUG
73 #define _DEBUG
74 #endif
75
76 /* Need to undef new if including crtdbg.h */
77 #ifdef new
78 #undef new
79 #endif
80
81 #include <crtdbg.h>
82
83 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
84 #define new new(__FILE__,__LINE__)
85 #endif
86
87 #endif
88
89 extern char *wxBuffer;
90 extern char *wxOsVersion;
91 extern wxList *wxWinHandleList;
92 extern wxList wxPendingDelete;
93 extern void wxSetKeyboardHook(bool doIt);
94 extern wxCursor *g_globalCursor;
95
96 HINSTANCE wxhInstance = 0;
97 static MSG s_currentMsg;
98 wxApp *wxTheApp = NULL;
99
100 // @@ why not const? and not static?
101 char wxFrameClassName[] = "wxFrameClass";
102 char wxMDIFrameClassName[] = "wxMDIFrameClass";
103 char wxMDIChildFrameClassName[] = "wxMDIChildFrameClass";
104 char wxPanelClassName[] = "wxPanelClass";
105 char wxCanvasClassName[] = "wxCanvasClass";
106
107 HICON wxSTD_FRAME_ICON = NULL;
108 HICON wxSTD_MDICHILDFRAME_ICON = NULL;
109 HICON wxSTD_MDIPARENTFRAME_ICON = NULL;
110
111 HICON wxDEFAULT_FRAME_ICON = NULL;
112 HICON wxDEFAULT_MDICHILDFRAME_ICON = NULL;
113 HICON wxDEFAULT_MDIPARENTFRAME_ICON = NULL;
114
115 HBRUSH wxDisableButtonBrush = 0;
116
117 LRESULT APIENTRY wxWndProc(HWND, UINT, WPARAM, LPARAM);
118
119 #if !USE_SHARED_LIBRARY
120 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
121
122 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
123 EVT_IDLE(wxApp::OnIdle)
124 EVT_END_SESSION(wxApp::OnEndSession)
125 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
126 END_EVENT_TABLE()
127 #endif
128
129 long wxApp::sm_lastMessageTime = 0;
130
131 #ifdef __WIN95__
132 static HINSTANCE gs_hRichEdit = NULL;
133 #endif
134
135 //// Initialize
136
137 bool wxApp::Initialize()
138 {
139 // Some people may wish to use this, but
140 // probably it shouldn't be here by default.
141 #ifdef __WXDEBUG__
142 // wxRedirectIOToConsole();
143 #endif
144
145 wxBuffer = new char[1500];
146
147 #ifdef wxUSE_VC_CRTDBG
148 // do check for memory leaks on program exit
149 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
150 // deallocated memory which may be used to simulate low-memory condition)
151 _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
152 #endif // debug build under MS VC++
153
154 wxClassInfo::InitializeClasses();
155
156 #if wxUSE_RESOURCES
157 wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
158 #endif
159
160 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
161 wxTheColourDatabase->Initialize();
162
163 wxInitializeStockLists();
164 wxInitializeStockObjects();
165
166 #if wxUSE_WX_RESOURCES
167 wxInitializeResourceSystem();
168 #endif
169
170 wxBitmap::InitStandardHandlers();
171
172 #if defined(__WIN95__)
173 InitCommonControls();
174 gs_hRichEdit = LoadLibrary("RICHED32.DLL");
175
176 if (gs_hRichEdit == NULL)
177 {
178 wxMessageBox("Could not initialise Rich Edit DLL");
179 }
180 #endif
181
182 int iMsg = 96;
183
184 // for OLE, enlarge message queue to be as large as possible
185 while (!SetMessageQueue(iMsg) && (iMsg -= 8));
186
187 /*
188 DWORD dwOleVer;
189 dwOleVer = CoBuildVersion();
190
191 // check the OLE library version
192 if (rmm != HIWORD(dwOleVer))
193 {
194 wxMessageBox("Incorrect version of OLE libraries.");
195 return FALSE;
196 }
197 */
198
199 #ifndef __GNUWIN32__
200 // we need to initialize OLE library
201 if ( FAILED(::OleInitialize(NULL)) )
202 wxFatalError(_("Cannot initialize OLE"));
203 #endif
204
205 #if CTL3D
206 if (!Ctl3dRegister(wxhInstance))
207 wxFatalError("Cannot register CTL3D");
208
209 Ctl3dAutoSubclass(wxhInstance);
210 #endif
211
212 g_globalCursor = new wxCursor;
213
214 wxSTD_FRAME_ICON = LoadIcon(wxhInstance, "wxSTD_FRAME");
215 wxSTD_MDIPARENTFRAME_ICON = LoadIcon(wxhInstance, "wxSTD_MDIPARENTFRAME");
216 wxSTD_MDICHILDFRAME_ICON = LoadIcon(wxhInstance, "wxSTD_MDICHILDFRAME");
217
218 wxDEFAULT_FRAME_ICON = LoadIcon(wxhInstance, "wxDEFAULT_FRAME");
219 wxDEFAULT_MDIPARENTFRAME_ICON = LoadIcon(wxhInstance, "wxDEFAULT_MDIPARENTFRAME");
220 wxDEFAULT_MDICHILDFRAME_ICON = LoadIcon(wxhInstance, "wxDEFAULT_MDICHILDFRAME");
221
222 RegisterWindowClasses();
223
224 // Create the brush for disabling bitmap buttons
225
226 LOGBRUSH lb ;
227 lb.lbStyle = BS_PATTERN;
228 lb.lbHatch = (int)LoadBitmap( wxhInstance, "wxDISABLE_BUTTON_BITMAP" ) ;
229 wxDisableButtonBrush = ::CreateBrushIndirect( & lb ) ;
230 ::DeleteObject( (HGDIOBJ)lb.lbHatch ) ;
231
232 #if wxUSE_PENWINDOWS
233 wxRegisterPenWin();
234 #endif
235
236 wxWinHandleList = new wxList(wxKEY_INTEGER);
237
238 // This is to foil optimizations in Visual C++ that
239 // throw out dummy.obj.
240 #if (_MSC_VER >= 800) && !defined(WXMAKINGDLL)
241 extern char wxDummyChar;
242 if (wxDummyChar) wxDummyChar++;
243 #endif
244
245 wxSetKeyboardHook(TRUE);
246
247 wxModule::RegisterModules();
248 if (!wxModule::InitializeModules())
249 return FALSE;
250 return TRUE;
251 }
252
253 //// RegisterWindowClasses
254
255 bool wxApp::RegisterWindowClasses()
256 {
257 ///////////////////////////////////////////////////////////////////////
258 // Register the frame window class.
259 WNDCLASS wndclass; // Structure used to register Windows class.
260
261 wndclass.style = CS_HREDRAW | CS_VREDRAW;
262 wndclass.lpfnWndProc = (WNDPROC)wxWndProc;
263 wndclass.cbClsExtra = 0;
264 wndclass.cbWndExtra = sizeof( DWORD ); // was 4
265 wndclass.hInstance = wxhInstance;
266 wndclass.hIcon = NULL; // wxSTD_FRAME_ICON;
267 wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );
268 wndclass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
269 // wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
270 wndclass.lpszMenuName = NULL;
271 #ifdef _MULTIPLE_INSTANCES
272 sprintf( wxFrameClassName,"wxFrameClass%d", wxhInstance );
273 #endif
274 wndclass.lpszClassName = wxFrameClassName;
275
276 if (!RegisterClass( &wndclass ))
277 {
278 // wxFatalError("Can't register Frame Window class");
279 }
280
281 ///////////////////////////////////////////////////////////////////////
282 // Register the MDI frame window class.
283 WNDCLASS wndclass1; // Structure used to register Windows class.
284
285 wndclass1.style = CS_HREDRAW | CS_VREDRAW;
286 wndclass1.lpfnWndProc = (WNDPROC)wxWndProc;
287 wndclass1.cbClsExtra = 0;
288 wndclass1.cbWndExtra = sizeof( DWORD ); // was 4
289 wndclass1.hInstance = wxhInstance;
290 wndclass1.hIcon = NULL; // wxSTD_MDIPARENTFRAME_ICON;
291 wndclass1.hCursor = LoadCursor( NULL, IDC_ARROW );
292 // wndclass1.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
293 wndclass1.hbrBackground = NULL;
294 wndclass1.lpszMenuName = NULL;
295
296 wndclass1.lpszClassName = wxMDIFrameClassName;
297 if (!RegisterClass( &wndclass1 ))
298 {
299 // wxFatalError("Can't register MDI Frame window class");
300 // return FALSE;
301 }
302
303 ///////////////////////////////////////////////////////////////////////
304 // Register the MDI child frame window class.
305 WNDCLASS wndclass4; // Structure used to register Windows class.
306
307 wndclass4.style = CS_HREDRAW | CS_VREDRAW;
308 wndclass4.lpfnWndProc = (WNDPROC)wxWndProc;
309 wndclass4.cbClsExtra = 0;
310 wndclass4.cbWndExtra = sizeof( DWORD ); // was 4
311 wndclass4.hInstance = wxhInstance;
312 wndclass4.hIcon = NULL; // wxSTD_MDICHILDFRAME_ICON;
313 wndclass4.hCursor = LoadCursor( NULL, IDC_ARROW );
314 // TODO: perhaps this should be NULL so that Windows doesn't
315 // paint the background itself (would OnEraseBackground duplicate
316 // this?)
317 wndclass4.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
318 // wndclass4.hbrBackground = NULL;
319 wndclass4.lpszMenuName = NULL;
320 wndclass4.lpszClassName = wxMDIChildFrameClassName;
321
322 if (!RegisterClass( &wndclass4 ))
323 {
324 // wxFatalError("Can't register MDI child frame window class");
325 // return FALSE;
326 }
327
328 ///////////////////////////////////////////////////////////////////////
329 // Register the panel window class.
330 WNDCLASS wndclass2; // Structure used to register Windows class.
331 memset(&wndclass2, 0, sizeof(WNDCLASS)); // start with NULL defaults
332 // Use CS_OWNDC to avoid messing about restoring the context
333 // for every graphic operation.
334 wndclass2.style = CS_HREDRAW | CS_VREDRAW;
335 wndclass2.lpfnWndProc = (WNDPROC)wxWndProc;
336 wndclass2.cbClsExtra = 0;
337 wndclass2.cbWndExtra = sizeof( DWORD ); // was 4
338 wndclass2.hInstance = wxhInstance;
339 wndclass2.hIcon = NULL;
340 wndclass2.hCursor = NULL;
341 // wndclass2.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1) ;
342 wndclass2.hbrBackground = (HBRUSH) GetStockObject( LTGRAY_BRUSH );
343 wndclass2.lpszMenuName = NULL;
344 wndclass2.lpszClassName = wxPanelClassName;
345 if (!RegisterClass( &wndclass2 ))
346 {
347 // wxFatalError("Can't register Panel Window class");
348 // return FALSE;
349 }
350
351 ///////////////////////////////////////////////////////////////////////
352 // Register the canvas and textsubwindow class name
353 WNDCLASS wndclass3; // Structure used to register Windows class.
354 memset(&wndclass3, 0, sizeof(WNDCLASS)); // start with NULL defaults
355 // Use CS_OWNDC to avoid messing about restoring the context
356 // for every graphic operation.
357 // wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS ;
358 // wxWin 2.0
359 wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
360 wndclass3.lpfnWndProc = (WNDPROC)wxWndProc;
361 wndclass3.cbClsExtra = 0;
362 wndclass3.cbWndExtra = sizeof( DWORD ); // was 4
363 wndclass3.hInstance = wxhInstance;
364 wndclass3.hIcon = NULL;
365 wndclass3.hCursor = NULL;
366 // wndclass3.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
367 wndclass3.hbrBackground = NULL;
368 wndclass3.lpszMenuName = NULL;
369 wndclass3.lpszClassName = wxCanvasClassName;
370 if (!RegisterClass( &wndclass3))
371 {
372 // wxFatalError("Can't register Canvas class");
373 // return FALSE;
374 }
375
376 return TRUE;
377 }
378
379 //// Convert Windows to argc, argv style
380
381 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine)
382 {
383 wxStringList args;
384
385 wxString cmdLine(lpCmdLine);
386 int count = 0;
387
388 // Get application name
389 char name[260]; // 260 is MAX_PATH value from windef.h
390 ::GetModuleFileName(wxhInstance, name, WXSIZEOF(name));
391
392 // GNUWIN32 already fills in the first arg with the application name.
393 #if !defined(__GNUWIN32__)
394 args.Add(name);
395 count ++;
396 #endif
397
398 strcpy(name, wxFileNameFromPath(name));
399 wxStripExtension(name);
400 wxTheApp->SetAppName(name);
401
402 // Break up string
403 // Treat strings enclosed in double-quotes as single arguments
404 int i = 0;
405 int len = cmdLine.Length();
406 while (i < len)
407 {
408 // Skip whitespace
409 while ((i < len) && isspace(cmdLine.GetChar(i)))
410 i ++;
411
412 if (i < len)
413 {
414 if (cmdLine.GetChar(i) == '"') // We found the start of a string
415 {
416 i ++;
417 int first = i;
418 while ((i < len) && (cmdLine.GetChar(i) != '"'))
419 i ++;
420
421 wxString arg(cmdLine.Mid(first, (i - first)));
422
423 args.Add(arg);
424 count ++;
425
426 if (i < len)
427 i ++; // Skip past 2nd quote
428 }
429 else // Unquoted argument
430 {
431 int first = i;
432 while ((i < len) && !isspace(cmdLine.GetChar(i)))
433 i ++;
434
435 wxString arg(cmdLine.Mid(first, (i - first)));
436
437 args.Add(arg);
438 count ++;
439 }
440 }
441 }
442
443 wxTheApp->argv = new char*[count + 1];
444 for (i = 0; i < count; i++)
445 {
446 wxString arg(args[i]);
447 wxTheApp->argv[i] = copystring((const char*)arg);
448 }
449 wxTheApp->argv[count] = NULL; // argv[] is a NULL-terminated list
450 wxTheApp->argc = count;
451 }
452
453 //// Cleans up any wxWindows internal structures left lying around
454
455 void wxApp::CleanUp()
456 {
457 //// COMMON CLEANUP
458 wxModule::CleanUpModules();
459
460 #if wxUSE_WX_RESOURCES
461 wxCleanUpResourceSystem();
462
463 // wxDefaultResourceTable->ClearTable();
464 #endif
465
466 // Indicate that the cursor can be freed,
467 // so that cursor won't be deleted by deleting
468 // the bitmap list before g_globalCursor goes out
469 // of scope (double deletion of the cursor).
470 wxSetCursor(wxNullCursor);
471 delete g_globalCursor;
472
473 wxDeleteStockObjects() ;
474
475 // Destroy all GDI lists, etc.
476 wxDeleteStockLists();
477
478 delete wxTheColourDatabase;
479 wxTheColourDatabase = NULL;
480
481 wxBitmap::CleanUpHandlers();
482
483 delete[] wxBuffer;
484 wxBuffer = NULL;
485
486 //// WINDOWS-SPECIFIC CLEANUP
487
488 wxSetKeyboardHook(FALSE);
489
490 #ifdef __WIN95__
491 if (gs_hRichEdit != NULL)
492 FreeLibrary(gs_hRichEdit);
493 #endif
494
495 #if wxUSE_PENWINDOWS
496 wxCleanUpPenWin();
497 #endif
498
499 if (wxSTD_FRAME_ICON)
500 DestroyIcon(wxSTD_FRAME_ICON);
501 if (wxSTD_MDICHILDFRAME_ICON)
502 DestroyIcon(wxSTD_MDICHILDFRAME_ICON);
503 if (wxSTD_MDIPARENTFRAME_ICON)
504 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON);
505
506 if (wxDEFAULT_FRAME_ICON)
507 DestroyIcon(wxDEFAULT_FRAME_ICON);
508 if (wxDEFAULT_MDICHILDFRAME_ICON)
509 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON);
510 if (wxDEFAULT_MDIPARENTFRAME_ICON)
511 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON);
512
513 if ( wxDisableButtonBrush )
514 ::DeleteObject( wxDisableButtonBrush ) ;
515
516 #ifndef __GNUWIN32__
517 ::OleUninitialize();
518 #endif
519
520 #if CTL3D
521 Ctl3dUnregister(wxhInstance);
522 #endif
523
524 if (wxWinHandleList)
525 delete wxWinHandleList ;
526
527 wxClassInfo::CleanUpClasses();
528
529 delete wxTheApp;
530 wxTheApp = NULL;
531
532 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
533 // At this point we want to check if there are any memory
534 // blocks that aren't part of the wxDebugContext itself,
535 // as a special case. Then when dumping we need to ignore
536 // wxDebugContext, too.
537 if (wxDebugContext::CountObjectsLeft() > 0)
538 {
539 wxLogDebug("There were memory leaks.");
540 wxDebugContext::Dump();
541 wxDebugContext::PrintStatistics();
542 }
543 // wxDebugContext::SetStream(NULL, NULL);
544 #endif
545
546 // do it as the very last thing because everything else can log messages
547 wxLog::DontCreateOnDemand();
548 delete wxLog::SetActiveTarget(NULL);
549 }
550
551 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
552
553 //// Main wxWindows entry point
554 int wxEntry(WXHINSTANCE hInstance,
555 WXHINSTANCE WXUNUSED(hPrevInstance),
556 char *lpCmdLine,
557 int nCmdShow,
558 bool enterLoop)
559 {
560 #if !defined(__WXDEBUG__) && !defined(__BORLANDC__) // take everything into a try-except block in release build
561 try {
562 #endif
563
564 wxhInstance = (HINSTANCE) hInstance;
565
566 if (!wxApp::Initialize())
567 return 0;
568
569 // create the application object or ensure that one already exists
570 if (!wxTheApp)
571 {
572 // The app may have declared a global application object, but we recommend
573 // the IMPLEMENT_APP macro is used instead, which sets an initializer
574 // function for delayed, dynamic app object construction.
575 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
576 "No initializer - use IMPLEMENT_APP macro." );
577
578 wxTheApp = (*wxApp::GetInitializerFunction()) ();
579 }
580
581 wxCHECK_MSG( wxTheApp, 0, "You have to define an instance of wxApp!" );
582
583 // save the WinMain() parameters
584 wxTheApp->ConvertToStandardCommandArgs(lpCmdLine);
585 wxTheApp->m_nCmdShow = nCmdShow;
586
587 // GUI-specific initialisation. In fact on Windows we don't have any,
588 // but this call is provided for compatibility across platforms.
589 wxTheApp->OnInitGui() ;
590
591 int retValue = 0;
592
593 if ( wxTheApp->OnInit() )
594 {
595 if ( enterLoop )
596 {
597 retValue = wxTheApp->OnRun();
598 }
599 }
600 //else: app initialization failed, so we skipped OnRun()
601
602 wxWindow *topWindow = wxTheApp->GetTopWindow();
603 if ( topWindow )
604 {
605 // Forcibly delete the window.
606 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
607 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
608 {
609 topWindow->Close(TRUE);
610 wxTheApp->DeletePendingObjects();
611 }
612 else
613 {
614 delete topWindow;
615 wxTheApp->SetTopWindow(NULL);
616 }
617 }
618
619 wxTheApp->OnExit();
620
621 // flush the logged messages if any
622 wxLog *pLog = wxLog::GetActiveTarget();
623 if ( pLog != NULL && pLog->HasPendingMessages() )
624 pLog->Flush();
625
626
627 wxApp::CleanUp();
628
629 return retValue;
630 #if !defined(__WXDEBUG__) && !defined(__BORLANDC__) // catch exceptions only in release build
631 }
632 except ( EXCEPTION_EXECUTE_HANDLER ) {
633 /*
634 if ( wxTheApp )
635 wxTheApp->OnFatalException();
636 */
637
638 ::ExitProcess(3); // the same exit code as abort()
639 }
640 #endif //debug
641 }
642
643 #else /* _WINDLL */
644
645 //// Entry point for DLLs
646
647 int wxEntry(WXHINSTANCE hInstance)
648 {
649 wxhInstance = (HINSTANCE) hInstance;
650 wxApp::Initialize();
651
652 // The app may have declared a global application object, but we recommend
653 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
654 // for delayed, dynamic app object construction.
655
656 if (!wxTheApp)
657 {
658 if (!wxApp::GetInitializerFunction())
659 {
660 MessageBox(NULL, "No initializer - use IMPLEMENT_APP macro.", "wxWindows Error", MB_APPLMODAL | MB_ICONSTOP | MB_OK);
661 return 0;
662 }
663
664 wxTheApp = (* wxApp::GetInitializerFunction()) ();
665 }
666
667 if (!wxTheApp) {
668 MessageBox(NULL, "You have to define an instance of wxApp!", "wxWindows Error", MB_APPLMODAL | MB_ICONSTOP | MB_OK);
669 return 0;
670 }
671
672 wxTheApp->argc = 0;
673 wxTheApp->argv = NULL;
674
675 wxTheApp->OnInitGui();
676
677 wxTheApp->OnInit();
678
679 if (wxTheApp->GetTopWindow() && wxTheApp->GetTopWindow()->GetHWND()) {
680 wxTheApp->GetTopWindow()->Show(TRUE);
681 }
682
683 return 1;
684 }
685 #endif // _WINDLL
686
687 //// Static member initialization
688
689 wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
690
691 wxApp::wxApp()
692 {
693 m_topWindow = NULL;
694 wxTheApp = this;
695 m_className = "";
696 m_wantDebugOutput = TRUE ;
697 m_appName = "";
698 argc = 0;
699 argv = NULL;
700 #ifdef __WXMSW__
701 m_printMode = wxPRINT_WINDOWS;
702 #else
703 m_printMode = wxPRINT_POSTSCRIPT;
704 #endif
705 m_exitOnFrameDelete = TRUE;
706 m_auto3D = TRUE;
707 }
708
709 wxApp::~wxApp()
710 {
711 // Delete command-line args
712 int i;
713 for (i = 0; i < argc; i++)
714 {
715 delete[] argv[i];
716 }
717 delete[] argv;
718 }
719
720 bool wxApp::Initialized()
721 {
722 #ifndef _WINDLL
723 if (GetTopWindow())
724 return TRUE;
725 else
726 return FALSE;
727 #endif
728 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
729 return TRUE;
730 #endif
731 }
732
733 /*
734 * Get and process a message, returning FALSE if WM_QUIT
735 * received.
736 *
737 */
738 bool wxApp::DoMessage()
739 {
740 if (!::GetMessage(&s_currentMsg, (HWND) NULL, 0, 0))
741 {
742 return FALSE;
743 }
744
745 // Process the message
746 if (!ProcessMessage((WXMSG *)&s_currentMsg))
747 {
748 ::TranslateMessage(&s_currentMsg);
749 wxApp::sm_lastMessageTime = s_currentMsg.time; /* MATTHEW: timeStamp impl. */
750 ::DispatchMessage(&s_currentMsg);
751 }
752 return TRUE;
753 }
754
755 /*
756 * Keep trying to process messages until WM_QUIT
757 * received.
758 *
759 * If there are messages to be processed, they will all be
760 * processed and OnIdle will not be called.
761 * When there are no more messages, OnIdle is called.
762 * If OnIdle requests more time,
763 * it will be repeatedly called so long as there are no pending messages.
764 * A 'feature' of this is that once OnIdle has decided that no more processing
765 * is required, then it won't get processing time until further messages
766 * are processed (it'll sit in DoMessage).
767 */
768
769 int wxApp::MainLoop()
770 {
771 m_keepGoing = TRUE;
772 while (m_keepGoing)
773 {
774 while (!::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) &&
775 ProcessIdle()) {}
776 if (!DoMessage())
777 m_keepGoing = FALSE;
778 }
779
780 return s_currentMsg.wParam;
781 }
782
783 // Returns TRUE if more time is needed.
784 bool wxApp::ProcessIdle()
785 {
786 wxIdleEvent event;
787 event.SetEventObject(this);
788 ProcessEvent(event);
789
790 return event.MoreRequested();
791 }
792
793 void wxApp::ExitMainLoop()
794 {
795 m_keepGoing = FALSE;
796 }
797
798 bool wxApp::Pending()
799 {
800 return (::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) != 0) ;
801 }
802
803 void wxApp::Dispatch()
804 {
805 if (!DoMessage())
806 m_keepGoing = FALSE;
807 }
808
809 /*
810 * Give all windows a chance to preprocess
811 * the message. Some may have accelerator tables, or have
812 * MDI complications.
813 */
814 bool wxApp::ProcessMessage(WXMSG *Msg)
815 {
816 MSG *msg = (MSG *)Msg;
817
818 HWND hWnd;
819
820 // Try translations first; find the youngest window with
821 // a translation table.
822 for (hWnd = msg->hwnd; hWnd != NULL; hWnd = ::GetParent(hWnd))
823 {
824 wxWindow *wnd = wxFindWinFromHandle((WXHWND) hWnd);
825 if (wnd)
826 {
827 if (wnd->MSWTranslateMessage(Msg))
828 return TRUE;
829 }
830 }
831
832 // Anyone for a non-translation message? Try youngest descendants first.
833 for (hWnd = msg->hwnd; hWnd != NULL; hWnd = ::GetParent(hWnd))
834 {
835 wxWindow *wnd = wxFindWinFromHandle((WXHWND) hWnd);
836 if (wnd)
837 {
838 if (wnd->MSWProcessMessage(Msg))
839 return TRUE;
840 }
841 }
842 return FALSE;
843 }
844
845 void wxApp::OnIdle(wxIdleEvent& event)
846 {
847 static bool inOnIdle = FALSE;
848
849 // Avoid recursion (via ProcessEvent default case)
850 if (inOnIdle)
851 return;
852
853 inOnIdle = TRUE;
854
855 // 'Garbage' collection of windows deleted with Close().
856 DeletePendingObjects();
857
858 // flush the logged messages if any
859 wxLog *pLog = wxLog::GetActiveTarget();
860 if ( pLog != NULL && pLog->HasPendingMessages() )
861 pLog->Flush();
862
863 // Send OnIdle events to all windows
864 bool needMore = SendIdleEvents();
865 // bool needMore = FALSE;
866
867 if (needMore)
868 event.RequestMore(TRUE);
869
870 inOnIdle = FALSE;
871 }
872
873 // Send idle event to all top-level windows
874 bool wxApp::SendIdleEvents()
875 {
876 bool needMore = FALSE;
877 wxNode* node = wxTopLevelWindows.First();
878 while (node)
879 {
880 wxWindow* win = (wxWindow*) node->Data();
881 if (SendIdleEvents(win))
882 needMore = TRUE;
883
884 node = node->Next();
885 }
886 return needMore;
887 }
888
889 // Send idle event to window and all subwindows
890 bool wxApp::SendIdleEvents(wxWindow* win)
891 {
892 bool needMore = FALSE;
893
894 wxIdleEvent event;
895 event.SetEventObject(win);
896 win->GetEventHandler()->ProcessEvent(event);
897
898 if (event.MoreRequested())
899 needMore = TRUE;
900
901 wxNode* node = win->GetChildren().First();
902 while (node)
903 {
904 wxWindow* win = (wxWindow*) node->Data();
905 if (SendIdleEvents(win))
906 needMore = TRUE;
907
908 node = node->Next();
909 }
910 return needMore ;
911 }
912
913 void wxApp::DeletePendingObjects()
914 {
915 wxNode *node = wxPendingDelete.First();
916 while (node)
917 {
918 wxObject *obj = (wxObject *)node->Data();
919
920 delete obj;
921
922 if (wxPendingDelete.Member(obj))
923 delete node;
924
925 // Deleting one object may have deleted other pending
926 // objects, so start from beginning of list again.
927 node = wxPendingDelete.First();
928 }
929 }
930
931 void wxApp::OnEndSession(wxCloseEvent& event)
932 {
933 if (GetTopWindow())
934 GetTopWindow()->Close(TRUE);
935 }
936
937 // Default behaviour: close the application with prompts. The
938 // user can veto the close, and therefore the end session.
939 void wxApp::OnQueryEndSession(wxCloseEvent& event)
940 {
941 if (GetTopWindow())
942 {
943 if (!GetTopWindow()->Close(!event.CanVeto()))
944 event.Veto(TRUE);
945 }
946 }
947
948 wxLog* wxApp::CreateLogTarget()
949 {
950 return new wxLogGui;
951 }
952
953 wxWindow* wxApp::GetTopWindow() const
954 {
955 if (m_topWindow)
956 return m_topWindow;
957 else if (wxTopLevelWindows.Number() > 0)
958 return (wxWindow*) wxTopLevelWindows.First()->Data();
959 else
960 return NULL;
961 }
962
963 int wxApp::GetComCtl32Version() const
964 {
965 // have we loaded COMCTL32 yet?
966 HMODULE theModule = ::GetModuleHandle("COMCTL32");
967 int version = 0;
968
969 // if so, then we can check for the version
970 if (theModule)
971 {
972 // InitCommonControlsEx is unique to 4.7 and later
973 FARPROC theProc = ::GetProcAddress(theModule, "InitCommonControlsEx");
974
975 if (! theProc)
976 { // not found, must be 4.00
977 version = 400;
978 }
979 else
980 {
981 // The following symbol are unique to 4.71
982 // DllInstall
983 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
984 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
985 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
986 // FlatSB_ShowScrollBar
987 // _DrawIndirectImageList _DuplicateImageList
988 // InitializeFlatSB
989 // UninitializeFlatSB
990 // we could check for any of these - I chose DllInstall
991 FARPROC theProc = ::GetProcAddress(theModule, "DllInstall");
992 if (! theProc)
993 {
994 // not found, must be 4.70
995 version = 470;
996 }
997 else
998 { // found, must be 4.71
999 version = 471;
1000 }
1001 }
1002 }
1003 return version;
1004 }
1005
1006 void wxExit()
1007 {
1008 wxApp::CleanUp();
1009 FatalAppExit(0, "Fatal error: exiting");
1010 }
1011
1012 // Yield to incoming messages
1013 bool wxYield()
1014 {
1015 MSG msg;
1016 // We want to go back to the main message loop
1017 // if we see a WM_QUIT. (?)
1018 while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) && msg.message != WM_QUIT)
1019 {
1020 if (!wxTheApp->DoMessage())
1021 break;
1022 }
1023
1024 return TRUE;
1025 }
1026
1027 HINSTANCE wxGetInstance()
1028 {
1029 return wxhInstance;
1030 }
1031
1032 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1033 // if in a separate file. So include it here to ensure it's linked.
1034 #if (defined(_MSC_VER) && !defined(__WIN32__)) || defined(__GNUWIN32__)
1035 #include "main.cpp"
1036 #endif