]> git.saurik.com Git - wxWidgets.git/blame - src/msw/app.cpp
wxEVT_COMMAND_CHOICE_SELECTED => wxEVT_COMMAND_COMBOBOX_SELECTED
[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
12#ifdef __GNUG__
a3b46648 13#pragma implementation "app.h"
2bda0e17
KB
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
2bda0e17
KB
17#include "wx/wxprec.h"
18
19#if defined(__BORLANDC__)
a3b46648 20#pragma hdrstop
2bda0e17
KB
21#endif
22
23#ifndef WX_PRECOMP
a5e0e655
VZ
24 #include "wx/frame.h"
25 #include "wx/app.h"
26 #include "wx/utils.h"
27 #include "wx/gdicmn.h"
28 #include "wx/pen.h"
29 #include "wx/brush.h"
30 #include "wx/cursor.h"
31 #include "wx/icon.h"
32 #include "wx/palette.h"
33 #include "wx/dc.h"
34 #include "wx/dialog.h"
35 #include "wx/msgdlg.h"
3d05544e 36 #include "wx/intl.h"
1c10eaf7 37 #include "wx/dynarray.h"
2bda0e17
KB
38#endif
39
40#include "wx/msw/private.h"
2bda0e17
KB
41#include "wx/log.h"
42#include "wx/module.h"
4bf78aae
JS
43
44#if wxUSE_THREADS
bee503b0
VZ
45 #include "wx/thread.h"
46
47 // define the array of MSG strutures
48 WX_DECLARE_OBJARRAY(MSG, wxMsgArray);
49
50 #include "wx/arrimpl.cpp"
51
52 WX_DEFINE_OBJARRAY(wxMsgArray);
53#endif // wxUSE_THREADS
2bda0e17 54
47d67540 55#if wxUSE_WX_RESOURCES
a5e0e655 56 #include "wx/resource.h"
2bda0e17
KB
57#endif
58
62448488
JS
59// To UG: there's no point in putting this #if here
60// if you don't do the same for the Ole calls further down.
61// Also, OLE is used not just for drag and drop (it's used by automatn.cpp).
62// #if wxUSE_DRAG_AND_DROP
ce3ed50d 63#if !defined(__GNUWIN32__) && !defined(__SC__) && !defined(__SALFORDC__)
1e6d9499 64#include <ole2.h>
d05237ea 65#endif
ce3ed50d 66
62448488
JS
67// #endif
68
2bda0e17 69#include <string.h>
a5e0e655 70#include <ctype.h>
2bda0e17 71
57c208c5 72#if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
a5e0e655 73 #include <commctrl.h>
2bda0e17
KB
74#endif
75
fd3f686c
VZ
76// use debug CRT functions for memory leak detections in VC++ if we're not
77// using wxWindows own methods
8a111b29 78#if defined(__WXDEBUG__) && defined(_MSC_VER) && !wxUSE_GLOBAL_MEMORY_OPERATORS && !defined(__NO_VC_CRTDBG__)
fd3f686c
VZ
79 #define wxUSE_VC_CRTDBG
80#else
81 #undef wxUSE_VC_CRTDBG
82#endif
83
84#ifdef wxUSE_VC_CRTDBG
a5e0e655
VZ
85 // VC++ uses this macro as debug/release mode indicator
86 #ifndef _DEBUG
87 #define _DEBUG
88 #endif
89
c030b70f
JS
90 /* Need to undef new if including crtdbg.h */
91 #ifdef new
f8a3e080 92 #undef new
c030b70f
JS
93 #endif
94
370382c7 95 #include <crtdbg.h>
c030b70f
JS
96
97 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
f8a3e080 98 #define new new(__FILE__,__LINE__)
c030b70f 99 #endif
f8a3e080 100#endif // wxUSE_VC_CRTDBG
370382c7 101
2bda0e17
KB
102extern char *wxBuffer;
103extern char *wxOsVersion;
104extern wxList *wxWinHandleList;
cde9f08e 105extern wxList WXDLLEXPORT wxPendingDelete;
2bda0e17
KB
106extern void wxSetKeyboardHook(bool doIt);
107extern wxCursor *g_globalCursor;
108
c4e7c2aa 109HINSTANCE wxhInstance = 0;
2bda0e17
KB
110static MSG s_currentMsg;
111wxApp *wxTheApp = NULL;
112
a5e0e655 113// @@ why not const? and not static?
2bda0e17
KB
114char wxFrameClassName[] = "wxFrameClass";
115char wxMDIFrameClassName[] = "wxMDIFrameClass";
116char wxMDIChildFrameClassName[] = "wxMDIChildFrameClass";
117char wxPanelClassName[] = "wxPanelClass";
118char wxCanvasClassName[] = "wxCanvasClass";
119
57c208c5
JS
120HICON wxSTD_FRAME_ICON = (HICON) NULL;
121HICON wxSTD_MDICHILDFRAME_ICON = (HICON) NULL;
122HICON wxSTD_MDIPARENTFRAME_ICON = (HICON) NULL;
2bda0e17 123
57c208c5
JS
124HICON wxDEFAULT_FRAME_ICON = (HICON) NULL;
125HICON wxDEFAULT_MDICHILDFRAME_ICON = (HICON) NULL;
126HICON wxDEFAULT_MDIPARENTFRAME_ICON = (HICON) NULL;
2bda0e17 127
57c208c5 128HBRUSH wxDisableButtonBrush = (HBRUSH) 0;
2bda0e17
KB
129
130LRESULT APIENTRY wxWndProc(HWND, UINT, WPARAM, LPARAM);
131
132#if !USE_SHARED_LIBRARY
a5e0e655
VZ
133 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
134
135 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
136 EVT_IDLE(wxApp::OnIdle)
387a3b02
JS
137 EVT_END_SESSION(wxApp::OnEndSession)
138 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
a5e0e655 139 END_EVENT_TABLE()
2bda0e17
KB
140#endif
141
142long wxApp::sm_lastMessageTime = 0;
143
57c208c5
JS
144#if defined(__WIN95__) && !defined(__TWIN32__)
145#define wxUSE_RICHEDIT 1
146#else
147#define wxUSE_RICHEDIT 0
148#endif
149
150#if wxUSE_RICHEDIT
151 static HINSTANCE gs_hRichEdit = (HINSTANCE) NULL;
2bda0e17
KB
152#endif
153
589f0e3e
JS
154//// Initialize
155
156bool wxApp::Initialize()
2bda0e17 157{
c030b70f
JS
158 // Some people may wish to use this, but
159 // probably it shouldn't be here by default.
160#ifdef __WXDEBUG__
161// wxRedirectIOToConsole();
162#endif
163
aa0b7e1e 164 wxBuffer = new char[1500];
589f0e3e 165
aa0b7e1e 166#ifdef wxUSE_VC_CRTDBG
589f0e3e
JS
167 // do check for memory leaks on program exit
168 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
169 // deallocated memory which may be used to simulate low-memory condition)
170 _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
aa0b7e1e 171#endif // debug build under MS VC++
589f0e3e 172
aa0b7e1e 173 wxClassInfo::InitializeClasses();
589f0e3e 174
aa0b7e1e 175#if wxUSE_RESOURCES
a5e0e655 176 wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
aa0b7e1e 177#endif
589f0e3e 178
aa0b7e1e
JS
179 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
180 wxTheColourDatabase->Initialize();
589f0e3e 181
aa0b7e1e
JS
182 wxInitializeStockLists();
183 wxInitializeStockObjects();
589f0e3e 184
aa0b7e1e 185#if wxUSE_WX_RESOURCES
a5e0e655 186 wxInitializeResourceSystem();
aa0b7e1e 187#endif
2bda0e17 188
aa0b7e1e 189 wxBitmap::InitStandardHandlers();
2bda0e17 190
aa0b7e1e 191#if defined(__WIN95__)
a5e0e655 192 InitCommonControls();
57c208c5
JS
193
194#if wxUSE_RICHEDIT
a5e0e655 195 gs_hRichEdit = LoadLibrary("RICHED32.DLL");
2bda0e17 196
57c208c5 197 if (gs_hRichEdit == (HINSTANCE) NULL)
a5e0e655
VZ
198 {
199 wxMessageBox("Could not initialise Rich Edit DLL");
200 }
57c208c5
JS
201#endif
202
aa0b7e1e 203#endif
2bda0e17 204
aa0b7e1e 205 int iMsg = 96;
4a9968f9 206
aa0b7e1e
JS
207 // for OLE, enlarge message queue to be as large as possible
208 while (!SetMessageQueue(iMsg) && (iMsg -= 8));
209
210/*
211 DWORD dwOleVer;
212 dwOleVer = CoBuildVersion();
4a9968f9 213
aa0b7e1e
JS
214 // check the OLE library version
215 if (rmm != HIWORD(dwOleVer))
216 {
217 wxMessageBox("Incorrect version of OLE libraries.");
218 return FALSE;
219 }
220*/
5de5db0e 221
ce3ed50d 222#if !defined(__GNUWIN32__) && !defined(__SC__) && !defined(__SALFORDC__)
a5e0e655
VZ
223 // we need to initialize OLE library
224 if ( FAILED(::OleInitialize(NULL)) )
225 wxFatalError(_("Cannot initialize OLE"));
5de5db0e 226#endif
2bda0e17 227
aa0b7e1e 228#if CTL3D
a5e0e655
VZ
229 if (!Ctl3dRegister(wxhInstance))
230 wxFatalError("Cannot register CTL3D");
2bda0e17 231
a5e0e655 232 Ctl3dAutoSubclass(wxhInstance);
aa0b7e1e 233#endif
2bda0e17 234
aa0b7e1e 235 g_globalCursor = new wxCursor;
589f0e3e 236
aa0b7e1e
JS
237 wxSTD_FRAME_ICON = LoadIcon(wxhInstance, "wxSTD_FRAME");
238 wxSTD_MDIPARENTFRAME_ICON = LoadIcon(wxhInstance, "wxSTD_MDIPARENTFRAME");
239 wxSTD_MDICHILDFRAME_ICON = LoadIcon(wxhInstance, "wxSTD_MDICHILDFRAME");
2bda0e17 240
aa0b7e1e
JS
241 wxDEFAULT_FRAME_ICON = LoadIcon(wxhInstance, "wxDEFAULT_FRAME");
242 wxDEFAULT_MDIPARENTFRAME_ICON = LoadIcon(wxhInstance, "wxDEFAULT_MDIPARENTFRAME");
243 wxDEFAULT_MDICHILDFRAME_ICON = LoadIcon(wxhInstance, "wxDEFAULT_MDICHILDFRAME");
2bda0e17 244
aa0b7e1e 245 RegisterWindowClasses();
2bda0e17 246
aa0b7e1e 247 // Create the brush for disabling bitmap buttons
2bda0e17 248
aa0b7e1e
JS
249 LOGBRUSH lb ;
250 lb.lbStyle = BS_PATTERN;
251 lb.lbHatch = (int)LoadBitmap( wxhInstance, "wxDISABLE_BUTTON_BITMAP" ) ;
252 wxDisableButtonBrush = ::CreateBrushIndirect( & lb ) ;
253 ::DeleteObject( (HGDIOBJ)lb.lbHatch ) ;
2bda0e17 254
aa0b7e1e 255#if wxUSE_PENWINDOWS
a5e0e655 256 wxRegisterPenWin();
aa0b7e1e 257#endif
2bda0e17 258
aa0b7e1e 259 wxWinHandleList = new wxList(wxKEY_INTEGER);
2bda0e17 260
aa0b7e1e
JS
261 // This is to foil optimizations in Visual C++ that
262 // throw out dummy.obj.
8cbd2bde
JS
263 // PLEASE DO NOT ALTER THIS.
264#if defined(_MSC_VER) && !defined(WXMAKINGDLL)
a5e0e655
VZ
265 extern char wxDummyChar;
266 if (wxDummyChar) wxDummyChar++;
aa0b7e1e 267#endif
a5e0e655 268
aa0b7e1e 269 wxSetKeyboardHook(TRUE);
2bda0e17 270
aa0b7e1e
JS
271 wxModule::RegisterModules();
272 if (!wxModule::InitializeModules())
273 return FALSE;
274 return TRUE;
2bda0e17
KB
275}
276
589f0e3e
JS
277//// RegisterWindowClasses
278
bb6290e3 279bool wxApp::RegisterWindowClasses()
2bda0e17
KB
280{
281///////////////////////////////////////////////////////////////////////
282// Register the frame window class.
283 WNDCLASS wndclass; // Structure used to register Windows class.
284
285 wndclass.style = CS_HREDRAW | CS_VREDRAW;
286 wndclass.lpfnWndProc = (WNDPROC)wxWndProc;
287 wndclass.cbClsExtra = 0;
288 wndclass.cbWndExtra = sizeof( DWORD ); // was 4
289 wndclass.hInstance = wxhInstance;
57c208c5
JS
290 wndclass.hIcon = (HICON) NULL; // wxSTD_FRAME_ICON;
291 wndclass.hCursor = LoadCursor( (HINSTANCE) NULL, IDC_ARROW );
2bda0e17
KB
292 wndclass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
293// wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
294 wndclass.lpszMenuName = NULL;
295#ifdef _MULTIPLE_INSTANCES
589f0e3e 296 sprintf( wxFrameClassName,"wxFrameClass%d", wxhInstance );
2bda0e17
KB
297#endif
298 wndclass.lpszClassName = wxFrameClassName;
299
300 if (!RegisterClass( &wndclass ))
301 {
302 // wxFatalError("Can't register Frame Window class");
303 }
304
305///////////////////////////////////////////////////////////////////////
306// Register the MDI frame window class.
307 WNDCLASS wndclass1; // Structure used to register Windows class.
308
309 wndclass1.style = CS_HREDRAW | CS_VREDRAW;
310 wndclass1.lpfnWndProc = (WNDPROC)wxWndProc;
311 wndclass1.cbClsExtra = 0;
312 wndclass1.cbWndExtra = sizeof( DWORD ); // was 4
313 wndclass1.hInstance = wxhInstance;
57c208c5
JS
314 wndclass1.hIcon = (HICON) NULL; // wxSTD_MDIPARENTFRAME_ICON;
315 wndclass1.hCursor = LoadCursor( (HINSTANCE) NULL, IDC_ARROW );
2bda0e17 316// wndclass1.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
57c208c5 317 wndclass1.hbrBackground = (HBRUSH) NULL;
2bda0e17
KB
318 wndclass1.lpszMenuName = NULL;
319
320 wndclass1.lpszClassName = wxMDIFrameClassName;
321 if (!RegisterClass( &wndclass1 ))
322 {
323// wxFatalError("Can't register MDI Frame window class");
a5e0e655 324// return FALSE;
2bda0e17
KB
325 }
326
327///////////////////////////////////////////////////////////////////////
328// Register the MDI child frame window class.
329 WNDCLASS wndclass4; // Structure used to register Windows class.
330
331 wndclass4.style = CS_HREDRAW | CS_VREDRAW;
332 wndclass4.lpfnWndProc = (WNDPROC)wxWndProc;
333 wndclass4.cbClsExtra = 0;
334 wndclass4.cbWndExtra = sizeof( DWORD ); // was 4
335 wndclass4.hInstance = wxhInstance;
57c208c5
JS
336 wndclass4.hIcon = (HICON) NULL; // wxSTD_MDICHILDFRAME_ICON;
337 wndclass4.hCursor = LoadCursor( (HINSTANCE) NULL, IDC_ARROW );
2bda0e17
KB
338 // TODO: perhaps this should be NULL so that Windows doesn't
339 // paint the background itself (would OnEraseBackground duplicate
340 // this?)
341 wndclass4.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
342// wndclass4.hbrBackground = NULL;
343 wndclass4.lpszMenuName = NULL;
344 wndclass4.lpszClassName = wxMDIChildFrameClassName;
345
346 if (!RegisterClass( &wndclass4 ))
347 {
348// wxFatalError("Can't register MDI child frame window class");
349// return FALSE;
350 }
351
352///////////////////////////////////////////////////////////////////////
353// Register the panel window class.
354 WNDCLASS wndclass2; // Structure used to register Windows class.
355 memset(&wndclass2, 0, sizeof(WNDCLASS)); // start with NULL defaults
356 // Use CS_OWNDC to avoid messing about restoring the context
357 // for every graphic operation.
358 wndclass2.style = CS_HREDRAW | CS_VREDRAW;
359 wndclass2.lpfnWndProc = (WNDPROC)wxWndProc;
360 wndclass2.cbClsExtra = 0;
361 wndclass2.cbWndExtra = sizeof( DWORD ); // was 4
362 wndclass2.hInstance = wxhInstance;
57c208c5
JS
363 wndclass2.hIcon = (HICON) NULL;
364 wndclass2.hCursor = (HCURSOR) NULL;
2bda0e17 365// wndclass2.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1) ;
c4e7c2aa 366 wndclass2.hbrBackground = (HBRUSH) GetStockObject( LTGRAY_BRUSH );
2bda0e17
KB
367 wndclass2.lpszMenuName = NULL;
368 wndclass2.lpszClassName = wxPanelClassName;
369 if (!RegisterClass( &wndclass2 ))
370 {
371// wxFatalError("Can't register Panel Window class");
372// return FALSE;
373 }
374
375///////////////////////////////////////////////////////////////////////
376// Register the canvas and textsubwindow class name
377 WNDCLASS wndclass3; // Structure used to register Windows class.
378 memset(&wndclass3, 0, sizeof(WNDCLASS)); // start with NULL defaults
379 // Use CS_OWNDC to avoid messing about restoring the context
380 // for every graphic operation.
d50b2a58 381// wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS ;
2bda0e17 382 // wxWin 2.0
d50b2a58 383 wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
2bda0e17
KB
384 wndclass3.lpfnWndProc = (WNDPROC)wxWndProc;
385 wndclass3.cbClsExtra = 0;
386 wndclass3.cbWndExtra = sizeof( DWORD ); // was 4
387 wndclass3.hInstance = wxhInstance;
57c208c5
JS
388 wndclass3.hIcon = (HICON) NULL;
389 wndclass3.hCursor = (HCURSOR) NULL;
2bda0e17 390// wndclass3.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
57c208c5 391 wndclass3.hbrBackground = (HBRUSH) NULL;
2bda0e17
KB
392 wndclass3.lpszMenuName = NULL;
393 wndclass3.lpszClassName = wxCanvasClassName;
394 if (!RegisterClass( &wndclass3))
395 {
396// wxFatalError("Can't register Canvas class");
397// return FALSE;
398 }
399
400 return TRUE;
401}
402
589f0e3e
JS
403//// Convert Windows to argc, argv style
404
405void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine)
406{
da36f544 407 wxStringList args;
589f0e3e 408
da36f544
JS
409 wxString cmdLine(lpCmdLine);
410 int count = 0;
589f0e3e 411
da36f544 412 // Get application name
c085e333 413 char name[260]; // 260 is MAX_PATH value from windef.h
da36f544
JS
414 ::GetModuleFileName(wxhInstance, name, WXSIZEOF(name));
415
416 // GNUWIN32 already fills in the first arg with the application name.
589f0e3e 417#if !defined(__GNUWIN32__)
da36f544
JS
418 args.Add(name);
419 count ++;
589f0e3e
JS
420#endif
421
da36f544
JS
422 strcpy(name, wxFileNameFromPath(name));
423 wxStripExtension(name);
424 wxTheApp->SetAppName(name);
589f0e3e 425
da36f544
JS
426 // Break up string
427 // Treat strings enclosed in double-quotes as single arguments
428 int i = 0;
429 int len = cmdLine.Length();
430 while (i < len)
a5e0e655 431 {
da36f544
JS
432 // Skip whitespace
433 while ((i < len) && isspace(cmdLine.GetChar(i)))
434 i ++;
435
436 if (i < len)
437 {
438 if (cmdLine.GetChar(i) == '"') // We found the start of a string
439 {
440 i ++;
441 int first = i;
442 while ((i < len) && (cmdLine.GetChar(i) != '"'))
443 i ++;
444
445 wxString arg(cmdLine.Mid(first, (i - first)));
446
447 args.Add(arg);
448 count ++;
449
450 if (i < len)
451 i ++; // Skip past 2nd quote
452 }
453 else // Unquoted argument
454 {
455 int first = i;
456 while ((i < len) && !isspace(cmdLine.GetChar(i)))
457 i ++;
458
459 wxString arg(cmdLine.Mid(first, (i - first)));
460
461 args.Add(arg);
462 count ++;
463 }
464 }
a5e0e655 465 }
a5e0e655 466
da36f544
JS
467 wxTheApp->argv = new char*[count + 1];
468 for (i = 0; i < count; i++)
a5e0e655 469 {
da36f544
JS
470 wxString arg(args[i]);
471 wxTheApp->argv[i] = copystring((const char*)arg);
a5e0e655 472 }
da36f544
JS
473 wxTheApp->argv[count] = NULL; // argv[] is a NULL-terminated list
474 wxTheApp->argc = count;
589f0e3e
JS
475}
476
477//// Cleans up any wxWindows internal structures left lying around
478
bb6290e3 479void wxApp::CleanUp()
2bda0e17 480{
589f0e3e 481 //// COMMON CLEANUP
2bda0e17
KB
482 wxModule::CleanUpModules();
483
47d67540 484#if wxUSE_WX_RESOURCES
589f0e3e
JS
485 wxCleanUpResourceSystem();
486
487// wxDefaultResourceTable->ClearTable();
488#endif
489
490 // Indicate that the cursor can be freed,
491 // so that cursor won't be deleted by deleting
492 // the bitmap list before g_globalCursor goes out
493 // of scope (double deletion of the cursor).
494 wxSetCursor(wxNullCursor);
495 delete g_globalCursor;
496
497 wxDeleteStockObjects() ;
498
499 // Destroy all GDI lists, etc.
500 wxDeleteStockLists();
501
502 delete wxTheColourDatabase;
503 wxTheColourDatabase = NULL;
504
589f0e3e
JS
505 wxBitmap::CleanUpHandlers();
506
507 delete[] wxBuffer;
508 wxBuffer = NULL;
509
510 //// WINDOWS-SPECIFIC CLEANUP
2bda0e17
KB
511
512 wxSetKeyboardHook(FALSE);
513
514#ifdef __WIN95__
57c208c5
JS
515
516#if wxUSE_RICHEDIT
517 if (gs_hRichEdit != (HINSTANCE) NULL)
2bda0e17
KB
518 FreeLibrary(gs_hRichEdit);
519#endif
520
57c208c5
JS
521#endif
522
47d67540 523#if wxUSE_PENWINDOWS
2bda0e17
KB
524 wxCleanUpPenWin();
525#endif
526
527 if (wxSTD_FRAME_ICON)
528 DestroyIcon(wxSTD_FRAME_ICON);
529 if (wxSTD_MDICHILDFRAME_ICON)
530 DestroyIcon(wxSTD_MDICHILDFRAME_ICON);
531 if (wxSTD_MDIPARENTFRAME_ICON)
532 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON);
533
534 if (wxDEFAULT_FRAME_ICON)
535 DestroyIcon(wxDEFAULT_FRAME_ICON);
536 if (wxDEFAULT_MDICHILDFRAME_ICON)
537 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON);
538 if (wxDEFAULT_MDIPARENTFRAME_ICON)
539 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON);
540
541 if ( wxDisableButtonBrush )
542 ::DeleteObject( wxDisableButtonBrush ) ;
543
ce3ed50d 544#if !defined(__GNUWIN32__) && !defined(__SC__) && !defined(__SALFORDC__)
2bda0e17 545 ::OleUninitialize();
5de5db0e 546#endif
2bda0e17
KB
547
548#if CTL3D
549 Ctl3dUnregister(wxhInstance);
550#endif
551
552 if (wxWinHandleList)
553 delete wxWinHandleList ;
d50b2a58 554
0c32066b
JS
555 wxClassInfo::CleanUpClasses();
556
184b5d99
JS
557 delete wxTheApp;
558 wxTheApp = NULL;
559
560#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
561 // At this point we want to check if there are any memory
562 // blocks that aren't part of the wxDebugContext itself,
563 // as a special case. Then when dumping we need to ignore
564 // wxDebugContext, too.
565 if (wxDebugContext::CountObjectsLeft() > 0)
566 {
567 wxLogDebug("There were memory leaks.");
568 wxDebugContext::Dump();
569 wxDebugContext::PrintStatistics();
570 }
571// wxDebugContext::SetStream(NULL, NULL);
572#endif
573
c54f78a2 574 // do it as the very last thing because everything else can log messages
370382c7 575 wxLog::DontCreateOnDemand();
c54f78a2 576 delete wxLog::SetActiveTarget(NULL);
2bda0e17
KB
577}
578
2bda0e17
KB
579#if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
580
f8a3e080
VZ
581// temporarily disable this warning
582#ifdef _MSC_VER
583 #pragma warning(disable: 4715) // not all control paths return a value
584#endif // Visual C++
585
589f0e3e 586//// Main wxWindows entry point
a5e0e655
VZ
587int wxEntry(WXHINSTANCE hInstance,
588 WXHINSTANCE WXUNUSED(hPrevInstance),
589 char *lpCmdLine,
590 int nCmdShow,
591 bool enterLoop)
2bda0e17 592{
f8a3e080
VZ
593 // take everything into a try-except block in release build
594 // FIXME other compilers must support Win32 SEH (structured exception
595 // handling) too, just find the appropriate keyword in their docs!
596 // Please note that it's _not_ the same as C++ exceptions!
597#if !defined(__WXDEBUG__) && defined(_MSC_VER)
598 #define CATCH_PROGRAM_EXCEPTIONS
599
600 __try {
601#else
602 #undef CATCH_PROGRAM_EXCEPTIONS
a5e0e655
VZ
603#endif
604
2bda0e17
KB
605 wxhInstance = (HINSTANCE) hInstance;
606
589f0e3e 607 if (!wxApp::Initialize())
2bda0e17
KB
608 return 0;
609
a5e0e655 610 // create the application object or ensure that one already exists
2bda0e17
KB
611 if (!wxTheApp)
612 {
a5e0e655
VZ
613 // The app may have declared a global application object, but we recommend
614 // the IMPLEMENT_APP macro is used instead, which sets an initializer
615 // function for delayed, dynamic app object construction.
616 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
617 "No initializer - use IMPLEMENT_APP macro." );
2bda0e17 618
a5e0e655 619 wxTheApp = (*wxApp::GetInitializerFunction()) ();
2bda0e17
KB
620 }
621
a5e0e655 622 wxCHECK_MSG( wxTheApp, 0, "You have to define an instance of wxApp!" );
2bda0e17 623
a5e0e655 624 // save the WinMain() parameters
589f0e3e 625 wxTheApp->ConvertToStandardCommandArgs(lpCmdLine);
2bda0e17
KB
626 wxTheApp->m_nCmdShow = nCmdShow;
627
628 // GUI-specific initialisation. In fact on Windows we don't have any,
629 // but this call is provided for compatibility across platforms.
630 wxTheApp->OnInitGui() ;
631
a5e0e655 632 int retValue = 0;
2bda0e17 633
a5e0e655
VZ
634 if ( wxTheApp->OnInit() )
635 {
636 if ( enterLoop )
637 {
638 retValue = wxTheApp->OnRun();
639 }
2bda0e17 640 }
a5e0e655 641 //else: app initialization failed, so we skipped OnRun()
2bda0e17 642
a5e0e655
VZ
643 wxWindow *topWindow = wxTheApp->GetTopWindow();
644 if ( topWindow )
2bda0e17 645 {
a5e0e655
VZ
646 // Forcibly delete the window.
647 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
648 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
649 {
650 topWindow->Close(TRUE);
651 wxTheApp->DeletePendingObjects();
652 }
653 else
654 {
655 delete topWindow;
656 wxTheApp->SetTopWindow(NULL);
657 }
2bda0e17 658 }
d50b2a58 659
2bda0e17 660 wxTheApp->OnExit();
f57fe24c
JS
661
662 // flush the logged messages if any
663 wxLog *pLog = wxLog::GetActiveTarget();
664 if ( pLog != NULL && pLog->HasPendingMessages() )
665 pLog->Flush();
666
667
2bda0e17
KB
668 wxApp::CleanUp();
669
2bda0e17 670 return retValue;
f8a3e080
VZ
671
672#ifdef CATCH_PROGRAM_EXCEPTIONS
a5e0e655 673 }
f8a3e080 674 __except ( EXCEPTION_EXECUTE_HANDLER ) {
a5e0e655
VZ
675 /*
676 if ( wxTheApp )
677 wxTheApp->OnFatalException();
678 */
679
680 ::ExitProcess(3); // the same exit code as abort()
f8a3e080
VZ
681
682 // NOTREACHED
a5e0e655 683 }
f8a3e080 684#endif // CATCH_PROGRAM_EXCEPTIONS
2bda0e17
KB
685}
686
f8a3e080
VZ
687// restore warning state
688#ifdef _MSC_VER
689 #pragma warning(default: 4715) // not all control paths return a value
690#endif // Visual C++
691
2bda0e17
KB
692#else /* _WINDLL */
693
589f0e3e
JS
694//// Entry point for DLLs
695
2bda0e17
KB
696int wxEntry(WXHINSTANCE hInstance)
697{
698 wxhInstance = (HINSTANCE) hInstance;
589f0e3e 699 wxApp::Initialize();
2bda0e17
KB
700
701 // The app may have declared a global application object, but we recommend
702 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
703 // for delayed, dynamic app object construction.
704
705 if (!wxTheApp)
706 {
a5e0e655
VZ
707 if (!wxApp::GetInitializerFunction())
708 {
709 MessageBox(NULL, "No initializer - use IMPLEMENT_APP macro.", "wxWindows Error", MB_APPLMODAL | MB_ICONSTOP | MB_OK);
710 return 0;
711 }
2bda0e17 712
a5e0e655 713 wxTheApp = (* wxApp::GetInitializerFunction()) ();
2bda0e17
KB
714 }
715
716 if (!wxTheApp) {
717 MessageBox(NULL, "You have to define an instance of wxApp!", "wxWindows Error", MB_APPLMODAL | MB_ICONSTOP | MB_OK);
718 return 0;
719 }
720
721 wxTheApp->argc = 0;
722 wxTheApp->argv = NULL;
723
724 wxTheApp->OnInitGui();
725
726 wxTheApp->OnInit();
727
728 if (wxTheApp->GetTopWindow() && wxTheApp->GetTopWindow()->GetHWND()) {
729 wxTheApp->GetTopWindow()->Show(TRUE);
730 }
731
732 return 1;
733}
734#endif // _WINDLL
735
589f0e3e
JS
736//// Static member initialization
737
2bda0e17
KB
738wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
739
bb6290e3 740wxApp::wxApp()
2bda0e17
KB
741{
742 m_topWindow = NULL;
743 wxTheApp = this;
2bda0e17 744 m_className = "";
2bda0e17
KB
745 m_wantDebugOutput = TRUE ;
746 m_appName = "";
747 argc = 0;
748 argv = NULL;
2049ba38 749#ifdef __WXMSW__
2bda0e17
KB
750 m_printMode = wxPRINT_WINDOWS;
751#else
752 m_printMode = wxPRINT_POSTSCRIPT;
753#endif
2bda0e17 754 m_exitOnFrameDelete = TRUE;
2bda0e17
KB
755 m_auto3D = TRUE;
756}
757
589f0e3e
JS
758wxApp::~wxApp()
759{
760 // Delete command-line args
761 int i;
762 for (i = 0; i < argc; i++)
763 {
764 delete[] argv[i];
765 }
da36f544 766 delete[] argv;
589f0e3e
JS
767}
768
bb6290e3 769bool wxApp::Initialized()
2bda0e17
KB
770{
771#ifndef _WINDLL
772 if (GetTopWindow())
773 return TRUE;
774 else
775 return FALSE;
776#endif
777#ifdef _WINDLL // Assume initialized if DLL (no way of telling)
778 return TRUE;
779#endif
780}
781
782/*
783 * Get and process a message, returning FALSE if WM_QUIT
bee503b0 784 * received (and also set the flag telling the app to exit the main loop)
2bda0e17
KB
785 *
786 */
bb6290e3 787bool wxApp::DoMessage()
2bda0e17 788{
bee503b0
VZ
789 BOOL rc = ::GetMessage(&s_currentMsg, (HWND) NULL, 0, 0);
790 if ( rc == 0 )
791 {
792 // got WM_QUIT
793 m_keepGoing = FALSE;
4a9968f9 794
bee503b0
VZ
795 return FALSE;
796 }
797 else if ( rc == -1 )
798 {
799 // should never happen, but let's test for it nevertheless
800 wxLogLastError("GetMessage");
801 }
802 else
803 {
804#if wxUSE_THREADS
805 wxASSERT_MSG( wxThread::IsMain(),
806 "only the main thread can process Windows messages" );
d50b2a58 807
bee503b0
VZ
808 static bool s_hadGuiLock = TRUE;
809 static wxMsgArray s_aSavedMessages;
810
811 // if a secondary thread owns is doing GUI calls, save all messages for
812 // later processing - we can't process them right now because it will
813 // lead to recursive library calls (and we're not reentrant)
814 if ( !wxGuiOwnedByMainThread() )
815 {
816 s_hadGuiLock = FALSE;
817
4a9968f9
VZ
818 // leave out WM_COMMAND messages: too dangerous, sometimes
819 // the message will be processed twice
820 if ( !wxIsWaitingForThread() ||
821 s_currentMsg.message != WM_COMMAND )
822 {
823 s_aSavedMessages.Add(s_currentMsg);
824 }
bee503b0
VZ
825
826 return TRUE;
827 }
828 else
829 {
830 // have we just regained the GUI lock? if so, post all of the saved
831 // messages
832 //
833 // FIXME of course, it's not _exactly_ the same as processing the
834 // messages normally - expect some things to break...
835 if ( !s_hadGuiLock )
836 {
837 s_hadGuiLock = TRUE;
838
839 size_t count = s_aSavedMessages.Count();
840 for ( size_t n = 0; n < count; n++ )
841 {
842 MSG& msg = s_aSavedMessages[n];
843
844 if ( !ProcessMessage((WXMSG *)&msg) )
845 {
846 ::TranslateMessage(&msg);
847 ::DispatchMessage(&msg);
848 }
849 }
850
851 s_aSavedMessages.Empty();
852 }
853 }
854#endif // wxUSE_THREADS
855
856 // Process the message
857 if ( !ProcessMessage((WXMSG *)&s_currentMsg) )
858 {
859 ::TranslateMessage(&s_currentMsg);
860 wxApp::sm_lastMessageTime = s_currentMsg.time; /* MATTHEW: timeStamp impl. */
861 ::DispatchMessage(&s_currentMsg);
862 }
863 }
864
865 return TRUE;
2bda0e17
KB
866}
867
868/*
869 * Keep trying to process messages until WM_QUIT
870 * received.
871 *
872 * If there are messages to be processed, they will all be
873 * processed and OnIdle will not be called.
874 * When there are no more messages, OnIdle is called.
875 * If OnIdle requests more time,
876 * it will be repeatedly called so long as there are no pending messages.
877 * A 'feature' of this is that once OnIdle has decided that no more processing
878 * is required, then it won't get processing time until further messages
879 * are processed (it'll sit in DoMessage).
880 */
881
bb6290e3 882int wxApp::MainLoop()
2bda0e17
KB
883{
884 m_keepGoing = TRUE;
bee503b0
VZ
885
886 while ( m_keepGoing )
2bda0e17 887 {
bee503b0
VZ
888 #if wxUSE_THREADS
889 wxMutexGuiLeaveOrEnter();
890 #endif // wxUSE_THREADS
891
892 while ( !::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) &&
893 ProcessIdle() )
894 {
895 }
896
897 DoMessage();
2bda0e17
KB
898 }
899
900 return s_currentMsg.wParam;
901}
902
903// Returns TRUE if more time is needed.
bb6290e3 904bool wxApp::ProcessIdle()
2bda0e17
KB
905{
906 wxIdleEvent event;
907 event.SetEventObject(this);
908 ProcessEvent(event);
909
910 return event.MoreRequested();
911}
912
bb6290e3 913void wxApp::ExitMainLoop()
2bda0e17
KB
914{
915 m_keepGoing = FALSE;
916}
917
bb6290e3 918bool wxApp::Pending()
2bda0e17
KB
919{
920 return (::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) != 0) ;
921}
922
bb6290e3 923void wxApp::Dispatch()
2bda0e17 924{
bee503b0 925 DoMessage();
2bda0e17
KB
926}
927
928/*
929 * Give all windows a chance to preprocess
930 * the message. Some may have accelerator tables, or have
931 * MDI complications.
932 */
933bool wxApp::ProcessMessage(WXMSG *Msg)
934{
935 MSG *msg = (MSG *)Msg;
936
937 HWND hWnd;
938
57a7b7c1
JS
939 // Try translations first; find the youngest window with
940 // a translation table.
57c208c5 941 for (hWnd = msg->hwnd; hWnd != (HWND) NULL; hWnd = ::GetParent(hWnd))
2bda0e17
KB
942 {
943 wxWindow *wnd = wxFindWinFromHandle((WXHWND) hWnd);
944 if (wnd)
945 {
57a7b7c1 946 if (wnd->MSWTranslateMessage(Msg))
2bda0e17 947 return TRUE;
2bda0e17
KB
948 }
949 }
950
57a7b7c1 951 // Anyone for a non-translation message? Try youngest descendants first.
57c208c5 952 for (hWnd = msg->hwnd; hWnd != (HWND) NULL; hWnd = ::GetParent(hWnd))
57a7b7c1
JS
953 {
954 wxWindow *wnd = wxFindWinFromHandle((WXHWND) hWnd);
955 if (wnd)
956 {
957 if (wnd->MSWProcessMessage(Msg))
958 return TRUE;
959 }
960 }
961 return FALSE;
2bda0e17
KB
962}
963
964void wxApp::OnIdle(wxIdleEvent& event)
965{
3222fde2 966 static bool s_inOnIdle = FALSE;
2bda0e17 967
3222fde2
VZ
968 // Avoid recursion (via ProcessEvent default case)
969 if ( s_inOnIdle )
970 return;
2bda0e17 971
3222fde2 972 s_inOnIdle = TRUE;
2bda0e17 973
3222fde2
VZ
974 // 'Garbage' collection of windows deleted with Close().
975 DeletePendingObjects();
2bda0e17 976
3222fde2
VZ
977 // flush the logged messages if any
978 wxLog *pLog = wxLog::GetActiveTarget();
979 if ( pLog != NULL && pLog->HasPendingMessages() )
980 pLog->Flush();
c54f78a2 981
3222fde2
VZ
982 // Send OnIdle events to all windows
983 if ( SendIdleEvents() )
984 {
985 // SendIdleEvents() returns TRUE if at least one window requested more
986 // idle events
987 event.RequestMore(TRUE);
988 }
2bda0e17 989
3222fde2 990 s_inOnIdle = FALSE;
2bda0e17
KB
991}
992
993// Send idle event to all top-level windows
bb6290e3 994bool wxApp::SendIdleEvents()
2bda0e17
KB
995{
996 bool needMore = FALSE;
3222fde2
VZ
997 wxNode* node = wxTopLevelWindows.First();
998 while (node)
999 {
1000 wxWindow* win = (wxWindow*) node->Data();
1001 if (SendIdleEvents(win))
2bda0e17
KB
1002 needMore = TRUE;
1003
3222fde2
VZ
1004 node = node->Next();
1005 }
1006
2bda0e17
KB
1007 return needMore;
1008}
1009
1010// Send idle event to window and all subwindows
1011bool wxApp::SendIdleEvents(wxWindow* win)
1012{
a5e0e655 1013 bool needMore = FALSE;
2bda0e17 1014
a5e0e655
VZ
1015 wxIdleEvent event;
1016 event.SetEventObject(win);
02800301 1017 win->GetEventHandler()->ProcessEvent(event);
2bda0e17 1018
a5e0e655
VZ
1019 if (event.MoreRequested())
1020 needMore = TRUE;
2bda0e17 1021
c0ed460c 1022 wxNode* node = win->GetChildren().First();
a5e0e655
VZ
1023 while (node)
1024 {
1025 wxWindow* win = (wxWindow*) node->Data();
1026 if (SendIdleEvents(win))
1027 needMore = TRUE;
2bda0e17 1028
a5e0e655
VZ
1029 node = node->Next();
1030 }
1031 return needMore ;
2bda0e17
KB
1032}
1033
bb6290e3 1034void wxApp::DeletePendingObjects()
2bda0e17
KB
1035{
1036 wxNode *node = wxPendingDelete.First();
1037 while (node)
1038 {
1039 wxObject *obj = (wxObject *)node->Data();
d50b2a58 1040
2bda0e17
KB
1041 delete obj;
1042
1043 if (wxPendingDelete.Member(obj))
1044 delete node;
1045
1046 // Deleting one object may have deleted other pending
1047 // objects, so start from beginning of list again.
1048 node = wxPendingDelete.First();
1049 }
1050}
1051
57c208c5 1052void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
387a3b02
JS
1053{
1054 if (GetTopWindow())
1055 GetTopWindow()->Close(TRUE);
1056}
1057
1058// Default behaviour: close the application with prompts. The
1059// user can veto the close, and therefore the end session.
1060void wxApp::OnQueryEndSession(wxCloseEvent& event)
1061{
1062 if (GetTopWindow())
1063 {
1064 if (!GetTopWindow()->Close(!event.CanVeto()))
1065 event.Veto(TRUE);
1066 }
1067}
1068
bb6290e3 1069wxLog* wxApp::CreateLogTarget()
2bda0e17
KB
1070{
1071 return new wxLogGui;
1072}
1073
bb6290e3 1074wxWindow* wxApp::GetTopWindow() const
2bda0e17
KB
1075{
1076 if (m_topWindow)
1077 return m_topWindow;
1078 else if (wxTopLevelWindows.Number() > 0)
1079 return (wxWindow*) wxTopLevelWindows.First()->Data();
1080 else
1081 return NULL;
1082}
1083
bb6290e3
JS
1084int wxApp::GetComCtl32Version() const
1085{
1086 // have we loaded COMCTL32 yet?
1087 HMODULE theModule = ::GetModuleHandle("COMCTL32");
1088 int version = 0;
d50b2a58 1089
bb6290e3
JS
1090 // if so, then we can check for the version
1091 if (theModule)
1092 {
1093 // InitCommonControlsEx is unique to 4.7 and later
1094 FARPROC theProc = ::GetProcAddress(theModule, "InitCommonControlsEx");
d50b2a58 1095
bb6290e3
JS
1096 if (! theProc)
1097 { // not found, must be 4.00
a5e0e655 1098 version = 400;
bb6290e3
JS
1099 }
1100 else
1101 {
a5e0e655
VZ
1102 // The following symbol are unique to 4.71
1103 // DllInstall
1104 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1105 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1106 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1107 // FlatSB_ShowScrollBar
1108 // _DrawIndirectImageList _DuplicateImageList
1109 // InitializeFlatSB
1110 // UninitializeFlatSB
1111 // we could check for any of these - I chose DllInstall
1112 FARPROC theProc = ::GetProcAddress(theModule, "DllInstall");
1113 if (! theProc)
1114 {
1115 // not found, must be 4.70
1116 version = 470;
1117 }
1118 else
1119 { // found, must be 4.71
1120 version = 471;
1121 }
bb6290e3
JS
1122 }
1123 }
1124 return version;
1125}
1126
1127void wxExit()
2bda0e17
KB
1128{
1129 wxApp::CleanUp();
1130 FatalAppExit(0, "Fatal error: exiting");
1131}
1132
1133// Yield to incoming messages
bb6290e3 1134bool wxYield()
2bda0e17
KB
1135{
1136 MSG msg;
1137 // We want to go back to the main message loop
1138 // if we see a WM_QUIT. (?)
1139 while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) && msg.message != WM_QUIT)
1140 {
bee503b0 1141 if ( !wxTheApp->DoMessage() )
2bda0e17
KB
1142 break;
1143 }
1144
1145 return TRUE;
1146}
1147
1148HINSTANCE wxGetInstance()
1149{
a5e0e655 1150 return wxhInstance;
2bda0e17
KB
1151}
1152
1153// For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1154// if in a separate file. So include it here to ensure it's linked.
57c208c5 1155#if (defined(_MSC_VER) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))
a5e0e655 1156 #include "main.cpp"
2bda0e17 1157#endif