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