]> git.saurik.com Git - wxWidgets.git/blob - src/x11/app.cpp
adding x server license terms for polygon methods
[wxWidgets.git] / src / x11 / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/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 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #include "wx/app.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/hash.h"
19 #include "wx/intl.h"
20 #include "wx/log.h"
21 #include "wx/utils.h"
22 #include "wx/frame.h"
23 #include "wx/icon.h"
24 #include "wx/dialog.h"
25 #include "wx/memory.h"
26 #include "wx/gdicmn.h"
27 #include "wx/module.h"
28 #include "wx/crt.h"
29 #endif
30
31 #include "wx/evtloop.h"
32 #include "wx/filename.h"
33
34 #include "wx/univ/theme.h"
35 #include "wx/univ/renderer.h"
36 #include "wx/generic/private/timer.h"
37
38 #if wxUSE_THREADS
39 #include "wx/thread.h"
40 #endif
41
42 #include "wx/x11/private.h"
43
44 #include <string.h>
45
46 //------------------------------------------------------------------------
47 // global data
48 //------------------------------------------------------------------------
49
50 wxWindowHash *wxWidgetHashTable = NULL;
51 wxWindowHash *wxClientWidgetHashTable = NULL;
52
53 static bool g_showIconic = false;
54 static wxSize g_initialSize = wxDefaultSize;
55
56 // This is required for wxFocusEvent::SetWindow(). It will only
57 // work for focus events which we provoke ourselves (by calling
58 // SetFocus()). It will not work for those events, which X11
59 // generates itself.
60 static wxWindow *g_nextFocus = NULL;
61 static wxWindow *g_prevFocus = NULL;
62
63 //------------------------------------------------------------------------
64 // X11 error handling
65 //------------------------------------------------------------------------
66
67 typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *);
68
69 XErrorHandlerFunc gs_pfnXErrorHandler = 0;
70
71 static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent)
72 {
73 // just forward to the default handler for now
74 if (gs_pfnXErrorHandler)
75 return gs_pfnXErrorHandler(dpy, xevent);
76 else
77 return 0;
78 }
79
80 //------------------------------------------------------------------------
81 // wxApp
82 //------------------------------------------------------------------------
83
84 long wxApp::sm_lastMessageTime = 0;
85
86 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
87
88 bool wxApp::Initialize(int& argC, wxChar **argV)
89 {
90 #if !wxUSE_NANOX
91 // install the X error handler
92 gs_pfnXErrorHandler = XSetErrorHandler( wxXErrorHandler );
93 #endif
94
95 wxString displayName;
96 bool syncDisplay = false;
97
98 int argCOrig = argC;
99 for ( int i = 0; i < argCOrig; i++ )
100 {
101 if (wxStrcmp( argV[i], wxT("-display") ) == 0)
102 {
103 if (i < (argCOrig - 1))
104 {
105 argV[i++] = NULL;
106
107 displayName = argV[i];
108
109 argV[i] = NULL;
110 argC -= 2;
111 }
112 }
113 else if (wxStrcmp( argV[i], wxT("-geometry") ) == 0)
114 {
115 if (i < (argCOrig - 1))
116 {
117 argV[i++] = NULL;
118
119 int w, h;
120 if (wxSscanf(argV[i], wxT("%dx%d"), &w, &h) != 2)
121 {
122 wxLogError( _("Invalid geometry specification '%s'"),
123 wxString(argV[i]).c_str() );
124 }
125 else
126 {
127 g_initialSize = wxSize(w, h);
128 }
129
130 argV[i] = NULL;
131 argC -= 2;
132 }
133 }
134 else if (wxStrcmp( argV[i], wxT("-sync") ) == 0)
135 {
136 syncDisplay = true;
137
138 argV[i] = NULL;
139 argC--;
140 }
141 else if (wxStrcmp( argV[i], wxT("-iconic") ) == 0)
142 {
143 g_showIconic = true;
144
145 argV[i] = NULL;
146 argC--;
147 }
148 }
149
150 if ( argC != argCOrig )
151 {
152 // remove the arguments we consumed
153 for ( int i = 0; i < argC; i++ )
154 {
155 while ( !argV[i] )
156 {
157 memmove(argV + i, argV + i + 1, (argCOrig - i)*sizeof(wxChar *));
158 }
159 }
160 }
161
162 // open and set up the X11 display
163 if ( !wxSetDisplay(displayName) )
164 {
165 wxLogError(_("wxWidgets could not open display. Exiting."));
166 return false;
167 }
168
169 Display *dpy = wxGlobalDisplay();
170 if (syncDisplay)
171 XSynchronize(dpy, True);
172
173 XSelectInput(dpy, XDefaultRootWindow(dpy), PropertyChangeMask);
174
175 wxSetDetectableAutoRepeat( true );
176
177
178 if ( !wxAppBase::Initialize(argC, argV) )
179 return false;
180
181 #if wxUSE_UNICODE
182 // Glib's type system required by Pango
183 g_type_init();
184 #endif
185
186 #if wxUSE_INTL
187 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
188 #endif
189
190 wxWidgetHashTable = new wxWindowHash;
191 wxClientWidgetHashTable = new wxWindowHash;
192
193 return true;
194 }
195
196 void wxApp::CleanUp()
197 {
198 wxDELETE(wxWidgetHashTable);
199 wxDELETE(wxClientWidgetHashTable);
200
201 wxAppBase::CleanUp();
202 }
203
204 wxApp::wxApp()
205 {
206 m_mainColormap = NULL;
207 m_topLevelWidget = NULL;
208 m_maxRequestSize = 0;
209 m_showIconic = false;
210 m_initialSize = wxDefaultSize;
211
212 #if !wxUSE_NANOX
213 m_visualInfo = NULL;
214 #endif
215 }
216
217 wxApp::~wxApp()
218 {
219 #if !wxUSE_NANOX
220 delete m_visualInfo;
221 #endif
222 }
223
224 #if !wxUSE_NANOX
225
226 //-----------------------------------------------------------------------
227 // X11 predicate function for exposure compression
228 //-----------------------------------------------------------------------
229
230 struct wxExposeInfo
231 {
232 Window window;
233 Bool found_non_matching;
234 };
235
236 extern "C"
237 Bool wxX11ExposePredicate (Display *WXUNUSED(display), XEvent *xevent, XPointer arg)
238 {
239 wxExposeInfo *info = (wxExposeInfo*) arg;
240
241 if (info->found_non_matching)
242 return FALSE;
243
244 if (xevent->xany.type != Expose)
245 {
246 info->found_non_matching = true;
247 return FALSE;
248 }
249
250 if (xevent->xexpose.window != info->window)
251 {
252 info->found_non_matching = true;
253 return FALSE;
254 }
255
256 return TRUE;
257 }
258
259 #endif // wxUSE_NANOX
260
261 //-----------------------------------------------------------------------
262 // Processes an X event, returning true if the event was processed.
263 //-----------------------------------------------------------------------
264
265 bool wxApp::ProcessXEvent(WXEvent* _event)
266 {
267 XEvent* event = (XEvent*) _event;
268
269 wxWindow* win = NULL;
270 Window window = XEventGetWindow(event);
271 #if 0
272 Window actualWindow = window;
273 #endif
274
275 // Find the first wxWindow that corresponds to this event window
276 // Because we're receiving events after a window
277 // has been destroyed, assume a 1:1 match between
278 // Window and wxWindow, so if it's not in the table,
279 // it must have been destroyed.
280
281 win = wxGetWindowFromTable(window);
282 if (!win)
283 {
284 #if wxUSE_TWO_WINDOWS
285 win = wxGetClientWindowFromTable(window);
286 if (!win)
287 #endif
288 return false;
289 }
290
291
292 switch (event->type)
293 {
294 case Expose:
295 {
296 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
297 if (event->xexpose.window != (Window)win->GetClientAreaWindow())
298 {
299 XEvent tmp_event;
300 wxExposeInfo info;
301 info.window = event->xexpose.window;
302 info.found_non_matching = false;
303 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event, wxX11ExposePredicate, (XPointer) &info ))
304 {
305 // Don't worry about optimizing redrawing the border etc.
306 }
307 win->NeedUpdateNcAreaInIdle();
308 }
309 else
310 #endif
311 {
312 win->GetUpdateRegion().Union( XExposeEventGetX(event), XExposeEventGetY(event),
313 XExposeEventGetWidth(event), XExposeEventGetHeight(event));
314 win->GetClearRegion().Union( XExposeEventGetX(event), XExposeEventGetY(event),
315 XExposeEventGetWidth(event), XExposeEventGetHeight(event));
316
317 #if !wxUSE_NANOX
318 XEvent tmp_event;
319 wxExposeInfo info;
320 info.window = event->xexpose.window;
321 info.found_non_matching = false;
322 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event, wxX11ExposePredicate, (XPointer) &info ))
323 {
324 win->GetUpdateRegion().Union( tmp_event.xexpose.x, tmp_event.xexpose.y,
325 tmp_event.xexpose.width, tmp_event.xexpose.height );
326
327 win->GetClearRegion().Union( tmp_event.xexpose.x, tmp_event.xexpose.y,
328 tmp_event.xexpose.width, tmp_event.xexpose.height );
329 }
330 #endif
331
332 // This simplifies the expose and clear areas to simple
333 // rectangles.
334 win->GetUpdateRegion() = win->GetUpdateRegion().GetBox();
335 win->GetClearRegion() = win->GetClearRegion().GetBox();
336
337 // If we only have one X11 window, always indicate
338 // that borders might have to be redrawn.
339 if (win->X11GetMainWindow() == win->GetClientAreaWindow())
340 win->NeedUpdateNcAreaInIdle();
341
342 // Only erase background, paint in idle time.
343 win->SendEraseEvents();
344
345 // EXPERIMENT
346 //win->Update();
347 }
348
349 return true;
350 }
351
352 #if !wxUSE_NANOX
353 case GraphicsExpose:
354 {
355 wxLogTrace( wxT("expose"), wxT("GraphicsExpose from %s"), win->GetName().c_str());
356
357 win->GetUpdateRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y,
358 event->xgraphicsexpose.width, event->xgraphicsexpose.height);
359
360 win->GetClearRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y,
361 event->xgraphicsexpose.width, event->xgraphicsexpose.height);
362
363 if (event->xgraphicsexpose.count == 0)
364 {
365 // Only erase background, paint in idle time.
366 win->SendEraseEvents();
367 // win->Update();
368 }
369
370 return true;
371 }
372 #endif
373
374 case KeyPress:
375 {
376 if (!win->IsEnabled())
377 return false;
378
379 wxKeyEvent keyEvent(wxEVT_KEY_DOWN);
380 wxTranslateKeyEvent(keyEvent, win, window, event);
381
382 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
383
384 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
385 if (win->HandleWindowEvent( keyEvent ))
386 return true;
387
388 keyEvent.SetEventType(wxEVT_CHAR);
389 // Do the translation again, retaining the ASCII
390 // code.
391 if (wxTranslateKeyEvent(keyEvent, win, window, event, true) &&
392 win->HandleWindowEvent( keyEvent ))
393 return true;
394
395 if ( (keyEvent.m_keyCode == WXK_TAB) &&
396 win->GetParent() && (win->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
397 {
398 wxNavigationKeyEvent new_event;
399 new_event.SetEventObject( win->GetParent() );
400 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
401 new_event.SetDirection( (keyEvent.m_keyCode == WXK_TAB) );
402 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
403 new_event.SetWindowChange( keyEvent.ControlDown() );
404 new_event.SetCurrentFocus( win );
405 return win->GetParent()->HandleWindowEvent( new_event );
406 }
407
408 return false;
409 }
410 case KeyRelease:
411 {
412 if (!win->IsEnabled())
413 return false;
414
415 wxKeyEvent keyEvent(wxEVT_KEY_UP);
416 wxTranslateKeyEvent(keyEvent, win, window, event);
417
418 return win->HandleWindowEvent( keyEvent );
419 }
420 case ConfigureNotify:
421 {
422 #if wxUSE_NANOX
423 if (event->update.utype == GR_UPDATE_SIZE)
424 #endif
425 {
426 wxTopLevelWindow *tlw = wxDynamicCast(win, wxTopLevelWindow);
427 if ( tlw )
428 {
429 tlw->SetConfigureGeometry( XConfigureEventGetX(event), XConfigureEventGetY(event),
430 XConfigureEventGetWidth(event), XConfigureEventGetHeight(event) );
431 }
432
433 if ( tlw && tlw->IsShown() )
434 {
435 tlw->SetNeedResizeInIdle();
436 }
437 else
438 {
439 wxSizeEvent sizeEvent( wxSize(XConfigureEventGetWidth(event), XConfigureEventGetHeight(event)), win->GetId() );
440 sizeEvent.SetEventObject( win );
441
442 return win->HandleWindowEvent( sizeEvent );
443 }
444 }
445 return false;
446 }
447 #if !wxUSE_NANOX
448 case PropertyNotify:
449 return HandlePropertyChange(_event);
450
451 case ClientMessage:
452 {
453 if (!win->IsEnabled())
454 return false;
455
456 Atom wm_delete_window = XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True);
457 Atom wm_protocols = XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True);
458
459 if (event->xclient.message_type == wm_protocols)
460 {
461 if ((Atom) (event->xclient.data.l[0]) == wm_delete_window)
462 {
463 win->Close(false);
464 return true;
465 }
466 }
467 return false;
468 }
469 #if 0
470 case DestroyNotify:
471 {
472 printf( "destroy from %s\n", win->GetName().c_str() );
473 break;
474 }
475 case CreateNotify:
476 {
477 printf( "create from %s\n", win->GetName().c_str() );
478 break;
479 }
480 case MapRequest:
481 {
482 printf( "map request from %s\n", win->GetName().c_str() );
483 break;
484 }
485 case ResizeRequest:
486 {
487 printf( "resize request from %s\n", win->GetName().c_str() );
488
489 Display *disp = (Display*) wxGetDisplay();
490 XEvent report;
491
492 // to avoid flicker
493 report = * event;
494 while( XCheckTypedWindowEvent (disp, actualWindow, ResizeRequest, &report));
495
496 wxSize sz = win->GetSize();
497 wxSizeEvent sizeEvent(sz, win->GetId());
498 sizeEvent.SetEventObject(win);
499
500 return win->HandleWindowEvent( sizeEvent );
501 }
502 #endif
503 #endif
504 #if wxUSE_NANOX
505 case GR_EVENT_TYPE_CLOSE_REQ:
506 {
507 if (win)
508 {
509 win->Close(false);
510 return true;
511 }
512 return false;
513 break;
514 }
515 #endif
516 case EnterNotify:
517 case LeaveNotify:
518 case ButtonPress:
519 case ButtonRelease:
520 case MotionNotify:
521 {
522 if (!win->IsEnabled())
523 return false;
524
525 // Here we check if the top level window is
526 // disabled, which is one aspect of modality.
527 wxWindow *tlw = win;
528 while (tlw && !tlw->IsTopLevel())
529 tlw = tlw->GetParent();
530 if (tlw && !tlw->IsEnabled())
531 return false;
532
533 if (event->type == ButtonPress)
534 {
535 if ((win != wxWindow::FindFocus()) && win->CanAcceptFocus())
536 {
537 // This might actually be done in wxWindow::SetFocus()
538 // and not here. TODO.
539 g_prevFocus = wxWindow::FindFocus();
540 g_nextFocus = win;
541
542 wxLogTrace( wxT("focus"), wxT("About to call SetFocus on %s of type %s due to button press"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
543
544 // Record the fact that this window is
545 // getting the focus, because we'll need to
546 // check if its parent is getting a bogus
547 // focus and duly ignore it.
548 // TODO: may need to have this code in SetFocus, too.
549 extern wxWindow* g_GettingFocus;
550 g_GettingFocus = win;
551 win->SetFocus();
552 }
553 }
554
555 #if !wxUSE_NANOX
556 if (event->type == LeaveNotify || event->type == EnterNotify)
557 {
558 // Throw out NotifyGrab and NotifyUngrab
559 if (event->xcrossing.mode != NotifyNormal)
560 return false;
561 }
562 #endif
563 wxMouseEvent wxevent;
564 wxTranslateMouseEvent(wxevent, win, window, event);
565 return win->HandleWindowEvent( wxevent );
566 }
567 case FocusIn:
568 #if !wxUSE_NANOX
569 if ((event->xfocus.detail != NotifyPointer) &&
570 (event->xfocus.mode == NotifyNormal))
571 #endif
572 {
573 wxLogTrace( wxT("focus"), wxT("FocusIn from %s of type %s"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
574
575 extern wxWindow* g_GettingFocus;
576 if (g_GettingFocus && g_GettingFocus->GetParent() == win)
577 {
578 // Ignore this, this can be a spurious FocusIn
579 // caused by a child having its focus set.
580 g_GettingFocus = NULL;
581 wxLogTrace( wxT("focus"), wxT("FocusIn from %s of type %s being deliberately ignored"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
582 return true;
583 }
584 else
585 {
586 wxFocusEvent focusEvent(wxEVT_SET_FOCUS, win->GetId());
587 focusEvent.SetEventObject(win);
588 focusEvent.SetWindow( g_prevFocus );
589 g_prevFocus = NULL;
590
591 return win->HandleWindowEvent(focusEvent);
592 }
593 }
594 return false;
595
596 case FocusOut:
597 #if !wxUSE_NANOX
598 if ((event->xfocus.detail != NotifyPointer) &&
599 (event->xfocus.mode == NotifyNormal))
600 #endif
601 {
602 wxLogTrace( wxT("focus"), wxT("FocusOut from %s of type %s"), win->GetName().c_str(), win->GetClassInfo()->GetClassName() );
603
604 wxFocusEvent focusEvent(wxEVT_KILL_FOCUS, win->GetId());
605 focusEvent.SetEventObject(win);
606 focusEvent.SetWindow( g_nextFocus );
607 g_nextFocus = NULL;
608 return win->HandleWindowEvent(focusEvent);
609 }
610 return false;
611 }
612
613 return false;
614 }
615
616 // This should be redefined in a derived class for
617 // handling property change events for XAtom IPC.
618 bool wxApp::HandlePropertyChange(WXEvent *WXUNUSED(event))
619 {
620 // by default do nothing special
621 // TODO: what to do for X11
622 // XtDispatchEvent((XEvent*) event);
623 return false;
624 }
625
626 void wxApp::WakeUpIdle()
627 {
628 // TODO: use wxMotif implementation?
629
630 // Wake up the idle handler processor, even if it is in another thread...
631 }
632
633
634 // Create display, and other initialization
635 bool wxApp::OnInitGui()
636 {
637 #if wxUSE_LOG
638 // Eventually this line will be removed, but for
639 // now we don't want to try popping up a dialog
640 // for error messages.
641 delete wxLog::SetActiveTarget(new wxLogStderr);
642 #endif
643
644 if (!wxAppBase::OnInitGui())
645 return false;
646
647 Display *dpy = wxGlobalDisplay();
648 GetMainColormap(dpy);
649
650 m_maxRequestSize = XMaxRequestSize(dpy);
651
652 #if !wxUSE_NANOX
653 m_visualInfo = new wxXVisualInfo;
654 wxFillXVisualInfo(m_visualInfo, dpy);
655 #endif
656
657 return true;
658 }
659
660 #if wxUSE_UNICODE
661
662 #include <pango/pango.h>
663 #include <pango/pangox.h>
664 #ifdef HAVE_PANGO_XFT
665 #include <pango/pangoxft.h>
666 #endif
667
668 PangoContext* wxApp::GetPangoContext()
669 {
670 static PangoContext *s_pangoContext = NULL;
671 if ( !s_pangoContext )
672 {
673 Display *dpy = wxGlobalDisplay();
674
675 #ifdef HAVE_PANGO_XFT
676 int xscreen = DefaultScreen(dpy);
677 static int use_xft = -1;
678 if (use_xft == -1)
679 {
680 wxString val = wxGetenv( L"GDK_USE_XFT" );
681 use_xft = val == L"1";
682 }
683
684 if (use_xft)
685 s_pangoContext = pango_xft_get_context(dpy, xscreen);
686 else
687 #endif // HAVE_PANGO_XFT
688 s_pangoContext = pango_x_get_context(dpy);
689
690 if (!PANGO_IS_CONTEXT(s_pangoContext))
691 {
692 wxLogError( wxT("No pango context.") );
693 }
694 }
695
696 return s_pangoContext;
697 }
698
699 #endif // wxUSE_UNICODE
700
701 WXColormap wxApp::GetMainColormap(WXDisplay* display)
702 {
703 if (!display) /* Must be called first with non-NULL display */
704 return m_mainColormap;
705
706 int defaultScreen = DefaultScreen((Display*) display);
707 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
708
709 Colormap c = DefaultColormapOfScreen(screen);
710
711 if (!m_mainColormap)
712 m_mainColormap = (WXColormap) c;
713
714 return (WXColormap) c;
715 }
716
717 Window wxGetWindowParent(Window window)
718 {
719 wxASSERT_MSG( window, wxT("invalid window") );
720
721 return (Window) 0;
722
723 #ifndef __VMS
724 // VMS chokes on unreacheable code
725 Window parent, root = 0;
726 #if wxUSE_NANOX
727 int noChildren = 0;
728 #else
729 unsigned int noChildren = 0;
730 #endif
731 Window* children = NULL;
732
733 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
734 int res = 1;
735 #if !wxUSE_NANOX
736 res =
737 #endif
738 XQueryTree((Display*) wxGetDisplay(), window, & root, & parent,
739 & children, & noChildren);
740 if (children)
741 XFree(children);
742 if (res)
743 return parent;
744 else
745 return (Window) 0;
746 #endif
747 }
748
749 void wxApp::Exit()
750 {
751 wxApp::CleanUp();
752
753 wxAppConsole::Exit();
754 }
755