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