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