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