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