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