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