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