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