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