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