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