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