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