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