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