]> git.saurik.com Git - wxWidgets.git/blob - src/motif/app.cpp
Restored sane way of using BC++ linker
[wxWidgets.git] / src / motif / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Purpose: wxApp
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "app.h"
14 #endif
15
16 #include "wx/frame.h"
17 #include "wx/app.h"
18 #include "wx/utils.h"
19 #include "wx/gdicmn.h"
20 #include "wx/pen.h"
21 #include "wx/brush.h"
22 #include "wx/cursor.h"
23 #include "wx/icon.h"
24 #include "wx/palette.h"
25 #include "wx/dc.h"
26 #include "wx/dialog.h"
27 #include "wx/msgdlg.h"
28 #include "wx/log.h"
29 #include "wx/module.h"
30 #include "wx/memory.h"
31 #include "wx/log.h"
32 #include "wx/intl.h"
33
34 #if wxUSE_THREADS
35 #include "wx/thread.h"
36 #endif
37
38 #if wxUSE_WX_RESOURCES
39 #include "wx/resource.h"
40 #endif
41
42 #ifdef __VMS__
43 #pragma message disable nosimpint
44 #endif
45 #include <Xm/Xm.h>
46 #include <X11/Xlib.h>
47 #include <X11/Xutil.h>
48 #include <X11/Xresource.h>
49 #include <X11/Xatom.h>
50 #ifdef __VMS__
51 #pragma message enable nosimpint
52 #endif
53
54 #include "wx/motif/private.h"
55
56 #include <string.h>
57
58 extern char *wxBuffer;
59 extern wxList wxPendingDelete;
60
61 wxApp *wxTheApp = NULL;
62
63 wxHashTable *wxWidgetHashTable = NULL;
64
65 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
66
67 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
68 EVT_IDLE(wxApp::OnIdle)
69 END_EVENT_TABLE()
70
71 #ifdef __WXDEBUG__
72 typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *);
73
74 XErrorHandlerFunc gs_pfnXErrorHandler = 0;
75
76 static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent)
77 {
78 // just forward to the default handler for now
79 return gs_pfnXErrorHandler(dpy, xevent);
80 }
81 #endif // __WXDEBUG__
82
83 long wxApp::sm_lastMessageTime = 0;
84
85 bool wxApp::Initialize()
86 {
87 wxBuffer = new char[BUFSIZ + 512];
88
89 wxClassInfo::InitializeClasses();
90
91 // GL: I'm annoyed ... I don't know where to put this and I don't want to
92 // create a module for that as it's part of the core.
93 #if wxUSE_THREADS
94 wxPendingEventsLocker = new wxCriticalSection();
95 #endif
96
97 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
98 wxTheColourDatabase->Initialize();
99
100 wxInitializeStockLists();
101 wxInitializeStockObjects();
102
103 #if wxUSE_WX_RESOURCES
104 wxInitializeResourceSystem();
105 #endif
106
107 // For PostScript printing
108 #if wxUSE_POSTSCRIPT
109 /* Done using wxModule now
110 wxInitializePrintSetupData();
111 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
112 wxThePrintPaperDatabase->CreateDatabase();
113 */
114 #endif
115
116 wxBitmap::InitStandardHandlers();
117
118 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
119
120 wxModule::RegisterModules();
121 if (!wxModule::InitializeModules()) return FALSE;
122
123 return TRUE;
124 }
125
126 void wxApp::CleanUp()
127 {
128 delete wxWidgetHashTable;
129 wxWidgetHashTable = NULL;
130
131 wxModule::CleanUpModules();
132
133 #if wxUSE_WX_RESOURCES
134 wxCleanUpResourceSystem();
135 #endif
136
137 wxDeleteStockObjects() ;
138
139 // Destroy all GDI lists, etc.
140
141 delete wxTheBrushList;
142 wxTheBrushList = NULL;
143
144 delete wxThePenList;
145 wxThePenList = NULL;
146
147 delete wxTheFontList;
148 wxTheFontList = NULL;
149
150 delete wxTheBitmapList;
151 wxTheBitmapList = NULL;
152
153 delete wxTheColourDatabase;
154 wxTheColourDatabase = NULL;
155
156 #if wxUSE_POSTSCRIPT
157 /* Done using wxModule now
158 wxInitializePrintSetupData(FALSE);
159 delete wxThePrintPaperDatabase;
160 wxThePrintPaperDatabase = NULL;
161 */
162 #endif
163
164 wxBitmap::CleanUpHandlers();
165
166 delete[] wxBuffer;
167 wxBuffer = NULL;
168
169 wxClassInfo::CleanUpClasses();
170
171 delete wxTheApp;
172 wxTheApp = NULL;
173
174 // GL: I'm annoyed ... I don't know where to put this and I don't want to
175 // create a module for that as it's part of the core.
176 #if wxUSE_THREADS
177 delete wxPendingEvents;
178 delete wxPendingEventsLocker;
179 #endif
180
181 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
182 // At this point we want to check if there are any memory
183 // blocks that aren't part of the wxDebugContext itself,
184 // as a special case. Then when dumping we need to ignore
185 // wxDebugContext, too.
186 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
187 {
188 wxLogDebug("There were memory leaks.\n");
189 wxDebugContext::Dump();
190 wxDebugContext::PrintStatistics();
191 }
192 #endif
193
194 // do it as the very last thing because everything else can log messages
195 wxLog::DontCreateOnDemand();
196 // do it as the very last thing because everything else can log messages
197 delete wxLog::SetActiveTarget(NULL);
198 }
199
200 int wxEntry( int argc, char *argv[] )
201 {
202 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
203 // This seems to be necessary since there are 'rogue'
204 // objects present at this point (perhaps global objects?)
205 // Setting a checkpoint will ignore them as far as the
206 // memory checking facility is concerned.
207 // Of course you may argue that memory allocated in globals should be
208 // checked, but this is a reasonable compromise.
209 wxDebugContext::SetCheckpoint();
210 #endif
211
212 if (!wxApp::Initialize())
213 return FALSE;
214
215 if (!wxTheApp)
216 {
217 if (!wxApp::GetInitializerFunction())
218 {
219 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
220 return 0;
221 };
222
223 wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) ();
224 };
225
226 if (!wxTheApp)
227 {
228 printf( "wxWindows error: wxTheApp == NULL\n" );
229 return 0;
230 };
231
232 wxTheApp->SetClassName(wxFileNameFromPath(argv[0]));
233 wxTheApp->SetAppName(wxFileNameFromPath(argv[0]));
234
235 wxTheApp->argc = argc;
236 wxTheApp->argv = argv;
237
238 // GUI-specific initialization, such as creating an app context.
239 wxTheApp->OnInitGui();
240
241 // Here frames insert themselves automatically into wxTopLevelWindows by
242 // getting created in OnInit().
243
244 int retValue = 0;
245 if (wxTheApp->OnInit())
246 {
247 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
248 }
249
250 // flush the logged messages if any
251 wxLog *pLog = wxLog::GetActiveTarget();
252 if ( pLog != NULL && pLog->HasPendingMessages() )
253 pLog->Flush();
254
255 delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used
256 // for further messages
257
258 if (wxTheApp->GetTopWindow())
259 {
260 delete wxTheApp->GetTopWindow();
261 wxTheApp->SetTopWindow(NULL);
262 }
263
264 wxTheApp->DeletePendingObjects();
265
266 wxTheApp->OnExit();
267
268 wxApp::CleanUp();
269
270 return retValue;
271 };
272
273 // Static member initialization
274 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
275
276 wxApp::wxApp()
277 {
278 m_topWindow = NULL;
279 wxTheApp = this;
280 m_className = "";
281 m_wantDebugOutput = TRUE ;
282 m_appName = "";
283 argc = 0;
284 argv = NULL;
285 m_exitOnFrameDelete = TRUE;
286
287 m_mainColormap = (WXColormap) NULL;
288 m_appContext = (WXAppContext) NULL;
289 m_topLevelWidget = (WXWidget) NULL;
290 m_maxRequestSize = 0;
291 m_initialDisplay = (WXDisplay*) 0;
292 }
293
294 bool wxApp::Initialized()
295 {
296 if (GetTopWindow())
297 return TRUE;
298 else
299 return FALSE;
300 }
301
302 int wxApp::MainLoop()
303 {
304 m_keepGoing = TRUE;
305
306 /*
307 * Sit around forever waiting to process X-events. Property Change
308 * event are handled special, because they have to refer to
309 * the root window rather than to a widget. therefore we can't
310 * use an Xt-eventhandler.
311 */
312
313 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
314 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
315 PropertyChangeMask);
316
317 XEvent event;
318
319 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
320 while (m_keepGoing)
321 {
322 XtAppNextEvent( (XtAppContext) wxTheApp->GetAppContext(), &event);
323
324 ProcessXEvent((WXEvent*) & event);
325
326 if (XtAppPending( (XtAppContext) wxTheApp->GetAppContext() ) == 0)
327 {
328 if (!ProcessIdle())
329 {
330 #if wxUSE_THREADS
331 // leave the main loop to give other threads a chance to
332 // perform their GUI work
333 wxMutexGuiLeave();
334 wxUsleep(20);
335 wxMutexGuiEnter();
336 #endif
337 }
338 }
339
340 }
341
342 return 0;
343 }
344
345 // Processes an X event.
346 void wxApp::ProcessXEvent(WXEvent* _event)
347 {
348 XEvent* event = (XEvent*) _event;
349
350 if (event->type == KeyPress)
351 {
352 #if 0 // def __WXDEBUG__
353 Widget widget = XtWindowToWidget(event->xany.display, event->xany.window);
354 wxLogDebug("Got key press event for 0x%08x (parent = 0x%08x)",
355 widget, XtParent(widget));
356 #endif // DEBUG
357
358 if (CheckForAccelerator(_event))
359 {
360 // Do nothing! We intercepted and processed the event as an
361 // accelerator.
362 return;
363 }
364 #if 1
365 // It seemed before that this hack was redundant and
366 // key down events were being generated by wxCanvasInputEvent.
367 // But no longer - why ???
368 //
369 else if (CheckForKeyDown(_event))
370 {
371 // We intercepted and processed the key down event
372 return;
373 }
374 #endif
375 else
376 {
377 XtDispatchEvent(event);
378 return;
379 }
380 }
381 else if (event->type == KeyRelease)
382 {
383 // TODO: work out why we still need this ! -michael
384 //
385 if (CheckForKeyUp(_event))
386 {
387 // We intercepted and processed the key up event
388 return;
389 }
390 else
391 {
392 XtDispatchEvent(event);
393 return;
394 }
395 }
396 else if (event->type == PropertyNotify)
397 {
398 HandlePropertyChange(_event);
399 return;
400 }
401 else if (event->type == ResizeRequest)
402 {
403 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
404 * If resize event, don't resize until the last resize event for this
405 * window is recieved. Prevents flicker as windows are resized.
406 */
407
408 Display *disp = XtDisplay((Widget) wxTheApp->GetTopLevelWidget());
409 Window win = event->xany.window;
410 XEvent report;
411
412 // to avoid flicker
413 report = * event;
414 while( XCheckTypedWindowEvent (disp, win, ResizeRequest, &report));
415
416 // TODO: when implementing refresh optimization, we can use
417 // XtAddExposureToRegion to expand the window's paint region.
418
419 XtDispatchEvent(event);
420 }
421 else
422 {
423 XtDispatchEvent(event);
424 }
425 }
426
427 // Returns TRUE if more time is needed.
428 bool wxApp::ProcessIdle()
429 {
430 wxIdleEvent event;
431 event.SetEventObject(this);
432 ProcessEvent(event);
433
434 return event.MoreRequested();
435 }
436
437 void wxApp::ExitMainLoop()
438 {
439 m_keepGoing = FALSE;
440 }
441
442 // Is a message/event pending?
443 bool wxApp::Pending()
444 {
445 XFlush(XtDisplay( (Widget) wxTheApp->GetTopLevelWidget() ));
446
447 // Fix by Doug from STI, to prevent a stall if non-X event
448 // is found.
449 return ((XtAppPending( (XtAppContext) GetAppContext() ) & XtIMXEvent) != 0) ;
450 }
451
452 // Dispatch a message.
453 void wxApp::Dispatch()
454 {
455 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
456
457 XEvent event;
458 XtAppNextEvent((XtAppContext) GetAppContext(), &event);
459 ProcessXEvent((WXEvent*) & event);
460 }
461
462 // This should be redefined in a derived class for
463 // handling property change events for XAtom IPC.
464 void wxApp::HandlePropertyChange(WXEvent *event)
465 {
466 // by default do nothing special
467 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
468 }
469
470 void wxApp::OnIdle(wxIdleEvent& event)
471 {
472 static bool inOnIdle = FALSE;
473
474 // Avoid recursion (via ProcessEvent default case)
475 if (inOnIdle)
476 return;
477
478 inOnIdle = TRUE;
479
480 // If there are pending events, we must process them: pending events
481 // are either events to the threads other than main or events posted
482 // with wxPostEvent() functions
483 // GRG: I have moved this here so that all pending events are processed
484 // before starting to delete any objects. This behaves better (in
485 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
486 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
487 // Changed Mar/2000 before 2.1.14
488
489 // Flush pending events.
490 ProcessPendingEvents();
491
492 // 'Garbage' collection of windows deleted with Close().
493 DeletePendingObjects();
494
495 // flush the logged messages if any
496 wxLog *pLog = wxLog::GetActiveTarget();
497 if ( pLog != NULL && pLog->HasPendingMessages() )
498 pLog->Flush();
499
500 // Send OnIdle events to all windows
501 bool needMore = SendIdleEvents();
502
503 if (needMore)
504 event.RequestMore(TRUE);
505
506 inOnIdle = FALSE;
507 }
508
509 void wxWakeUpIdle()
510 {
511 // **** please implement me! ****
512 // Wake up the idle handler processor, even if it is in another thread...
513 }
514
515
516 // Send idle event to all top-level windows
517 bool wxApp::SendIdleEvents()
518 {
519 bool needMore = FALSE;
520
521 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
522 while (node)
523 {
524 wxWindow* win = node->GetData();
525 if (SendIdleEvents(win))
526 needMore = TRUE;
527 node = node->GetNext();
528 }
529
530 return needMore;
531 }
532
533 // Send idle event to window and all subwindows
534 bool wxApp::SendIdleEvents(wxWindow* win)
535 {
536 bool needMore = FALSE;
537
538 wxIdleEvent event;
539 event.SetEventObject(win);
540 win->ProcessEvent(event);
541
542 if (event.MoreRequested())
543 needMore = TRUE;
544
545 wxNode* node = win->GetChildren().First();
546 while (node)
547 {
548 wxWindow* win = (wxWindow*) node->Data();
549 if (SendIdleEvents(win))
550 needMore = TRUE;
551
552 node = node->Next();
553 }
554 return needMore ;
555 }
556
557 void wxApp::DeletePendingObjects()
558 {
559 wxNode *node = wxPendingDelete.First();
560 while (node)
561 {
562 wxObject *obj = (wxObject *)node->Data();
563
564 delete obj;
565
566 if (wxPendingDelete.Member(obj))
567 delete node;
568
569 // Deleting one object may have deleted other pending
570 // objects, so start from beginning of list again.
571 node = wxPendingDelete.First();
572 }
573 }
574
575 // Create an application context
576 bool wxApp::OnInitGui()
577 {
578 XtToolkitInitialize() ;
579 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext() ;
580 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
581 (const char*) wxTheApp->GetClassName(), NULL, 0,
582 # if XtSpecificationRelease < 5
583 (Cardinal*) &argc,
584 # else
585 &argc,
586 # endif
587 argv);
588
589 if (!dpy) {
590 wxString className(wxTheApp->GetClassName());
591 wxLogError(_("wxWindows could not open display for '%s': exiting."),
592 (const char*) className);
593 exit(-1);
594 }
595 m_initialDisplay = (WXDisplay*) dpy;
596
597 #ifdef __WXDEBUG__
598 // install the X error handler
599 gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler);
600 #endif // __WXDEBUG__
601
602 wxTheApp->m_topLevelWidget = (WXWidget) XtAppCreateShell((String)NULL, (const char*) wxTheApp->GetClassName(),
603 applicationShellWidgetClass,dpy,
604 NULL,0) ;
605
606 // Add general resize proc
607 XtActionsRec rec;
608 rec.string = "resize";
609 rec.proc = (XtActionProc)wxWidgetResizeProc;
610 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
611
612 GetMainColormap(dpy);
613 m_maxRequestSize = XMaxRequestSize((Display*) dpy);
614
615 return TRUE;
616 }
617
618 WXColormap wxApp::GetMainColormap(WXDisplay* display)
619 {
620 if (!display) /* Must be called first with non-NULL display */
621 return m_mainColormap;
622
623 int defaultScreen = DefaultScreen((Display*) display);
624 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
625
626 Colormap c = DefaultColormapOfScreen(screen);
627
628 if (!m_mainColormap)
629 m_mainColormap = (WXColormap) c;
630
631 return (WXColormap) c;
632 }
633
634 // Returns TRUE if an accelerator has been processed
635 bool wxApp::CheckForAccelerator(WXEvent* event)
636 {
637 XEvent* xEvent = (XEvent*) event;
638 if (xEvent->xany.type == KeyPress)
639 {
640 // Find a wxWindow for this window
641 // TODO: should get display for the window, not the current display
642 Widget widget = XtWindowToWidget((Display*) wxGetDisplay(), xEvent->xany.window);
643 wxWindow* win = NULL;
644
645 // Find the first wxWindow that corresponds to this event window
646 while (widget && !(win = wxGetWindowFromTable(widget)))
647 widget = XtParent(widget);
648
649 if (!widget || !win)
650 return FALSE;
651
652 wxKeyEvent keyEvent(wxEVT_CHAR);
653 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent);
654
655 // Now we have a wxKeyEvent and we have a wxWindow.
656 // Go up the hierarchy until we find a matching accelerator,
657 // or we get to the top.
658 while (win)
659 {
660 if (win->ProcessAccelerator(keyEvent))
661 return TRUE;
662 win = win->GetParent();
663 }
664 return FALSE;
665 }
666 return FALSE;
667 }
668
669 bool wxApp::CheckForKeyDown(WXEvent* event)
670 {
671 XEvent* xEvent = (XEvent*) event;
672 if (xEvent->xany.type == KeyPress)
673 {
674 Widget widget = XtWindowToWidget((Display*) wxGetDisplay(),
675 xEvent->xany.window);
676 wxWindow* win = NULL;
677
678 // Find the first wxWindow that corresponds to this event window
679 while (widget && !(win = wxGetWindowFromTable(widget)))
680 widget = XtParent(widget);
681
682 if (!widget || !win)
683 return FALSE;
684
685 wxKeyEvent keyEvent(wxEVT_KEY_DOWN);
686 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent);
687
688 return win->ProcessEvent( keyEvent );
689 }
690
691 return FALSE;
692 }
693
694 bool wxApp::CheckForKeyUp(WXEvent* event)
695 {
696 XEvent* xEvent = (XEvent*) event;
697 if (xEvent->xany.type == KeyRelease)
698 {
699 Widget widget = XtWindowToWidget((Display*) wxGetDisplay(),
700 xEvent->xany.window);
701 wxWindow* win = NULL;
702
703 // Find the first wxWindow that corresponds to this event window
704 while (widget && !(win = wxGetWindowFromTable(widget)))
705 widget = XtParent(widget);
706
707 if (!widget || !win)
708 return FALSE;
709
710 wxKeyEvent keyEvent(wxEVT_KEY_UP);
711 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent);
712
713 return win->ProcessEvent( keyEvent );
714 }
715
716 return FALSE;
717 }
718
719 void wxExit()
720 {
721 int retValue = 0;
722 if (wxTheApp)
723 retValue = wxTheApp->OnExit();
724
725 wxApp::CleanUp();
726 /*
727 * Exit in some platform-specific way. Not recommended that the app calls this:
728 * only for emergencies.
729 */
730 exit(retValue);
731 }
732
733 // Yield to other processes
734 bool wxYield()
735 {
736 while (wxTheApp && wxTheApp->Pending())
737 wxTheApp->Dispatch();
738
739 // VZ: is it the same as this (taken from old wxExecute)?
740 #if 0
741 XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
742 #endif
743
744 return TRUE;
745 }
746
747 // TODO use XmGetPixmap (?) to get the really standard icons!
748
749 #include "wx/generic/info.xpm"
750 #include "wx/generic/error.xpm"
751 #include "wx/generic/question.xpm"
752 #include "wx/generic/warning.xpm"
753
754 wxIcon
755 wxApp::GetStdIcon(int which) const
756 {
757 switch(which)
758 {
759 case wxICON_INFORMATION:
760 return wxIcon(info_xpm);
761
762 case wxICON_QUESTION:
763 return wxIcon(question_xpm);
764
765 case wxICON_EXCLAMATION:
766 return wxIcon(warning_xpm);
767
768 default:
769 wxFAIL_MSG("requested non existent standard icon");
770 // still fall through
771
772 case wxICON_HAND:
773 return wxIcon(error_xpm);
774 }
775 }
776
777 // ----------------------------------------------------------------------------
778 // accessors for C modules
779 // ----------------------------------------------------------------------------
780
781 extern "C" XtAppContext wxGetAppContext()
782 {
783 return (XtAppContext)wxTheApp->GetAppContext();
784 }