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