]> git.saurik.com Git - wxWidgets.git/blame - src/msw/app.cpp
wxExecute() fixes and doc updates: the return value for sync exec case is now
[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
750b78ba 251 wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
2bda0e17
KB
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
750b78ba 275 wndclass1.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
2bda0e17
KB
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
750b78ba 297 wndclass4.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
2bda0e17
KB
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
750b78ba 322 wndclass2.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
2bda0e17
KB
323 wndclass2.lpfnWndProc = (WNDPROC)wxWndProc;
324 wndclass2.cbClsExtra = 0;
325 wndclass2.cbWndExtra = sizeof( DWORD ); // was 4
326 wndclass2.hInstance = wxhInstance;
57c208c5
JS
327 wndclass2.hIcon = (HICON) NULL;
328 wndclass2.hCursor = (HCURSOR) NULL;
2bda0e17 329// wndclass2.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1) ;
c4e7c2aa 330 wndclass2.hbrBackground = (HBRUSH) GetStockObject( LTGRAY_BRUSH );
2bda0e17
KB
331 wndclass2.lpszMenuName = NULL;
332 wndclass2.lpszClassName = wxPanelClassName;
333 if (!RegisterClass( &wndclass2 ))
334 {
335// wxFatalError("Can't register Panel Window class");
336// return FALSE;
337 }
338
339///////////////////////////////////////////////////////////////////////
340// Register the canvas and textsubwindow class name
341 WNDCLASS wndclass3; // Structure used to register Windows class.
342 memset(&wndclass3, 0, sizeof(WNDCLASS)); // start with NULL defaults
343 // Use CS_OWNDC to avoid messing about restoring the context
344 // for every graphic operation.
d50b2a58 345// wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS ;
2bda0e17 346 // wxWin 2.0
d50b2a58 347 wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
2bda0e17
KB
348 wndclass3.lpfnWndProc = (WNDPROC)wxWndProc;
349 wndclass3.cbClsExtra = 0;
350 wndclass3.cbWndExtra = sizeof( DWORD ); // was 4
351 wndclass3.hInstance = wxhInstance;
57c208c5
JS
352 wndclass3.hIcon = (HICON) NULL;
353 wndclass3.hCursor = (HCURSOR) NULL;
2bda0e17 354// wndclass3.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
57c208c5 355 wndclass3.hbrBackground = (HBRUSH) NULL;
2bda0e17
KB
356 wndclass3.lpszMenuName = NULL;
357 wndclass3.lpszClassName = wxCanvasClassName;
358 if (!RegisterClass( &wndclass3))
359 {
360// wxFatalError("Can't register Canvas class");
361// return FALSE;
362 }
363
364 return TRUE;
365}
366
589f0e3e
JS
367//// Convert Windows to argc, argv style
368
369void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine)
370{
da36f544 371 wxStringList args;
589f0e3e 372
da36f544
JS
373 wxString cmdLine(lpCmdLine);
374 int count = 0;
589f0e3e 375
da36f544 376 // Get application name
c085e333 377 char name[260]; // 260 is MAX_PATH value from windef.h
da36f544
JS
378 ::GetModuleFileName(wxhInstance, name, WXSIZEOF(name));
379
380 // GNUWIN32 already fills in the first arg with the application name.
ca5c8b2d
JS
381 // JACS: apparently not now (b20 and above?)
382#if 0 // !defined(__GNUWIN32__)
da36f544
JS
383 args.Add(name);
384 count ++;
589f0e3e
JS
385#endif
386
da36f544
JS
387 strcpy(name, wxFileNameFromPath(name));
388 wxStripExtension(name);
389 wxTheApp->SetAppName(name);
589f0e3e 390
da36f544
JS
391 // Break up string
392 // Treat strings enclosed in double-quotes as single arguments
393 int i = 0;
394 int len = cmdLine.Length();
395 while (i < len)
a5e0e655 396 {
da36f544
JS
397 // Skip whitespace
398 while ((i < len) && isspace(cmdLine.GetChar(i)))
399 i ++;
400
401 if (i < len)
402 {
403 if (cmdLine.GetChar(i) == '"') // We found the start of a string
404 {
405 i ++;
406 int first = i;
407 while ((i < len) && (cmdLine.GetChar(i) != '"'))
408 i ++;
409
410 wxString arg(cmdLine.Mid(first, (i - first)));
411
412 args.Add(arg);
413 count ++;
414
415 if (i < len)
416 i ++; // Skip past 2nd quote
417 }
418 else // Unquoted argument
419 {
420 int first = i;
421 while ((i < len) && !isspace(cmdLine.GetChar(i)))
422 i ++;
423
424 wxString arg(cmdLine.Mid(first, (i - first)));
425
426 args.Add(arg);
427 count ++;
428 }
429 }
a5e0e655 430 }
a5e0e655 431
da36f544
JS
432 wxTheApp->argv = new char*[count + 1];
433 for (i = 0; i < count; i++)
a5e0e655 434 {
da36f544
JS
435 wxString arg(args[i]);
436 wxTheApp->argv[i] = copystring((const char*)arg);
a5e0e655 437 }
da36f544
JS
438 wxTheApp->argv[count] = NULL; // argv[] is a NULL-terminated list
439 wxTheApp->argc = count;
589f0e3e
JS
440}
441
442//// Cleans up any wxWindows internal structures left lying around
443
bb6290e3 444void wxApp::CleanUp()
2bda0e17 445{
589f0e3e 446 //// COMMON CLEANUP
f5e5bd66
JS
447
448 // flush the logged messages if any
449 wxLog *pLog = wxLog::GetActiveTarget();
450 if ( pLog != NULL && pLog->HasPendingMessages() )
451 pLog->Flush();
452
453 // One last chance for pending objects to be cleaned up
454 wxTheApp->DeletePendingObjects();
455
2bda0e17
KB
456 wxModule::CleanUpModules();
457
47d67540 458#if wxUSE_WX_RESOURCES
589f0e3e
JS
459 wxCleanUpResourceSystem();
460
461// wxDefaultResourceTable->ClearTable();
462#endif
463
464 // Indicate that the cursor can be freed,
465 // so that cursor won't be deleted by deleting
466 // the bitmap list before g_globalCursor goes out
467 // of scope (double deletion of the cursor).
468 wxSetCursor(wxNullCursor);
469 delete g_globalCursor;
470
471 wxDeleteStockObjects() ;
472
473 // Destroy all GDI lists, etc.
474 wxDeleteStockLists();
475
476 delete wxTheColourDatabase;
477 wxTheColourDatabase = NULL;
478
589f0e3e
JS
479 wxBitmap::CleanUpHandlers();
480
481 delete[] wxBuffer;
482 wxBuffer = NULL;
483
484 //// WINDOWS-SPECIFIC CLEANUP
2bda0e17
KB
485
486 wxSetKeyboardHook(FALSE);
487
488#ifdef __WIN95__
57c208c5
JS
489
490#if wxUSE_RICHEDIT
491 if (gs_hRichEdit != (HINSTANCE) NULL)
2bda0e17
KB
492 FreeLibrary(gs_hRichEdit);
493#endif
494
57c208c5
JS
495#endif
496
47d67540 497#if wxUSE_PENWINDOWS
2bda0e17
KB
498 wxCleanUpPenWin();
499#endif
500
501 if (wxSTD_FRAME_ICON)
502 DestroyIcon(wxSTD_FRAME_ICON);
503 if (wxSTD_MDICHILDFRAME_ICON)
504 DestroyIcon(wxSTD_MDICHILDFRAME_ICON);
505 if (wxSTD_MDIPARENTFRAME_ICON)
506 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON);
507
508 if (wxDEFAULT_FRAME_ICON)
509 DestroyIcon(wxDEFAULT_FRAME_ICON);
510 if (wxDEFAULT_MDICHILDFRAME_ICON)
511 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON);
512 if (wxDEFAULT_MDIPARENTFRAME_ICON)
513 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON);
514
515 if ( wxDisableButtonBrush )
516 ::DeleteObject( wxDisableButtonBrush ) ;
517
ce3ed50d 518#if !defined(__GNUWIN32__) && !defined(__SC__) && !defined(__SALFORDC__)
2bda0e17 519 ::OleUninitialize();
5de5db0e 520#endif
2bda0e17 521
1f112209 522#if wxUSE_CTL3D
2bda0e17
KB
523 Ctl3dUnregister(wxhInstance);
524#endif
525
526 if (wxWinHandleList)
527 delete wxWinHandleList ;
d50b2a58 528
0c32066b
JS
529 wxClassInfo::CleanUpClasses();
530
184b5d99
JS
531 delete wxTheApp;
532 wxTheApp = NULL;
533
534#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
535 // At this point we want to check if there are any memory
536 // blocks that aren't part of the wxDebugContext itself,
537 // as a special case. Then when dumping we need to ignore
538 // wxDebugContext, too.
539 if (wxDebugContext::CountObjectsLeft() > 0)
540 {
541 wxLogDebug("There were memory leaks.");
542 wxDebugContext::Dump();
543 wxDebugContext::PrintStatistics();
544 }
545// wxDebugContext::SetStream(NULL, NULL);
546#endif
547
c54f78a2 548 // do it as the very last thing because everything else can log messages
370382c7 549 wxLog::DontCreateOnDemand();
c54f78a2 550 delete wxLog::SetActiveTarget(NULL);
2bda0e17
KB
551}
552
2bda0e17
KB
553#if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
554
f8a3e080 555// temporarily disable this warning
3f4a0c5b 556#ifdef __VISUALC__
f8a3e080
VZ
557 #pragma warning(disable: 4715) // not all control paths return a value
558#endif // Visual C++
559
589f0e3e 560//// Main wxWindows entry point
a5e0e655
VZ
561int wxEntry(WXHINSTANCE hInstance,
562 WXHINSTANCE WXUNUSED(hPrevInstance),
563 char *lpCmdLine,
564 int nCmdShow,
565 bool enterLoop)
2bda0e17 566{
6e0d9d43
VZ
567 // do check for memory leaks on program exit
568 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
569 // deallocated memory which may be used to simulate low-memory condition)
570 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
571
f8a3e080
VZ
572 // take everything into a try-except block in release build
573 // FIXME other compilers must support Win32 SEH (structured exception
574 // handling) too, just find the appropriate keyword in their docs!
575 // Please note that it's _not_ the same as C++ exceptions!
3f4a0c5b 576#if !defined(__WXDEBUG__) && defined(__VISUALC__)
f8a3e080
VZ
577 #define CATCH_PROGRAM_EXCEPTIONS
578
3f4a0c5b 579 __try {
f8a3e080
VZ
580#else
581 #undef CATCH_PROGRAM_EXCEPTIONS
a5e0e655
VZ
582#endif
583
2bda0e17
KB
584 wxhInstance = (HINSTANCE) hInstance;
585
589f0e3e 586 if (!wxApp::Initialize())
2bda0e17
KB
587 return 0;
588
a5e0e655 589 // create the application object or ensure that one already exists
2bda0e17
KB
590 if (!wxTheApp)
591 {
a5e0e655
VZ
592 // The app may have declared a global application object, but we recommend
593 // the IMPLEMENT_APP macro is used instead, which sets an initializer
594 // function for delayed, dynamic app object construction.
595 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
596 "No initializer - use IMPLEMENT_APP macro." );
2bda0e17 597
a5e0e655 598 wxTheApp = (*wxApp::GetInitializerFunction()) ();
2bda0e17
KB
599 }
600
a5e0e655 601 wxCHECK_MSG( wxTheApp, 0, "You have to define an instance of wxApp!" );
2bda0e17 602
a5e0e655 603 // save the WinMain() parameters
589f0e3e 604 wxTheApp->ConvertToStandardCommandArgs(lpCmdLine);
2bda0e17
KB
605 wxTheApp->m_nCmdShow = nCmdShow;
606
607 // GUI-specific initialisation. In fact on Windows we don't have any,
608 // but this call is provided for compatibility across platforms.
609 wxTheApp->OnInitGui() ;
610
a5e0e655 611 int retValue = 0;
2bda0e17 612
a5e0e655
VZ
613 if ( wxTheApp->OnInit() )
614 {
615 if ( enterLoop )
616 {
617 retValue = wxTheApp->OnRun();
618 }
f5e5bd66
JS
619 else
620 // We want to initialize, but not run or exit immediately.
621 return 1;
2bda0e17 622 }
a5e0e655 623 //else: app initialization failed, so we skipped OnRun()
2bda0e17 624
a5e0e655
VZ
625 wxWindow *topWindow = wxTheApp->GetTopWindow();
626 if ( topWindow )
2bda0e17 627 {
a5e0e655
VZ
628 // Forcibly delete the window.
629 if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
630 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
631 {
632 topWindow->Close(TRUE);
633 wxTheApp->DeletePendingObjects();
634 }
635 else
636 {
637 delete topWindow;
638 wxTheApp->SetTopWindow(NULL);
639 }
2bda0e17 640 }
d50b2a58 641
2bda0e17 642 wxTheApp->OnExit();
f57fe24c 643
2bda0e17
KB
644 wxApp::CleanUp();
645
2bda0e17 646 return retValue;
f8a3e080
VZ
647
648#ifdef CATCH_PROGRAM_EXCEPTIONS
a5e0e655 649 }
f8a3e080 650 __except ( EXCEPTION_EXECUTE_HANDLER ) {
a5e0e655
VZ
651 /*
652 if ( wxTheApp )
653 wxTheApp->OnFatalException();
654 */
655
656 ::ExitProcess(3); // the same exit code as abort()
f8a3e080
VZ
657
658 // NOTREACHED
a5e0e655 659 }
f8a3e080 660#endif // CATCH_PROGRAM_EXCEPTIONS
2bda0e17
KB
661}
662
f8a3e080 663// restore warning state
3f4a0c5b 664#ifdef __VISUALC__
f8a3e080
VZ
665 #pragma warning(default: 4715) // not all control paths return a value
666#endif // Visual C++
667
2bda0e17
KB
668#else /* _WINDLL */
669
589f0e3e
JS
670//// Entry point for DLLs
671
2bda0e17
KB
672int wxEntry(WXHINSTANCE hInstance)
673{
674 wxhInstance = (HINSTANCE) hInstance;
589f0e3e 675 wxApp::Initialize();
2bda0e17
KB
676
677 // The app may have declared a global application object, but we recommend
678 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
679 // for delayed, dynamic app object construction.
680
681 if (!wxTheApp)
682 {
a5e0e655
VZ
683 if (!wxApp::GetInitializerFunction())
684 {
685 MessageBox(NULL, "No initializer - use IMPLEMENT_APP macro.", "wxWindows Error", MB_APPLMODAL | MB_ICONSTOP | MB_OK);
686 return 0;
687 }
2bda0e17 688
a5e0e655 689 wxTheApp = (* wxApp::GetInitializerFunction()) ();
2bda0e17
KB
690 }
691
692 if (!wxTheApp) {
693 MessageBox(NULL, "You have to define an instance of wxApp!", "wxWindows Error", MB_APPLMODAL | MB_ICONSTOP | MB_OK);
694 return 0;
695 }
696
697 wxTheApp->argc = 0;
698 wxTheApp->argv = NULL;
699
700 wxTheApp->OnInitGui();
701
702 wxTheApp->OnInit();
703
704 if (wxTheApp->GetTopWindow() && wxTheApp->GetTopWindow()->GetHWND()) {
705 wxTheApp->GetTopWindow()->Show(TRUE);
706 }
707
708 return 1;
709}
710#endif // _WINDLL
711
589f0e3e
JS
712//// Static member initialization
713
2bda0e17
KB
714wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
715
bb6290e3 716wxApp::wxApp()
2bda0e17
KB
717{
718 m_topWindow = NULL;
719 wxTheApp = this;
2bda0e17 720 m_className = "";
2bda0e17
KB
721 m_wantDebugOutput = TRUE ;
722 m_appName = "";
723 argc = 0;
724 argv = NULL;
2049ba38 725#ifdef __WXMSW__
2bda0e17
KB
726 m_printMode = wxPRINT_WINDOWS;
727#else
728 m_printMode = wxPRINT_POSTSCRIPT;
729#endif
2bda0e17 730 m_exitOnFrameDelete = TRUE;
2bda0e17
KB
731 m_auto3D = TRUE;
732}
733
589f0e3e
JS
734wxApp::~wxApp()
735{
736 // Delete command-line args
737 int i;
738 for (i = 0; i < argc; i++)
739 {
740 delete[] argv[i];
741 }
da36f544 742 delete[] argv;
589f0e3e
JS
743}
744
bb6290e3 745bool wxApp::Initialized()
2bda0e17
KB
746{
747#ifndef _WINDLL
748 if (GetTopWindow())
749 return TRUE;
750 else
751 return FALSE;
752#endif
753#ifdef _WINDLL // Assume initialized if DLL (no way of telling)
754 return TRUE;
755#endif
756}
757
758/*
759 * Get and process a message, returning FALSE if WM_QUIT
bee503b0 760 * received (and also set the flag telling the app to exit the main loop)
2bda0e17
KB
761 *
762 */
bb6290e3 763bool wxApp::DoMessage()
2bda0e17 764{
bee503b0
VZ
765 BOOL rc = ::GetMessage(&s_currentMsg, (HWND) NULL, 0, 0);
766 if ( rc == 0 )
767 {
768 // got WM_QUIT
769 m_keepGoing = FALSE;
4a9968f9 770
bee503b0
VZ
771 return FALSE;
772 }
773 else if ( rc == -1 )
774 {
775 // should never happen, but let's test for it nevertheless
776 wxLogLastError("GetMessage");
777 }
778 else
779 {
780#if wxUSE_THREADS
781 wxASSERT_MSG( wxThread::IsMain(),
782 "only the main thread can process Windows messages" );
d50b2a58 783
bee503b0
VZ
784 static bool s_hadGuiLock = TRUE;
785 static wxMsgArray s_aSavedMessages;
786
787 // if a secondary thread owns is doing GUI calls, save all messages for
788 // later processing - we can't process them right now because it will
789 // lead to recursive library calls (and we're not reentrant)
790 if ( !wxGuiOwnedByMainThread() )
791 {
792 s_hadGuiLock = FALSE;
793
4a9968f9
VZ
794 // leave out WM_COMMAND messages: too dangerous, sometimes
795 // the message will be processed twice
796 if ( !wxIsWaitingForThread() ||
797 s_currentMsg.message != WM_COMMAND )
798 {
799 s_aSavedMessages.Add(s_currentMsg);
800 }
bee503b0
VZ
801
802 return TRUE;
803 }
804 else
805 {
806 // have we just regained the GUI lock? if so, post all of the saved
807 // messages
808 //
809 // FIXME of course, it's not _exactly_ the same as processing the
810 // messages normally - expect some things to break...
811 if ( !s_hadGuiLock )
812 {
813 s_hadGuiLock = TRUE;
814
815 size_t count = s_aSavedMessages.Count();
816 for ( size_t n = 0; n < count; n++ )
817 {
818 MSG& msg = s_aSavedMessages[n];
819
820 if ( !ProcessMessage((WXMSG *)&msg) )
821 {
822 ::TranslateMessage(&msg);
823 ::DispatchMessage(&msg);
824 }
825 }
826
827 s_aSavedMessages.Empty();
828 }
829 }
830#endif // wxUSE_THREADS
831
832 // Process the message
833 if ( !ProcessMessage((WXMSG *)&s_currentMsg) )
834 {
835 ::TranslateMessage(&s_currentMsg);
836 wxApp::sm_lastMessageTime = s_currentMsg.time; /* MATTHEW: timeStamp impl. */
837 ::DispatchMessage(&s_currentMsg);
838 }
839 }
840
841 return TRUE;
2bda0e17
KB
842}
843
844/*
845 * Keep trying to process messages until WM_QUIT
846 * received.
847 *
848 * If there are messages to be processed, they will all be
849 * processed and OnIdle will not be called.
850 * When there are no more messages, OnIdle is called.
851 * If OnIdle requests more time,
852 * it will be repeatedly called so long as there are no pending messages.
853 * A 'feature' of this is that once OnIdle has decided that no more processing
854 * is required, then it won't get processing time until further messages
855 * are processed (it'll sit in DoMessage).
856 */
857
bb6290e3 858int wxApp::MainLoop()
2bda0e17
KB
859{
860 m_keepGoing = TRUE;
bee503b0
VZ
861
862 while ( m_keepGoing )
2bda0e17 863 {
bee503b0
VZ
864 #if wxUSE_THREADS
865 wxMutexGuiLeaveOrEnter();
866 #endif // wxUSE_THREADS
867
868 while ( !::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) &&
869 ProcessIdle() )
870 {
871 }
872
873 DoMessage();
2bda0e17
KB
874 }
875
876 return s_currentMsg.wParam;
877}
878
879// Returns TRUE if more time is needed.
bb6290e3 880bool wxApp::ProcessIdle()
2bda0e17
KB
881{
882 wxIdleEvent event;
883 event.SetEventObject(this);
884 ProcessEvent(event);
885
886 return event.MoreRequested();
887}
888
bb6290e3 889void wxApp::ExitMainLoop()
2bda0e17
KB
890{
891 m_keepGoing = FALSE;
892}
893
bb6290e3 894bool wxApp::Pending()
2bda0e17
KB
895{
896 return (::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) != 0) ;
897}
898
bb6290e3 899void wxApp::Dispatch()
2bda0e17 900{
bee503b0 901 DoMessage();
2bda0e17
KB
902}
903
904/*
905 * Give all windows a chance to preprocess
906 * the message. Some may have accelerator tables, or have
907 * MDI complications.
908 */
909bool wxApp::ProcessMessage(WXMSG *Msg)
910{
911 MSG *msg = (MSG *)Msg;
912
913 HWND hWnd;
914
57a7b7c1
JS
915 // Try translations first; find the youngest window with
916 // a translation table.
57c208c5 917 for (hWnd = msg->hwnd; hWnd != (HWND) NULL; hWnd = ::GetParent(hWnd))
2bda0e17
KB
918 {
919 wxWindow *wnd = wxFindWinFromHandle((WXHWND) hWnd);
920 if (wnd)
921 {
57a7b7c1 922 if (wnd->MSWTranslateMessage(Msg))
2bda0e17 923 return TRUE;
2bda0e17
KB
924 }
925 }
926
57a7b7c1 927 // Anyone for a non-translation message? Try youngest descendants first.
57c208c5 928 for (hWnd = msg->hwnd; hWnd != (HWND) NULL; hWnd = ::GetParent(hWnd))
57a7b7c1
JS
929 {
930 wxWindow *wnd = wxFindWinFromHandle((WXHWND) hWnd);
931 if (wnd)
932 {
933 if (wnd->MSWProcessMessage(Msg))
934 return TRUE;
935 }
936 }
937 return FALSE;
2bda0e17
KB
938}
939
940void wxApp::OnIdle(wxIdleEvent& event)
941{
3222fde2 942 static bool s_inOnIdle = FALSE;
2bda0e17 943
3222fde2
VZ
944 // Avoid recursion (via ProcessEvent default case)
945 if ( s_inOnIdle )
946 return;
2bda0e17 947
3222fde2 948 s_inOnIdle = TRUE;
2bda0e17 949
3222fde2
VZ
950 // 'Garbage' collection of windows deleted with Close().
951 DeletePendingObjects();
2bda0e17 952
3222fde2
VZ
953 // flush the logged messages if any
954 wxLog *pLog = wxLog::GetActiveTarget();
955 if ( pLog != NULL && pLog->HasPendingMessages() )
956 pLog->Flush();
c54f78a2 957
3222fde2
VZ
958 // Send OnIdle events to all windows
959 if ( SendIdleEvents() )
960 {
961 // SendIdleEvents() returns TRUE if at least one window requested more
962 // idle events
963 event.RequestMore(TRUE);
964 }
2bda0e17 965
3222fde2 966 s_inOnIdle = FALSE;
2bda0e17
KB
967}
968
969// Send idle event to all top-level windows
bb6290e3 970bool wxApp::SendIdleEvents()
2bda0e17
KB
971{
972 bool needMore = FALSE;
3222fde2
VZ
973 wxNode* node = wxTopLevelWindows.First();
974 while (node)
975 {
976 wxWindow* win = (wxWindow*) node->Data();
977 if (SendIdleEvents(win))
2bda0e17
KB
978 needMore = TRUE;
979
3222fde2
VZ
980 node = node->Next();
981 }
982
2bda0e17
KB
983 return needMore;
984}
985
986// Send idle event to window and all subwindows
987bool wxApp::SendIdleEvents(wxWindow* win)
988{
a5e0e655 989 bool needMore = FALSE;
2bda0e17 990
a5e0e655
VZ
991 wxIdleEvent event;
992 event.SetEventObject(win);
02800301 993 win->GetEventHandler()->ProcessEvent(event);
2bda0e17 994
a5e0e655
VZ
995 if (event.MoreRequested())
996 needMore = TRUE;
2bda0e17 997
c0ed460c 998 wxNode* node = win->GetChildren().First();
a5e0e655
VZ
999 while (node)
1000 {
1001 wxWindow* win = (wxWindow*) node->Data();
1002 if (SendIdleEvents(win))
1003 needMore = TRUE;
2bda0e17 1004
a5e0e655
VZ
1005 node = node->Next();
1006 }
1007 return needMore ;
2bda0e17
KB
1008}
1009
bb6290e3 1010void wxApp::DeletePendingObjects()
2bda0e17
KB
1011{
1012 wxNode *node = wxPendingDelete.First();
1013 while (node)
1014 {
1015 wxObject *obj = (wxObject *)node->Data();
d50b2a58 1016
2bda0e17
KB
1017 delete obj;
1018
1019 if (wxPendingDelete.Member(obj))
1020 delete node;
1021
1022 // Deleting one object may have deleted other pending
1023 // objects, so start from beginning of list again.
1024 node = wxPendingDelete.First();
1025 }
1026}
1027
57c208c5 1028void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
387a3b02
JS
1029{
1030 if (GetTopWindow())
1031 GetTopWindow()->Close(TRUE);
1032}
1033
1034// Default behaviour: close the application with prompts. The
1035// user can veto the close, and therefore the end session.
1036void wxApp::OnQueryEndSession(wxCloseEvent& event)
1037{
1038 if (GetTopWindow())
1039 {
1040 if (!GetTopWindow()->Close(!event.CanVeto()))
1041 event.Veto(TRUE);
1042 }
1043}
1044
bb6290e3 1045wxLog* wxApp::CreateLogTarget()
2bda0e17
KB
1046{
1047 return new wxLogGui;
1048}
1049
bb6290e3 1050wxWindow* wxApp::GetTopWindow() const
2bda0e17
KB
1051{
1052 if (m_topWindow)
1053 return m_topWindow;
1054 else if (wxTopLevelWindows.Number() > 0)
1055 return (wxWindow*) wxTopLevelWindows.First()->Data();
1056 else
1057 return NULL;
1058}
1059
bb6290e3
JS
1060int wxApp::GetComCtl32Version() const
1061{
1062 // have we loaded COMCTL32 yet?
1063 HMODULE theModule = ::GetModuleHandle("COMCTL32");
1064 int version = 0;
d50b2a58 1065
bb6290e3
JS
1066 // if so, then we can check for the version
1067 if (theModule)
1068 {
1069 // InitCommonControlsEx is unique to 4.7 and later
1070 FARPROC theProc = ::GetProcAddress(theModule, "InitCommonControlsEx");
d50b2a58 1071
bb6290e3
JS
1072 if (! theProc)
1073 { // not found, must be 4.00
a5e0e655 1074 version = 400;
bb6290e3
JS
1075 }
1076 else
1077 {
a5e0e655
VZ
1078 // The following symbol are unique to 4.71
1079 // DllInstall
1080 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1081 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1082 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1083 // FlatSB_ShowScrollBar
1084 // _DrawIndirectImageList _DuplicateImageList
1085 // InitializeFlatSB
1086 // UninitializeFlatSB
1087 // we could check for any of these - I chose DllInstall
1088 FARPROC theProc = ::GetProcAddress(theModule, "DllInstall");
1089 if (! theProc)
1090 {
1091 // not found, must be 4.70
1092 version = 470;
1093 }
1094 else
1095 { // found, must be 4.71
1096 version = 471;
1097 }
bb6290e3
JS
1098 }
1099 }
1100 return version;
1101}
1102
1103void wxExit()
2bda0e17
KB
1104{
1105 wxApp::CleanUp();
1106 FatalAppExit(0, "Fatal error: exiting");
1107}
1108
1109// Yield to incoming messages
bb6290e3 1110bool wxYield()
2bda0e17
KB
1111{
1112 MSG msg;
1113 // We want to go back to the main message loop
1114 // if we see a WM_QUIT. (?)
1115 while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) && msg.message != WM_QUIT)
1116 {
bee503b0 1117 if ( !wxTheApp->DoMessage() )
2bda0e17
KB
1118 break;
1119 }
1120
1121 return TRUE;
1122}
1123
1124HINSTANCE wxGetInstance()
1125{
a5e0e655 1126 return wxhInstance;
2bda0e17
KB
1127}
1128
1129// For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1130// if in a separate file. So include it here to ensure it's linked.
3f4a0c5b 1131#if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))
a5e0e655 1132 #include "main.cpp"
2bda0e17 1133#endif