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