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