]> git.saurik.com Git - wxWidgets.git/blame - src/msw/app.cpp
blind fix for wxGIFDecoder::ReadGIF
[wxWidgets.git] / src / msw / app.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
3// Purpose: wxApp
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
a5e0e655 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
e5c0b16a
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
e5c0b16a 21 #pragma implementation "app.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
2bda0e17
KB
25#include "wx/wxprec.h"
26
27#if defined(__BORLANDC__)
e5c0b16a 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
e5c0b16a
VZ
32 #include "wx/frame.h"
33 #include "wx/app.h"
34 #include "wx/utils.h"
35 #include "wx/gdicmn.h"
36 #include "wx/pen.h"
37 #include "wx/brush.h"
38 #include "wx/cursor.h"
39 #include "wx/icon.h"
40 #include "wx/palette.h"
41 #include "wx/dc.h"
42 #include "wx/dialog.h"
43 #include "wx/msgdlg.h"
44 #include "wx/intl.h"
45 #include "wx/dynarray.h"
72cdf4c9
VZ
46 #include "wx/wxchar.h"
47 #include "wx/icon.h"
2bda0e17
KB
48#endif
49
2bda0e17
KB
50#include "wx/log.h"
51#include "wx/module.h"
4bf78aae 52
4286a5b5
RR
53#include "wx/msw/private.h"
54
4bf78aae 55#if wxUSE_THREADS
bee503b0
VZ
56 #include "wx/thread.h"
57
58 // define the array of MSG strutures
59 WX_DECLARE_OBJARRAY(MSG, wxMsgArray);
60
61 #include "wx/arrimpl.cpp"
62
63 WX_DEFINE_OBJARRAY(wxMsgArray);
64#endif // wxUSE_THREADS
2bda0e17 65
47d67540 66#if wxUSE_WX_RESOURCES
e5c0b16a 67 #include "wx/resource.h"
2bda0e17
KB
68#endif
69
8614c467
VZ
70#if wxUSE_TOOLTIPS
71 #include "wx/tooltip.h"
72#endif // wxUSE_TOOLTIPS
73
c42404a5
VZ
74// OLE is used for drag-and-drop, clipboard, OLE Automation..., but some
75// compilers don't support it (missing headers, libs, ...)
76#if defined(__GNUWIN32_OLD__) || defined(__SC__) || defined(__SALFORDC__)
e5c0b16a
VZ
77 #undef wxUSE_OLE
78
79 #define wxUSE_OLE 0
80#endif // broken compilers
81
82#if wxUSE_OLE
6e0d9d43 83 #include <ole2.h>
d05237ea 84#endif
ce3ed50d 85
2bda0e17 86#include <string.h>
a5e0e655 87#include <ctype.h>
2bda0e17 88
04ef50df 89#if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__) || defined(__WXMICROWIN__)) && !defined(__CYGWIN10__))
e5c0b16a 90 #include <commctrl.h>
2bda0e17
KB
91#endif
92
04ef50df 93#ifndef __WXMICROWIN__
6e0d9d43 94#include "wx/msw/msvcrt.h"
04ef50df 95#endif
370382c7 96
bdc72a22
VZ
97// ----------------------------------------------------------------------------
98// conditional compilation
99// ----------------------------------------------------------------------------
100
101// The macro _WIN32_IE is defined by commctrl.h (unless it had already been
102// defined before) and shows us what common control features are available
103// during the compile time (it doesn't mean that they will be available during
104// the run-time, use GetComCtl32Version() to test for them!). The possible
105// values are:
106//
107// 0x0200 for comctl32.dll 4.00 shipped with Win95/NT 4.0
108// 0x0300 4.70 IE 3.x
109// 0x0400 4.71 IE 4.0
110// 0x0401 4.72 IE 4.01 and Win98
111// 0x0500 5.00 IE 5.x and NT 5.0 (Win2000)
112
113#ifndef _WIN32_IE
114 // minimal set of features by default
115 #define _WIN32_IE 0x0200
116#endif
117
38189fd5 118#if _WIN32_IE >= 0x0300 && !defined(__MINGW32__)
036bc7d9
VZ
119 #include <shlwapi.h>
120#endif
121
e5c0b16a
VZ
122// ---------------------------------------------------------------------------
123// global variables
124// ---------------------------------------------------------------------------
125
837e5743 126extern wxChar *wxBuffer;
cde9f08e 127extern wxList WXDLLEXPORT wxPendingDelete;
04ef50df 128#ifndef __WXMICROWIN__
2bda0e17 129extern void wxSetKeyboardHook(bool doIt);
04ef50df 130#endif
2bda0e17 131
42e69d6b 132MSG s_currentMsg;
2bda0e17
KB
133wxApp *wxTheApp = NULL;
134
193fe989
VZ
135// NB: all "NoRedraw" classes must have the same names as the "normal" classes
136// with NR suffix - wxWindow::MSWCreate() supposes this
2ffa221c
VZ
137const wxChar *wxFrameClassName = wxT("wxFrameClass");
138const wxChar *wxFrameClassNameNoRedraw = wxT("wxFrameClassNR");
139const wxChar *wxMDIFrameClassName = wxT("wxMDIFrameClass");
140const wxChar *wxMDIFrameClassNameNoRedraw = wxT("wxMDIFrameClassNR");
141const wxChar *wxMDIChildFrameClassName = wxT("wxMDIChildFrameClass");
142const wxChar *wxMDIChildFrameClassNameNoRedraw = wxT("wxMDIChildFrameClassNR");
143const wxChar *wxPanelClassName = wxT("wxPanelClass");
d3f5d09d 144const wxChar *wxPanelClassNameNR = wxT("wxPanelClassNR");
2ffa221c 145const wxChar *wxCanvasClassName = wxT("wxCanvasClass");
d3f5d09d 146const wxChar *wxCanvasClassNameNR = wxT("wxCanvasClassNR");
2bda0e17 147
57c208c5
JS
148HICON wxSTD_FRAME_ICON = (HICON) NULL;
149HICON wxSTD_MDICHILDFRAME_ICON = (HICON) NULL;
150HICON wxSTD_MDIPARENTFRAME_ICON = (HICON) NULL;
2bda0e17 151
57c208c5
JS
152HICON wxDEFAULT_FRAME_ICON = (HICON) NULL;
153HICON wxDEFAULT_MDICHILDFRAME_ICON = (HICON) NULL;
154HICON wxDEFAULT_MDIPARENTFRAME_ICON = (HICON) NULL;
2bda0e17 155
57c208c5 156HBRUSH wxDisableButtonBrush = (HBRUSH) 0;
2bda0e17 157
3135f4a7 158LRESULT WXDLLEXPORT APIENTRY wxWndProc(HWND, UINT, WPARAM, LPARAM);
2bda0e17 159
bd3277fe
VZ
160// FIXME wxUSE_ON_FATAL_EXCEPTION is only supported for VC++ now because it
161// needs compiler support for Win32 SEH. Others (especially Borland)
162// probably have it too, but I'm not sure about how it works
f6bcfd97
BP
163// JACS: get 'Cannot use __try in functions that require unwinding
164// in Unicode mode, so disabling.
165#if !defined(__VISUALC__) || defined(__WIN16__) || defined(UNICODE)
bd3277fe
VZ
166 #undef wxUSE_ON_FATAL_EXCEPTION
167 #define wxUSE_ON_FATAL_EXCEPTION 0
168#endif // VC++
169
3b415ba4
VZ
170#if wxUSE_ON_FATAL_EXCEPTION
171 static bool gs_handleExceptions = FALSE;
172#endif
173
e5c0b16a
VZ
174// ===========================================================================
175// implementation
176// ===========================================================================
589f0e3e 177
e5c0b16a
VZ
178// ---------------------------------------------------------------------------
179// wxApp
180// ---------------------------------------------------------------------------
181
f6bcfd97 182IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
e5c0b16a 183
f6bcfd97
BP
184BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
185 EVT_IDLE(wxApp::OnIdle)
186 EVT_END_SESSION(wxApp::OnEndSession)
187 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
188END_EVENT_TABLE()
e5c0b16a 189
e5c0b16a 190//// Initialize
589f0e3e 191bool wxApp::Initialize()
2bda0e17 192{
f6bcfd97
BP
193 // the first thing to do is to check if we're trying to run an Unicode
194 // program under Win9x - if so, abort right now as it has no chance to
195 // work
196#if wxUSE_UNICODE
197 if ( wxGetOsVersion() != wxWINDOWS_NT )
198 {
199 // note that we can use MessageBoxW() as it's implemented even under
200 // Win9x - OTOH, we can't use wxGetTranslation() because the file APIs
201 // used by wxLocale are not
202 ::MessageBox
203 (
204 NULL,
205 _T("This program uses Unicode and requires Windows NT/2000.\nProgram aborted."),
206 _T("wxWindows Fatal Error"),
207 MB_ICONERROR | MB_OK
208 );
209
210 return FALSE;
211 }
212#endif // wxUSE_UNICODE
213
c030b70f
JS
214 // Some people may wish to use this, but
215 // probably it shouldn't be here by default.
216#ifdef __WXDEBUG__
e5c0b16a 217 // wxRedirectIOToConsole();
c030b70f
JS
218#endif
219
837e5743 220 wxBuffer = new wxChar[1500]; // FIXME
589f0e3e 221
aa0b7e1e 222 wxClassInfo::InitializeClasses();
589f0e3e 223
4d3a259a 224#if wxUSE_THREADS
8e193f38 225 wxPendingEventsLocker = new wxCriticalSection;
4d3a259a
GL
226#endif
227
aa0b7e1e
JS
228 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
229 wxTheColourDatabase->Initialize();
589f0e3e 230
aa0b7e1e
JS
231 wxInitializeStockLists();
232 wxInitializeStockObjects();
589f0e3e 233
aa0b7e1e 234#if wxUSE_WX_RESOURCES
a5e0e655 235 wxInitializeResourceSystem();
aa0b7e1e 236#endif
2bda0e17 237
aa0b7e1e 238 wxBitmap::InitStandardHandlers();
2bda0e17 239
04ef50df 240#if defined(__WIN95__) && !defined(__WXMICROWIN__)
a5e0e655 241 InitCommonControls();
e5c0b16a 242#endif // __WIN95__
2bda0e17 243
8cb172b4 244#if wxUSE_OLE || wxUSE_DRAG_AND_DROP
c49245f8
VZ
245
246#ifdef __WIN16__
e5c0b16a 247 // for OLE, enlarge message queue to be as large as possible
aa0b7e1e 248 int iMsg = 96;
c49245f8
VZ
249 while (!SetMessageQueue(iMsg) && (iMsg -= 8))
250 ;
251#endif // Win16
8cb172b4 252
a5e0e655
VZ
253 // we need to initialize OLE library
254 if ( FAILED(::OleInitialize(NULL)) )
e5c0b16a 255 wxLogError(_("Cannot initialize OLE"));
1e6feb95 256
c49245f8 257#endif // wxUSE_OLE
2bda0e17 258
1f112209 259#if wxUSE_CTL3D
a5e0e655 260 if (!Ctl3dRegister(wxhInstance))
223d09f6 261 wxLogError(wxT("Cannot register CTL3D"));
2bda0e17 262
a5e0e655 263 Ctl3dAutoSubclass(wxhInstance);
1e6feb95 264#endif // wxUSE_CTL3D
2bda0e17 265
87a1e308
VZ
266 // VZ: these icons are not in wx.rc anyhow (but should they?)!
267#if 0
223d09f6
KB
268 wxSTD_FRAME_ICON = LoadIcon(wxhInstance, wxT("wxSTD_FRAME"));
269 wxSTD_MDIPARENTFRAME_ICON = LoadIcon(wxhInstance, wxT("wxSTD_MDIPARENTFRAME"));
270 wxSTD_MDICHILDFRAME_ICON = LoadIcon(wxhInstance, wxT("wxSTD_MDICHILDFRAME"));
2bda0e17 271
223d09f6
KB
272 wxDEFAULT_FRAME_ICON = LoadIcon(wxhInstance, wxT("wxDEFAULT_FRAME"));
273 wxDEFAULT_MDIPARENTFRAME_ICON = LoadIcon(wxhInstance, wxT("wxDEFAULT_MDIPARENTFRAME"));
274 wxDEFAULT_MDICHILDFRAME_ICON = LoadIcon(wxhInstance, wxT("wxDEFAULT_MDICHILDFRAME"));
87a1e308 275#endif // 0
2bda0e17 276
aa0b7e1e 277 RegisterWindowClasses();
2bda0e17 278
04ef50df 279#ifndef __WXMICROWIN__
aa0b7e1e 280 // Create the brush for disabling bitmap buttons
2bda0e17 281
42e69d6b 282 LOGBRUSH lb;
aa0b7e1e 283 lb.lbStyle = BS_PATTERN;
223d09f6 284 lb.lbHatch = (int)LoadBitmap( wxhInstance, wxT("wxDISABLE_BUTTON_BITMAP") );
3a5ffa81
VZ
285 if ( lb.lbHatch )
286 {
287 wxDisableButtonBrush = ::CreateBrushIndirect( & lb );
288 ::DeleteObject( (HGDIOBJ)lb.lbHatch );
289 }
290 //else: wxWindows resources are probably not linked in
04ef50df 291#endif
2bda0e17 292
aa0b7e1e 293#if wxUSE_PENWINDOWS
a5e0e655 294 wxRegisterPenWin();
aa0b7e1e 295#endif
2bda0e17 296
1bffa913 297 wxWinHandleHash = new wxWinHashTable(wxKEY_INTEGER, 100);
2bda0e17 298
6e0d9d43 299 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
8cbd2bde 300 // PLEASE DO NOT ALTER THIS.
7fee680b 301#if defined(__VISUALC__) && defined(__WIN16__) && !defined(WXMAKINGDLL)
a5e0e655
VZ
302 extern char wxDummyChar;
303 if (wxDummyChar) wxDummyChar++;
aa0b7e1e 304#endif
a5e0e655 305
04ef50df 306#ifndef __WXMICROWIN__
aa0b7e1e 307 wxSetKeyboardHook(TRUE);
04ef50df 308#endif
2bda0e17 309
aa0b7e1e
JS
310 wxModule::RegisterModules();
311 if (!wxModule::InitializeModules())
312 return FALSE;
313 return TRUE;
2bda0e17
KB
314}
315
42e69d6b
VZ
316// ---------------------------------------------------------------------------
317// RegisterWindowClasses
318// ---------------------------------------------------------------------------
589f0e3e 319
b782f2e0
VZ
320// TODO we should only register classes really used by the app. For this it
321// would be enough to just delay the class registration until an attempt
322// to create a window of this class is made.
bb6290e3 323bool wxApp::RegisterWindowClasses()
2bda0e17 324{
42e69d6b 325 WNDCLASS wndclass;
e5c0b16a 326
193fe989
VZ
327 // for each class we register one with CS_(V|H)REDRAW style and one
328 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
329 static const long styleNormal = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
330 static const long styleNoRedraw = CS_DBLCLKS;
331
42e69d6b 332 // the fields which are common to all classes
e5c0b16a
VZ
333 wndclass.lpfnWndProc = (WNDPROC)wxWndProc;
334 wndclass.cbClsExtra = 0;
193fe989 335 wndclass.cbWndExtra = sizeof( DWORD ); // VZ: what is this DWORD used for?
e5c0b16a 336 wndclass.hInstance = wxhInstance;
42e69d6b
VZ
337 wndclass.hIcon = (HICON) NULL;
338 wndclass.hCursor = ::LoadCursor((HINSTANCE)NULL, IDC_ARROW);
e5c0b16a 339 wndclass.lpszMenuName = NULL;
42e69d6b
VZ
340
341 // Register the frame window class.
342 wndclass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE + 1);
e5c0b16a 343 wndclass.lpszClassName = wxFrameClassName;
b782f2e0 344 wndclass.style = styleNormal;
e5c0b16a 345
42e69d6b 346 if ( !RegisterClass(&wndclass) )
e5c0b16a 347 {
f6bcfd97 348 wxLogLastError(wxT("RegisterClass(frame)"));
42e69d6b
VZ
349
350 return FALSE;
e5c0b16a
VZ
351 }
352
193fe989
VZ
353 // "no redraw" frame
354 wndclass.lpszClassName = wxFrameClassNameNoRedraw;
355 wndclass.style = styleNoRedraw;
356
357 if ( !RegisterClass(&wndclass) )
358 {
f6bcfd97 359 wxLogLastError(wxT("RegisterClass(no redraw frame)"));
193fe989
VZ
360
361 return FALSE;
362 }
363
e5c0b16a 364 // Register the MDI frame window class.
42e69d6b 365 wndclass.hbrBackground = (HBRUSH)NULL; // paint MDI frame ourselves
b782f2e0
VZ
366 wndclass.lpszClassName = wxMDIFrameClassName;
367 wndclass.style = styleNormal;
42e69d6b
VZ
368
369 if ( !RegisterClass(&wndclass) )
e5c0b16a 370 {
f6bcfd97 371 wxLogLastError(wxT("RegisterClass(MDI parent)"));
42e69d6b
VZ
372
373 return FALSE;
e5c0b16a
VZ
374 }
375
193fe989 376 // "no redraw" MDI frame
b782f2e0 377 wndclass.lpszClassName = wxMDIFrameClassNameNoRedraw;
193fe989
VZ
378 wndclass.style = styleNoRedraw;
379
380 if ( !RegisterClass(&wndclass) )
381 {
f6bcfd97 382 wxLogLastError(wxT("RegisterClass(no redraw MDI parent frame)"));
193fe989
VZ
383
384 return FALSE;
385 }
386
e5c0b16a 387 // Register the MDI child frame window class.
42e69d6b
VZ
388 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
389 wndclass.lpszClassName = wxMDIChildFrameClassName;
b782f2e0 390 wndclass.style = styleNormal;
42e69d6b
VZ
391
392 if ( !RegisterClass(&wndclass) )
e5c0b16a 393 {
f6bcfd97 394 wxLogLastError(wxT("RegisterClass(MDI child)"));
42e69d6b
VZ
395
396 return FALSE;
e5c0b16a
VZ
397 }
398
193fe989
VZ
399 // "no redraw" MDI child frame
400 wndclass.lpszClassName = wxMDIChildFrameClassNameNoRedraw;
401 wndclass.style = styleNoRedraw;
402
403 if ( !RegisterClass(&wndclass) )
404 {
f6bcfd97 405 wxLogLastError(wxT("RegisterClass(no redraw MDI child)"));
193fe989
VZ
406
407 return FALSE;
408 }
409
e5c0b16a 410 // Register the panel window class.
42e69d6b
VZ
411 wndclass.hbrBackground = (HBRUSH) GetStockObject( LTGRAY_BRUSH );
412 wndclass.lpszClassName = wxPanelClassName;
b782f2e0 413 wndclass.style = styleNormal;
42e69d6b
VZ
414
415 if ( !RegisterClass(&wndclass) )
e5c0b16a 416 {
f6bcfd97 417 wxLogLastError(wxT("RegisterClass(panel)"));
42e69d6b
VZ
418
419 return FALSE;
e5c0b16a
VZ
420 }
421
d3f5d09d
UM
422 // Register the no redraw panel window class.
423 wndclass.lpszClassName = wxPanelClassNameNR;
424 wndclass.style = styleNoRedraw;
425
426 if ( !RegisterClass(&wndclass) )
427 {
f6bcfd97 428 wxLogLastError(wxT("RegisterClass(no redraw panel)"));
d3f5d09d
UM
429
430 return FALSE;
431 }
432
e5c0b16a 433 // Register the canvas and textsubwindow class name
42e69d6b
VZ
434 wndclass.hbrBackground = (HBRUSH)NULL;
435 wndclass.lpszClassName = wxCanvasClassName;
436
437 if ( !RegisterClass(&wndclass) )
e5c0b16a 438 {
f6bcfd97 439 wxLogLastError(wxT("RegisterClass(canvas)"));
42e69d6b
VZ
440
441 return FALSE;
e5c0b16a
VZ
442 }
443
d3f5d09d
UM
444 wndclass.lpszClassName = wxCanvasClassNameNR;
445 wndclass.style = styleNoRedraw;
446 if ( !RegisterClass(&wndclass) )
447 {
f6bcfd97 448 wxLogLastError(wxT("RegisterClass(no redraw canvas)"));
d3f5d09d
UM
449
450 return FALSE;
451 }
452
e5c0b16a 453 return TRUE;
2bda0e17
KB
454}
455
9787a4b6
VZ
456// ---------------------------------------------------------------------------
457// UnregisterWindowClasses
458// ---------------------------------------------------------------------------
459
460bool wxApp::UnregisterWindowClasses()
461{
462 bool retval = TRUE;
463
464 // frame window class.
465 if ( !UnregisterClass(wxFrameClassName, wxhInstance) )
466 {
467 wxLogLastError(wxT("UnregisterClass(frame)"));
468
469 retval = FALSE;
470 }
471
472 // "no redraw" frame
473 if ( !UnregisterClass(wxFrameClassNameNoRedraw, wxhInstance) )
474 {
475 wxLogLastError(wxT("UnregisterClass(no redraw frame)"));
476
477 return FALSE;
478 }
479
480 // MDI frame window class.
481 if ( !UnregisterClass(wxMDIFrameClassName, wxhInstance) )
482 {
483 wxLogLastError(wxT("UnregisterClass(MDI parent)"));
484
485 retval = FALSE;
486 }
487
488 // "no redraw" MDI frame
489 if ( !UnregisterClass(wxMDIFrameClassNameNoRedraw, wxhInstance) )
490 {
491 wxLogLastError(wxT("UnregisterClass(no redraw MDI parent frame)"));
492
493 retval = FALSE;
494 }
495
496 // MDI child frame window class.
497 if ( !UnregisterClass(wxMDIChildFrameClassName, wxhInstance) )
498 {
499 wxLogLastError(wxT("UnregisterClass(MDI child)"));
500
501 retval = FALSE;
502 }
503
504 // "no redraw" MDI child frame
505 if ( !UnregisterClass(wxMDIChildFrameClassNameNoRedraw, wxhInstance) )
506 {
507 wxLogLastError(wxT("UnregisterClass(no redraw MDI child)"));
508
509 retval = FALSE;
510 }
511
512 // panel window class.
513 if ( !UnregisterClass(wxPanelClassName, wxhInstance) )
514 {
515 wxLogLastError(wxT("UnregisterClass(panel)"));
516
517 retval = FALSE;
518 }
519
520 // no redraw panel window class.
521 if ( !UnregisterClass(wxPanelClassNameNR, wxhInstance) )
522 {
523 wxLogLastError(wxT("UnregisterClass(no redraw panel)"));
524
525 retval = FALSE;
526 }
527
528 // canvas and textsubwindow class name
529 if ( !UnregisterClass(wxCanvasClassName, wxhInstance) )
530 {
531 wxLogLastError(wxT("UnregisterClass(canvas)"));
532
533 retval = FALSE;
534 }
535
536 if ( !UnregisterClass(wxCanvasClassNameNR, wxhInstance) )
537 {
538 wxLogLastError(wxT("UnregisterClass(no redraw canvas)"));
539
540 retval = FALSE;
541 }
542
543 return retval;
544}
545
42e69d6b
VZ
546// ---------------------------------------------------------------------------
547// Convert Windows to argc, argv style
548// ---------------------------------------------------------------------------
589f0e3e
JS
549
550void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine)
551{
da36f544 552 wxStringList args;
589f0e3e 553
da36f544
JS
554 wxString cmdLine(lpCmdLine);
555 int count = 0;
589f0e3e 556
da36f544 557 // Get application name
837e5743 558 wxChar name[260]; // 260 is MAX_PATH value from windef.h
da36f544
JS
559 ::GetModuleFileName(wxhInstance, name, WXSIZEOF(name));
560
da36f544 561 args.Add(name);
50ef256e 562 count++;
589f0e3e 563
837e5743 564 wxStrcpy(name, wxFileNameFromPath(name));
da36f544
JS
565 wxStripExtension(name);
566 wxTheApp->SetAppName(name);
589f0e3e 567
da36f544
JS
568 // Break up string
569 // Treat strings enclosed in double-quotes as single arguments
570 int i = 0;
571 int len = cmdLine.Length();
572 while (i < len)
a5e0e655 573 {
da36f544 574 // Skip whitespace
837e5743 575 while ((i < len) && wxIsspace(cmdLine.GetChar(i)))
da36f544
JS
576 i ++;
577
578 if (i < len)
579 {
223d09f6 580 if (cmdLine.GetChar(i) == wxT('"')) // We found the start of a string
da36f544
JS
581 {
582 i ++;
583 int first = i;
223d09f6 584 while ((i < len) && (cmdLine.GetChar(i) != wxT('"')))
da36f544
JS
585 i ++;
586
587 wxString arg(cmdLine.Mid(first, (i - first)));
588
589 args.Add(arg);
590 count ++;
591
592 if (i < len)
593 i ++; // Skip past 2nd quote
594 }
595 else // Unquoted argument
596 {
597 int first = i;
837e5743 598 while ((i < len) && !wxIsspace(cmdLine.GetChar(i)))
da36f544
JS
599 i ++;
600
601 wxString arg(cmdLine.Mid(first, (i - first)));
602
603 args.Add(arg);
604 count ++;
605 }
606 }
a5e0e655 607 }
a5e0e655 608
837e5743 609 wxTheApp->argv = new wxChar*[count + 1];
da36f544 610 for (i = 0; i < count; i++)
a5e0e655 611 {
da36f544 612 wxString arg(args[i]);
837e5743 613 wxTheApp->argv[i] = copystring((const wxChar*)arg);
a5e0e655 614 }
da36f544
JS
615 wxTheApp->argv[count] = NULL; // argv[] is a NULL-terminated list
616 wxTheApp->argc = count;
589f0e3e
JS
617}
618
619//// Cleans up any wxWindows internal structures left lying around
620
bb6290e3 621void wxApp::CleanUp()
2bda0e17 622{
e5c0b16a
VZ
623 //// COMMON CLEANUP
624
546db2a8 625#if wxUSE_LOG
e5c0b16a
VZ
626 // flush the logged messages if any and install a 'safer' log target: the
627 // default one (wxLogGui) can't be used after the resources are freed just
628 // below and the user suppliedo ne might be even more unsafe (using any
629 // wxWindows GUI function is unsafe starting from now)
630 wxLog::DontCreateOnDemand();
f5e5bd66 631
e5c0b16a
VZ
632 // this will flush the old messages if any
633 delete wxLog::SetActiveTarget(new wxLogStderr);
546db2a8 634#endif // wxUSE_LOG
f5e5bd66 635
e5c0b16a
VZ
636 // One last chance for pending objects to be cleaned up
637 wxTheApp->DeletePendingObjects();
f5e5bd66 638
e5c0b16a 639 wxModule::CleanUpModules();
2bda0e17 640
47d67540 641#if wxUSE_WX_RESOURCES
e5c0b16a 642 wxCleanUpResourceSystem();
589f0e3e 643
e5c0b16a 644 // wxDefaultResourceTable->ClearTable();
589f0e3e
JS
645#endif
646
42e69d6b 647 wxDeleteStockObjects();
589f0e3e 648
e5c0b16a
VZ
649 // Destroy all GDI lists, etc.
650 wxDeleteStockLists();
589f0e3e 651
e5c0b16a
VZ
652 delete wxTheColourDatabase;
653 wxTheColourDatabase = NULL;
589f0e3e 654
e5c0b16a 655 wxBitmap::CleanUpHandlers();
589f0e3e 656
e5c0b16a
VZ
657 delete[] wxBuffer;
658 wxBuffer = NULL;
589f0e3e 659
e5c0b16a 660 //// WINDOWS-SPECIFIC CLEANUP
2bda0e17 661
04ef50df 662#ifndef __WXMICROWIN__
e5c0b16a 663 wxSetKeyboardHook(FALSE);
04ef50df 664#endif
2bda0e17 665
47d67540 666#if wxUSE_PENWINDOWS
e5c0b16a 667 wxCleanUpPenWin();
2bda0e17
KB
668#endif
669
e5c0b16a
VZ
670 if (wxSTD_FRAME_ICON)
671 DestroyIcon(wxSTD_FRAME_ICON);
672 if (wxSTD_MDICHILDFRAME_ICON)
673 DestroyIcon(wxSTD_MDICHILDFRAME_ICON);
674 if (wxSTD_MDIPARENTFRAME_ICON)
675 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON);
676
677 if (wxDEFAULT_FRAME_ICON)
678 DestroyIcon(wxDEFAULT_FRAME_ICON);
679 if (wxDEFAULT_MDICHILDFRAME_ICON)
680 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON);
681 if (wxDEFAULT_MDIPARENTFRAME_ICON)
682 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON);
683
684 if ( wxDisableButtonBrush )
42e69d6b 685 ::DeleteObject( wxDisableButtonBrush );
e5c0b16a
VZ
686
687#if wxUSE_OLE
688 ::OleUninitialize();
5de5db0e 689#endif
2bda0e17 690
9787a4b6
VZ
691#ifdef WXMAKINGDLL
692 // for an EXE the classes are unregistered when it terminates but DLL may
693 // be loaded several times (load/unload/load) into the same process in
694 // which case the registration will fail after the first time if we don't
695 // unregister the classes now
696 UnregisterWindowClasses();
697#endif // WXMAKINGDLL
698
1f112209 699#if wxUSE_CTL3D
e5c0b16a 700 Ctl3dUnregister(wxhInstance);
2bda0e17
KB
701#endif
702
1bffa913 703 delete wxWinHandleHash;
d50b2a58 704
3135f4a7 705 // GL: I'm annoyed ... I don't know where to put this and I don't want to
4d3a259a 706 // create a module for that as it's part of the core.
4d3a259a 707 delete wxPendingEvents;
1e6feb95 708
8e193f38 709#if wxUSE_THREADS
4d3a259a 710 delete wxPendingEventsLocker;
1e6feb95 711 // If we don't do the following, we get an apparent memory leak
f80eabe5 712#if wxUSE_VALIDATORS
63863e09 713 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
1e6feb95
VZ
714#endif // wxUSE_VALIDATORS
715#endif // wxUSE_THREADS
4d3a259a 716
e5c0b16a 717 wxClassInfo::CleanUpClasses();
0c32066b 718
e5c0b16a
VZ
719 delete wxTheApp;
720 wxTheApp = NULL;
184b5d99
JS
721
722#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
e5c0b16a
VZ
723 // At this point we want to check if there are any memory
724 // blocks that aren't part of the wxDebugContext itself,
725 // as a special case. Then when dumping we need to ignore
726 // wxDebugContext, too.
d13c32e9 727 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
e5c0b16a 728 {
5fa399c9 729 wxLogMessage(wxT("There were memory leaks."));
e5c0b16a
VZ
730 wxDebugContext::Dump();
731 wxDebugContext::PrintStatistics();
732 }
733 // wxDebugContext::SetStream(NULL, NULL);
184b5d99
JS
734#endif
735
546db2a8 736#if wxUSE_LOG
e5c0b16a
VZ
737 // do it as the very last thing because everything else can log messages
738 delete wxLog::SetActiveTarget(NULL);
546db2a8 739#endif // wxUSE_LOG
2bda0e17
KB
740}
741
7ece89c6
RD
742//----------------------------------------------------------------------
743// Entry point helpers, used by wxPython
744//----------------------------------------------------------------------
745
f6bcfd97 746int WXDLLEXPORT wxEntryStart( int WXUNUSED(argc), char** WXUNUSED(argv) )
7ece89c6
RD
747{
748 return wxApp::Initialize();
749}
750
f6bcfd97 751int WXDLLEXPORT wxEntryInitGui()
7ece89c6 752{
1e6feb95 753 return wxTheApp->OnInitGui();
7ece89c6
RD
754}
755
756void WXDLLEXPORT wxEntryCleanup()
757{
758 wxApp::CleanUp();
759}
760
761
2bda0e17
KB
762#if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
763
e5c0b16a
VZ
764// temporarily disable this warning which would be generated in release builds
765// because of __try
3f4a0c5b 766#ifdef __VISUALC__
f8a3e080
VZ
767 #pragma warning(disable: 4715) // not all control paths return a value
768#endif // Visual C++
769
7ece89c6
RD
770//----------------------------------------------------------------------
771// Main wxWindows entry point
772//----------------------------------------------------------------------
a5e0e655
VZ
773int wxEntry(WXHINSTANCE hInstance,
774 WXHINSTANCE WXUNUSED(hPrevInstance),
775 char *lpCmdLine,
776 int nCmdShow,
777 bool enterLoop)
2bda0e17 778{
6e0d9d43
VZ
779 // do check for memory leaks on program exit
780 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
781 // deallocated memory which may be used to simulate low-memory condition)
04ef50df 782#ifndef __WXMICROWIN__
6e0d9d43 783 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
04ef50df
JS
784#endif
785
6418cb93
SC
786#ifdef __MWERKS__
787#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
788 // This seems to be necessary since there are 'rogue'
789 // objects present at this point (perhaps global objects?)
790 // Setting a checkpoint will ignore them as far as the
791 // memory checking facility is concerned.
792 // Of course you may argue that memory allocated in globals should be
793 // checked, but this is a reasonable compromise.
794 wxDebugContext::SetCheckpoint();
795#endif
796#endif
3b415ba4
VZ
797
798 // take everything into a try-except block to be able to call
799 // OnFatalException() if necessary
3b415ba4 800#if wxUSE_ON_FATAL_EXCEPTION
3f4a0c5b 801 __try {
a5e0e655 802#endif
e5c0b16a 803 wxhInstance = (HINSTANCE) hInstance;
2bda0e17 804
7ece89c6 805 if (!wxEntryStart(0,0))
e5c0b16a 806 return 0;
2bda0e17 807
e5c0b16a
VZ
808 // create the application object or ensure that one already exists
809 if (!wxTheApp)
810 {
811 // The app may have declared a global application object, but we recommend
812 // the IMPLEMENT_APP macro is used instead, which sets an initializer
813 // function for delayed, dynamic app object construction.
814 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
223d09f6 815 wxT("No initializer - use IMPLEMENT_APP macro.") );
f8a3e080 816
8cb172b4 817 wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
e5c0b16a
VZ
818 }
819
223d09f6 820 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
e5c0b16a
VZ
821
822 // save the WinMain() parameters
823 wxTheApp->ConvertToStandardCommandArgs(lpCmdLine);
824 wxTheApp->m_nCmdShow = nCmdShow;
a5e0e655 825
36edded9
JS
826 // We really don't want timestamps by default, because it means
827 // we can't simply double-click on the error message and get to that
828 // line in the source. So VC++ at least, let's have a sensible default.
829#ifdef __VISUALC__
830 wxLog::SetTimestamp(NULL);
831#endif
832
e5c0b16a
VZ
833 int retValue = 0;
834
6afafc42
VZ
835 // it is common to create a modal dialog in OnInit() (to ask/notify the
836 // user about something) but it wouldn't work if we don't change the
837 // "exit on delete last frame" flag here as when this dialog is
838 // deleted, the app would terminate (it was the last top level window
839 // as the main frame wasn't created yet!), so disable this behaviour
840 // temproarily
841 bool exitOnLastFrameDelete = wxTheApp->GetExitOnFrameDelete();
842 wxTheApp->SetExitOnFrameDelete(FALSE);
843
844 // init the app
1e6feb95 845 retValue = wxEntryInitGui() && wxTheApp->OnInit() ? 0 : -1;
6afafc42
VZ
846
847 // restore the old flag value
848 wxTheApp->SetExitOnFrameDelete(exitOnLastFrameDelete);
849
850 if ( retValue == 0 )
e5c0b16a
VZ
851 {
852 if ( enterLoop )
853 {
6afafc42 854 // run the main loop
e5c0b16a
VZ
855 retValue = wxTheApp->OnRun();
856 }
857 else
6afafc42
VZ
858 {
859 // we want to initialize, but not run or exit immediately.
e5c0b16a 860 return 1;
6afafc42 861 }
e5c0b16a
VZ
862 }
863 //else: app initialization failed, so we skipped OnRun()
864
865 wxWindow *topWindow = wxTheApp->GetTopWindow();
866 if ( topWindow )
867 {
868 // Forcibly delete the window.
869 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
870 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
871 {
872 topWindow->Close(TRUE);
873 wxTheApp->DeletePendingObjects();
874 }
875 else
876 {
877 delete topWindow;
878 wxTheApp->SetTopWindow(NULL);
879 }
880 }
881
882 wxTheApp->OnExit();
883
7ece89c6 884 wxEntryCleanup();
e5c0b16a
VZ
885
886 return retValue;
887
3b415ba4 888#if wxUSE_ON_FATAL_EXCEPTION
e5c0b16a 889 }
3b415ba4
VZ
890 __except ( gs_handleExceptions ? EXCEPTION_EXECUTE_HANDLER
891 : EXCEPTION_CONTINUE_SEARCH ) {
892 if ( wxTheApp )
f6bcfd97
BP
893 {
894 // give the user a chance to do something special about this
e5c0b16a 895 wxTheApp->OnFatalException();
f6bcfd97 896 }
e5c0b16a
VZ
897
898 ::ExitProcess(3); // the same exit code as abort()
899
900 // NOTREACHED
901 }
3b415ba4 902#endif // wxUSE_ON_FATAL_EXCEPTION
2bda0e17
KB
903}
904
f8a3e080 905// restore warning state
3f4a0c5b 906#ifdef __VISUALC__
f8a3e080
VZ
907 #pragma warning(default: 4715) // not all control paths return a value
908#endif // Visual C++
909
2bda0e17
KB
910#else /* _WINDLL */
911
7ece89c6
RD
912//----------------------------------------------------------------------
913// Entry point for wxWindows + the App in a DLL
914//----------------------------------------------------------------------
589f0e3e 915
2bda0e17
KB
916int wxEntry(WXHINSTANCE hInstance)
917{
e5c0b16a 918 wxhInstance = (HINSTANCE) hInstance;
7ece89c6 919 wxEntryStart(0, 0);
2bda0e17 920
e5c0b16a
VZ
921 // The app may have declared a global application object, but we recommend
922 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
923 // for delayed, dynamic app object construction.
924 if (!wxTheApp)
a5e0e655 925 {
e5c0b16a
VZ
926 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
927 "No initializer - use IMPLEMENT_APP macro." );
2bda0e17 928
e5c0b16a
VZ
929 wxTheApp = (* wxApp::GetInitializerFunction()) ();
930 }
2bda0e17 931
e5c0b16a 932 wxCHECK_MSG( wxTheApp, 0, "You have to define an instance of wxApp!" );
2bda0e17 933
e5c0b16a
VZ
934 wxTheApp->argc = 0;
935 wxTheApp->argv = NULL;
2bda0e17 936
7ece89c6 937 wxEntryInitGui();
2bda0e17 938
e5c0b16a 939 wxTheApp->OnInit();
2bda0e17 940
e5c0b16a
VZ
941 wxWindow *topWindow = wxTheApp->GetTopWindow();
942 if ( topWindow && topWindow->GetHWND())
943 {
944 topWindow->Show(TRUE);
945 }
2bda0e17 946
e5c0b16a 947 return 1;
2bda0e17
KB
948}
949#endif // _WINDLL
950
589f0e3e
JS
951//// Static member initialization
952
c94ad3c3 953wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
2bda0e17 954
bb6290e3 955wxApp::wxApp()
2bda0e17 956{
e5c0b16a
VZ
957 argc = 0;
958 argv = NULL;
e5c0b16a 959 m_printMode = wxPRINT_WINDOWS;
e5c0b16a 960 m_auto3D = TRUE;
2bda0e17
KB
961}
962
589f0e3e
JS
963wxApp::~wxApp()
964{
e5c0b16a
VZ
965 // Delete command-line args
966 int i;
967 for (i = 0; i < argc; i++)
968 {
969 delete[] argv[i];
970 }
971 delete[] argv;
589f0e3e
JS
972}
973
bb6290e3 974bool wxApp::Initialized()
2bda0e17
KB
975{
976#ifndef _WINDLL
e5c0b16a
VZ
977 if (GetTopWindow())
978 return TRUE;
979 else
980 return FALSE;
3b415ba4 981#else // Assume initialized if DLL (no way of telling)
e5c0b16a 982 return TRUE;
2bda0e17
KB
983#endif
984}
985
986/*
987 * Get and process a message, returning FALSE if WM_QUIT
bee503b0 988 * received (and also set the flag telling the app to exit the main loop)
2bda0e17
KB
989 *
990 */
bb6290e3 991bool wxApp::DoMessage()
2bda0e17 992{
bee503b0
VZ
993 BOOL rc = ::GetMessage(&s_currentMsg, (HWND) NULL, 0, 0);
994 if ( rc == 0 )
995 {
996 // got WM_QUIT
997 m_keepGoing = FALSE;
4a9968f9 998
bee503b0
VZ
999 return FALSE;
1000 }
1001 else if ( rc == -1 )
1002 {
1003 // should never happen, but let's test for it nevertheless
f6bcfd97 1004 wxLogLastError(wxT("GetMessage"));
bee503b0
VZ
1005 }
1006 else
1007 {
1008#if wxUSE_THREADS
1009 wxASSERT_MSG( wxThread::IsMain(),
223d09f6 1010 wxT("only the main thread can process Windows messages") );
d50b2a58 1011
bee503b0
VZ
1012 static bool s_hadGuiLock = TRUE;
1013 static wxMsgArray s_aSavedMessages;
1014
1015 // if a secondary thread owns is doing GUI calls, save all messages for
1016 // later processing - we can't process them right now because it will
1017 // lead to recursive library calls (and we're not reentrant)
1018 if ( !wxGuiOwnedByMainThread() )
1019 {
1020 s_hadGuiLock = FALSE;
1021
4a9968f9
VZ
1022 // leave out WM_COMMAND messages: too dangerous, sometimes
1023 // the message will be processed twice
1024 if ( !wxIsWaitingForThread() ||
e5c0b16a 1025 s_currentMsg.message != WM_COMMAND )
4a9968f9
VZ
1026 {
1027 s_aSavedMessages.Add(s_currentMsg);
1028 }
bee503b0
VZ
1029
1030 return TRUE;
1031 }
1032 else
1033 {
1034 // have we just regained the GUI lock? if so, post all of the saved
1035 // messages
1036 //
1037 // FIXME of course, it's not _exactly_ the same as processing the
1038 // messages normally - expect some things to break...
1039 if ( !s_hadGuiLock )
1040 {
1041 s_hadGuiLock = TRUE;
1042
1043 size_t count = s_aSavedMessages.Count();
1044 for ( size_t n = 0; n < count; n++ )
1045 {
1046 MSG& msg = s_aSavedMessages[n];
1047
1048 if ( !ProcessMessage((WXMSG *)&msg) )
1049 {
1050 ::TranslateMessage(&msg);
1051 ::DispatchMessage(&msg);
1052 }
1053 }
1054
1055 s_aSavedMessages.Empty();
1056 }
1057 }
1058#endif // wxUSE_THREADS
1059
1060 // Process the message
ed45e263 1061 DoMessage((WXMSG *)&s_currentMsg);
bee503b0
VZ
1062 }
1063
1064 return TRUE;
2bda0e17
KB
1065}
1066
ed45e263
VZ
1067void wxApp::DoMessage(WXMSG *pMsg)
1068{
1069 if ( !ProcessMessage(pMsg) )
1070 {
1071 ::TranslateMessage((MSG *)pMsg);
1072 ::DispatchMessage((MSG *)pMsg);
1073 }
1074}
1075
2bda0e17
KB
1076/*
1077 * Keep trying to process messages until WM_QUIT
1078 * received.
1079 *
1080 * If there are messages to be processed, they will all be
1081 * processed and OnIdle will not be called.
1082 * When there are no more messages, OnIdle is called.
1083 * If OnIdle requests more time,
1084 * it will be repeatedly called so long as there are no pending messages.
1085 * A 'feature' of this is that once OnIdle has decided that no more processing
1086 * is required, then it won't get processing time until further messages
1087 * are processed (it'll sit in DoMessage).
1088 */
1089
bb6290e3 1090int wxApp::MainLoop()
2bda0e17 1091{
e5c0b16a 1092 m_keepGoing = TRUE;
bee503b0 1093
e5c0b16a
VZ
1094 while ( m_keepGoing )
1095 {
1096#if wxUSE_THREADS
bee503b0 1097 wxMutexGuiLeaveOrEnter();
e5c0b16a 1098#endif // wxUSE_THREADS
bee503b0 1099
b6c588e1
VZ
1100 while ( !Pending() && ProcessIdle() )
1101 ;
7214297d 1102
b6c588e1 1103 // a message came or no more idle processing to do
e5c0b16a
VZ
1104 DoMessage();
1105 }
2bda0e17 1106
e5c0b16a 1107 return s_currentMsg.wParam;
2bda0e17
KB
1108}
1109
1110// Returns TRUE if more time is needed.
bb6290e3 1111bool wxApp::ProcessIdle()
2bda0e17
KB
1112{
1113 wxIdleEvent event;
1114 event.SetEventObject(this);
1115 ProcessEvent(event);
1116
1117 return event.MoreRequested();
1118}
1119
bb6290e3 1120void wxApp::ExitMainLoop()
2bda0e17 1121{
b6c588e1 1122 // VZ: why not ::PostQuitMessage()?
e5c0b16a 1123 m_keepGoing = FALSE;
2bda0e17
KB
1124}
1125
bb6290e3 1126bool wxApp::Pending()
2bda0e17 1127{
b6c588e1 1128 return ::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) != 0;
2bda0e17
KB
1129}
1130
bb6290e3 1131void wxApp::Dispatch()
2bda0e17 1132{
bee503b0 1133 DoMessage();
2bda0e17
KB
1134}
1135
1136/*
1137 * Give all windows a chance to preprocess
1138 * the message. Some may have accelerator tables, or have
1139 * MDI complications.
1140 */
2a47d3c1 1141
d3f0a137 1142bool wxApp::ProcessMessage(WXMSG *wxmsg)
2bda0e17 1143{
d3f0a137 1144 MSG *msg = (MSG *)wxmsg;
2a3caeb5
VZ
1145 HWND hwnd = msg->hwnd;
1146 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
2bda0e17 1147
761989ff
VZ
1148 // this may happen if the event occured in a standard modeless dialog (the
1149 // only example of which I know of is the find/replace dialog) - then call
1150 // IsDialogMessage() to make TAB navigation in it work
1151 if ( !wndThis )
1152 {
2a3caeb5
VZ
1153 // we need to find the dialog containing this control as
1154 // IsDialogMessage() just eats all the messages (i.e. returns TRUE for
1155 // them) if we call it for the control itself
de7f0860 1156 while ( hwnd && ::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD )
2a3caeb5
VZ
1157 {
1158 hwnd = ::GetParent(hwnd);
1159 }
1160
de7f0860 1161 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
761989ff
VZ
1162 }
1163
8614c467
VZ
1164#if wxUSE_TOOLTIPS
1165 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
1166 // popup the tooltip bubbles
761989ff 1167 if ( (msg->message == WM_MOUSEMOVE) )
834362a2 1168 {
8614c467
VZ
1169 wxToolTip *tt = wndThis->GetToolTip();
1170 if ( tt )
1171 {
1172 tt->RelayEvent(wxmsg);
1173 }
834362a2 1174 }
8614c467 1175#endif // wxUSE_TOOLTIPS
834362a2 1176
a37d422a
VZ
1177 // allow the window to prevent certain messages from being
1178 // translated/processed (this is currently used by wxTextCtrl to always
1179 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
1180 if ( !wndThis->MSWShouldPreProcessMessage(wxmsg) )
1181 {
1182 return FALSE;
1183 }
1184
2a3caeb5 1185 // try translations first: the accelerators override everything
8614c467 1186 wxWindow *wnd;
42bcb12b 1187
d3f0a137 1188 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
2bda0e17 1189 {
2a3caeb5 1190 if ( wnd->MSWTranslateMessage(wxmsg))
d3f0a137 1191 return TRUE;
3bdc265d
VZ
1192
1193 // stop at first top level window, i.e. don't try to process the key
1194 // strokes originating in a dialog using the accelerators of the parent
1195 // frame - this doesn't make much sense
1196 if ( wnd->IsTopLevel() )
2a3caeb5
VZ
1197 break;
1198 }
1199
a37d422a
VZ
1200 // now try the other hooks (kbd navigation is handled here): we start from
1201 // wndThis->GetParent() because wndThis->MSWProcessMessage() was already
1202 // called above
1203 for ( wnd = wndThis->GetParent(); wnd; wnd = wnd->GetParent() )
2a3caeb5
VZ
1204 {
1205 if ( wnd->MSWProcessMessage(wxmsg) )
1206 return TRUE;
57a7b7c1 1207 }
d3f0a137 1208
a37d422a 1209 // no special preprocessing for this message, dispatch it normally
e5c0b16a 1210 return FALSE;
2bda0e17
KB
1211}
1212
1213void wxApp::OnIdle(wxIdleEvent& event)
1214{
3222fde2 1215 static bool s_inOnIdle = FALSE;
2bda0e17 1216
3222fde2
VZ
1217 // Avoid recursion (via ProcessEvent default case)
1218 if ( s_inOnIdle )
1219 return;
2bda0e17 1220
3222fde2 1221 s_inOnIdle = TRUE;
2bda0e17 1222
52e52bea
GRG
1223 // If there are pending events, we must process them: pending events
1224 // are either events to the threads other than main or events posted
1225 // with wxPostEvent() functions
1226 // GRG: I have moved this here so that all pending events are processed
1227 // before starting to delete any objects. This behaves better (in
1228 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
1229 // behaviour. Changed Feb/2000 before 2.1.14
1230 ProcessPendingEvents();
1231
3222fde2
VZ
1232 // 'Garbage' collection of windows deleted with Close().
1233 DeletePendingObjects();
2bda0e17 1234
546db2a8 1235#if wxUSE_LOG
3222fde2 1236 // flush the logged messages if any
2ed3265e 1237 wxLog::FlushActive();
546db2a8 1238#endif // wxUSE_LOG
c54f78a2 1239
aef94d68
JS
1240#if wxUSE_DC_CACHEING
1241 // automated DC cache management: clear the cached DCs and bitmap
1242 // if it's likely that the app has finished with them, that is, we
1243 // get an idle event and we're not dragging anything.
4624defa 1244 if (!::GetKeyState(MK_LBUTTON) && !::GetKeyState(MK_MBUTTON) && !::GetKeyState(MK_RBUTTON))
aef94d68
JS
1245 wxDC::ClearCache();
1246#endif // wxUSE_DC_CACHEING
1247
3222fde2
VZ
1248 // Send OnIdle events to all windows
1249 if ( SendIdleEvents() )
1250 {
1251 // SendIdleEvents() returns TRUE if at least one window requested more
1252 // idle events
1253 event.RequestMore(TRUE);
1254 }
2bda0e17 1255
3222fde2 1256 s_inOnIdle = FALSE;
2bda0e17
KB
1257}
1258
1259// Send idle event to all top-level windows
bb6290e3 1260bool wxApp::SendIdleEvents()
2bda0e17
KB
1261{
1262 bool needMore = FALSE;
e146b8c8 1263
f1d534df 1264 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
3222fde2
VZ
1265 while (node)
1266 {
e146b8c8 1267 wxWindow* win = node->GetData();
3222fde2 1268 if (SendIdleEvents(win))
2bda0e17 1269 needMore = TRUE;
e146b8c8 1270 node = node->GetNext();
3222fde2
VZ
1271 }
1272
2bda0e17
KB
1273 return needMore;
1274}
1275
1276// Send idle event to window and all subwindows
1277bool wxApp::SendIdleEvents(wxWindow* win)
1278{
e5c0b16a 1279 bool needMore = FALSE;
2bda0e17 1280
e5c0b16a
VZ
1281 wxIdleEvent event;
1282 event.SetEventObject(win);
1283 win->GetEventHandler()->ProcessEvent(event);
2bda0e17 1284
e5c0b16a
VZ
1285 if (event.MoreRequested())
1286 needMore = TRUE;
2bda0e17 1287
e5c0b16a
VZ
1288 wxNode* node = win->GetChildren().First();
1289 while (node)
1290 {
1291 wxWindow* win = (wxWindow*) node->Data();
1292 if (SendIdleEvents(win))
1293 needMore = TRUE;
2bda0e17 1294
e5c0b16a
VZ
1295 node = node->Next();
1296 }
42e69d6b 1297 return needMore;
2bda0e17
KB
1298}
1299
bb6290e3 1300void wxApp::DeletePendingObjects()
2bda0e17 1301{
e5c0b16a
VZ
1302 wxNode *node = wxPendingDelete.First();
1303 while (node)
1304 {
1305 wxObject *obj = (wxObject *)node->Data();
d50b2a58 1306
e5c0b16a 1307 delete obj;
2bda0e17 1308
e5c0b16a
VZ
1309 if (wxPendingDelete.Member(obj))
1310 delete node;
2bda0e17 1311
e5c0b16a
VZ
1312 // Deleting one object may have deleted other pending
1313 // objects, so start from beginning of list again.
1314 node = wxPendingDelete.First();
1315 }
2bda0e17
KB
1316}
1317
57c208c5 1318void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
387a3b02
JS
1319{
1320 if (GetTopWindow())
1321 GetTopWindow()->Close(TRUE);
1322}
1323
1324// Default behaviour: close the application with prompts. The
1325// user can veto the close, and therefore the end session.
1326void wxApp::OnQueryEndSession(wxCloseEvent& event)
1327{
1328 if (GetTopWindow())
1329 {
1330 if (!GetTopWindow()->Close(!event.CanVeto()))
1331 event.Veto(TRUE);
1332 }
1333}
1334
6d167489
VZ
1335/* static */
1336int wxApp::GetComCtl32Version()
1337{
04ef50df
JS
1338#ifdef __WXMICROWIN__
1339 return 0;
1340#else
6d167489 1341 // cache the result
bdc72a22
VZ
1342 static int s_verComCtl32 = -1;
1343
1344 wxCRIT_SECT_DECLARE(csComCtl32);
1345 wxCRIT_SECT_LOCKER(lock, csComCtl32);
6d167489
VZ
1346
1347 if ( s_verComCtl32 == -1 )
1348 {
bdc72a22 1349 // initally assume no comctl32.dll at all
6d167489
VZ
1350 s_verComCtl32 = 0;
1351
bdc72a22
VZ
1352 // do we have it?
1353 HMODULE hModuleComCtl32 = ::GetModuleHandle(wxT("COMCTL32"));
6d167489
VZ
1354
1355 // if so, then we can check for the version
bdc72a22 1356 if ( hModuleComCtl32 )
bb6290e3 1357 {
bdc72a22
VZ
1358 // try to use DllGetVersion() if available in _headers_
1359 #ifdef DLLVER_PLATFORM_WINDOWS // defined in shlwapi.h
1360 DLLGETVERSIONPROC pfnDllGetVersion = (DLLGETVERSIONPROC)
f6bcfd97 1361 ::GetProcAddress(hModuleComCtl32, "DllGetVersion");
bdc72a22 1362 if ( pfnDllGetVersion )
6d167489 1363 {
bdc72a22
VZ
1364 DLLVERSIONINFO dvi;
1365 dvi.cbSize = sizeof(dvi);
1366
1367 HRESULT hr = (*pfnDllGetVersion)(&dvi);
1368 if ( FAILED(hr) )
1369 {
1370 wxLogApiError(_T("DllGetVersion"), hr);
1371 }
1372 else
1373 {
1374 // this is incompatible with _WIN32_IE values, but
1375 // compatible with the other values returned by
1376 // GetComCtl32Version()
1377 s_verComCtl32 = 100*dvi.dwMajorVersion +
1378 dvi.dwMinorVersion;
1379 }
6d167489 1380 }
bdc72a22
VZ
1381 #endif
1382 // DllGetVersion() unavailable either during compile or
1383 // run-time, try to guess the version otherwise
1384 if ( !s_verComCtl32 )
1385 {
1386 // InitCommonControlsEx is unique to 4.70 and later
1387 FARPROC theProc = ::GetProcAddress
1388 (
1389 hModuleComCtl32,
a8ee71c7 1390 "InitCommonControlsEx"
bdc72a22
VZ
1391 );
1392
1393 if ( !theProc )
1394 {
1395 // not found, must be 4.00
1396 s_verComCtl32 = 400;
1397 }
1398 else
1399 {
1400 // many symbols appeared in comctl32 4.71, could use
1401 // any of them except may be DllInstall
1402 theProc = ::GetProcAddress
1403 (
1404 hModuleComCtl32,
a8ee71c7 1405 "InitializeFlatSB"
bdc72a22
VZ
1406 );
1407 if ( !theProc )
1408 {
1409 // not found, must be 4.70
1410 s_verComCtl32 = 470;
1411 }
1412 else
1413 {
1414 // found, must be 4.71
1415 s_verComCtl32 = 471;
1416 }
1417 }
6d167489 1418 }
bb6290e3
JS
1419 }
1420 }
6d167489
VZ
1421
1422 return s_verComCtl32;
04ef50df 1423#endif
bb6290e3
JS
1424}
1425
1426void wxExit()
2bda0e17 1427{
e5c0b16a
VZ
1428 wxLogError(_("Fatal error: exiting"));
1429
1430 wxApp::CleanUp();
3f8e5072 1431 exit(0);
2bda0e17
KB
1432}
1433
1434// Yield to incoming messages
cb2713bf 1435
8461e4c2 1436bool wxApp::Yield(bool onlyIfNeeded)
2bda0e17 1437{
8461e4c2
VZ
1438 // MT-FIXME
1439 static bool s_inYield = FALSE;
1440
2ed3265e
VZ
1441 // disable log flushing from here because a call to wxYield() shouldn't
1442 // normally result in message boxes popping up &c
1443 wxLog::Suspend();
1444
8461e4c2
VZ
1445 if ( s_inYield )
1446 {
1447 if ( !onlyIfNeeded )
1448 {
1449 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1450 }
33ac7e6f 1451
8461e4c2
VZ
1452 return FALSE;
1453 }
1454
1455 s_inYield = TRUE;
cb2713bf 1456
8e193f38
VZ
1457 // we don't want to process WM_QUIT from here - it should be processed in
1458 // the main event loop in order to stop it
e5c0b16a 1459 MSG msg;
8e193f38
VZ
1460 while ( PeekMessage(&msg, (HWND)0, 0, 0, PM_NOREMOVE) &&
1461 msg.message != WM_QUIT )
e5c0b16a 1462 {
5b615ed8
VZ
1463#if wxUSE_THREADS
1464 wxMutexGuiLeaveOrEnter();
1465#endif // wxUSE_THREADS
1466
e5c0b16a
VZ
1467 if ( !wxTheApp->DoMessage() )
1468 break;
1469 }
8e193f38 1470
8461e4c2
VZ
1471 // if there are pending events, we must process them.
1472 ProcessPendingEvents();
e5c0b16a 1473
2ed3265e
VZ
1474 // let the logs be flashed again
1475 wxLog::Resume();
1476
8461e4c2 1477 s_inYield = FALSE;
cb2713bf 1478
e5c0b16a 1479 return TRUE;
2bda0e17 1480}
094637f6 1481
3b415ba4
VZ
1482bool wxHandleFatalExceptions(bool doit)
1483{
1484#if wxUSE_ON_FATAL_EXCEPTION
1485 // assume this can only be called from the main thread
1486 gs_handleExceptions = doit;
1487
1488 return TRUE;
1489#else
8461e4c2
VZ
1490 wxFAIL_MSG(_T("set wxUSE_ON_FATAL_EXCEPTION to 1 to use this function"));
1491
1492 (void)doit;
3b415ba4
VZ
1493 return FALSE;
1494#endif
1495}
1496
9779893b
RD
1497//-----------------------------------------------------------------------------
1498// wxWakeUpIdle
1499//-----------------------------------------------------------------------------
1500
1501void wxWakeUpIdle()
1502{
1503 // Send the top window a dummy message so idle handler processing will
1504 // start up again. Doing it this way ensures that the idle handler
b568d04f
VZ
1505 // wakes up in the right thread (see also wxWakeUpMainThread() which does
1506 // the same for the main app thread only)
9779893b 1507 wxWindow *topWindow = wxTheApp->GetTopWindow();
b568d04f
VZ
1508 if ( topWindow )
1509 {
1510 if ( !::PostMessage(GetHwndOf(topWindow), WM_NULL, 0, 0) )
1511 {
1512 // should never happen
f6bcfd97 1513 wxLogLastError(wxT("PostMessage(WM_NULL)"));
b568d04f 1514 }
9779893b
RD
1515 }
1516}
1517
1518//-----------------------------------------------------------------------------
1519
ebea0891
KB
1520wxIcon
1521wxApp::GetStdIcon(int which) const
1522{
094637f6
VZ
1523 switch(which)
1524 {
1525 case wxICON_INFORMATION:
1526 return wxIcon("wxICON_INFO");
1527
1528 case wxICON_QUESTION:
1529 return wxIcon("wxICON_QUESTION");
1530
1531 case wxICON_EXCLAMATION:
1532 return wxIcon("wxICON_WARNING");
1533
1534 default:
223d09f6 1535 wxFAIL_MSG(wxT("requested non existent standard icon"));
094637f6
VZ
1536 // still fall through
1537
1538 case wxICON_HAND:
1539 return wxIcon("wxICON_ERROR");
1540 }
ebea0891
KB
1541}
1542
2bda0e17
KB
1543// For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1544// if in a separate file. So include it here to ensure it's linked.
31e78e0c 1545#if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__) && !defined(WXMAKINGDLL))
e5c0b16a 1546#include "main.cpp"
2bda0e17 1547#endif