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