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