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