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