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