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