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