]> git.saurik.com Git - wxWidgets.git/blob - src/motif/app.cpp
added wxUSE_NOTEBOOK to include/wx/msw/setup0.h, removed wxUSE_RADIOBUTTON (we
[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 #ifdef __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 // 'Garbage' collection of windows deleted with Close().
481 DeletePendingObjects();
482
483 #if wxUSE_THREADS
484 // Flush pending events.
485 ProcessPendingEvents();
486 #endif
487
488 // flush the logged messages if any
489 wxLog *pLog = wxLog::GetActiveTarget();
490 if ( pLog != NULL && pLog->HasPendingMessages() )
491 pLog->Flush();
492
493 // Send OnIdle events to all windows
494 bool needMore = SendIdleEvents();
495
496 if (needMore)
497 event.RequestMore(TRUE);
498
499 inOnIdle = FALSE;
500 }
501
502 void wxWakeUpIdle()
503 {
504 // **** please implement me! ****
505 // Wake up the idle handler processor, even if it is in another thread...
506 }
507
508
509 // Send idle event to all top-level windows
510 bool wxApp::SendIdleEvents()
511 {
512 bool needMore = FALSE;
513
514 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
515 while (node)
516 {
517 wxWindow* win = node->GetData();
518 if (SendIdleEvents(win))
519 needMore = TRUE;
520 node = node->GetNext();
521 }
522
523 return needMore;
524 }
525
526 // Send idle event to window and all subwindows
527 bool wxApp::SendIdleEvents(wxWindow* win)
528 {
529 bool needMore = FALSE;
530
531 wxIdleEvent event;
532 event.SetEventObject(win);
533 win->ProcessEvent(event);
534
535 if (event.MoreRequested())
536 needMore = TRUE;
537
538 wxNode* node = win->GetChildren().First();
539 while (node)
540 {
541 wxWindow* win = (wxWindow*) node->Data();
542 if (SendIdleEvents(win))
543 needMore = TRUE;
544
545 node = node->Next();
546 }
547 return needMore ;
548 }
549
550 void wxApp::DeletePendingObjects()
551 {
552 wxNode *node = wxPendingDelete.First();
553 while (node)
554 {
555 wxObject *obj = (wxObject *)node->Data();
556
557 delete obj;
558
559 if (wxPendingDelete.Member(obj))
560 delete node;
561
562 // Deleting one object may have deleted other pending
563 // objects, so start from beginning of list again.
564 node = wxPendingDelete.First();
565 }
566 }
567
568 // Create an application context
569 bool wxApp::OnInitGui()
570 {
571 XtToolkitInitialize() ;
572 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext() ;
573 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
574 (const char*) wxTheApp->GetClassName(), NULL, 0,
575 # if XtSpecificationRelease < 5
576 (Cardinal*) &argc,
577 # else
578 &argc,
579 # endif
580 argv);
581
582 if (!dpy) {
583 wxString className(wxTheApp->GetClassName());
584 wxLogError(_("wxWindows could not open display for '%s': exiting."),
585 (const char*) className);
586 exit(-1);
587 }
588 m_initialDisplay = (WXDisplay*) dpy;
589
590 #ifdef __WXDEBUG__
591 // install the X error handler
592 gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler);
593 #endif // __WXDEBUG__
594
595 wxTheApp->m_topLevelWidget = (WXWidget) XtAppCreateShell((String)NULL, (const char*) wxTheApp->GetClassName(),
596 applicationShellWidgetClass,dpy,
597 NULL,0) ;
598
599 // Add general resize proc
600 XtActionsRec rec;
601 rec.string = "resize";
602 rec.proc = (XtActionProc)wxWidgetResizeProc;
603 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
604
605 GetMainColormap(dpy);
606 m_maxRequestSize = XMaxRequestSize((Display*) dpy);
607
608 return TRUE;
609 }
610
611 WXColormap wxApp::GetMainColormap(WXDisplay* display)
612 {
613 if (!display) /* Must be called first with non-NULL display */
614 return m_mainColormap;
615
616 int defaultScreen = DefaultScreen((Display*) display);
617 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
618
619 Colormap c = DefaultColormapOfScreen(screen);
620
621 if (!m_mainColormap)
622 m_mainColormap = (WXColormap) c;
623
624 return (WXColormap) c;
625 }
626
627 // Returns TRUE if an accelerator has been processed
628 bool wxApp::CheckForAccelerator(WXEvent* event)
629 {
630 XEvent* xEvent = (XEvent*) event;
631 if (xEvent->xany.type == KeyPress)
632 {
633 // Find a wxWindow for this window
634 // TODO: should get display for the window, not the current display
635 Widget widget = XtWindowToWidget((Display*) wxGetDisplay(), xEvent->xany.window);
636 wxWindow* win = NULL;
637
638 // Find the first wxWindow that corresponds to this event window
639 while (widget && !(win = wxGetWindowFromTable(widget)))
640 widget = XtParent(widget);
641
642 if (!widget || !win)
643 return FALSE;
644
645 wxKeyEvent keyEvent(wxEVT_CHAR);
646 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent);
647
648 // Now we have a wxKeyEvent and we have a wxWindow.
649 // Go up the hierarchy until we find a matching accelerator,
650 // or we get to the top.
651 while (win)
652 {
653 if (win->ProcessAccelerator(keyEvent))
654 return TRUE;
655 win = win->GetParent();
656 }
657 return FALSE;
658 }
659 return FALSE;
660 }
661
662 bool wxApp::CheckForKeyDown(WXEvent* event)
663 {
664 XEvent* xEvent = (XEvent*) event;
665 if (xEvent->xany.type == KeyPress)
666 {
667 Widget widget = XtWindowToWidget((Display*) wxGetDisplay(),
668 xEvent->xany.window);
669 wxWindow* win = NULL;
670
671 // Find the first wxWindow that corresponds to this event window
672 while (widget && !(win = wxGetWindowFromTable(widget)))
673 widget = XtParent(widget);
674
675 if (!widget || !win)
676 return FALSE;
677
678 wxKeyEvent keyEvent(wxEVT_KEY_DOWN);
679 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent);
680
681 return win->ProcessEvent( keyEvent );
682 }
683
684 return FALSE;
685 }
686
687 bool wxApp::CheckForKeyUp(WXEvent* event)
688 {
689 XEvent* xEvent = (XEvent*) event;
690 if (xEvent->xany.type == KeyRelease)
691 {
692 Widget widget = XtWindowToWidget((Display*) wxGetDisplay(),
693 xEvent->xany.window);
694 wxWindow* win = NULL;
695
696 // Find the first wxWindow that corresponds to this event window
697 while (widget && !(win = wxGetWindowFromTable(widget)))
698 widget = XtParent(widget);
699
700 if (!widget || !win)
701 return FALSE;
702
703 wxKeyEvent keyEvent(wxEVT_KEY_UP);
704 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent);
705
706 return win->ProcessEvent( keyEvent );
707 }
708
709 return FALSE;
710 }
711
712 void wxExit()
713 {
714 int retValue = 0;
715 if (wxTheApp)
716 retValue = wxTheApp->OnExit();
717
718 wxApp::CleanUp();
719 /*
720 * Exit in some platform-specific way. Not recommended that the app calls this:
721 * only for emergencies.
722 */
723 exit(retValue);
724 }
725
726 // Yield to other processes
727 bool wxYield()
728 {
729 while (wxTheApp && wxTheApp->Pending())
730 wxTheApp->Dispatch();
731
732 // VZ: is it the same as this (taken from old wxExecute)?
733 #if 0
734 XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
735 #endif
736
737 return TRUE;
738 }
739
740 // TODO use XmGetPixmap (?) to get the really standard icons!
741
742 #include "wx/generic/info.xpm"
743 #include "wx/generic/error.xpm"
744 #include "wx/generic/question.xpm"
745 #include "wx/generic/warning.xpm"
746
747 wxIcon
748 wxApp::GetStdIcon(int which) const
749 {
750 switch(which)
751 {
752 case wxICON_INFORMATION:
753 return wxIcon(info_xpm);
754
755 case wxICON_QUESTION:
756 return wxIcon(question_xpm);
757
758 case wxICON_EXCLAMATION:
759 return wxIcon(warning_xpm);
760
761 default:
762 wxFAIL_MSG("requested non existent standard icon");
763 // still fall through
764
765 case wxICON_HAND:
766 return wxIcon(error_xpm);
767 }
768 }
769
770 // ----------------------------------------------------------------------------
771 // accessors for C modules
772 // ----------------------------------------------------------------------------
773
774 extern "C" XtAppContext wxGetAppContext()
775 {
776 return (XtAppContext)wxTheApp->GetAppContext();
777 }