]> git.saurik.com Git - wxWidgets.git/blame - src/msw/app.cpp
wxCriticalSection changes undone - should work now
[wxWidgets.git] / src / msw / app.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
3// Purpose: wxApp
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
a5e0e655 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
e5c0b16a
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
e5c0b16a 21 #pragma implementation "app.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
2bda0e17
KB
25#include "wx/wxprec.h"
26
27#if defined(__BORLANDC__)
e5c0b16a 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
e5c0b16a
VZ
32 #include "wx/frame.h"
33 #include "wx/app.h"
34 #include "wx/utils.h"
35 #include "wx/gdicmn.h"
36 #include "wx/pen.h"
37 #include "wx/brush.h"
38 #include "wx/cursor.h"
39 #include "wx/icon.h"
40 #include "wx/palette.h"
41 #include "wx/dc.h"
42 #include "wx/dialog.h"
43 #include "wx/msgdlg.h"
44 #include "wx/intl.h"
45 #include "wx/dynarray.h"
2bda0e17
KB
46#endif
47
48#include "wx/msw/private.h"
2bda0e17
KB
49#include "wx/log.h"
50#include "wx/module.h"
4bf78aae
JS
51
52#if wxUSE_THREADS
bee503b0
VZ
53 #include "wx/thread.h"
54
55 // define the array of MSG strutures
56 WX_DECLARE_OBJARRAY(MSG, wxMsgArray);
57
58 #include "wx/arrimpl.cpp"
59
60 WX_DEFINE_OBJARRAY(wxMsgArray);
61#endif // wxUSE_THREADS
2bda0e17 62
47d67540 63#if wxUSE_WX_RESOURCES
e5c0b16a 64 #include "wx/resource.h"
2bda0e17
KB
65#endif
66
e5c0b16a
VZ
67// OLE is used for drag-and-drop, clipboard, OLE Automation...
68#if defined(__GNUWIN32__) || defined(__SC__) || defined(__SALFORDC__)
69 #undef wxUSE_OLE
70
71 #define wxUSE_OLE 0
72#endif // broken compilers
73
74#if wxUSE_OLE
6e0d9d43 75 #include <ole2.h>
d05237ea 76#endif
ce3ed50d 77
2bda0e17 78#include <string.h>
a5e0e655 79#include <ctype.h>
2bda0e17 80
57c208c5 81#if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
e5c0b16a 82 #include <commctrl.h>
2bda0e17
KB
83#endif
84
6e0d9d43 85#include "wx/msw/msvcrt.h"
370382c7 86
e5c0b16a
VZ
87// ---------------------------------------------------------------------------
88// global variables
89// ---------------------------------------------------------------------------
90
2bda0e17
KB
91extern char *wxBuffer;
92extern char *wxOsVersion;
93extern wxList *wxWinHandleList;
cde9f08e 94extern wxList WXDLLEXPORT wxPendingDelete;
2bda0e17
KB
95extern void wxSetKeyboardHook(bool doIt);
96extern wxCursor *g_globalCursor;
97
c4e7c2aa 98HINSTANCE wxhInstance = 0;
2bda0e17
KB
99static MSG s_currentMsg;
100wxApp *wxTheApp = NULL;
101
e5c0b16a 102// FIXME why not const? and not static?
2bda0e17
KB
103char wxFrameClassName[] = "wxFrameClass";
104char wxMDIFrameClassName[] = "wxMDIFrameClass";
105char wxMDIChildFrameClassName[] = "wxMDIChildFrameClass";
106char wxPanelClassName[] = "wxPanelClass";
107char wxCanvasClassName[] = "wxCanvasClass";
108
57c208c5
JS
109HICON wxSTD_FRAME_ICON = (HICON) NULL;
110HICON wxSTD_MDICHILDFRAME_ICON = (HICON) NULL;
111HICON wxSTD_MDIPARENTFRAME_ICON = (HICON) NULL;
2bda0e17 112
57c208c5
JS
113HICON wxDEFAULT_FRAME_ICON = (HICON) NULL;
114HICON wxDEFAULT_MDICHILDFRAME_ICON = (HICON) NULL;
115HICON wxDEFAULT_MDIPARENTFRAME_ICON = (HICON) NULL;
2bda0e17 116
57c208c5 117HBRUSH wxDisableButtonBrush = (HBRUSH) 0;
2bda0e17
KB
118
119LRESULT APIENTRY wxWndProc(HWND, UINT, WPARAM, LPARAM);
120
57c208c5 121#if defined(__WIN95__) && !defined(__TWIN32__)
e5c0b16a 122 #define wxUSE_RICHEDIT 1
57c208c5 123#else
e5c0b16a 124 #define wxUSE_RICHEDIT 0
57c208c5
JS
125#endif
126
127#if wxUSE_RICHEDIT
e5c0b16a 128 static HINSTANCE gs_hRichEdit = (HINSTANCE) NULL;
2bda0e17
KB
129#endif
130
e5c0b16a
VZ
131// ===========================================================================
132// implementation
133// ===========================================================================
589f0e3e 134
e5c0b16a
VZ
135// ---------------------------------------------------------------------------
136// wxApp
137// ---------------------------------------------------------------------------
138
139#if !USE_SHARED_LIBRARY
140 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
141
142 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
143 EVT_IDLE(wxApp::OnIdle)
144 EVT_END_SESSION(wxApp::OnEndSession)
145 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
146 END_EVENT_TABLE()
147#endif
148
149long wxApp::sm_lastMessageTime = 0;
150
151//// Initialize
589f0e3e 152bool wxApp::Initialize()
2bda0e17 153{
c030b70f
JS
154 // Some people may wish to use this, but
155 // probably it shouldn't be here by default.
156#ifdef __WXDEBUG__
e5c0b16a 157 // wxRedirectIOToConsole();
c030b70f
JS
158#endif
159
e5c0b16a 160 wxBuffer = new char[1500]; // FIXME
589f0e3e 161
aa0b7e1e 162 wxClassInfo::InitializeClasses();
589f0e3e 163
aa0b7e1e 164#if wxUSE_RESOURCES
a5e0e655 165 wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
aa0b7e1e 166#endif
589f0e3e 167
aa0b7e1e
JS
168 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
169 wxTheColourDatabase->Initialize();
589f0e3e 170
aa0b7e1e
JS
171 wxInitializeStockLists();
172 wxInitializeStockObjects();
589f0e3e 173
aa0b7e1e 174#if wxUSE_WX_RESOURCES
a5e0e655 175 wxInitializeResourceSystem();
aa0b7e1e 176#endif
2bda0e17 177
aa0b7e1e 178 wxBitmap::InitStandardHandlers();
2bda0e17 179
aa0b7e1e 180#if defined(__WIN95__)
a5e0e655 181 InitCommonControls();
57c208c5
JS
182
183#if wxUSE_RICHEDIT
a5e0e655 184 gs_hRichEdit = LoadLibrary("RICHED32.DLL");
2bda0e17 185
57c208c5 186 if (gs_hRichEdit == (HINSTANCE) NULL)
a5e0e655 187 {
e5c0b16a 188 wxLogError(_("Could not initialise Rich Edit DLL"));
a5e0e655 189 }
e5c0b16a 190#endif // wxUSE_RICHEDIT
57c208c5 191
e5c0b16a 192#endif // __WIN95__
2bda0e17 193
e5c0b16a 194 // for OLE, enlarge message queue to be as large as possible
aa0b7e1e 195 int iMsg = 96;
e5c0b16a 196 while (!SetMessageQueue(iMsg) && (iMsg -= 8));
4a9968f9 197
e5c0b16a
VZ
198 /*
199 DWORD dwOleVer;
200 dwOleVer = CoBuildVersion();
4a9968f9 201
aa0b7e1e
JS
202 // check the OLE library version
203 if (rmm != HIWORD(dwOleVer))
204 {
e5c0b16a
VZ
205 wxMessageBox("Incorrect version of OLE libraries.");
206 return FALSE;
aa0b7e1e 207 }
e5c0b16a 208 */
5de5db0e 209
e5c0b16a 210#if wxUSE_OLE
a5e0e655
VZ
211 // we need to initialize OLE library
212 if ( FAILED(::OleInitialize(NULL)) )
e5c0b16a 213 wxLogError(_("Cannot initialize OLE"));
5de5db0e 214#endif
2bda0e17 215
1f112209 216#if wxUSE_CTL3D
a5e0e655 217 if (!Ctl3dRegister(wxhInstance))
e5c0b16a 218 wxLogError("Cannot register CTL3D");
2bda0e17 219
a5e0e655 220 Ctl3dAutoSubclass(wxhInstance);
aa0b7e1e 221#endif
2bda0e17 222
aa0b7e1e 223 g_globalCursor = new wxCursor;
589f0e3e 224
aa0b7e1e
JS
225 wxSTD_FRAME_ICON = LoadIcon(wxhInstance, "wxSTD_FRAME");
226 wxSTD_MDIPARENTFRAME_ICON = LoadIcon(wxhInstance, "wxSTD_MDIPARENTFRAME");
227 wxSTD_MDICHILDFRAME_ICON = LoadIcon(wxhInstance, "wxSTD_MDICHILDFRAME");
2bda0e17 228
aa0b7e1e
JS
229 wxDEFAULT_FRAME_ICON = LoadIcon(wxhInstance, "wxDEFAULT_FRAME");
230 wxDEFAULT_MDIPARENTFRAME_ICON = LoadIcon(wxhInstance, "wxDEFAULT_MDIPARENTFRAME");
231 wxDEFAULT_MDICHILDFRAME_ICON = LoadIcon(wxhInstance, "wxDEFAULT_MDICHILDFRAME");
2bda0e17 232
aa0b7e1e 233 RegisterWindowClasses();
2bda0e17 234
aa0b7e1e 235 // Create the brush for disabling bitmap buttons
2bda0e17 236
aa0b7e1e
JS
237 LOGBRUSH lb ;
238 lb.lbStyle = BS_PATTERN;
239 lb.lbHatch = (int)LoadBitmap( wxhInstance, "wxDISABLE_BUTTON_BITMAP" ) ;
240 wxDisableButtonBrush = ::CreateBrushIndirect( & lb ) ;
241 ::DeleteObject( (HGDIOBJ)lb.lbHatch ) ;
2bda0e17 242
aa0b7e1e 243#if wxUSE_PENWINDOWS
a5e0e655 244 wxRegisterPenWin();
aa0b7e1e 245#endif
2bda0e17 246
aa0b7e1e 247 wxWinHandleList = new wxList(wxKEY_INTEGER);
2bda0e17 248
6e0d9d43 249 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
8cbd2bde 250 // PLEASE DO NOT ALTER THIS.
3f4a0c5b 251#if defined(__VISUALC__) && !defined(WXMAKINGDLL)
a5e0e655
VZ
252 extern char wxDummyChar;
253 if (wxDummyChar) wxDummyChar++;
aa0b7e1e 254#endif
a5e0e655 255
aa0b7e1e 256 wxSetKeyboardHook(TRUE);
2bda0e17 257
aa0b7e1e
JS
258 wxModule::RegisterModules();
259 if (!wxModule::InitializeModules())
260 return FALSE;
261 return TRUE;
2bda0e17
KB
262}
263
589f0e3e
JS
264//// RegisterWindowClasses
265
bb6290e3 266bool wxApp::RegisterWindowClasses()
2bda0e17 267{
e5c0b16a
VZ
268 ///////////////////////////////////////////////////////////////////////
269 // Register the frame window class.
270 WNDCLASS wndclass; // Structure used to register Windows class.
271
272 wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
273 wndclass.lpfnWndProc = (WNDPROC)wxWndProc;
274 wndclass.cbClsExtra = 0;
275 wndclass.cbWndExtra = sizeof( DWORD ); // was 4
276 wndclass.hInstance = wxhInstance;
277 wndclass.hIcon = (HICON) NULL; // wxSTD_FRAME_ICON;
278 wndclass.hCursor = LoadCursor( (HINSTANCE) NULL, IDC_ARROW );
279 wndclass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
280 // wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
281 wndclass.lpszMenuName = NULL;
2bda0e17 282#ifdef _MULTIPLE_INSTANCES
e5c0b16a 283 sprintf( wxFrameClassName,"wxFrameClass%d", wxhInstance );
2bda0e17 284#endif
e5c0b16a
VZ
285 wndclass.lpszClassName = wxFrameClassName;
286
287 if (!RegisterClass( &wndclass ))
288 {
289 // wxFatalError("Can't register Frame Window class");
290 }
291
292 ///////////////////////////////////////////////////////////////////////
293 // Register the MDI frame window class.
294 WNDCLASS wndclass1; // Structure used to register Windows class.
295
296 wndclass1.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
297 wndclass1.lpfnWndProc = (WNDPROC)wxWndProc;
298 wndclass1.cbClsExtra = 0;
299 wndclass1.cbWndExtra = sizeof( DWORD ); // was 4
300 wndclass1.hInstance = wxhInstance;
301 wndclass1.hIcon = (HICON) NULL; // wxSTD_MDIPARENTFRAME_ICON;
302 wndclass1.hCursor = LoadCursor( (HINSTANCE) NULL, IDC_ARROW );
303 // wndclass1.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
304 wndclass1.hbrBackground = (HBRUSH) NULL;
305 wndclass1.lpszMenuName = NULL;
306
307 wndclass1.lpszClassName = wxMDIFrameClassName;
308 if (!RegisterClass( &wndclass1 ))
309 {
310 // wxFatalError("Can't register MDI Frame window class");
311 // return FALSE;
312 }
313
314 ///////////////////////////////////////////////////////////////////////
315 // Register the MDI child frame window class.
316 WNDCLASS wndclass4; // Structure used to register Windows class.
317
318 wndclass4.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
319 wndclass4.lpfnWndProc = (WNDPROC)wxWndProc;
320 wndclass4.cbClsExtra = 0;
321 wndclass4.cbWndExtra = sizeof( DWORD ); // was 4
322 wndclass4.hInstance = wxhInstance;
323 wndclass4.hIcon = (HICON) NULL; // wxSTD_MDICHILDFRAME_ICON;
324 wndclass4.hCursor = LoadCursor( (HINSTANCE) NULL, IDC_ARROW );
325 // TODO: perhaps this should be NULL so that Windows doesn't
326 // paint the background itself (would OnEraseBackground duplicate
327 // this?)
328 wndclass4.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
329 // wndclass4.hbrBackground = NULL;
330 wndclass4.lpszMenuName = NULL;
331 wndclass4.lpszClassName = wxMDIChildFrameClassName;
332
333 if (!RegisterClass( &wndclass4 ))
334 {
335 // wxFatalError("Can't register MDI child frame window class");
336 // return FALSE;
337 }
338
339 ///////////////////////////////////////////////////////////////////////
340 // Register the panel window class.
341 WNDCLASS wndclass2; // Structure used to register Windows class.
342 memset(&wndclass2, 0, sizeof(WNDCLASS)); // start with NULL defaults
343 wndclass2.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
344 wndclass2.lpfnWndProc = (WNDPROC)wxWndProc;
345 wndclass2.cbClsExtra = 0;
346 wndclass2.cbWndExtra = sizeof( DWORD ); // was 4
347 wndclass2.hInstance = wxhInstance;
348 wndclass2.hIcon = (HICON) NULL;
1aff8b02 349 wndclass2.hCursor = LoadCursor( (HINSTANCE) NULL, IDC_ARROW );
e5c0b16a
VZ
350 // wndclass2.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1) ;
351 wndclass2.hbrBackground = (HBRUSH) GetStockObject( LTGRAY_BRUSH );
352 wndclass2.lpszMenuName = NULL;
353 wndclass2.lpszClassName = wxPanelClassName;
354 if (!RegisterClass( &wndclass2 ))
355 {
356 // wxFatalError("Can't register Panel Window class");
357 // return FALSE;
358 }
359
360 ///////////////////////////////////////////////////////////////////////
361 // Register the canvas and textsubwindow class name
362 WNDCLASS wndclass3; // Structure used to register Windows class.
363 memset(&wndclass3, 0, sizeof(WNDCLASS)); // start with NULL defaults
364 // Use CS_OWNDC to avoid messing about restoring the context
365 // for every graphic operation.
366 // wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS ;
367 // wxWin 2.0
368 wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
369 wndclass3.lpfnWndProc = (WNDPROC)wxWndProc;
370 wndclass3.cbClsExtra = 0;
371 wndclass3.cbWndExtra = sizeof( DWORD ); // was 4
372 wndclass3.hInstance = wxhInstance;
373 wndclass3.hIcon = (HICON) NULL;
1aff8b02 374 wndclass3.hCursor = LoadCursor( (HINSTANCE) NULL, IDC_ARROW );
e5c0b16a
VZ
375 // wndclass3.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
376 wndclass3.hbrBackground = (HBRUSH) NULL;
377 wndclass3.lpszMenuName = NULL;
378 wndclass3.lpszClassName = wxCanvasClassName;
379 if (!RegisterClass( &wndclass3))
380 {
381 // wxFatalError("Can't register Canvas class");
382 // return FALSE;
383 }
384
385 return TRUE;
2bda0e17
KB
386}
387
589f0e3e
JS
388//// Convert Windows to argc, argv style
389
390void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine)
391{
da36f544 392 wxStringList args;
589f0e3e 393
da36f544
JS
394 wxString cmdLine(lpCmdLine);
395 int count = 0;
589f0e3e 396
da36f544 397 // Get application name
c085e333 398 char name[260]; // 260 is MAX_PATH value from windef.h
da36f544
JS
399 ::GetModuleFileName(wxhInstance, name, WXSIZEOF(name));
400
da36f544 401 args.Add(name);
50ef256e 402 count++;
589f0e3e 403
da36f544
JS
404 strcpy(name, wxFileNameFromPath(name));
405 wxStripExtension(name);
406 wxTheApp->SetAppName(name);
589f0e3e 407
da36f544
JS
408 // Break up string
409 // Treat strings enclosed in double-quotes as single arguments
410 int i = 0;
411 int len = cmdLine.Length();
412 while (i < len)
a5e0e655 413 {
da36f544
JS
414 // Skip whitespace
415 while ((i < len) && isspace(cmdLine.GetChar(i)))
416 i ++;
417
418 if (i < len)
419 {
420 if (cmdLine.GetChar(i) == '"') // We found the start of a string
421 {
422 i ++;
423 int first = i;
424 while ((i < len) && (cmdLine.GetChar(i) != '"'))
425 i ++;
426
427 wxString arg(cmdLine.Mid(first, (i - first)));
428
429 args.Add(arg);
430 count ++;
431
432 if (i < len)
433 i ++; // Skip past 2nd quote
434 }
435 else // Unquoted argument
436 {
437 int first = i;
438 while ((i < len) && !isspace(cmdLine.GetChar(i)))
439 i ++;
440
441 wxString arg(cmdLine.Mid(first, (i - first)));
442
443 args.Add(arg);
444 count ++;
445 }
446 }
a5e0e655 447 }
a5e0e655 448
da36f544
JS
449 wxTheApp->argv = new char*[count + 1];
450 for (i = 0; i < count; i++)
a5e0e655 451 {
da36f544
JS
452 wxString arg(args[i]);
453 wxTheApp->argv[i] = copystring((const char*)arg);
a5e0e655 454 }
da36f544
JS
455 wxTheApp->argv[count] = NULL; // argv[] is a NULL-terminated list
456 wxTheApp->argc = count;
589f0e3e
JS
457}
458
459//// Cleans up any wxWindows internal structures left lying around
460
bb6290e3 461void wxApp::CleanUp()
2bda0e17 462{
e5c0b16a
VZ
463 //// COMMON CLEANUP
464
465 // flush the logged messages if any and install a 'safer' log target: the
466 // default one (wxLogGui) can't be used after the resources are freed just
467 // below and the user suppliedo ne might be even more unsafe (using any
468 // wxWindows GUI function is unsafe starting from now)
469 wxLog::DontCreateOnDemand();
f5e5bd66 470
e5c0b16a
VZ
471 // this will flush the old messages if any
472 delete wxLog::SetActiveTarget(new wxLogStderr);
f5e5bd66 473
e5c0b16a
VZ
474 // One last chance for pending objects to be cleaned up
475 wxTheApp->DeletePendingObjects();
f5e5bd66 476
e5c0b16a 477 wxModule::CleanUpModules();
2bda0e17 478
47d67540 479#if wxUSE_WX_RESOURCES
e5c0b16a 480 wxCleanUpResourceSystem();
589f0e3e 481
e5c0b16a 482 // wxDefaultResourceTable->ClearTable();
589f0e3e
JS
483#endif
484
e5c0b16a
VZ
485 // Indicate that the cursor can be freed, so that cursor won't be deleted
486 // by deleting the bitmap list before g_globalCursor goes out of scope
487 // (double deletion of the cursor).
488 wxSetCursor(wxNullCursor);
489 delete g_globalCursor;
2a47d3c1 490 g_globalCursor = NULL;
589f0e3e 491
e5c0b16a 492 wxDeleteStockObjects() ;
589f0e3e 493
e5c0b16a
VZ
494 // Destroy all GDI lists, etc.
495 wxDeleteStockLists();
589f0e3e 496
e5c0b16a
VZ
497 delete wxTheColourDatabase;
498 wxTheColourDatabase = NULL;
589f0e3e 499
e5c0b16a 500 wxBitmap::CleanUpHandlers();
589f0e3e 501
e5c0b16a
VZ
502 delete[] wxBuffer;
503 wxBuffer = NULL;
589f0e3e 504
e5c0b16a 505 //// WINDOWS-SPECIFIC CLEANUP
2bda0e17 506
e5c0b16a 507 wxSetKeyboardHook(FALSE);
2bda0e17
KB
508
509#ifdef __WIN95__
57c208c5
JS
510
511#if wxUSE_RICHEDIT
e5c0b16a
VZ
512 if (gs_hRichEdit != (HINSTANCE) NULL)
513 FreeLibrary(gs_hRichEdit);
2bda0e17
KB
514#endif
515
57c208c5
JS
516#endif
517
47d67540 518#if wxUSE_PENWINDOWS
e5c0b16a 519 wxCleanUpPenWin();
2bda0e17
KB
520#endif
521
e5c0b16a
VZ
522 if (wxSTD_FRAME_ICON)
523 DestroyIcon(wxSTD_FRAME_ICON);
524 if (wxSTD_MDICHILDFRAME_ICON)
525 DestroyIcon(wxSTD_MDICHILDFRAME_ICON);
526 if (wxSTD_MDIPARENTFRAME_ICON)
527 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON);
528
529 if (wxDEFAULT_FRAME_ICON)
530 DestroyIcon(wxDEFAULT_FRAME_ICON);
531 if (wxDEFAULT_MDICHILDFRAME_ICON)
532 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON);
533 if (wxDEFAULT_MDIPARENTFRAME_ICON)
534 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON);
535
536 if ( wxDisableButtonBrush )
537 ::DeleteObject( wxDisableButtonBrush ) ;
538
539#if wxUSE_OLE
540 ::OleUninitialize();
5de5db0e 541#endif
2bda0e17 542
1f112209 543#if wxUSE_CTL3D
e5c0b16a 544 Ctl3dUnregister(wxhInstance);
2bda0e17
KB
545#endif
546
e5c0b16a
VZ
547 if (wxWinHandleList)
548 delete wxWinHandleList ;
d50b2a58 549
e5c0b16a 550 wxClassInfo::CleanUpClasses();
0c32066b 551
e5c0b16a
VZ
552 delete wxTheApp;
553 wxTheApp = NULL;
184b5d99
JS
554
555#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
e5c0b16a
VZ
556 // At this point we want to check if there are any memory
557 // blocks that aren't part of the wxDebugContext itself,
558 // as a special case. Then when dumping we need to ignore
559 // wxDebugContext, too.
560 if (wxDebugContext::CountObjectsLeft() > 0)
561 {
562 wxLogDebug("There were memory leaks.");
563 wxDebugContext::Dump();
564 wxDebugContext::PrintStatistics();
565 }
566 // wxDebugContext::SetStream(NULL, NULL);
184b5d99
JS
567#endif
568
e5c0b16a
VZ
569 // do it as the very last thing because everything else can log messages
570 delete wxLog::SetActiveTarget(NULL);
2bda0e17
KB
571}
572
2bda0e17
KB
573#if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
574
e5c0b16a
VZ
575// temporarily disable this warning which would be generated in release builds
576// because of __try
3f4a0c5b 577#ifdef __VISUALC__
f8a3e080
VZ
578 #pragma warning(disable: 4715) // not all control paths return a value
579#endif // Visual C++
580
589f0e3e 581//// Main wxWindows entry point
a5e0e655
VZ
582int wxEntry(WXHINSTANCE hInstance,
583 WXHINSTANCE WXUNUSED(hPrevInstance),
584 char *lpCmdLine,
585 int nCmdShow,
586 bool enterLoop)
2bda0e17 587{
6e0d9d43
VZ
588 // do check for memory leaks on program exit
589 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
590 // deallocated memory which may be used to simulate low-memory condition)
591 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
592
e5c0b16a
VZ
593 // take everything into a try-except block in release build
594 // FIXME other compilers must support Win32 SEH (structured exception
595 // handling) too, just find the appropriate keyword in their docs!
596 // Please note that it's _not_ the same as C++ exceptions!
3f4a0c5b 597#if !defined(__WXDEBUG__) && defined(__VISUALC__)
f8a3e080
VZ
598 #define CATCH_PROGRAM_EXCEPTIONS
599
3f4a0c5b 600 __try {
f8a3e080
VZ
601#else
602 #undef CATCH_PROGRAM_EXCEPTIONS
a5e0e655
VZ
603#endif
604
e5c0b16a 605 wxhInstance = (HINSTANCE) hInstance;
2bda0e17 606
e5c0b16a
VZ
607 if (!wxApp::Initialize())
608 return 0;
2bda0e17 609
e5c0b16a
VZ
610 // create the application object or ensure that one already exists
611 if (!wxTheApp)
612 {
613 // The app may have declared a global application object, but we recommend
614 // the IMPLEMENT_APP macro is used instead, which sets an initializer
615 // function for delayed, dynamic app object construction.
616 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
617 "No initializer - use IMPLEMENT_APP macro." );
f8a3e080 618
e5c0b16a
VZ
619 wxTheApp = (*wxApp::GetInitializerFunction()) ();
620 }
621
622 wxCHECK_MSG( wxTheApp, 0, "You have to define an instance of wxApp!" );
623
624 // save the WinMain() parameters
625 wxTheApp->ConvertToStandardCommandArgs(lpCmdLine);
626 wxTheApp->m_nCmdShow = nCmdShow;
a5e0e655 627
e5c0b16a
VZ
628 // GUI-specific initialisation. In fact on Windows we don't have any,
629 // but this call is provided for compatibility across platforms.
630 wxTheApp->OnInitGui() ;
f8a3e080 631
e5c0b16a
VZ
632 int retValue = 0;
633
634 if ( wxTheApp->OnInit() )
635 {
636 if ( enterLoop )
637 {
638 retValue = wxTheApp->OnRun();
639 }
640 else
641 // We want to initialize, but not run or exit immediately.
642 return 1;
643 }
644 //else: app initialization failed, so we skipped OnRun()
645
646 wxWindow *topWindow = wxTheApp->GetTopWindow();
647 if ( topWindow )
648 {
649 // Forcibly delete the window.
650 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
651 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
652 {
653 topWindow->Close(TRUE);
654 wxTheApp->DeletePendingObjects();
655 }
656 else
657 {
658 delete topWindow;
659 wxTheApp->SetTopWindow(NULL);
660 }
661 }
662
663 wxTheApp->OnExit();
664
665 wxApp::CleanUp();
666
667 return retValue;
668
669#ifdef CATCH_PROGRAM_EXCEPTIONS
670 }
671 __except ( EXCEPTION_EXECUTE_HANDLER ) {
672 /*
673 if ( wxTheApp )
674 wxTheApp->OnFatalException();
675 */
676
677 // using wxLog would be unsafe here
678 ::MessageBox(NULL,
679 _("Unrecoverable program error detected: "
680 " the application will terminate."),
681 _("Fatal Error"),
682 MB_APPLMODAL | MB_ICONSTOP | MB_OK);
683
684 ::ExitProcess(3); // the same exit code as abort()
685
686 // NOTREACHED
687 }
f8a3e080 688#endif // CATCH_PROGRAM_EXCEPTIONS
2bda0e17
KB
689}
690
f8a3e080 691// restore warning state
3f4a0c5b 692#ifdef __VISUALC__
f8a3e080
VZ
693 #pragma warning(default: 4715) // not all control paths return a value
694#endif // Visual C++
695
2bda0e17
KB
696#else /* _WINDLL */
697
589f0e3e
JS
698//// Entry point for DLLs
699
2bda0e17
KB
700int wxEntry(WXHINSTANCE hInstance)
701{
e5c0b16a
VZ
702 wxhInstance = (HINSTANCE) hInstance;
703 wxApp::Initialize();
2bda0e17 704
e5c0b16a
VZ
705 // The app may have declared a global application object, but we recommend
706 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
707 // for delayed, dynamic app object construction.
708 if (!wxTheApp)
a5e0e655 709 {
e5c0b16a
VZ
710 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
711 "No initializer - use IMPLEMENT_APP macro." );
2bda0e17 712
e5c0b16a
VZ
713 wxTheApp = (* wxApp::GetInitializerFunction()) ();
714 }
2bda0e17 715
e5c0b16a 716 wxCHECK_MSG( wxTheApp, 0, "You have to define an instance of wxApp!" );
2bda0e17 717
e5c0b16a
VZ
718 wxTheApp->argc = 0;
719 wxTheApp->argv = NULL;
2bda0e17 720
e5c0b16a 721 wxTheApp->OnInitGui();
2bda0e17 722
e5c0b16a 723 wxTheApp->OnInit();
2bda0e17 724
e5c0b16a
VZ
725 wxWindow *topWindow = wxTheApp->GetTopWindow();
726 if ( topWindow && topWindow->GetHWND())
727 {
728 topWindow->Show(TRUE);
729 }
2bda0e17 730
e5c0b16a 731 return 1;
2bda0e17
KB
732}
733#endif // _WINDLL
734
589f0e3e
JS
735//// Static member initialization
736
2bda0e17
KB
737wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
738
bb6290e3 739wxApp::wxApp()
2bda0e17 740{
e5c0b16a
VZ
741 m_topWindow = NULL;
742 wxTheApp = this;
743 m_className = "";
744 m_wantDebugOutput = TRUE ;
745 m_appName = "";
746 argc = 0;
747 argv = NULL;
2049ba38 748#ifdef __WXMSW__
e5c0b16a 749 m_printMode = wxPRINT_WINDOWS;
2bda0e17 750#else
e5c0b16a 751 m_printMode = wxPRINT_POSTSCRIPT;
2bda0e17 752#endif
e5c0b16a
VZ
753 m_exitOnFrameDelete = TRUE;
754 m_auto3D = TRUE;
2bda0e17
KB
755}
756
589f0e3e
JS
757wxApp::~wxApp()
758{
e5c0b16a
VZ
759 // Delete command-line args
760 int i;
761 for (i = 0; i < argc; i++)
762 {
763 delete[] argv[i];
764 }
765 delete[] argv;
589f0e3e
JS
766}
767
bb6290e3 768bool wxApp::Initialized()
2bda0e17
KB
769{
770#ifndef _WINDLL
e5c0b16a
VZ
771 if (GetTopWindow())
772 return TRUE;
773 else
774 return FALSE;
2bda0e17
KB
775#endif
776#ifdef _WINDLL // Assume initialized if DLL (no way of telling)
e5c0b16a 777 return TRUE;
2bda0e17
KB
778#endif
779}
780
781/*
782 * Get and process a message, returning FALSE if WM_QUIT
bee503b0 783 * received (and also set the flag telling the app to exit the main loop)
2bda0e17
KB
784 *
785 */
bb6290e3 786bool wxApp::DoMessage()
2bda0e17 787{
bee503b0
VZ
788 BOOL rc = ::GetMessage(&s_currentMsg, (HWND) NULL, 0, 0);
789 if ( rc == 0 )
790 {
791 // got WM_QUIT
792 m_keepGoing = FALSE;
4a9968f9 793
bee503b0
VZ
794 return FALSE;
795 }
796 else if ( rc == -1 )
797 {
798 // should never happen, but let's test for it nevertheless
799 wxLogLastError("GetMessage");
800 }
801 else
802 {
803#if wxUSE_THREADS
804 wxASSERT_MSG( wxThread::IsMain(),
805 "only the main thread can process Windows messages" );
d50b2a58 806
bee503b0
VZ
807 static bool s_hadGuiLock = TRUE;
808 static wxMsgArray s_aSavedMessages;
809
810 // if a secondary thread owns is doing GUI calls, save all messages for
811 // later processing - we can't process them right now because it will
812 // lead to recursive library calls (and we're not reentrant)
813 if ( !wxGuiOwnedByMainThread() )
814 {
815 s_hadGuiLock = FALSE;
816
4a9968f9
VZ
817 // leave out WM_COMMAND messages: too dangerous, sometimes
818 // the message will be processed twice
819 if ( !wxIsWaitingForThread() ||
e5c0b16a 820 s_currentMsg.message != WM_COMMAND )
4a9968f9
VZ
821 {
822 s_aSavedMessages.Add(s_currentMsg);
823 }
bee503b0
VZ
824
825 return TRUE;
826 }
827 else
828 {
829 // have we just regained the GUI lock? if so, post all of the saved
830 // messages
831 //
832 // FIXME of course, it's not _exactly_ the same as processing the
833 // messages normally - expect some things to break...
834 if ( !s_hadGuiLock )
835 {
836 s_hadGuiLock = TRUE;
837
838 size_t count = s_aSavedMessages.Count();
839 for ( size_t n = 0; n < count; n++ )
840 {
841 MSG& msg = s_aSavedMessages[n];
842
843 if ( !ProcessMessage((WXMSG *)&msg) )
844 {
845 ::TranslateMessage(&msg);
846 ::DispatchMessage(&msg);
847 }
848 }
849
850 s_aSavedMessages.Empty();
851 }
852 }
853#endif // wxUSE_THREADS
854
855 // Process the message
856 if ( !ProcessMessage((WXMSG *)&s_currentMsg) )
857 {
858 ::TranslateMessage(&s_currentMsg);
859 wxApp::sm_lastMessageTime = s_currentMsg.time; /* MATTHEW: timeStamp impl. */
860 ::DispatchMessage(&s_currentMsg);
861 }
862 }
863
864 return TRUE;
2bda0e17
KB
865}
866
867/*
868 * Keep trying to process messages until WM_QUIT
869 * received.
870 *
871 * If there are messages to be processed, they will all be
872 * processed and OnIdle will not be called.
873 * When there are no more messages, OnIdle is called.
874 * If OnIdle requests more time,
875 * it will be repeatedly called so long as there are no pending messages.
876 * A 'feature' of this is that once OnIdle has decided that no more processing
877 * is required, then it won't get processing time until further messages
878 * are processed (it'll sit in DoMessage).
879 */
880
bb6290e3 881int wxApp::MainLoop()
2bda0e17 882{
e5c0b16a 883 m_keepGoing = TRUE;
bee503b0 884
e5c0b16a
VZ
885 while ( m_keepGoing )
886 {
887#if wxUSE_THREADS
bee503b0 888 wxMutexGuiLeaveOrEnter();
e5c0b16a 889#endif // wxUSE_THREADS
bee503b0 890
e5c0b16a
VZ
891 while ( !::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) &&
892 ProcessIdle() )
893 {
894 }
bee503b0 895
e5c0b16a
VZ
896 DoMessage();
897 }
2bda0e17 898
e5c0b16a 899 return s_currentMsg.wParam;
2bda0e17
KB
900}
901
902// Returns TRUE if more time is needed.
bb6290e3 903bool wxApp::ProcessIdle()
2bda0e17
KB
904{
905 wxIdleEvent event;
906 event.SetEventObject(this);
907 ProcessEvent(event);
908
909 return event.MoreRequested();
910}
911
bb6290e3 912void wxApp::ExitMainLoop()
2bda0e17 913{
e5c0b16a 914 m_keepGoing = FALSE;
2bda0e17
KB
915}
916
bb6290e3 917bool wxApp::Pending()
2bda0e17 918{
e5c0b16a 919 return (::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) != 0) ;
2bda0e17
KB
920}
921
bb6290e3 922void wxApp::Dispatch()
2bda0e17 923{
bee503b0 924 DoMessage();
2bda0e17
KB
925}
926
927/*
928 * Give all windows a chance to preprocess
929 * the message. Some may have accelerator tables, or have
930 * MDI complications.
931 */
2a47d3c1 932
d3f0a137 933bool wxApp::ProcessMessage(WXMSG *wxmsg)
2bda0e17 934{
d3f0a137
VZ
935 MSG *msg = (MSG *)wxmsg;
936 HWND hWnd = msg->hwnd;
937 wxWindow *wndThis = wxFindWinFromHandle((WXHWND)hWnd), *wnd;
2bda0e17 938
e5c0b16a
VZ
939 // Try translations first; find the youngest window with
940 // a translation table.
d3f0a137 941 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
2bda0e17 942 {
d3f0a137
VZ
943 if ( wnd->MSWTranslateMessage(wxmsg) )
944 return TRUE;
2bda0e17 945 }
2bda0e17 946
e5c0b16a 947 // Anyone for a non-translation message? Try youngest descendants first.
d3f0a137 948 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
57a7b7c1 949 {
d3f0a137
VZ
950 if ( wnd->MSWProcessMessage(wxmsg) )
951 return TRUE;
57a7b7c1 952 }
d3f0a137 953
e5c0b16a 954 return FALSE;
2bda0e17
KB
955}
956
957void wxApp::OnIdle(wxIdleEvent& event)
958{
3222fde2 959 static bool s_inOnIdle = FALSE;
2bda0e17 960
3222fde2
VZ
961 // Avoid recursion (via ProcessEvent default case)
962 if ( s_inOnIdle )
963 return;
2bda0e17 964
3222fde2 965 s_inOnIdle = TRUE;
2bda0e17 966
3222fde2
VZ
967 // 'Garbage' collection of windows deleted with Close().
968 DeletePendingObjects();
2bda0e17 969
3222fde2
VZ
970 // flush the logged messages if any
971 wxLog *pLog = wxLog::GetActiveTarget();
972 if ( pLog != NULL && pLog->HasPendingMessages() )
973 pLog->Flush();
c54f78a2 974
3222fde2
VZ
975 // Send OnIdle events to all windows
976 if ( SendIdleEvents() )
977 {
978 // SendIdleEvents() returns TRUE if at least one window requested more
979 // idle events
980 event.RequestMore(TRUE);
981 }
2bda0e17 982
3222fde2 983 s_inOnIdle = FALSE;
2bda0e17
KB
984}
985
986// Send idle event to all top-level windows
bb6290e3 987bool wxApp::SendIdleEvents()
2bda0e17
KB
988{
989 bool needMore = FALSE;
3222fde2
VZ
990 wxNode* node = wxTopLevelWindows.First();
991 while (node)
992 {
993 wxWindow* win = (wxWindow*) node->Data();
994 if (SendIdleEvents(win))
2bda0e17
KB
995 needMore = TRUE;
996
3222fde2
VZ
997 node = node->Next();
998 }
999
2bda0e17
KB
1000 return needMore;
1001}
1002
1003// Send idle event to window and all subwindows
1004bool wxApp::SendIdleEvents(wxWindow* win)
1005{
e5c0b16a 1006 bool needMore = FALSE;
2bda0e17 1007
e5c0b16a
VZ
1008 wxIdleEvent event;
1009 event.SetEventObject(win);
1010 win->GetEventHandler()->ProcessEvent(event);
2bda0e17 1011
e5c0b16a
VZ
1012 if (event.MoreRequested())
1013 needMore = TRUE;
2bda0e17 1014
e5c0b16a
VZ
1015 wxNode* node = win->GetChildren().First();
1016 while (node)
1017 {
1018 wxWindow* win = (wxWindow*) node->Data();
1019 if (SendIdleEvents(win))
1020 needMore = TRUE;
2bda0e17 1021
e5c0b16a
VZ
1022 node = node->Next();
1023 }
1024 return needMore ;
2bda0e17
KB
1025}
1026
bb6290e3 1027void wxApp::DeletePendingObjects()
2bda0e17 1028{
e5c0b16a
VZ
1029 wxNode *node = wxPendingDelete.First();
1030 while (node)
1031 {
1032 wxObject *obj = (wxObject *)node->Data();
d50b2a58 1033
e5c0b16a 1034 delete obj;
2bda0e17 1035
e5c0b16a
VZ
1036 if (wxPendingDelete.Member(obj))
1037 delete node;
2bda0e17 1038
e5c0b16a
VZ
1039 // Deleting one object may have deleted other pending
1040 // objects, so start from beginning of list again.
1041 node = wxPendingDelete.First();
1042 }
2bda0e17
KB
1043}
1044
57c208c5 1045void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
387a3b02
JS
1046{
1047 if (GetTopWindow())
1048 GetTopWindow()->Close(TRUE);
1049}
1050
1051// Default behaviour: close the application with prompts. The
1052// user can veto the close, and therefore the end session.
1053void wxApp::OnQueryEndSession(wxCloseEvent& event)
1054{
1055 if (GetTopWindow())
1056 {
1057 if (!GetTopWindow()->Close(!event.CanVeto()))
1058 event.Veto(TRUE);
1059 }
1060}
1061
bb6290e3 1062wxLog* wxApp::CreateLogTarget()
2bda0e17
KB
1063{
1064 return new wxLogGui;
1065}
1066
bb6290e3 1067wxWindow* wxApp::GetTopWindow() const
2bda0e17
KB
1068{
1069 if (m_topWindow)
1070 return m_topWindow;
1071 else if (wxTopLevelWindows.Number() > 0)
1072 return (wxWindow*) wxTopLevelWindows.First()->Data();
1073 else
1074 return NULL;
1075}
1076
bb6290e3
JS
1077int wxApp::GetComCtl32Version() const
1078{
1079 // have we loaded COMCTL32 yet?
1080 HMODULE theModule = ::GetModuleHandle("COMCTL32");
1081 int version = 0;
d50b2a58 1082
bb6290e3
JS
1083 // if so, then we can check for the version
1084 if (theModule)
1085 {
1086 // InitCommonControlsEx is unique to 4.7 and later
1087 FARPROC theProc = ::GetProcAddress(theModule, "InitCommonControlsEx");
d50b2a58 1088
bb6290e3
JS
1089 if (! theProc)
1090 { // not found, must be 4.00
e5c0b16a 1091 version = 400;
bb6290e3
JS
1092 }
1093 else
1094 {
e5c0b16a
VZ
1095 // The following symbol are unique to 4.71
1096 // DllInstall
1097 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1098 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1099 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1100 // FlatSB_ShowScrollBar
1101 // _DrawIndirectImageList _DuplicateImageList
1102 // InitializeFlatSB
1103 // UninitializeFlatSB
1104 // we could check for any of these - I chose DllInstall
1105 FARPROC theProc = ::GetProcAddress(theModule, "DllInstall");
1106 if (! theProc)
1107 {
1108 // not found, must be 4.70
1109 version = 470;
1110 }
1111 else
1112 { // found, must be 4.71
1113 version = 471;
1114 }
bb6290e3
JS
1115 }
1116 }
1117 return version;
1118}
1119
1120void wxExit()
2bda0e17 1121{
e5c0b16a
VZ
1122 wxLogError(_("Fatal error: exiting"));
1123
1124 wxApp::CleanUp();
2bda0e17
KB
1125}
1126
1127// Yield to incoming messages
bb6290e3 1128bool wxYield()
2bda0e17 1129{
e5c0b16a
VZ
1130 MSG msg;
1131 // We want to go back to the main message loop
1132 // if we see a WM_QUIT. (?)
1133 while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) && msg.message != WM_QUIT)
1134 {
1135 if ( !wxTheApp->DoMessage() )
1136 break;
1137 }
1138
1139 return TRUE;
2bda0e17
KB
1140}
1141
1142HINSTANCE wxGetInstance()
1143{
e5c0b16a 1144 return wxhInstance;
2bda0e17
KB
1145}
1146
b97b1af8
RD
1147void wxSetInstance(HINSTANCE hInst)
1148{
1149 wxhInstance = hInst;
1150}
1151
2bda0e17
KB
1152// For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1153// if in a separate file. So include it here to ensure it's linked.
3f4a0c5b 1154#if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))
e5c0b16a 1155#include "main.cpp"
2bda0e17 1156#endif