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