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