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