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