]> git.saurik.com Git - wxWidgets.git/blame - src/msw/app.cpp
fixed VC++ warning
[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;
097f29c2 284 lb.lbColor = 0;
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
1bffa913 298 wxWinHandleHash = new wxWinHashTable(wxKEY_INTEGER, 100);
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
1bffa913 704 delete wxWinHandleHash;
d50b2a58 705
3135f4a7 706 // GL: I'm annoyed ... I don't know where to put this and I don't want to
4d3a259a 707 // create a module for that as it's part of the core.
4d3a259a 708 delete wxPendingEvents;
1e6feb95 709
8e193f38 710#if wxUSE_THREADS
4d3a259a 711 delete wxPendingEventsLocker;
1e6feb95 712 // If we don't do the following, we get an apparent memory leak
f80eabe5 713#if wxUSE_VALIDATORS
63863e09 714 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
1e6feb95
VZ
715#endif // wxUSE_VALIDATORS
716#endif // wxUSE_THREADS
4d3a259a 717
e5c0b16a 718 wxClassInfo::CleanUpClasses();
0c32066b 719
e5c0b16a
VZ
720 delete wxTheApp;
721 wxTheApp = NULL;
184b5d99
JS
722
723#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
e5c0b16a
VZ
724 // At this point we want to check if there are any memory
725 // blocks that aren't part of the wxDebugContext itself,
726 // as a special case. Then when dumping we need to ignore
727 // wxDebugContext, too.
d13c32e9 728 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
e5c0b16a 729 {
5fa399c9 730 wxLogMessage(wxT("There were memory leaks."));
e5c0b16a
VZ
731 wxDebugContext::Dump();
732 wxDebugContext::PrintStatistics();
733 }
734 // wxDebugContext::SetStream(NULL, NULL);
184b5d99
JS
735#endif
736
546db2a8 737#if wxUSE_LOG
e5c0b16a
VZ
738 // do it as the very last thing because everything else can log messages
739 delete wxLog::SetActiveTarget(NULL);
546db2a8 740#endif // wxUSE_LOG
2bda0e17
KB
741}
742
7ece89c6
RD
743//----------------------------------------------------------------------
744// Entry point helpers, used by wxPython
745//----------------------------------------------------------------------
746
f6bcfd97 747int WXDLLEXPORT wxEntryStart( int WXUNUSED(argc), char** WXUNUSED(argv) )
7ece89c6
RD
748{
749 return wxApp::Initialize();
750}
751
f6bcfd97 752int WXDLLEXPORT wxEntryInitGui()
7ece89c6 753{
1e6feb95 754 return wxTheApp->OnInitGui();
7ece89c6
RD
755}
756
757void WXDLLEXPORT wxEntryCleanup()
758{
759 wxApp::CleanUp();
760}
761
762
2bda0e17
KB
763#if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
764
e5c0b16a
VZ
765// temporarily disable this warning which would be generated in release builds
766// because of __try
3f4a0c5b 767#ifdef __VISUALC__
f8a3e080
VZ
768 #pragma warning(disable: 4715) // not all control paths return a value
769#endif // Visual C++
770
7ece89c6
RD
771//----------------------------------------------------------------------
772// Main wxWindows entry point
773//----------------------------------------------------------------------
a5e0e655
VZ
774int wxEntry(WXHINSTANCE hInstance,
775 WXHINSTANCE WXUNUSED(hPrevInstance),
776 char *lpCmdLine,
777 int nCmdShow,
778 bool enterLoop)
2bda0e17 779{
6e0d9d43
VZ
780 // do check for memory leaks on program exit
781 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
782 // deallocated memory which may be used to simulate low-memory condition)
04ef50df 783#ifndef __WXMICROWIN__
6e0d9d43 784 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
04ef50df
JS
785#endif
786
6418cb93
SC
787#ifdef __MWERKS__
788#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
789 // This seems to be necessary since there are 'rogue'
790 // objects present at this point (perhaps global objects?)
791 // Setting a checkpoint will ignore them as far as the
792 // memory checking facility is concerned.
793 // Of course you may argue that memory allocated in globals should be
794 // checked, but this is a reasonable compromise.
795 wxDebugContext::SetCheckpoint();
796#endif
797#endif
3b415ba4
VZ
798
799 // take everything into a try-except block to be able to call
800 // OnFatalException() if necessary
3b415ba4 801#if wxUSE_ON_FATAL_EXCEPTION
3f4a0c5b 802 __try {
a5e0e655 803#endif
e5c0b16a 804 wxhInstance = (HINSTANCE) hInstance;
2bda0e17 805
7ece89c6 806 if (!wxEntryStart(0,0))
e5c0b16a 807 return 0;
2bda0e17 808
e5c0b16a
VZ
809 // create the application object or ensure that one already exists
810 if (!wxTheApp)
811 {
812 // The app may have declared a global application object, but we recommend
813 // the IMPLEMENT_APP macro is used instead, which sets an initializer
814 // function for delayed, dynamic app object construction.
815 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
223d09f6 816 wxT("No initializer - use IMPLEMENT_APP macro.") );
f8a3e080 817
8cb172b4 818 wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) ();
e5c0b16a
VZ
819 }
820
223d09f6 821 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
e5c0b16a
VZ
822
823 // save the WinMain() parameters
824 wxTheApp->ConvertToStandardCommandArgs(lpCmdLine);
825 wxTheApp->m_nCmdShow = nCmdShow;
a5e0e655 826
36edded9
JS
827 // We really don't want timestamps by default, because it means
828 // we can't simply double-click on the error message and get to that
829 // line in the source. So VC++ at least, let's have a sensible default.
830#ifdef __VISUALC__
831 wxLog::SetTimestamp(NULL);
832#endif
833
e5c0b16a
VZ
834 int retValue = 0;
835
6afafc42
VZ
836 // it is common to create a modal dialog in OnInit() (to ask/notify the
837 // user about something) but it wouldn't work if we don't change the
838 // "exit on delete last frame" flag here as when this dialog is
839 // deleted, the app would terminate (it was the last top level window
840 // as the main frame wasn't created yet!), so disable this behaviour
841 // temproarily
842 bool exitOnLastFrameDelete = wxTheApp->GetExitOnFrameDelete();
843 wxTheApp->SetExitOnFrameDelete(FALSE);
844
845 // init the app
1e6feb95 846 retValue = wxEntryInitGui() && wxTheApp->OnInit() ? 0 : -1;
6afafc42
VZ
847
848 // restore the old flag value
849 wxTheApp->SetExitOnFrameDelete(exitOnLastFrameDelete);
850
851 if ( retValue == 0 )
e5c0b16a
VZ
852 {
853 if ( enterLoop )
854 {
6afafc42 855 // run the main loop
e5c0b16a
VZ
856 retValue = wxTheApp->OnRun();
857 }
858 else
6afafc42
VZ
859 {
860 // we want to initialize, but not run or exit immediately.
e5c0b16a 861 return 1;
6afafc42 862 }
e5c0b16a
VZ
863 }
864 //else: app initialization failed, so we skipped OnRun()
865
866 wxWindow *topWindow = wxTheApp->GetTopWindow();
867 if ( topWindow )
868 {
869 // Forcibly delete the window.
870 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
871 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
872 {
873 topWindow->Close(TRUE);
874 wxTheApp->DeletePendingObjects();
875 }
876 else
877 {
878 delete topWindow;
879 wxTheApp->SetTopWindow(NULL);
880 }
881 }
882
883 wxTheApp->OnExit();
884
7ece89c6 885 wxEntryCleanup();
e5c0b16a
VZ
886
887 return retValue;
888
3b415ba4 889#if wxUSE_ON_FATAL_EXCEPTION
e5c0b16a 890 }
3b415ba4
VZ
891 __except ( gs_handleExceptions ? EXCEPTION_EXECUTE_HANDLER
892 : EXCEPTION_CONTINUE_SEARCH ) {
893 if ( wxTheApp )
f6bcfd97
BP
894 {
895 // give the user a chance to do something special about this
e5c0b16a 896 wxTheApp->OnFatalException();
f6bcfd97 897 }
e5c0b16a
VZ
898
899 ::ExitProcess(3); // the same exit code as abort()
900
901 // NOTREACHED
902 }
3b415ba4 903#endif // wxUSE_ON_FATAL_EXCEPTION
2bda0e17
KB
904}
905
f8a3e080 906// restore warning state
3f4a0c5b 907#ifdef __VISUALC__
f8a3e080
VZ
908 #pragma warning(default: 4715) // not all control paths return a value
909#endif // Visual C++
910
2bda0e17
KB
911#else /* _WINDLL */
912
7ece89c6
RD
913//----------------------------------------------------------------------
914// Entry point for wxWindows + the App in a DLL
915//----------------------------------------------------------------------
589f0e3e 916
2bda0e17
KB
917int wxEntry(WXHINSTANCE hInstance)
918{
e5c0b16a 919 wxhInstance = (HINSTANCE) hInstance;
7ece89c6 920 wxEntryStart(0, 0);
2bda0e17 921
e5c0b16a
VZ
922 // The app may have declared a global application object, but we recommend
923 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
924 // for delayed, dynamic app object construction.
925 if (!wxTheApp)
a5e0e655 926 {
e5c0b16a
VZ
927 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
928 "No initializer - use IMPLEMENT_APP macro." );
2bda0e17 929
e5c0b16a
VZ
930 wxTheApp = (* wxApp::GetInitializerFunction()) ();
931 }
2bda0e17 932
e5c0b16a 933 wxCHECK_MSG( wxTheApp, 0, "You have to define an instance of wxApp!" );
2bda0e17 934
e5c0b16a
VZ
935 wxTheApp->argc = 0;
936 wxTheApp->argv = NULL;
2bda0e17 937
7ece89c6 938 wxEntryInitGui();
2bda0e17 939
e5c0b16a 940 wxTheApp->OnInit();
2bda0e17 941
e5c0b16a
VZ
942 wxWindow *topWindow = wxTheApp->GetTopWindow();
943 if ( topWindow && topWindow->GetHWND())
944 {
945 topWindow->Show(TRUE);
946 }
2bda0e17 947
e5c0b16a 948 return 1;
2bda0e17
KB
949}
950#endif // _WINDLL
951
589f0e3e
JS
952//// Static member initialization
953
c94ad3c3 954wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
2bda0e17 955
bb6290e3 956wxApp::wxApp()
2bda0e17 957{
e5c0b16a
VZ
958 argc = 0;
959 argv = NULL;
e5c0b16a 960 m_printMode = wxPRINT_WINDOWS;
e5c0b16a 961 m_auto3D = TRUE;
2bda0e17
KB
962}
963
589f0e3e
JS
964wxApp::~wxApp()
965{
e5c0b16a
VZ
966 // Delete command-line args
967 int i;
968 for (i = 0; i < argc; i++)
969 {
970 delete[] argv[i];
971 }
972 delete[] argv;
589f0e3e
JS
973}
974
bb6290e3 975bool wxApp::Initialized()
2bda0e17
KB
976{
977#ifndef _WINDLL
e5c0b16a
VZ
978 if (GetTopWindow())
979 return TRUE;
980 else
981 return FALSE;
3b415ba4 982#else // Assume initialized if DLL (no way of telling)
e5c0b16a 983 return TRUE;
2bda0e17
KB
984#endif
985}
986
987/*
988 * Get and process a message, returning FALSE if WM_QUIT
bee503b0 989 * received (and also set the flag telling the app to exit the main loop)
2bda0e17
KB
990 *
991 */
bb6290e3 992bool wxApp::DoMessage()
2bda0e17 993{
bee503b0
VZ
994 BOOL rc = ::GetMessage(&s_currentMsg, (HWND) NULL, 0, 0);
995 if ( rc == 0 )
996 {
997 // got WM_QUIT
998 m_keepGoing = FALSE;
4a9968f9 999
bee503b0
VZ
1000 return FALSE;
1001 }
1002 else if ( rc == -1 )
1003 {
1004 // should never happen, but let's test for it nevertheless
f6bcfd97 1005 wxLogLastError(wxT("GetMessage"));
bee503b0
VZ
1006 }
1007 else
1008 {
1009#if wxUSE_THREADS
1010 wxASSERT_MSG( wxThread::IsMain(),
223d09f6 1011 wxT("only the main thread can process Windows messages") );
d50b2a58 1012
bee503b0
VZ
1013 static bool s_hadGuiLock = TRUE;
1014 static wxMsgArray s_aSavedMessages;
1015
1016 // if a secondary thread owns is doing GUI calls, save all messages for
1017 // later processing - we can't process them right now because it will
1018 // lead to recursive library calls (and we're not reentrant)
1019 if ( !wxGuiOwnedByMainThread() )
1020 {
1021 s_hadGuiLock = FALSE;
1022
4a9968f9
VZ
1023 // leave out WM_COMMAND messages: too dangerous, sometimes
1024 // the message will be processed twice
1025 if ( !wxIsWaitingForThread() ||
e5c0b16a 1026 s_currentMsg.message != WM_COMMAND )
4a9968f9
VZ
1027 {
1028 s_aSavedMessages.Add(s_currentMsg);
1029 }
bee503b0
VZ
1030
1031 return TRUE;
1032 }
1033 else
1034 {
1035 // have we just regained the GUI lock? if so, post all of the saved
1036 // messages
1037 //
1038 // FIXME of course, it's not _exactly_ the same as processing the
1039 // messages normally - expect some things to break...
1040 if ( !s_hadGuiLock )
1041 {
1042 s_hadGuiLock = TRUE;
1043
1044 size_t count = s_aSavedMessages.Count();
1045 for ( size_t n = 0; n < count; n++ )
1046 {
1047 MSG& msg = s_aSavedMessages[n];
1048
1049 if ( !ProcessMessage((WXMSG *)&msg) )
1050 {
1051 ::TranslateMessage(&msg);
1052 ::DispatchMessage(&msg);
1053 }
1054 }
1055
1056 s_aSavedMessages.Empty();
1057 }
1058 }
1059#endif // wxUSE_THREADS
1060
1061 // Process the message
ed45e263 1062 DoMessage((WXMSG *)&s_currentMsg);
bee503b0
VZ
1063 }
1064
1065 return TRUE;
2bda0e17
KB
1066}
1067
ed45e263
VZ
1068void wxApp::DoMessage(WXMSG *pMsg)
1069{
1070 if ( !ProcessMessage(pMsg) )
1071 {
1072 ::TranslateMessage((MSG *)pMsg);
1073 ::DispatchMessage((MSG *)pMsg);
1074 }
1075}
1076
2bda0e17
KB
1077/*
1078 * Keep trying to process messages until WM_QUIT
1079 * received.
1080 *
1081 * If there are messages to be processed, they will all be
1082 * processed and OnIdle will not be called.
1083 * When there are no more messages, OnIdle is called.
1084 * If OnIdle requests more time,
1085 * it will be repeatedly called so long as there are no pending messages.
1086 * A 'feature' of this is that once OnIdle has decided that no more processing
1087 * is required, then it won't get processing time until further messages
1088 * are processed (it'll sit in DoMessage).
1089 */
1090
bb6290e3 1091int wxApp::MainLoop()
2bda0e17 1092{
e5c0b16a 1093 m_keepGoing = TRUE;
bee503b0 1094
e5c0b16a
VZ
1095 while ( m_keepGoing )
1096 {
1097#if wxUSE_THREADS
bee503b0 1098 wxMutexGuiLeaveOrEnter();
e5c0b16a 1099#endif // wxUSE_THREADS
bee503b0 1100
b6c588e1
VZ
1101 while ( !Pending() && ProcessIdle() )
1102 ;
7214297d 1103
b6c588e1 1104 // a message came or no more idle processing to do
e5c0b16a
VZ
1105 DoMessage();
1106 }
2bda0e17 1107
e5c0b16a 1108 return s_currentMsg.wParam;
2bda0e17
KB
1109}
1110
1111// Returns TRUE if more time is needed.
bb6290e3 1112bool wxApp::ProcessIdle()
2bda0e17
KB
1113{
1114 wxIdleEvent event;
1115 event.SetEventObject(this);
1116 ProcessEvent(event);
1117
1118 return event.MoreRequested();
1119}
1120
bb6290e3 1121void wxApp::ExitMainLoop()
2bda0e17 1122{
b6c588e1 1123 // VZ: why not ::PostQuitMessage()?
e5c0b16a 1124 m_keepGoing = FALSE;
2bda0e17
KB
1125}
1126
bb6290e3 1127bool wxApp::Pending()
2bda0e17 1128{
b6c588e1 1129 return ::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) != 0;
2bda0e17
KB
1130}
1131
bb6290e3 1132void wxApp::Dispatch()
2bda0e17 1133{
bee503b0 1134 DoMessage();
2bda0e17
KB
1135}
1136
1137/*
1138 * Give all windows a chance to preprocess
1139 * the message. Some may have accelerator tables, or have
1140 * MDI complications.
1141 */
2a47d3c1 1142
d3f0a137 1143bool wxApp::ProcessMessage(WXMSG *wxmsg)
2bda0e17 1144{
d3f0a137 1145 MSG *msg = (MSG *)wxmsg;
2a3caeb5
VZ
1146 HWND hwnd = msg->hwnd;
1147 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
2bda0e17 1148
761989ff
VZ
1149 // this may happen if the event occured in a standard modeless dialog (the
1150 // only example of which I know of is the find/replace dialog) - then call
1151 // IsDialogMessage() to make TAB navigation in it work
1152 if ( !wndThis )
1153 {
2a3caeb5
VZ
1154 // we need to find the dialog containing this control as
1155 // IsDialogMessage() just eats all the messages (i.e. returns TRUE for
1156 // them) if we call it for the control itself
de7f0860 1157 while ( hwnd && ::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD )
2a3caeb5
VZ
1158 {
1159 hwnd = ::GetParent(hwnd);
1160 }
1161
de7f0860 1162 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
761989ff
VZ
1163 }
1164
8614c467
VZ
1165#if wxUSE_TOOLTIPS
1166 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
1167 // popup the tooltip bubbles
761989ff 1168 if ( (msg->message == WM_MOUSEMOVE) )
834362a2 1169 {
8614c467
VZ
1170 wxToolTip *tt = wndThis->GetToolTip();
1171 if ( tt )
1172 {
1173 tt->RelayEvent(wxmsg);
1174 }
834362a2 1175 }
8614c467 1176#endif // wxUSE_TOOLTIPS
834362a2 1177
a37d422a
VZ
1178 // allow the window to prevent certain messages from being
1179 // translated/processed (this is currently used by wxTextCtrl to always
1180 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
1181 if ( !wndThis->MSWShouldPreProcessMessage(wxmsg) )
1182 {
1183 return FALSE;
1184 }
1185
2a3caeb5 1186 // try translations first: the accelerators override everything
8614c467 1187 wxWindow *wnd;
42bcb12b 1188
d3f0a137 1189 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
2bda0e17 1190 {
2a3caeb5 1191 if ( wnd->MSWTranslateMessage(wxmsg))
d3f0a137 1192 return TRUE;
3bdc265d
VZ
1193
1194 // stop at first top level window, i.e. don't try to process the key
1195 // strokes originating in a dialog using the accelerators of the parent
1196 // frame - this doesn't make much sense
1197 if ( wnd->IsTopLevel() )
2a3caeb5
VZ
1198 break;
1199 }
1200
a37d422a
VZ
1201 // now try the other hooks (kbd navigation is handled here): we start from
1202 // wndThis->GetParent() because wndThis->MSWProcessMessage() was already
1203 // called above
1204 for ( wnd = wndThis->GetParent(); wnd; wnd = wnd->GetParent() )
2a3caeb5
VZ
1205 {
1206 if ( wnd->MSWProcessMessage(wxmsg) )
1207 return TRUE;
57a7b7c1 1208 }
d3f0a137 1209
a37d422a 1210 // no special preprocessing for this message, dispatch it normally
e5c0b16a 1211 return FALSE;
2bda0e17
KB
1212}
1213
1214void wxApp::OnIdle(wxIdleEvent& event)
1215{
3222fde2 1216 static bool s_inOnIdle = FALSE;
2bda0e17 1217
3222fde2
VZ
1218 // Avoid recursion (via ProcessEvent default case)
1219 if ( s_inOnIdle )
1220 return;
2bda0e17 1221
3222fde2 1222 s_inOnIdle = TRUE;
2bda0e17 1223
52e52bea
GRG
1224 // If there are pending events, we must process them: pending events
1225 // are either events to the threads other than main or events posted
1226 // with wxPostEvent() functions
1227 // GRG: I have moved this here so that all pending events are processed
1228 // before starting to delete any objects. This behaves better (in
1229 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
1230 // behaviour. Changed Feb/2000 before 2.1.14
1231 ProcessPendingEvents();
1232
3222fde2
VZ
1233 // 'Garbage' collection of windows deleted with Close().
1234 DeletePendingObjects();
2bda0e17 1235
546db2a8 1236#if wxUSE_LOG
3222fde2 1237 // flush the logged messages if any
2ed3265e 1238 wxLog::FlushActive();
546db2a8 1239#endif // wxUSE_LOG
c54f78a2 1240
aef94d68
JS
1241#if wxUSE_DC_CACHEING
1242 // automated DC cache management: clear the cached DCs and bitmap
1243 // if it's likely that the app has finished with them, that is, we
1244 // get an idle event and we're not dragging anything.
4624defa 1245 if (!::GetKeyState(MK_LBUTTON) && !::GetKeyState(MK_MBUTTON) && !::GetKeyState(MK_RBUTTON))
aef94d68
JS
1246 wxDC::ClearCache();
1247#endif // wxUSE_DC_CACHEING
1248
3222fde2
VZ
1249 // Send OnIdle events to all windows
1250 if ( SendIdleEvents() )
1251 {
1252 // SendIdleEvents() returns TRUE if at least one window requested more
1253 // idle events
1254 event.RequestMore(TRUE);
1255 }
2bda0e17 1256
3222fde2 1257 s_inOnIdle = FALSE;
2bda0e17
KB
1258}
1259
1260// Send idle event to all top-level windows
bb6290e3 1261bool wxApp::SendIdleEvents()
2bda0e17
KB
1262{
1263 bool needMore = FALSE;
e146b8c8 1264
f1d534df 1265 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
3222fde2
VZ
1266 while (node)
1267 {
e146b8c8 1268 wxWindow* win = node->GetData();
3222fde2 1269 if (SendIdleEvents(win))
2bda0e17 1270 needMore = TRUE;
e146b8c8 1271 node = node->GetNext();
3222fde2
VZ
1272 }
1273
2bda0e17
KB
1274 return needMore;
1275}
1276
1277// Send idle event to window and all subwindows
1278bool wxApp::SendIdleEvents(wxWindow* win)
1279{
e5c0b16a 1280 bool needMore = FALSE;
2bda0e17 1281
e5c0b16a
VZ
1282 wxIdleEvent event;
1283 event.SetEventObject(win);
1284 win->GetEventHandler()->ProcessEvent(event);
2bda0e17 1285
e5c0b16a
VZ
1286 if (event.MoreRequested())
1287 needMore = TRUE;
2bda0e17 1288
e5c0b16a
VZ
1289 wxNode* node = win->GetChildren().First();
1290 while (node)
1291 {
1292 wxWindow* win = (wxWindow*) node->Data();
1293 if (SendIdleEvents(win))
1294 needMore = TRUE;
2bda0e17 1295
e5c0b16a
VZ
1296 node = node->Next();
1297 }
42e69d6b 1298 return needMore;
2bda0e17
KB
1299}
1300
bb6290e3 1301void wxApp::DeletePendingObjects()
2bda0e17 1302{
e5c0b16a
VZ
1303 wxNode *node = wxPendingDelete.First();
1304 while (node)
1305 {
1306 wxObject *obj = (wxObject *)node->Data();
d50b2a58 1307
e5c0b16a 1308 delete obj;
2bda0e17 1309
e5c0b16a
VZ
1310 if (wxPendingDelete.Member(obj))
1311 delete node;
2bda0e17 1312
e5c0b16a
VZ
1313 // Deleting one object may have deleted other pending
1314 // objects, so start from beginning of list again.
1315 node = wxPendingDelete.First();
1316 }
2bda0e17
KB
1317}
1318
57c208c5 1319void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
387a3b02
JS
1320{
1321 if (GetTopWindow())
1322 GetTopWindow()->Close(TRUE);
1323}
1324
1325// Default behaviour: close the application with prompts. The
1326// user can veto the close, and therefore the end session.
1327void wxApp::OnQueryEndSession(wxCloseEvent& event)
1328{
1329 if (GetTopWindow())
1330 {
1331 if (!GetTopWindow()->Close(!event.CanVeto()))
1332 event.Veto(TRUE);
1333 }
1334}
1335
6d167489
VZ
1336/* static */
1337int wxApp::GetComCtl32Version()
1338{
04ef50df
JS
1339#ifdef __WXMICROWIN__
1340 return 0;
1341#else
6d167489 1342 // cache the result
bdc72a22
VZ
1343 static int s_verComCtl32 = -1;
1344
1345 wxCRIT_SECT_DECLARE(csComCtl32);
1346 wxCRIT_SECT_LOCKER(lock, csComCtl32);
6d167489
VZ
1347
1348 if ( s_verComCtl32 == -1 )
1349 {
bdc72a22 1350 // initally assume no comctl32.dll at all
6d167489
VZ
1351 s_verComCtl32 = 0;
1352
bdc72a22
VZ
1353 // do we have it?
1354 HMODULE hModuleComCtl32 = ::GetModuleHandle(wxT("COMCTL32"));
6d167489
VZ
1355
1356 // if so, then we can check for the version
bdc72a22 1357 if ( hModuleComCtl32 )
bb6290e3 1358 {
bdc72a22
VZ
1359 // try to use DllGetVersion() if available in _headers_
1360 #ifdef DLLVER_PLATFORM_WINDOWS // defined in shlwapi.h
1361 DLLGETVERSIONPROC pfnDllGetVersion = (DLLGETVERSIONPROC)
f6bcfd97 1362 ::GetProcAddress(hModuleComCtl32, "DllGetVersion");
bdc72a22 1363 if ( pfnDllGetVersion )
6d167489 1364 {
bdc72a22
VZ
1365 DLLVERSIONINFO dvi;
1366 dvi.cbSize = sizeof(dvi);
1367
1368 HRESULT hr = (*pfnDllGetVersion)(&dvi);
1369 if ( FAILED(hr) )
1370 {
1371 wxLogApiError(_T("DllGetVersion"), hr);
1372 }
1373 else
1374 {
1375 // this is incompatible with _WIN32_IE values, but
1376 // compatible with the other values returned by
1377 // GetComCtl32Version()
1378 s_verComCtl32 = 100*dvi.dwMajorVersion +
1379 dvi.dwMinorVersion;
1380 }
6d167489 1381 }
bdc72a22
VZ
1382 #endif
1383 // DllGetVersion() unavailable either during compile or
1384 // run-time, try to guess the version otherwise
1385 if ( !s_verComCtl32 )
1386 {
1387 // InitCommonControlsEx is unique to 4.70 and later
1388 FARPROC theProc = ::GetProcAddress
1389 (
1390 hModuleComCtl32,
a8ee71c7 1391 "InitCommonControlsEx"
bdc72a22
VZ
1392 );
1393
1394 if ( !theProc )
1395 {
1396 // not found, must be 4.00
1397 s_verComCtl32 = 400;
1398 }
1399 else
1400 {
1401 // many symbols appeared in comctl32 4.71, could use
1402 // any of them except may be DllInstall
1403 theProc = ::GetProcAddress
1404 (
1405 hModuleComCtl32,
a8ee71c7 1406 "InitializeFlatSB"
bdc72a22
VZ
1407 );
1408 if ( !theProc )
1409 {
1410 // not found, must be 4.70
1411 s_verComCtl32 = 470;
1412 }
1413 else
1414 {
1415 // found, must be 4.71
1416 s_verComCtl32 = 471;
1417 }
1418 }
6d167489 1419 }
bb6290e3
JS
1420 }
1421 }
6d167489
VZ
1422
1423 return s_verComCtl32;
04ef50df 1424#endif
bb6290e3
JS
1425}
1426
1427void wxExit()
2bda0e17 1428{
e5c0b16a
VZ
1429 wxLogError(_("Fatal error: exiting"));
1430
1431 wxApp::CleanUp();
3f8e5072 1432 exit(0);
2bda0e17
KB
1433}
1434
1435// Yield to incoming messages
cb2713bf 1436
8461e4c2 1437bool wxApp::Yield(bool onlyIfNeeded)
2bda0e17 1438{
8461e4c2
VZ
1439 // MT-FIXME
1440 static bool s_inYield = FALSE;
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
8461e4c2
VZ
1446 if ( s_inYield )
1447 {
1448 if ( !onlyIfNeeded )
1449 {
1450 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1451 }
33ac7e6f 1452
8461e4c2
VZ
1453 return FALSE;
1454 }
1455
1456 s_inYield = TRUE;
cb2713bf 1457
8e193f38
VZ
1458 // we don't want to process WM_QUIT from here - it should be processed in
1459 // the main event loop in order to stop it
e5c0b16a 1460 MSG msg;
8e193f38
VZ
1461 while ( PeekMessage(&msg, (HWND)0, 0, 0, PM_NOREMOVE) &&
1462 msg.message != WM_QUIT )
e5c0b16a 1463 {
5b615ed8
VZ
1464#if wxUSE_THREADS
1465 wxMutexGuiLeaveOrEnter();
1466#endif // wxUSE_THREADS
1467
e5c0b16a
VZ
1468 if ( !wxTheApp->DoMessage() )
1469 break;
1470 }
8e193f38 1471
8461e4c2
VZ
1472 // if there are pending events, we must process them.
1473 ProcessPendingEvents();
e5c0b16a 1474
2ed3265e
VZ
1475 // let the logs be flashed again
1476 wxLog::Resume();
1477
8461e4c2 1478 s_inYield = FALSE;
cb2713bf 1479
e5c0b16a 1480 return TRUE;
2bda0e17 1481}
094637f6 1482
3b415ba4
VZ
1483bool wxHandleFatalExceptions(bool doit)
1484{
1485#if wxUSE_ON_FATAL_EXCEPTION
1486 // assume this can only be called from the main thread
1487 gs_handleExceptions = doit;
1488
1489 return TRUE;
1490#else
8461e4c2
VZ
1491 wxFAIL_MSG(_T("set wxUSE_ON_FATAL_EXCEPTION to 1 to use this function"));
1492
1493 (void)doit;
3b415ba4
VZ
1494 return FALSE;
1495#endif
1496}
1497
9779893b
RD
1498//-----------------------------------------------------------------------------
1499// wxWakeUpIdle
1500//-----------------------------------------------------------------------------
1501
1502void wxWakeUpIdle()
1503{
1504 // Send the top window a dummy message so idle handler processing will
1505 // start up again. Doing it this way ensures that the idle handler
b568d04f
VZ
1506 // wakes up in the right thread (see also wxWakeUpMainThread() which does
1507 // the same for the main app thread only)
9779893b 1508 wxWindow *topWindow = wxTheApp->GetTopWindow();
b568d04f
VZ
1509 if ( topWindow )
1510 {
1511 if ( !::PostMessage(GetHwndOf(topWindow), WM_NULL, 0, 0) )
1512 {
1513 // should never happen
f6bcfd97 1514 wxLogLastError(wxT("PostMessage(WM_NULL)"));
b568d04f 1515 }
9779893b
RD
1516 }
1517}
1518
1519//-----------------------------------------------------------------------------
1520
ebea0891
KB
1521wxIcon
1522wxApp::GetStdIcon(int which) const
1523{
094637f6
VZ
1524 switch(which)
1525 {
1526 case wxICON_INFORMATION:
1527 return wxIcon("wxICON_INFO");
1528
1529 case wxICON_QUESTION:
1530 return wxIcon("wxICON_QUESTION");
1531
1532 case wxICON_EXCLAMATION:
1533 return wxIcon("wxICON_WARNING");
1534
1535 default:
223d09f6 1536 wxFAIL_MSG(wxT("requested non existent standard icon"));
094637f6
VZ
1537 // still fall through
1538
1539 case wxICON_HAND:
1540 return wxIcon("wxICON_ERROR");
1541 }
ebea0891
KB
1542}
1543
2bda0e17
KB
1544// For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1545// if in a separate file. So include it here to ensure it's linked.
31e78e0c 1546#if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__) && !defined(WXMAKINGDLL))
e5c0b16a 1547#include "main.cpp"
2bda0e17 1548#endif