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