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