]> git.saurik.com Git - wxWidgets.git/blob - src/msw/app.cpp
Simplified BC++ makefiles; C++Builder 1.0 compilation seems to work;
[wxWidgets.git] / src / msw / app.cpp
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 #include "wx/wxprec.h"
18
19 #if defined(__BORLANDC__)
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
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"
36 #endif
37
38 #include "wx/msw/private.h"
39 #include "wx/log.h"
40 #include "wx/module.h"
41
42 #if wxUSE_THREADS
43 #include "wx/thread.h"
44 #endif
45
46 #if wxUSE_WX_RESOURCES
47 #include "wx/resource.h"
48 #endif
49
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__
55 #include <ole2.h>
56 #endif
57 // #endif
58
59 #include <string.h>
60 #include <ctype.h>
61
62 #if defined(__WIN95__) && !defined(__GNUWIN32__)
63 #include <commctrl.h>
64 #endif
65
66 // use debug CRT functions for memory leak detections in VC++ if we're not
67 // using wxWindows own methods
68 #if defined(__WXDEBUG__) && defined(_MSC_VER) && !wxUSE_GLOBAL_MEMORY_OPERATORS && !defined(__NO_VC_CRTDBG__)
69 #define wxUSE_VC_CRTDBG
70 #else
71 #undef wxUSE_VC_CRTDBG
72 #endif
73
74 #ifdef wxUSE_VC_CRTDBG
75 // VC++ uses this macro as debug/release mode indicator
76 #ifndef _DEBUG
77 #define _DEBUG
78 #endif
79
80 /* Need to undef new if including crtdbg.h */
81 #ifdef new
82 #undef new
83 #endif
84
85 #include <crtdbg.h>
86
87 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
88 #define new new(__FILE__,__LINE__)
89 #endif
90
91 #endif
92
93 extern char *wxBuffer;
94 extern char *wxOsVersion;
95 extern wxList *wxWinHandleList;
96 extern wxList wxPendingDelete;
97 extern void wxSetKeyboardHook(bool doIt);
98 extern wxCursor *g_globalCursor;
99
100 HINSTANCE wxhInstance = 0;
101 static MSG s_currentMsg;
102 wxApp *wxTheApp = NULL;
103
104 // @@ why not const? and not static?
105 char wxFrameClassName[] = "wxFrameClass";
106 char wxMDIFrameClassName[] = "wxMDIFrameClass";
107 char wxMDIChildFrameClassName[] = "wxMDIChildFrameClass";
108 char wxPanelClassName[] = "wxPanelClass";
109 char wxCanvasClassName[] = "wxCanvasClass";
110
111 HICON wxSTD_FRAME_ICON = NULL;
112 HICON wxSTD_MDICHILDFRAME_ICON = NULL;
113 HICON wxSTD_MDIPARENTFRAME_ICON = NULL;
114
115 HICON wxDEFAULT_FRAME_ICON = NULL;
116 HICON wxDEFAULT_MDICHILDFRAME_ICON = NULL;
117 HICON wxDEFAULT_MDIPARENTFRAME_ICON = NULL;
118
119 HBRUSH wxDisableButtonBrush = 0;
120
121 LRESULT APIENTRY wxWndProc(HWND, UINT, WPARAM, LPARAM);
122
123 #if !USE_SHARED_LIBRARY
124 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
125
126 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
127 EVT_IDLE(wxApp::OnIdle)
128 EVT_END_SESSION(wxApp::OnEndSession)
129 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
130 END_EVENT_TABLE()
131 #endif
132
133 long wxApp::sm_lastMessageTime = 0;
134
135 #ifdef __WIN95__
136 static HINSTANCE gs_hRichEdit = NULL;
137 #endif
138
139 //// Initialize
140
141 bool wxApp::Initialize()
142 {
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
149 wxBuffer = new char[1500];
150
151 #ifdef wxUSE_VC_CRTDBG
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);
156 #endif // debug build under MS VC++
157
158 wxClassInfo::InitializeClasses();
159
160 #if wxUSE_RESOURCES
161 wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
162 #endif
163
164 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
165 wxTheColourDatabase->Initialize();
166
167 wxInitializeStockLists();
168 wxInitializeStockObjects();
169
170 #if wxUSE_WX_RESOURCES
171 wxInitializeResourceSystem();
172 #endif
173
174 wxBitmap::InitStandardHandlers();
175
176 #if defined(__WIN95__)
177 InitCommonControls();
178 gs_hRichEdit = LoadLibrary("RICHED32.DLL");
179
180 if (gs_hRichEdit == NULL)
181 {
182 wxMessageBox("Could not initialise Rich Edit DLL");
183 }
184 #endif
185
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 */
202
203 #ifndef __GNUWIN32__
204 // we need to initialize OLE library
205 if ( FAILED(::OleInitialize(NULL)) )
206 wxFatalError(_("Cannot initialize OLE"));
207 #endif
208
209 #if CTL3D
210 if (!Ctl3dRegister(wxhInstance))
211 wxFatalError("Cannot register CTL3D");
212
213 Ctl3dAutoSubclass(wxhInstance);
214 #endif
215
216 g_globalCursor = new wxCursor;
217
218 wxSTD_FRAME_ICON = LoadIcon(wxhInstance, "wxSTD_FRAME");
219 wxSTD_MDIPARENTFRAME_ICON = LoadIcon(wxhInstance, "wxSTD_MDIPARENTFRAME");
220 wxSTD_MDICHILDFRAME_ICON = LoadIcon(wxhInstance, "wxSTD_MDICHILDFRAME");
221
222 wxDEFAULT_FRAME_ICON = LoadIcon(wxhInstance, "wxDEFAULT_FRAME");
223 wxDEFAULT_MDIPARENTFRAME_ICON = LoadIcon(wxhInstance, "wxDEFAULT_MDIPARENTFRAME");
224 wxDEFAULT_MDICHILDFRAME_ICON = LoadIcon(wxhInstance, "wxDEFAULT_MDICHILDFRAME");
225
226 RegisterWindowClasses();
227
228 // Create the brush for disabling bitmap buttons
229
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 ) ;
235
236 #if wxUSE_PENWINDOWS
237 wxRegisterPenWin();
238 #endif
239
240 wxWinHandleList = new wxList(wxKEY_INTEGER);
241
242 // This is to foil optimizations in Visual C++ that
243 // throw out dummy.obj.
244 #if (_MSC_VER >= 800) && !defined(WXMAKINGDLL)
245 extern char wxDummyChar;
246 if (wxDummyChar) wxDummyChar++;
247 #endif
248
249 wxSetKeyboardHook(TRUE);
250
251 wxModule::RegisterModules();
252 if (!wxModule::InitializeModules())
253 return FALSE;
254 return TRUE;
255 }
256
257 //// RegisterWindowClasses
258
259 bool wxApp::RegisterWindowClasses()
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
276 sprintf( wxFrameClassName,"wxFrameClass%d", wxhInstance );
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");
304 // return FALSE;
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) ;
346 wndclass2.hbrBackground = (HBRUSH) GetStockObject( LTGRAY_BRUSH );
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.
361 // wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS ;
362 // wxWin 2.0
363 wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
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
383 //// Convert Windows to argc, argv style
384
385 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine)
386 {
387 wxStringList args;
388
389 wxString cmdLine(lpCmdLine);
390 int count = 0;
391
392 // Get application name
393 char name[260]; // 260 is MAX_PATH value from windef.h
394 ::GetModuleFileName(wxhInstance, name, WXSIZEOF(name));
395
396 // GNUWIN32 already fills in the first arg with the application name.
397 #if !defined(__GNUWIN32__)
398 args.Add(name);
399 count ++;
400 #endif
401
402 strcpy(name, wxFileNameFromPath(name));
403 wxStripExtension(name);
404 wxTheApp->SetAppName(name);
405
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)
411 {
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 }
445 }
446
447 wxTheApp->argv = new char*[count + 1];
448 for (i = 0; i < count; i++)
449 {
450 wxString arg(args[i]);
451 wxTheApp->argv[i] = copystring((const char*)arg);
452 }
453 wxTheApp->argv[count] = NULL; // argv[] is a NULL-terminated list
454 wxTheApp->argc = count;
455 }
456
457 //// Cleans up any wxWindows internal structures left lying around
458
459 void wxApp::CleanUp()
460 {
461 //// COMMON CLEANUP
462 wxModule::CleanUpModules();
463
464 #if wxUSE_WX_RESOURCES
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
485 wxBitmap::CleanUpHandlers();
486
487 delete[] wxBuffer;
488 wxBuffer = NULL;
489
490 //// WINDOWS-SPECIFIC CLEANUP
491
492 wxSetKeyboardHook(FALSE);
493
494 #ifdef __WIN95__
495 if (gs_hRichEdit != NULL)
496 FreeLibrary(gs_hRichEdit);
497 #endif
498
499 #if wxUSE_PENWINDOWS
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
520 #ifndef __GNUWIN32__
521 ::OleUninitialize();
522 #endif
523
524 #if CTL3D
525 Ctl3dUnregister(wxhInstance);
526 #endif
527
528 if (wxWinHandleList)
529 delete wxWinHandleList ;
530
531 wxClassInfo::CleanUpClasses();
532
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
550 // do it as the very last thing because everything else can log messages
551 wxLog::DontCreateOnDemand();
552 delete wxLog::SetActiveTarget(NULL);
553 }
554
555 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
556
557 //// Main wxWindows entry point
558 int wxEntry(WXHINSTANCE hInstance,
559 WXHINSTANCE WXUNUSED(hPrevInstance),
560 char *lpCmdLine,
561 int nCmdShow,
562 bool enterLoop)
563 {
564 #if !defined(__WXDEBUG__) && !defined(__BORLANDC__) // take everything into a try-except block in release build
565 try {
566 #endif
567
568 wxhInstance = (HINSTANCE) hInstance;
569
570 if (!wxApp::Initialize())
571 return 0;
572
573 // create the application object or ensure that one already exists
574 if (!wxTheApp)
575 {
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." );
581
582 wxTheApp = (*wxApp::GetInitializerFunction()) ();
583 }
584
585 wxCHECK_MSG( wxTheApp, 0, "You have to define an instance of wxApp!" );
586
587 // save the WinMain() parameters
588 wxTheApp->ConvertToStandardCommandArgs(lpCmdLine);
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
595 int retValue = 0;
596
597 if ( wxTheApp->OnInit() )
598 {
599 if ( enterLoop )
600 {
601 retValue = wxTheApp->OnRun();
602 }
603 }
604 //else: app initialization failed, so we skipped OnRun()
605
606 wxWindow *topWindow = wxTheApp->GetTopWindow();
607 if ( topWindow )
608 {
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 }
621 }
622
623 wxTheApp->OnExit();
624
625 // flush the logged messages if any
626 wxLog *pLog = wxLog::GetActiveTarget();
627 if ( pLog != NULL && pLog->HasPendingMessages() )
628 pLog->Flush();
629
630
631 wxApp::CleanUp();
632
633 return retValue;
634 #if !defined(__WXDEBUG__) && !defined(__BORLANDC__) // catch exceptions only in release build
635 }
636 except ( EXCEPTION_EXECUTE_HANDLER ) {
637 /*
638 if ( wxTheApp )
639 wxTheApp->OnFatalException();
640 */
641
642 ::ExitProcess(3); // the same exit code as abort()
643 }
644 #endif //debug
645 }
646
647 #else /* _WINDLL */
648
649 //// Entry point for DLLs
650
651 int wxEntry(WXHINSTANCE hInstance)
652 {
653 wxhInstance = (HINSTANCE) hInstance;
654 wxApp::Initialize();
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 {
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 }
667
668 wxTheApp = (* wxApp::GetInitializerFunction()) ();
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
691 //// Static member initialization
692
693 wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
694
695 wxApp::wxApp()
696 {
697 m_topWindow = NULL;
698 wxTheApp = this;
699 m_className = "";
700 m_wantDebugOutput = TRUE ;
701 m_appName = "";
702 argc = 0;
703 argv = NULL;
704 #ifdef __WXMSW__
705 m_printMode = wxPRINT_WINDOWS;
706 #else
707 m_printMode = wxPRINT_POSTSCRIPT;
708 #endif
709 m_exitOnFrameDelete = TRUE;
710 m_auto3D = TRUE;
711 }
712
713 wxApp::~wxApp()
714 {
715 // Delete command-line args
716 int i;
717 for (i = 0; i < argc; i++)
718 {
719 delete[] argv[i];
720 }
721 delete[] argv;
722 }
723
724 bool wxApp::Initialized()
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 */
742 bool wxApp::DoMessage()
743 {
744 if (!::GetMessage(&s_currentMsg, (HWND) NULL, 0, 0))
745 {
746 return FALSE;
747 }
748
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
773 int wxApp::MainLoop()
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.
788 bool wxApp::ProcessIdle()
789 {
790 wxIdleEvent event;
791 event.SetEventObject(this);
792 ProcessEvent(event);
793
794 return event.MoreRequested();
795 }
796
797 void wxApp::ExitMainLoop()
798 {
799 m_keepGoing = FALSE;
800 }
801
802 bool wxApp::Pending()
803 {
804 return (::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) != 0) ;
805 }
806
807 void wxApp::Dispatch()
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 */
818 bool wxApp::ProcessMessage(WXMSG *Msg)
819 {
820 MSG *msg = (MSG *)Msg;
821
822 HWND hWnd;
823
824 // Try translations first; find the youngest window with
825 // a translation table.
826 for (hWnd = msg->hwnd; hWnd != NULL; hWnd = ::GetParent(hWnd))
827 {
828 wxWindow *wnd = wxFindWinFromHandle((WXHWND) hWnd);
829 if (wnd)
830 {
831 if (wnd->MSWTranslateMessage(Msg))
832 return TRUE;
833 }
834 }
835
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;
847 }
848
849 void wxApp::OnIdle(wxIdleEvent& event)
850 {
851 static bool s_inOnIdle = FALSE;
852
853 // Avoid recursion (via ProcessEvent default case)
854 if ( s_inOnIdle )
855 return;
856
857 s_inOnIdle = TRUE;
858
859 // 'Garbage' collection of windows deleted with Close().
860 DeletePendingObjects();
861
862 // flush the logged messages if any
863 wxLog *pLog = wxLog::GetActiveTarget();
864 if ( pLog != NULL && pLog->HasPendingMessages() )
865 pLog->Flush();
866
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 }
874 #if wxUSE_THREADS
875 // give a chance to all other threads to perform GUI calls
876 wxMutexGuiLeave();
877 ::Sleep(0);
878 wxMutexGuiEnter();
879 #endif
880
881 s_inOnIdle = FALSE;
882 }
883
884 // Send idle event to all top-level windows
885 bool wxApp::SendIdleEvents()
886 {
887 bool needMore = FALSE;
888 wxNode* node = wxTopLevelWindows.First();
889 while (node)
890 {
891 wxWindow* win = (wxWindow*) node->Data();
892 if (SendIdleEvents(win))
893 needMore = TRUE;
894
895 node = node->Next();
896 }
897
898 return needMore;
899 }
900
901 // Send idle event to window and all subwindows
902 bool wxApp::SendIdleEvents(wxWindow* win)
903 {
904 bool needMore = FALSE;
905
906 wxIdleEvent event;
907 event.SetEventObject(win);
908 win->GetEventHandler()->ProcessEvent(event);
909
910 if (event.MoreRequested())
911 needMore = TRUE;
912
913 wxNode* node = win->GetChildren().First();
914 while (node)
915 {
916 wxWindow* win = (wxWindow*) node->Data();
917 if (SendIdleEvents(win))
918 needMore = TRUE;
919
920 node = node->Next();
921 }
922 return needMore ;
923 }
924
925 void wxApp::DeletePendingObjects()
926 {
927 wxNode *node = wxPendingDelete.First();
928 while (node)
929 {
930 wxObject *obj = (wxObject *)node->Data();
931
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
943 void 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.
951 void wxApp::OnQueryEndSession(wxCloseEvent& event)
952 {
953 if (GetTopWindow())
954 {
955 if (!GetTopWindow()->Close(!event.CanVeto()))
956 event.Veto(TRUE);
957 }
958 }
959
960 wxLog* wxApp::CreateLogTarget()
961 {
962 return new wxLogGui;
963 }
964
965 wxWindow* wxApp::GetTopWindow() const
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
975 int wxApp::GetComCtl32Version() const
976 {
977 // have we loaded COMCTL32 yet?
978 HMODULE theModule = ::GetModuleHandle("COMCTL32");
979 int version = 0;
980
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");
986
987 if (! theProc)
988 { // not found, must be 4.00
989 version = 400;
990 }
991 else
992 {
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 }
1013 }
1014 }
1015 return version;
1016 }
1017
1018 void wxExit()
1019 {
1020 wxApp::CleanUp();
1021 FatalAppExit(0, "Fatal error: exiting");
1022 }
1023
1024 // Yield to incoming messages
1025 bool wxYield()
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
1039 HINSTANCE wxGetInstance()
1040 {
1041 return wxhInstance;
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__)
1047 #include "main.cpp"
1048 #endif