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