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