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