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