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