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