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