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