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