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