]> git.saurik.com Git - wxWidgets.git/blob - src/msw/app.cpp
wxInputConsumer
[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 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "app.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #if defined(__BORLANDC__)
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/frame.h"
33 #include "wx/app.h"
34 #include "wx/utils.h"
35 #include "wx/gdicmn.h"
36 #include "wx/pen.h"
37 #include "wx/brush.h"
38 #include "wx/cursor.h"
39 #include "wx/icon.h"
40 #include "wx/palette.h"
41 #include "wx/dc.h"
42 #include "wx/dialog.h"
43 #include "wx/msgdlg.h"
44 #include "wx/intl.h"
45 #include "wx/dynarray.h"
46 #include "wx/wxchar.h"
47 #include "wx/icon.h"
48 #endif
49
50 #include "wx/log.h"
51 #include "wx/module.h"
52
53 #include "wx/msw/private.h"
54
55 #if wxUSE_THREADS
56 #include "wx/thread.h"
57
58 // define the array of MSG strutures
59 WX_DECLARE_OBJARRAY(MSG, wxMsgArray);
60
61 #include "wx/arrimpl.cpp"
62
63 WX_DEFINE_OBJARRAY(wxMsgArray);
64 #endif // wxUSE_THREADS
65
66 #if wxUSE_WX_RESOURCES
67 #include "wx/resource.h"
68 #endif
69
70 #if wxUSE_TOOLTIPS
71 #include "wx/tooltip.h"
72 #endif // wxUSE_TOOLTIPS
73
74 // OLE is used for drag-and-drop, clipboard, OLE Automation..., but some
75 // compilers don't support it (missing headers, libs, ...)
76 #if defined(__GNUWIN32_OLD__) || defined(__SC__) || defined(__SALFORDC__)
77 #undef wxUSE_OLE
78
79 #define wxUSE_OLE 0
80 #endif // broken compilers
81
82 #if wxUSE_OLE
83 #include <ole2.h>
84 #endif
85
86 #include <string.h>
87 #include <ctype.h>
88
89 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__) || defined(__WXMICROWIN__)) && !defined(__CYGWIN10__))
90 #include <commctrl.h>
91 #endif
92
93 #ifndef __WXMICROWIN__
94 #include "wx/msw/msvcrt.h"
95 #endif
96
97 // ----------------------------------------------------------------------------
98 // conditional compilation
99 // ----------------------------------------------------------------------------
100
101 // The macro _WIN32_IE is defined by commctrl.h (unless it had already been
102 // defined before) and shows us what common control features are available
103 // during the compile time (it doesn't mean that they will be available during
104 // the run-time, use GetComCtl32Version() to test for them!). The possible
105 // values are:
106 //
107 // 0x0200 for comctl32.dll 4.00 shipped with Win95/NT 4.0
108 // 0x0300 4.70 IE 3.x
109 // 0x0400 4.71 IE 4.0
110 // 0x0401 4.72 IE 4.01 and Win98
111 // 0x0500 5.00 IE 5.x and NT 5.0 (Win2000)
112
113 #ifndef _WIN32_IE
114 // minimal set of features by default
115 #define _WIN32_IE 0x0200
116 #endif
117
118 #if _WIN32_IE >= 0x0300 \
119 && !( defined(__MINGW32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
120 #include <shlwapi.h>
121 #endif
122
123 // ---------------------------------------------------------------------------
124 // global variables
125 // ---------------------------------------------------------------------------
126
127 extern wxChar *wxBuffer;
128 extern wxList *wxWinHandleList;
129 extern wxList WXDLLEXPORT wxPendingDelete;
130 #ifndef __WXMICROWIN__
131 extern void wxSetKeyboardHook(bool doIt);
132 #endif
133
134 MSG s_currentMsg;
135 wxApp *wxTheApp = NULL;
136
137 // NB: all "NoRedraw" classes must have the same names as the "normal" classes
138 // with NR suffix - wxWindow::MSWCreate() supposes this
139 const wxChar *wxFrameClassName = wxT("wxFrameClass");
140 const wxChar *wxFrameClassNameNoRedraw = wxT("wxFrameClassNR");
141 const wxChar *wxMDIFrameClassName = wxT("wxMDIFrameClass");
142 const wxChar *wxMDIFrameClassNameNoRedraw = wxT("wxMDIFrameClassNR");
143 const wxChar *wxMDIChildFrameClassName = wxT("wxMDIChildFrameClass");
144 const wxChar *wxMDIChildFrameClassNameNoRedraw = wxT("wxMDIChildFrameClassNR");
145 const wxChar *wxPanelClassName = wxT("wxPanelClass");
146 const wxChar *wxPanelClassNameNR = wxT("wxPanelClassNR");
147 const wxChar *wxCanvasClassName = wxT("wxCanvasClass");
148 const wxChar *wxCanvasClassNameNR = wxT("wxCanvasClassNR");
149
150 HICON wxSTD_FRAME_ICON = (HICON) NULL;
151 HICON wxSTD_MDICHILDFRAME_ICON = (HICON) NULL;
152 HICON wxSTD_MDIPARENTFRAME_ICON = (HICON) NULL;
153
154 HICON wxDEFAULT_FRAME_ICON = (HICON) NULL;
155 HICON wxDEFAULT_MDICHILDFRAME_ICON = (HICON) NULL;
156 HICON wxDEFAULT_MDIPARENTFRAME_ICON = (HICON) NULL;
157
158 HBRUSH wxDisableButtonBrush = (HBRUSH) 0;
159
160 LRESULT WXDLLEXPORT APIENTRY wxWndProc(HWND, UINT, WPARAM, LPARAM);
161
162 // FIXME wxUSE_ON_FATAL_EXCEPTION is only supported for VC++ now because it
163 // needs compiler support for Win32 SEH. Others (especially Borland)
164 // probably have it too, but I'm not sure about how it works
165 // JACS: get 'Cannot use __try in functions that require unwinding
166 // in Unicode mode, so disabling.
167 #if !defined(__VISUALC__) || defined(__WIN16__) || defined(UNICODE)
168 #undef wxUSE_ON_FATAL_EXCEPTION
169 #define wxUSE_ON_FATAL_EXCEPTION 0
170 #endif // VC++
171
172 #if wxUSE_ON_FATAL_EXCEPTION
173 static bool gs_handleExceptions = FALSE;
174 #endif
175
176 // ===========================================================================
177 // implementation
178 // ===========================================================================
179
180 // ---------------------------------------------------------------------------
181 // wxApp
182 // ---------------------------------------------------------------------------
183
184 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
185
186 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
187 EVT_IDLE(wxApp::OnIdle)
188 EVT_END_SESSION(wxApp::OnEndSession)
189 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
190 END_EVENT_TABLE()
191
192 //// Initialize
193 bool wxApp::Initialize()
194 {
195 // the first thing to do is to check if we're trying to run an Unicode
196 // program under Win9x - if so, abort right now as it has no chance to
197 // work
198 #if wxUSE_UNICODE
199 if ( wxGetOsVersion() != wxWINDOWS_NT )
200 {
201 // note that we can use MessageBoxW() as it's implemented even under
202 // Win9x - OTOH, we can't use wxGetTranslation() because the file APIs
203 // used by wxLocale are not
204 ::MessageBox
205 (
206 NULL,
207 _T("This program uses Unicode and requires Windows NT/2000.\nProgram aborted."),
208 _T("wxWindows Fatal Error"),
209 MB_ICONERROR | MB_OK
210 );
211
212 return FALSE;
213 }
214 #endif // wxUSE_UNICODE
215
216 // Some people may wish to use this, but
217 // probably it shouldn't be here by default.
218 #ifdef __WXDEBUG__
219 // wxRedirectIOToConsole();
220 #endif
221
222 wxBuffer = new wxChar[1500]; // FIXME
223
224 wxClassInfo::InitializeClasses();
225
226 #if wxUSE_THREADS
227 wxPendingEventsLocker = new wxCriticalSection;
228 #endif
229
230 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
231 wxTheColourDatabase->Initialize();
232
233 wxInitializeStockLists();
234 wxInitializeStockObjects();
235
236 #if wxUSE_WX_RESOURCES
237 wxInitializeResourceSystem();
238 #endif
239
240 wxBitmap::InitStandardHandlers();
241
242 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
243 InitCommonControls();
244 #endif // __WIN95__
245
246 #if wxUSE_OLE || wxUSE_DRAG_AND_DROP
247
248 #ifdef __WIN16__
249 // for OLE, enlarge message queue to be as large as possible
250 int iMsg = 96;
251 while (!SetMessageQueue(iMsg) && (iMsg -= 8))
252 ;
253 #endif // Win16
254
255 // we need to initialize OLE library
256 if ( FAILED(::OleInitialize(NULL)) )
257 wxLogError(_("Cannot initialize OLE"));
258
259 #endif // wxUSE_OLE
260
261 #if wxUSE_CTL3D
262 if (!Ctl3dRegister(wxhInstance))
263 wxLogError(wxT("Cannot register CTL3D"));
264
265 Ctl3dAutoSubclass(wxhInstance);
266 #endif // wxUSE_CTL3D
267
268 // VZ: these icons are not in wx.rc anyhow (but should they?)!
269 #if 0
270 wxSTD_FRAME_ICON = LoadIcon(wxhInstance, wxT("wxSTD_FRAME"));
271 wxSTD_MDIPARENTFRAME_ICON = LoadIcon(wxhInstance, wxT("wxSTD_MDIPARENTFRAME"));
272 wxSTD_MDICHILDFRAME_ICON = LoadIcon(wxhInstance, wxT("wxSTD_MDICHILDFRAME"));
273
274 wxDEFAULT_FRAME_ICON = LoadIcon(wxhInstance, wxT("wxDEFAULT_FRAME"));
275 wxDEFAULT_MDIPARENTFRAME_ICON = LoadIcon(wxhInstance, wxT("wxDEFAULT_MDIPARENTFRAME"));
276 wxDEFAULT_MDICHILDFRAME_ICON = LoadIcon(wxhInstance, wxT("wxDEFAULT_MDICHILDFRAME"));
277 #endif // 0
278
279 RegisterWindowClasses();
280
281 #ifndef __WXMICROWIN__
282 // Create the brush for disabling bitmap buttons
283
284 LOGBRUSH lb;
285 lb.lbStyle = BS_PATTERN;
286 lb.lbHatch = (int)LoadBitmap( wxhInstance, wxT("wxDISABLE_BUTTON_BITMAP") );
287 if ( lb.lbHatch )
288 {
289 wxDisableButtonBrush = ::CreateBrushIndirect( & lb );
290 ::DeleteObject( (HGDIOBJ)lb.lbHatch );
291 }
292 //else: wxWindows resources are probably not linked in
293 #endif
294
295 #if wxUSE_PENWINDOWS
296 wxRegisterPenWin();
297 #endif
298
299 wxWinHandleList = new wxList(wxKEY_INTEGER);
300
301 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
302 // PLEASE DO NOT ALTER THIS.
303 #if defined(__VISUALC__) && defined(__WIN16__) && !defined(WXMAKINGDLL)
304 extern char wxDummyChar;
305 if (wxDummyChar) wxDummyChar++;
306 #endif
307
308 #ifndef __WXMICROWIN__
309 wxSetKeyboardHook(TRUE);
310 #endif
311
312 wxModule::RegisterModules();
313 if (!wxModule::InitializeModules())
314 return FALSE;
315 return TRUE;
316 }
317
318 // ---------------------------------------------------------------------------
319 // RegisterWindowClasses
320 // ---------------------------------------------------------------------------
321
322 // TODO we should only register classes really used by the app. For this it
323 // would be enough to just delay the class registration until an attempt
324 // to create a window of this class is made.
325 bool wxApp::RegisterWindowClasses()
326 {
327 WNDCLASS wndclass;
328
329 // for each class we register one with CS_(V|H)REDRAW style and one
330 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
331 static const long styleNormal = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
332 static const long styleNoRedraw = CS_DBLCLKS;
333
334 // the fields which are common to all classes
335 wndclass.lpfnWndProc = (WNDPROC)wxWndProc;
336 wndclass.cbClsExtra = 0;
337 wndclass.cbWndExtra = sizeof( DWORD ); // VZ: what is this DWORD used for?
338 wndclass.hInstance = wxhInstance;
339 wndclass.hIcon = (HICON) NULL;
340 wndclass.hCursor = ::LoadCursor((HINSTANCE)NULL, IDC_ARROW);
341 wndclass.lpszMenuName = NULL;
342
343 // Register the frame window class.
344 wndclass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE + 1);
345 wndclass.lpszClassName = wxFrameClassName;
346 wndclass.style = styleNormal;
347
348 if ( !RegisterClass(&wndclass) )
349 {
350 wxLogLastError(wxT("RegisterClass(frame)"));
351
352 return FALSE;
353 }
354
355 // "no redraw" frame
356 wndclass.lpszClassName = wxFrameClassNameNoRedraw;
357 wndclass.style = styleNoRedraw;
358
359 if ( !RegisterClass(&wndclass) )
360 {
361 wxLogLastError(wxT("RegisterClass(no redraw frame)"));
362
363 return FALSE;
364 }
365
366 // Register the MDI frame window class.
367 wndclass.hbrBackground = (HBRUSH)NULL; // paint MDI frame ourselves
368 wndclass.lpszClassName = wxMDIFrameClassName;
369 wndclass.style = styleNormal;
370
371 if ( !RegisterClass(&wndclass) )
372 {
373 wxLogLastError(wxT("RegisterClass(MDI parent)"));
374
375 return FALSE;
376 }
377
378 // "no redraw" MDI frame
379 wndclass.lpszClassName = wxMDIFrameClassNameNoRedraw;
380 wndclass.style = styleNoRedraw;
381
382 if ( !RegisterClass(&wndclass) )
383 {
384 wxLogLastError(wxT("RegisterClass(no redraw MDI parent frame)"));
385
386 return FALSE;
387 }
388
389 // Register the MDI child frame window class.
390 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
391 wndclass.lpszClassName = wxMDIChildFrameClassName;
392 wndclass.style = styleNormal;
393
394 if ( !RegisterClass(&wndclass) )
395 {
396 wxLogLastError(wxT("RegisterClass(MDI child)"));
397
398 return FALSE;
399 }
400
401 // "no redraw" MDI child frame
402 wndclass.lpszClassName = wxMDIChildFrameClassNameNoRedraw;
403 wndclass.style = styleNoRedraw;
404
405 if ( !RegisterClass(&wndclass) )
406 {
407 wxLogLastError(wxT("RegisterClass(no redraw MDI child)"));
408
409 return FALSE;
410 }
411
412 // Register the panel window class.
413 wndclass.hbrBackground = (HBRUSH) GetStockObject( LTGRAY_BRUSH );
414 wndclass.lpszClassName = wxPanelClassName;
415 wndclass.style = styleNormal;
416
417 if ( !RegisterClass(&wndclass) )
418 {
419 wxLogLastError(wxT("RegisterClass(panel)"));
420
421 return FALSE;
422 }
423
424 // Register the no redraw panel window class.
425 wndclass.lpszClassName = wxPanelClassNameNR;
426 wndclass.style = styleNoRedraw;
427
428 if ( !RegisterClass(&wndclass) )
429 {
430 wxLogLastError(wxT("RegisterClass(no redraw panel)"));
431
432 return FALSE;
433 }
434
435 // Register the canvas and textsubwindow class name
436 wndclass.hbrBackground = (HBRUSH)NULL;
437 wndclass.lpszClassName = wxCanvasClassName;
438
439 if ( !RegisterClass(&wndclass) )
440 {
441 wxLogLastError(wxT("RegisterClass(canvas)"));
442
443 return FALSE;
444 }
445
446 wndclass.lpszClassName = wxCanvasClassNameNR;
447 wndclass.style = styleNoRedraw;
448 if ( !RegisterClass(&wndclass) )
449 {
450 wxLogLastError(wxT("RegisterClass(no redraw canvas)"));
451
452 return FALSE;
453 }
454
455 return TRUE;
456 }
457
458 // ---------------------------------------------------------------------------
459 // UnregisterWindowClasses
460 // ---------------------------------------------------------------------------
461
462 bool wxApp::UnregisterWindowClasses()
463 {
464 bool retval = TRUE;
465
466 // frame window class.
467 if ( !UnregisterClass(wxFrameClassName, wxhInstance) )
468 {
469 wxLogLastError(wxT("UnregisterClass(frame)"));
470
471 retval = FALSE;
472 }
473
474 // "no redraw" frame
475 if ( !UnregisterClass(wxFrameClassNameNoRedraw, wxhInstance) )
476 {
477 wxLogLastError(wxT("UnregisterClass(no redraw frame)"));
478
479 return FALSE;
480 }
481
482 // MDI frame window class.
483 if ( !UnregisterClass(wxMDIFrameClassName, wxhInstance) )
484 {
485 wxLogLastError(wxT("UnregisterClass(MDI parent)"));
486
487 retval = FALSE;
488 }
489
490 // "no redraw" MDI frame
491 if ( !UnregisterClass(wxMDIFrameClassNameNoRedraw, wxhInstance) )
492 {
493 wxLogLastError(wxT("UnregisterClass(no redraw MDI parent frame)"));
494
495 retval = FALSE;
496 }
497
498 // MDI child frame window class.
499 if ( !UnregisterClass(wxMDIChildFrameClassName, wxhInstance) )
500 {
501 wxLogLastError(wxT("UnregisterClass(MDI child)"));
502
503 retval = FALSE;
504 }
505
506 // "no redraw" MDI child frame
507 if ( !UnregisterClass(wxMDIChildFrameClassNameNoRedraw, wxhInstance) )
508 {
509 wxLogLastError(wxT("UnregisterClass(no redraw MDI child)"));
510
511 retval = FALSE;
512 }
513
514 // panel window class.
515 if ( !UnregisterClass(wxPanelClassName, wxhInstance) )
516 {
517 wxLogLastError(wxT("UnregisterClass(panel)"));
518
519 retval = FALSE;
520 }
521
522 // no redraw panel window class.
523 if ( !UnregisterClass(wxPanelClassNameNR, wxhInstance) )
524 {
525 wxLogLastError(wxT("UnregisterClass(no redraw panel)"));
526
527 retval = FALSE;
528 }
529
530 // canvas and textsubwindow class name
531 if ( !UnregisterClass(wxCanvasClassName, wxhInstance) )
532 {
533 wxLogLastError(wxT("UnregisterClass(canvas)"));
534
535 retval = FALSE;
536 }
537
538 if ( !UnregisterClass(wxCanvasClassNameNR, wxhInstance) )
539 {
540 wxLogLastError(wxT("UnregisterClass(no redraw canvas)"));
541
542 retval = FALSE;
543 }
544
545 return retval;
546 }
547
548 // ---------------------------------------------------------------------------
549 // Convert Windows to argc, argv style
550 // ---------------------------------------------------------------------------
551
552 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine)
553 {
554 wxStringList args;
555
556 wxString cmdLine(lpCmdLine);
557 int count = 0;
558
559 // Get application name
560 wxChar name[260]; // 260 is MAX_PATH value from windef.h
561 ::GetModuleFileName(wxhInstance, name, WXSIZEOF(name));
562
563 args.Add(name);
564 count++;
565
566 wxStrcpy(name, wxFileNameFromPath(name));
567 wxStripExtension(name);
568 wxTheApp->SetAppName(name);
569
570 // Break up string
571 // Treat strings enclosed in double-quotes as single arguments
572 int i = 0;
573 int len = cmdLine.Length();
574 while (i < len)
575 {
576 // Skip whitespace
577 while ((i < len) && wxIsspace(cmdLine.GetChar(i)))
578 i ++;
579
580 if (i < len)
581 {
582 if (cmdLine.GetChar(i) == wxT('"')) // We found the start of a string
583 {
584 i ++;
585 int first = i;
586 while ((i < len) && (cmdLine.GetChar(i) != wxT('"')))
587 i ++;
588
589 wxString arg(cmdLine.Mid(first, (i - first)));
590
591 args.Add(arg);
592 count ++;
593
594 if (i < len)
595 i ++; // Skip past 2nd quote
596 }
597 else // Unquoted argument
598 {
599 int first = i;
600 while ((i < len) && !wxIsspace(cmdLine.GetChar(i)))
601 i ++;
602
603 wxString arg(cmdLine.Mid(first, (i - first)));
604
605 args.Add(arg);
606 count ++;
607 }
608 }
609 }
610
611 wxTheApp->argv = new wxChar*[count + 1];
612 for (i = 0; i < count; i++)
613 {
614 wxString arg(args[i]);
615 wxTheApp->argv[i] = copystring((const wxChar*)arg);
616 }
617 wxTheApp->argv[count] = NULL; // argv[] is a NULL-terminated list
618 wxTheApp->argc = count;
619 }
620
621 //// Cleans up any wxWindows internal structures left lying around
622
623 void wxApp::CleanUp()
624 {
625 //// COMMON CLEANUP
626
627 #if wxUSE_LOG
628 // flush the logged messages if any and install a 'safer' log target: the
629 // default one (wxLogGui) can't be used after the resources are freed just
630 // below and the user suppliedo ne might be even more unsafe (using any
631 // wxWindows GUI function is unsafe starting from now)
632 wxLog::DontCreateOnDemand();
633
634 // this will flush the old messages if any
635 delete wxLog::SetActiveTarget(new wxLogStderr);
636 #endif // wxUSE_LOG
637
638 // One last chance for pending objects to be cleaned up
639 wxTheApp->DeletePendingObjects();
640
641 wxModule::CleanUpModules();
642
643 #if wxUSE_WX_RESOURCES
644 wxCleanUpResourceSystem();
645
646 // wxDefaultResourceTable->ClearTable();
647 #endif
648
649 wxDeleteStockObjects();
650
651 // Destroy all GDI lists, etc.
652 wxDeleteStockLists();
653
654 delete wxTheColourDatabase;
655 wxTheColourDatabase = NULL;
656
657 wxBitmap::CleanUpHandlers();
658
659 delete[] wxBuffer;
660 wxBuffer = NULL;
661
662 //// WINDOWS-SPECIFIC CLEANUP
663
664 #ifndef __WXMICROWIN__
665 wxSetKeyboardHook(FALSE);
666 #endif
667
668 #if wxUSE_PENWINDOWS
669 wxCleanUpPenWin();
670 #endif
671
672 if (wxSTD_FRAME_ICON)
673 DestroyIcon(wxSTD_FRAME_ICON);
674 if (wxSTD_MDICHILDFRAME_ICON)
675 DestroyIcon(wxSTD_MDICHILDFRAME_ICON);
676 if (wxSTD_MDIPARENTFRAME_ICON)
677 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON);
678
679 if (wxDEFAULT_FRAME_ICON)
680 DestroyIcon(wxDEFAULT_FRAME_ICON);
681 if (wxDEFAULT_MDICHILDFRAME_ICON)
682 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON);
683 if (wxDEFAULT_MDIPARENTFRAME_ICON)
684 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON);
685
686 if ( wxDisableButtonBrush )
687 ::DeleteObject( wxDisableButtonBrush );
688
689 #if wxUSE_OLE
690 ::OleUninitialize();
691 #endif
692
693 #ifdef WXMAKINGDLL
694 // for an EXE the classes are unregistered when it terminates but DLL may
695 // be loaded several times (load/unload/load) into the same process in
696 // which case the registration will fail after the first time if we don't
697 // unregister the classes now
698 UnregisterWindowClasses();
699 #endif // WXMAKINGDLL
700
701 #if wxUSE_CTL3D
702 Ctl3dUnregister(wxhInstance);
703 #endif
704
705 if (wxWinHandleList)
706 delete wxWinHandleList;
707
708 // GL: I'm annoyed ... I don't know where to put this and I don't want to
709 // create a module for that as it's part of the core.
710 delete wxPendingEvents;
711
712 #if wxUSE_THREADS
713 delete wxPendingEventsLocker;
714 // If we don't do the following, we get an apparent memory leak
715 #if wxUSE_VALIDATORS
716 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
717 #endif // wxUSE_VALIDATORS
718 #endif // wxUSE_THREADS
719
720 wxClassInfo::CleanUpClasses();
721
722 delete wxTheApp;
723 wxTheApp = NULL;
724
725 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
726 // At this point we want to check if there are any memory
727 // blocks that aren't part of the wxDebugContext itself,
728 // as a special case. Then when dumping we need to ignore
729 // wxDebugContext, too.
730 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
731 {
732 wxLogMessage(wxT("There were memory leaks."));
733 wxDebugContext::Dump();
734 wxDebugContext::PrintStatistics();
735 }
736 // wxDebugContext::SetStream(NULL, NULL);
737 #endif
738
739 #if wxUSE_LOG
740 // do it as the very last thing because everything else can log messages
741 delete wxLog::SetActiveTarget(NULL);
742 #endif // wxUSE_LOG
743 }
744
745 //----------------------------------------------------------------------
746 // Entry point helpers, used by wxPython
747 //----------------------------------------------------------------------
748
749 int WXDLLEXPORT wxEntryStart( int WXUNUSED(argc), char** WXUNUSED(argv) )
750 {
751 return wxApp::Initialize();
752 }
753
754 int WXDLLEXPORT wxEntryInitGui()
755 {
756 return wxTheApp->OnInitGui();
757 }
758
759 void WXDLLEXPORT wxEntryCleanup()
760 {
761 wxApp::CleanUp();
762 }
763
764
765 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
766
767 // temporarily disable this warning which would be generated in release builds
768 // because of __try
769 #ifdef __VISUALC__
770 #pragma warning(disable: 4715) // not all control paths return a value
771 #endif // Visual C++
772
773 //----------------------------------------------------------------------
774 // Main wxWindows entry point
775 //----------------------------------------------------------------------
776 int wxEntry(WXHINSTANCE hInstance,
777 WXHINSTANCE WXUNUSED(hPrevInstance),
778 char *lpCmdLine,
779 int nCmdShow,
780 bool enterLoop)
781 {
782 // do check for memory leaks on program exit
783 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
784 // deallocated memory which may be used to simulate low-memory condition)
785 #ifndef __WXMICROWIN__
786 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
787 #endif
788
789 #ifdef __MWERKS__
790 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
791 // This seems to be necessary since there are 'rogue'
792 // objects present at this point (perhaps global objects?)
793 // Setting a checkpoint will ignore them as far as the
794 // memory checking facility is concerned.
795 // Of course you may argue that memory allocated in globals should be
796 // checked, but this is a reasonable compromise.
797 wxDebugContext::SetCheckpoint();
798 #endif
799 #endif
800
801 // take everything into a try-except block to be able to call
802 // OnFatalException() if necessary
803 #if wxUSE_ON_FATAL_EXCEPTION
804 __try {
805 #endif
806 wxhInstance = (HINSTANCE) hInstance;
807
808 if (!wxEntryStart(0,0))
809 return 0;
810
811 // create the application object or ensure that one already exists
812 if (!wxTheApp)
813 {
814 // The app may have declared a global application object, but we recommend
815 // the IMPLEMENT_APP macro is used instead, which sets an initializer
816 // function for delayed, dynamic app object construction.
817 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
818 wxT("No initializer - use IMPLEMENT_APP macro.") );
819
820 wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
821 }
822
823 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
824
825 // save the WinMain() parameters
826 wxTheApp->ConvertToStandardCommandArgs(lpCmdLine);
827 wxTheApp->m_nCmdShow = nCmdShow;
828
829 // We really don't want timestamps by default, because it means
830 // we can't simply double-click on the error message and get to that
831 // line in the source. So VC++ at least, let's have a sensible default.
832 #ifdef __VISUALC__
833 wxLog::SetTimestamp(NULL);
834 #endif
835
836 int retValue = 0;
837
838 // it is common to create a modal dialog in OnInit() (to ask/notify the
839 // user about something) but it wouldn't work if we don't change the
840 // "exit on delete last frame" flag here as when this dialog is
841 // deleted, the app would terminate (it was the last top level window
842 // as the main frame wasn't created yet!), so disable this behaviour
843 // temproarily
844 bool exitOnLastFrameDelete = wxTheApp->GetExitOnFrameDelete();
845 wxTheApp->SetExitOnFrameDelete(FALSE);
846
847 // init the app
848 retValue = wxEntryInitGui() && wxTheApp->OnInit() ? 0 : -1;
849
850 // restore the old flag value
851 wxTheApp->SetExitOnFrameDelete(exitOnLastFrameDelete);
852
853 if ( retValue == 0 )
854 {
855 if ( enterLoop )
856 {
857 // run the main loop
858 retValue = wxTheApp->OnRun();
859 }
860 else
861 {
862 // we want to initialize, but not run or exit immediately.
863 return 1;
864 }
865 }
866 //else: app initialization failed, so we skipped OnRun()
867
868 wxWindow *topWindow = wxTheApp->GetTopWindow();
869 if ( topWindow )
870 {
871 // Forcibly delete the window.
872 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
873 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
874 {
875 topWindow->Close(TRUE);
876 wxTheApp->DeletePendingObjects();
877 }
878 else
879 {
880 delete topWindow;
881 wxTheApp->SetTopWindow(NULL);
882 }
883 }
884
885 wxTheApp->OnExit();
886
887 wxEntryCleanup();
888
889 return retValue;
890
891 #if wxUSE_ON_FATAL_EXCEPTION
892 }
893 __except ( gs_handleExceptions ? EXCEPTION_EXECUTE_HANDLER
894 : EXCEPTION_CONTINUE_SEARCH ) {
895 if ( wxTheApp )
896 {
897 // give the user a chance to do something special about this
898 wxTheApp->OnFatalException();
899 }
900
901 ::ExitProcess(3); // the same exit code as abort()
902
903 // NOTREACHED
904 }
905 #endif // wxUSE_ON_FATAL_EXCEPTION
906 }
907
908 // restore warning state
909 #ifdef __VISUALC__
910 #pragma warning(default: 4715) // not all control paths return a value
911 #endif // Visual C++
912
913 #else /* _WINDLL */
914
915 //----------------------------------------------------------------------
916 // Entry point for wxWindows + the App in a DLL
917 //----------------------------------------------------------------------
918
919 int wxEntry(WXHINSTANCE hInstance)
920 {
921 wxhInstance = (HINSTANCE) hInstance;
922 wxEntryStart(0, 0);
923
924 // The app may have declared a global application object, but we recommend
925 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
926 // for delayed, dynamic app object construction.
927 if (!wxTheApp)
928 {
929 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
930 "No initializer - use IMPLEMENT_APP macro." );
931
932 wxTheApp = (* wxApp::GetInitializerFunction()) ();
933 }
934
935 wxCHECK_MSG( wxTheApp, 0, "You have to define an instance of wxApp!" );
936
937 wxTheApp->argc = 0;
938 wxTheApp->argv = NULL;
939
940 wxEntryInitGui();
941
942 wxTheApp->OnInit();
943
944 wxWindow *topWindow = wxTheApp->GetTopWindow();
945 if ( topWindow && topWindow->GetHWND())
946 {
947 topWindow->Show(TRUE);
948 }
949
950 return 1;
951 }
952 #endif // _WINDLL
953
954 //// Static member initialization
955
956 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
957
958 wxApp::wxApp()
959 {
960 argc = 0;
961 argv = NULL;
962 m_printMode = wxPRINT_WINDOWS;
963 m_auto3D = TRUE;
964 }
965
966 wxApp::~wxApp()
967 {
968 // Delete command-line args
969 int i;
970 for (i = 0; i < argc; i++)
971 {
972 delete[] argv[i];
973 }
974 delete[] argv;
975 }
976
977 bool wxApp::Initialized()
978 {
979 #ifndef _WINDLL
980 if (GetTopWindow())
981 return TRUE;
982 else
983 return FALSE;
984 #else // Assume initialized if DLL (no way of telling)
985 return TRUE;
986 #endif
987 }
988
989 /*
990 * Get and process a message, returning FALSE if WM_QUIT
991 * received (and also set the flag telling the app to exit the main loop)
992 *
993 */
994 bool wxApp::DoMessage()
995 {
996 BOOL rc = ::GetMessage(&s_currentMsg, (HWND) NULL, 0, 0);
997 if ( rc == 0 )
998 {
999 // got WM_QUIT
1000 m_keepGoing = FALSE;
1001
1002 return FALSE;
1003 }
1004 else if ( rc == -1 )
1005 {
1006 // should never happen, but let's test for it nevertheless
1007 wxLogLastError(wxT("GetMessage"));
1008 }
1009 else
1010 {
1011 #if wxUSE_THREADS
1012 wxASSERT_MSG( wxThread::IsMain(),
1013 wxT("only the main thread can process Windows messages") );
1014
1015 static bool s_hadGuiLock = TRUE;
1016 static wxMsgArray s_aSavedMessages;
1017
1018 // if a secondary thread owns is doing GUI calls, save all messages for
1019 // later processing - we can't process them right now because it will
1020 // lead to recursive library calls (and we're not reentrant)
1021 if ( !wxGuiOwnedByMainThread() )
1022 {
1023 s_hadGuiLock = FALSE;
1024
1025 // leave out WM_COMMAND messages: too dangerous, sometimes
1026 // the message will be processed twice
1027 if ( !wxIsWaitingForThread() ||
1028 s_currentMsg.message != WM_COMMAND )
1029 {
1030 s_aSavedMessages.Add(s_currentMsg);
1031 }
1032
1033 return TRUE;
1034 }
1035 else
1036 {
1037 // have we just regained the GUI lock? if so, post all of the saved
1038 // messages
1039 //
1040 // FIXME of course, it's not _exactly_ the same as processing the
1041 // messages normally - expect some things to break...
1042 if ( !s_hadGuiLock )
1043 {
1044 s_hadGuiLock = TRUE;
1045
1046 size_t count = s_aSavedMessages.Count();
1047 for ( size_t n = 0; n < count; n++ )
1048 {
1049 MSG& msg = s_aSavedMessages[n];
1050
1051 if ( !ProcessMessage((WXMSG *)&msg) )
1052 {
1053 ::TranslateMessage(&msg);
1054 ::DispatchMessage(&msg);
1055 }
1056 }
1057
1058 s_aSavedMessages.Empty();
1059 }
1060 }
1061 #endif // wxUSE_THREADS
1062
1063 // Process the message
1064 DoMessage((WXMSG *)&s_currentMsg);
1065 }
1066
1067 return TRUE;
1068 }
1069
1070 void wxApp::DoMessage(WXMSG *pMsg)
1071 {
1072 if ( !ProcessMessage(pMsg) )
1073 {
1074 ::TranslateMessage((MSG *)pMsg);
1075 ::DispatchMessage((MSG *)pMsg);
1076 }
1077 }
1078
1079 /*
1080 * Keep trying to process messages until WM_QUIT
1081 * received.
1082 *
1083 * If there are messages to be processed, they will all be
1084 * processed and OnIdle will not be called.
1085 * When there are no more messages, OnIdle is called.
1086 * If OnIdle requests more time,
1087 * it will be repeatedly called so long as there are no pending messages.
1088 * A 'feature' of this is that once OnIdle has decided that no more processing
1089 * is required, then it won't get processing time until further messages
1090 * are processed (it'll sit in DoMessage).
1091 */
1092
1093 int wxApp::MainLoop()
1094 {
1095 m_keepGoing = TRUE;
1096
1097 while ( m_keepGoing )
1098 {
1099 #if wxUSE_THREADS
1100 wxMutexGuiLeaveOrEnter();
1101 #endif // wxUSE_THREADS
1102
1103 while ( !Pending() && ProcessIdle() )
1104 ;
1105
1106 // a message came or no more idle processing to do
1107 DoMessage();
1108 }
1109
1110 return s_currentMsg.wParam;
1111 }
1112
1113 // Returns TRUE if more time is needed.
1114 bool wxApp::ProcessIdle()
1115 {
1116 wxIdleEvent event;
1117 event.SetEventObject(this);
1118 ProcessEvent(event);
1119
1120 return event.MoreRequested();
1121 }
1122
1123 void wxApp::ExitMainLoop()
1124 {
1125 // VZ: why not ::PostQuitMessage()?
1126 m_keepGoing = FALSE;
1127 }
1128
1129 bool wxApp::Pending()
1130 {
1131 return ::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) != 0;
1132 }
1133
1134 void wxApp::Dispatch()
1135 {
1136 DoMessage();
1137 }
1138
1139 /*
1140 * Give all windows a chance to preprocess
1141 * the message. Some may have accelerator tables, or have
1142 * MDI complications.
1143 */
1144
1145 bool wxApp::ProcessMessage(WXMSG *wxmsg)
1146 {
1147 MSG *msg = (MSG *)wxmsg;
1148 HWND hwnd = msg->hwnd;
1149 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
1150
1151 // this may happen if the event occured in a standard modeless dialog (the
1152 // only example of which I know of is the find/replace dialog) - then call
1153 // IsDialogMessage() to make TAB navigation in it work
1154 if ( !wndThis )
1155 {
1156 // we need to find the dialog containing this control as
1157 // IsDialogMessage() just eats all the messages (i.e. returns TRUE for
1158 // them) if we call it for the control itself
1159 while ( hwnd && ::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD )
1160 {
1161 hwnd = ::GetParent(hwnd);
1162 }
1163
1164 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
1165 }
1166
1167 #if wxUSE_TOOLTIPS
1168 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
1169 // popup the tooltip bubbles
1170 if ( (msg->message == WM_MOUSEMOVE) )
1171 {
1172 wxToolTip *tt = wndThis->GetToolTip();
1173 if ( tt )
1174 {
1175 tt->RelayEvent(wxmsg);
1176 }
1177 }
1178 #endif // wxUSE_TOOLTIPS
1179
1180 // allow the window to prevent certain messages from being
1181 // translated/processed (this is currently used by wxTextCtrl to always
1182 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
1183 if ( !wndThis->MSWShouldPreProcessMessage(wxmsg) )
1184 {
1185 return FALSE;
1186 }
1187
1188 // try translations first: the accelerators override everything
1189 wxWindow *wnd;
1190
1191 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
1192 {
1193 if ( wnd->MSWTranslateMessage(wxmsg))
1194 return TRUE;
1195
1196 // stop at first top level window, i.e. don't try to process the key
1197 // strokes originating in a dialog using the accelerators of the parent
1198 // frame - this doesn't make much sense
1199 if ( wnd->IsTopLevel() )
1200 break;
1201 }
1202
1203 // now try the other hooks (kbd navigation is handled here): we start from
1204 // wndThis->GetParent() because wndThis->MSWProcessMessage() was already
1205 // called above
1206 for ( wnd = wndThis->GetParent(); wnd; wnd = wnd->GetParent() )
1207 {
1208 if ( wnd->MSWProcessMessage(wxmsg) )
1209 return TRUE;
1210 }
1211
1212 // no special preprocessing for this message, dispatch it normally
1213 return FALSE;
1214 }
1215
1216 void wxApp::OnIdle(wxIdleEvent& event)
1217 {
1218 static bool s_inOnIdle = FALSE;
1219
1220 // Avoid recursion (via ProcessEvent default case)
1221 if ( s_inOnIdle )
1222 return;
1223
1224 s_inOnIdle = TRUE;
1225
1226 // If there are pending events, we must process them: pending events
1227 // are either events to the threads other than main or events posted
1228 // with wxPostEvent() functions
1229 // GRG: I have moved this here so that all pending events are processed
1230 // before starting to delete any objects. This behaves better (in
1231 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
1232 // behaviour. Changed Feb/2000 before 2.1.14
1233 ProcessPendingEvents();
1234
1235 // 'Garbage' collection of windows deleted with Close().
1236 DeletePendingObjects();
1237
1238 #if wxUSE_LOG
1239 // flush the logged messages if any
1240 wxLog::FlushActive();
1241 #endif // wxUSE_LOG
1242
1243 #if wxUSE_DC_CACHEING
1244 // automated DC cache management: clear the cached DCs and bitmap
1245 // if it's likely that the app has finished with them, that is, we
1246 // get an idle event and we're not dragging anything.
1247 if (!::GetKeyState(MK_LBUTTON) && !::GetKeyState(MK_MBUTTON) && !::GetKeyState(MK_RBUTTON))
1248 wxDC::ClearCache();
1249 #endif // wxUSE_DC_CACHEING
1250
1251 // Send OnIdle events to all windows
1252 if ( SendIdleEvents() )
1253 {
1254 // SendIdleEvents() returns TRUE if at least one window requested more
1255 // idle events
1256 event.RequestMore(TRUE);
1257 }
1258
1259 s_inOnIdle = FALSE;
1260 }
1261
1262 // Send idle event to all top-level windows
1263 bool wxApp::SendIdleEvents()
1264 {
1265 bool needMore = FALSE;
1266
1267 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
1268 while (node)
1269 {
1270 wxWindow* win = node->GetData();
1271 if (SendIdleEvents(win))
1272 needMore = TRUE;
1273 node = node->GetNext();
1274 }
1275
1276 return needMore;
1277 }
1278
1279 // Send idle event to window and all subwindows
1280 bool wxApp::SendIdleEvents(wxWindow* win)
1281 {
1282 bool needMore = FALSE;
1283
1284 wxIdleEvent event;
1285 event.SetEventObject(win);
1286 win->GetEventHandler()->ProcessEvent(event);
1287
1288 if (event.MoreRequested())
1289 needMore = TRUE;
1290
1291 wxNode* node = win->GetChildren().First();
1292 while (node)
1293 {
1294 wxWindow* win = (wxWindow*) node->Data();
1295 if (SendIdleEvents(win))
1296 needMore = TRUE;
1297
1298 node = node->Next();
1299 }
1300 return needMore;
1301 }
1302
1303 void wxApp::DeletePendingObjects()
1304 {
1305 wxNode *node = wxPendingDelete.First();
1306 while (node)
1307 {
1308 wxObject *obj = (wxObject *)node->Data();
1309
1310 delete obj;
1311
1312 if (wxPendingDelete.Member(obj))
1313 delete node;
1314
1315 // Deleting one object may have deleted other pending
1316 // objects, so start from beginning of list again.
1317 node = wxPendingDelete.First();
1318 }
1319 }
1320
1321 void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1322 {
1323 if (GetTopWindow())
1324 GetTopWindow()->Close(TRUE);
1325 }
1326
1327 // Default behaviour: close the application with prompts. The
1328 // user can veto the close, and therefore the end session.
1329 void wxApp::OnQueryEndSession(wxCloseEvent& event)
1330 {
1331 if (GetTopWindow())
1332 {
1333 if (!GetTopWindow()->Close(!event.CanVeto()))
1334 event.Veto(TRUE);
1335 }
1336 }
1337
1338 /* static */
1339 int wxApp::GetComCtl32Version()
1340 {
1341 #ifdef __WXMICROWIN__
1342 return 0;
1343 #else
1344 // cache the result
1345 static int s_verComCtl32 = -1;
1346
1347 wxCRIT_SECT_DECLARE(csComCtl32);
1348 wxCRIT_SECT_LOCKER(lock, csComCtl32);
1349
1350 if ( s_verComCtl32 == -1 )
1351 {
1352 // initally assume no comctl32.dll at all
1353 s_verComCtl32 = 0;
1354
1355 // do we have it?
1356 HMODULE hModuleComCtl32 = ::GetModuleHandle(wxT("COMCTL32"));
1357
1358 // if so, then we can check for the version
1359 if ( hModuleComCtl32 )
1360 {
1361 // try to use DllGetVersion() if available in _headers_
1362 #ifdef DLLVER_PLATFORM_WINDOWS // defined in shlwapi.h
1363 DLLGETVERSIONPROC pfnDllGetVersion = (DLLGETVERSIONPROC)
1364 ::GetProcAddress(hModuleComCtl32, "DllGetVersion");
1365 if ( pfnDllGetVersion )
1366 {
1367 DLLVERSIONINFO dvi;
1368 dvi.cbSize = sizeof(dvi);
1369
1370 HRESULT hr = (*pfnDllGetVersion)(&dvi);
1371 if ( FAILED(hr) )
1372 {
1373 wxLogApiError(_T("DllGetVersion"), hr);
1374 }
1375 else
1376 {
1377 // this is incompatible with _WIN32_IE values, but
1378 // compatible with the other values returned by
1379 // GetComCtl32Version()
1380 s_verComCtl32 = 100*dvi.dwMajorVersion +
1381 dvi.dwMinorVersion;
1382 }
1383 }
1384 #endif
1385 // DllGetVersion() unavailable either during compile or
1386 // run-time, try to guess the version otherwise
1387 if ( !s_verComCtl32 )
1388 {
1389 // InitCommonControlsEx is unique to 4.70 and later
1390 FARPROC theProc = ::GetProcAddress
1391 (
1392 hModuleComCtl32,
1393 "InitCommonControlsEx"
1394 );
1395
1396 if ( !theProc )
1397 {
1398 // not found, must be 4.00
1399 s_verComCtl32 = 400;
1400 }
1401 else
1402 {
1403 // many symbols appeared in comctl32 4.71, could use
1404 // any of them except may be DllInstall
1405 theProc = ::GetProcAddress
1406 (
1407 hModuleComCtl32,
1408 "InitializeFlatSB"
1409 );
1410 if ( !theProc )
1411 {
1412 // not found, must be 4.70
1413 s_verComCtl32 = 470;
1414 }
1415 else
1416 {
1417 // found, must be 4.71
1418 s_verComCtl32 = 471;
1419 }
1420 }
1421 }
1422 }
1423 }
1424
1425 return s_verComCtl32;
1426 #endif
1427 }
1428
1429 void wxExit()
1430 {
1431 wxLogError(_("Fatal error: exiting"));
1432
1433 wxApp::CleanUp();
1434 exit(0);
1435 }
1436
1437 // Yield to incoming messages
1438
1439 static bool gs_inYield = FALSE;
1440
1441 bool wxYield()
1442 {
1443 // disable log flushing from here because a call to wxYield() shouldn't
1444 // normally result in message boxes popping up &c
1445 wxLog::Suspend();
1446
1447 #ifdef __WXDEBUG__
1448 if (gs_inYield)
1449 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1450 #endif
1451
1452 gs_inYield = TRUE;
1453
1454 // we don't want to process WM_QUIT from here - it should be processed in
1455 // the main event loop in order to stop it
1456 MSG msg;
1457 while ( PeekMessage(&msg, (HWND)0, 0, 0, PM_NOREMOVE) &&
1458 msg.message != WM_QUIT )
1459 {
1460 #if wxUSE_THREADS
1461 wxMutexGuiLeaveOrEnter();
1462 #endif // wxUSE_THREADS
1463
1464 if ( !wxTheApp->DoMessage() )
1465 break;
1466 }
1467
1468 // If they are pending events, we must process them.
1469 if (wxTheApp)
1470 wxTheApp->ProcessPendingEvents();
1471
1472 // let the logs be flashed again
1473 wxLog::Resume();
1474
1475 gs_inYield = FALSE;
1476
1477 return TRUE;
1478 }
1479
1480 // Yield to incoming messages; but fail silently if recursion is detected.
1481 bool wxYieldIfNeeded()
1482 {
1483 if (gs_inYield)
1484 return FALSE;
1485
1486 return wxYield();
1487 }
1488
1489 bool wxHandleFatalExceptions(bool doit)
1490 {
1491 #if wxUSE_ON_FATAL_EXCEPTION
1492 // assume this can only be called from the main thread
1493 gs_handleExceptions = doit;
1494
1495 return TRUE;
1496 #else
1497 wxFAIL_MSG(_T("set wxUSE_ON_FATAL_EXCEPTION to 1 to sue this function"));
1498
1499 (void)doit;
1500 return FALSE;
1501 #endif
1502 }
1503
1504 //-----------------------------------------------------------------------------
1505 // wxWakeUpIdle
1506 //-----------------------------------------------------------------------------
1507
1508 void wxWakeUpIdle()
1509 {
1510 // Send the top window a dummy message so idle handler processing will
1511 // start up again. Doing it this way ensures that the idle handler
1512 // wakes up in the right thread (see also wxWakeUpMainThread() which does
1513 // the same for the main app thread only)
1514 wxWindow *topWindow = wxTheApp->GetTopWindow();
1515 if ( topWindow )
1516 {
1517 if ( !::PostMessage(GetHwndOf(topWindow), WM_NULL, 0, 0) )
1518 {
1519 // should never happen
1520 wxLogLastError(wxT("PostMessage(WM_NULL)"));
1521 }
1522 }
1523 }
1524
1525 //-----------------------------------------------------------------------------
1526
1527 wxIcon
1528 wxApp::GetStdIcon(int which) const
1529 {
1530 switch(which)
1531 {
1532 case wxICON_INFORMATION:
1533 return wxIcon("wxICON_INFO");
1534
1535 case wxICON_QUESTION:
1536 return wxIcon("wxICON_QUESTION");
1537
1538 case wxICON_EXCLAMATION:
1539 return wxIcon("wxICON_WARNING");
1540
1541 default:
1542 wxFAIL_MSG(wxT("requested non existent standard icon"));
1543 // still fall through
1544
1545 case wxICON_HAND:
1546 return wxIcon("wxICON_ERROR");
1547 }
1548 }
1549
1550 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1551 // if in a separate file. So include it here to ensure it's linked.
1552 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__) && !defined(WXMAKINGDLL))
1553 #include "main.cpp"
1554 #endif