1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/app.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
24 #include "wx/dialog.h"
25 #include "wx/memory.h"
26 #include "wx/gdicmn.h"
27 #include "wx/module.h"
31 #include "wx/evtloop.h"
32 #include "wx/filename.h"
34 #include "wx/univ/theme.h"
35 #include "wx/univ/renderer.h"
36 #include "wx/generic/private/timer.h"
39 #include "wx/thread.h"
42 #include "wx/x11/private.h"
46 //------------------------------------------------------------------------
48 //------------------------------------------------------------------------
50 wxWindowHash
*wxWidgetHashTable
= NULL
;
51 wxWindowHash
*wxClientWidgetHashTable
= NULL
;
53 static bool g_showIconic
= false;
54 static wxSize g_initialSize
= wxDefaultSize
;
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
60 static wxWindow
*g_nextFocus
= NULL
;
61 static wxWindow
*g_prevFocus
= NULL
;
63 //------------------------------------------------------------------------
65 //------------------------------------------------------------------------
67 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
69 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
71 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
73 // just forward to the default handler for now
74 if (gs_pfnXErrorHandler
)
75 return gs_pfnXErrorHandler(dpy
, xevent
);
80 //------------------------------------------------------------------------
82 //------------------------------------------------------------------------
84 long wxApp::sm_lastMessageTime
= 0;
86 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
88 bool wxApp::Initialize(int& argC
, wxChar
**argV
)
91 // install the X error handler
92 gs_pfnXErrorHandler
= XSetErrorHandler( wxXErrorHandler
);
96 bool syncDisplay
= false;
99 for ( int i
= 0; i
< argCOrig
; i
++ )
101 if (wxStrcmp( argV
[i
], wxT("-display") ) == 0)
103 if (i
< (argCOrig
- 1))
107 displayName
= argV
[i
];
113 else if (wxStrcmp( argV
[i
], wxT("-geometry") ) == 0)
115 if (i
< (argCOrig
- 1))
120 if (wxSscanf(argV
[i
], wxT("%dx%d"), &w
, &h
) != 2)
122 wxLogError( _("Invalid geometry specification '%s'"),
123 wxString(argV
[i
]).c_str() );
127 g_initialSize
= wxSize(w
, h
);
134 else if (wxStrcmp( argV
[i
], wxT("-sync") ) == 0)
141 else if (wxStrcmp( argV
[i
], wxT("-iconic") ) == 0)
150 if ( argC
!= argCOrig
)
152 // remove the arguments we consumed
153 for ( int i
= 0; i
< argC
; i
++ )
157 memmove(argV
+ i
, argV
+ i
+ 1, (argCOrig
- i
)*sizeof(wxChar
*));
162 // open and set up the X11 display
163 if ( !wxSetDisplay(displayName
) )
165 wxLogError(_("wxWidgets could not open display. Exiting."));
169 Display
*dpy
= wxGlobalDisplay();
171 XSynchronize(dpy
, True
);
173 XSelectInput(dpy
, XDefaultRootWindow(dpy
), PropertyChangeMask
);
175 wxSetDetectableAutoRepeat( true );
178 if ( !wxAppBase::Initialize(argC
, argV
) )
182 // Glib's type system required by Pango
187 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
190 wxWidgetHashTable
= new wxWindowHash
;
191 wxClientWidgetHashTable
= new wxWindowHash
;
196 void wxApp::CleanUp()
198 delete wxWidgetHashTable
;
199 wxWidgetHashTable
= NULL
;
200 delete wxClientWidgetHashTable
;
201 wxClientWidgetHashTable
= NULL
;
203 wxAppBase::CleanUp();
208 m_mainColormap
= NULL
;
209 m_topLevelWidget
= NULL
;
210 m_maxRequestSize
= 0;
211 m_showIconic
= false;
212 m_initialSize
= wxDefaultSize
;
228 //-----------------------------------------------------------------------
229 // X11 predicate function for exposure compression
230 //-----------------------------------------------------------------------
235 Bool found_non_matching
;
239 Bool
wxX11ExposePredicate (Display
*WXUNUSED(display
), XEvent
*xevent
, XPointer arg
)
241 wxExposeInfo
*info
= (wxExposeInfo
*) arg
;
243 if (info
->found_non_matching
)
246 if (xevent
->xany
.type
!= Expose
)
248 info
->found_non_matching
= true;
252 if (xevent
->xexpose
.window
!= info
->window
)
254 info
->found_non_matching
= true;
261 #endif // wxUSE_NANOX
263 //-----------------------------------------------------------------------
264 // Processes an X event, returning true if the event was processed.
265 //-----------------------------------------------------------------------
267 bool wxApp::ProcessXEvent(WXEvent
* _event
)
269 XEvent
* event
= (XEvent
*) _event
;
271 wxWindow
* win
= NULL
;
272 Window window
= XEventGetWindow(event
);
274 Window actualWindow
= window
;
277 // Find the first wxWindow that corresponds to this event window
278 // Because we're receiving events after a window
279 // has been destroyed, assume a 1:1 match between
280 // Window and wxWindow, so if it's not in the table,
281 // it must have been destroyed.
283 win
= wxGetWindowFromTable(window
);
286 #if wxUSE_TWO_WINDOWS
287 win
= wxGetClientWindowFromTable(window
);
298 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
299 if (event
->xexpose
.window
!= (Window
)win
->GetClientAreaWindow())
303 info
.window
= event
->xexpose
.window
;
304 info
.found_non_matching
= false;
305 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, wxX11ExposePredicate
, (XPointer
) &info
))
307 // Don't worry about optimizing redrawing the border etc.
309 win
->NeedUpdateNcAreaInIdle();
314 win
->GetUpdateRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
315 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
316 win
->GetClearRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
317 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
322 info
.window
= event
->xexpose
.window
;
323 info
.found_non_matching
= false;
324 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, wxX11ExposePredicate
, (XPointer
) &info
))
326 win
->GetUpdateRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
327 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
329 win
->GetClearRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
330 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
334 // This simplifies the expose and clear areas to simple
336 win
->GetUpdateRegion() = win
->GetUpdateRegion().GetBox();
337 win
->GetClearRegion() = win
->GetClearRegion().GetBox();
339 // If we only have one X11 window, always indicate
340 // that borders might have to be redrawn.
341 if (win
->X11GetMainWindow() == win
->GetClientAreaWindow())
342 win
->NeedUpdateNcAreaInIdle();
344 // Only erase background, paint in idle time.
345 win
->SendEraseEvents();
357 wxLogTrace( wxT("expose"), wxT("GraphicsExpose from %s"), win
->GetName().c_str());
359 win
->GetUpdateRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
360 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
362 win
->GetClearRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
363 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
365 if (event
->xgraphicsexpose
.count
== 0)
367 // Only erase background, paint in idle time.
368 win
->SendEraseEvents();
378 if (!win
->IsEnabled())
381 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
382 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
384 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
386 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
387 if (win
->HandleWindowEvent( keyEvent
))
390 keyEvent
.SetEventType(wxEVT_CHAR
);
391 // Do the translation again, retaining the ASCII
393 if (wxTranslateKeyEvent(keyEvent
, win
, window
, event
, true) &&
394 win
->HandleWindowEvent( keyEvent
))
397 if ( (keyEvent
.m_keyCode
== WXK_TAB
) &&
398 win
->GetParent() && (win
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
400 wxNavigationKeyEvent new_event
;
401 new_event
.SetEventObject( win
->GetParent() );
402 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
403 new_event
.SetDirection( (keyEvent
.m_keyCode
== WXK_TAB
) );
404 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
405 new_event
.SetWindowChange( keyEvent
.ControlDown() );
406 new_event
.SetCurrentFocus( win
);
407 return win
->GetParent()->HandleWindowEvent( new_event
);
414 if (!win
->IsEnabled())
417 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
418 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
420 return win
->HandleWindowEvent( keyEvent
);
422 case ConfigureNotify
:
425 if (event
->update
.utype
== GR_UPDATE_SIZE
)
428 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
431 tlw
->SetConfigureGeometry( XConfigureEventGetX(event
), XConfigureEventGetY(event
),
432 XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
) );
435 if ( tlw
&& tlw
->IsShown() )
437 tlw
->SetNeedResizeInIdle();
441 wxSizeEvent
sizeEvent( wxSize(XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
)), win
->GetId() );
442 sizeEvent
.SetEventObject( win
);
444 return win
->HandleWindowEvent( sizeEvent
);
451 return HandlePropertyChange(_event
);
455 if (!win
->IsEnabled())
458 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);
459 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);
461 if (event
->xclient
.message_type
== wm_protocols
)
463 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
474 printf( "destroy from %s\n", win
->GetName().c_str() );
479 printf( "create from %s\n", win
->GetName().c_str() );
484 printf( "map request from %s\n", win
->GetName().c_str() );
489 printf( "resize request from %s\n", win
->GetName().c_str() );
491 Display
*disp
= (Display
*) wxGetDisplay();
496 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
498 wxSize sz
= win
->GetSize();
499 wxSizeEvent
sizeEvent(sz
, win
->GetId());
500 sizeEvent
.SetEventObject(win
);
502 return win
->HandleWindowEvent( sizeEvent
);
507 case GR_EVENT_TYPE_CLOSE_REQ
:
524 if (!win
->IsEnabled())
527 // Here we check if the top level window is
528 // disabled, which is one aspect of modality.
530 while (tlw
&& !tlw
->IsTopLevel())
531 tlw
= tlw
->GetParent();
532 if (tlw
&& !tlw
->IsEnabled())
535 if (event
->type
== ButtonPress
)
537 if ((win
!= wxWindow::FindFocus()) && win
->CanAcceptFocus())
539 // This might actually be done in wxWindow::SetFocus()
540 // and not here. TODO.
541 g_prevFocus
= wxWindow::FindFocus();
544 wxLogTrace( wxT("focus"), wxT("About to call SetFocus on %s of type %s due to button press"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
546 // Record the fact that this window is
547 // getting the focus, because we'll need to
548 // check if its parent is getting a bogus
549 // focus and duly ignore it.
550 // TODO: may need to have this code in SetFocus, too.
551 extern wxWindow
* g_GettingFocus
;
552 g_GettingFocus
= win
;
558 if (event
->type
== LeaveNotify
|| event
->type
== EnterNotify
)
560 // Throw out NotifyGrab and NotifyUngrab
561 if (event
->xcrossing
.mode
!= NotifyNormal
)
565 wxMouseEvent wxevent
;
566 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
567 return win
->HandleWindowEvent( wxevent
);
571 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
572 (event
->xfocus
.mode
== NotifyNormal
))
575 wxLogTrace( wxT("focus"), wxT("FocusIn from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
577 extern wxWindow
* g_GettingFocus
;
578 if (g_GettingFocus
&& g_GettingFocus
->GetParent() == win
)
580 // Ignore this, this can be a spurious FocusIn
581 // caused by a child having its focus set.
582 g_GettingFocus
= NULL
;
583 wxLogTrace( wxT("focus"), wxT("FocusIn from %s of type %s being deliberately ignored"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
588 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
589 focusEvent
.SetEventObject(win
);
590 focusEvent
.SetWindow( g_prevFocus
);
593 return win
->HandleWindowEvent(focusEvent
);
600 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
601 (event
->xfocus
.mode
== NotifyNormal
))
604 wxLogTrace( wxT("focus"), wxT("FocusOut from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
606 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
607 focusEvent
.SetEventObject(win
);
608 focusEvent
.SetWindow( g_nextFocus
);
610 return win
->HandleWindowEvent(focusEvent
);
618 // This should be redefined in a derived class for
619 // handling property change events for XAtom IPC.
620 bool wxApp::HandlePropertyChange(WXEvent
*WXUNUSED(event
))
622 // by default do nothing special
623 // TODO: what to do for X11
624 // XtDispatchEvent((XEvent*) event);
628 void wxApp::WakeUpIdle()
630 // TODO: use wxMotif implementation?
632 // Wake up the idle handler processor, even if it is in another thread...
636 // Create display, and other initialization
637 bool wxApp::OnInitGui()
640 // Eventually this line will be removed, but for
641 // now we don't want to try popping up a dialog
642 // for error messages.
643 delete wxLog::SetActiveTarget(new wxLogStderr
);
646 if (!wxAppBase::OnInitGui())
649 Display
*dpy
= wxGlobalDisplay();
650 GetMainColormap(dpy
);
652 m_maxRequestSize
= XMaxRequestSize(dpy
);
655 m_visualInfo
= new wxXVisualInfo
;
656 wxFillXVisualInfo(m_visualInfo
, dpy
);
664 #include <pango/pango.h>
665 #include <pango/pangox.h>
666 #ifdef HAVE_PANGO_XFT
667 #include <pango/pangoxft.h>
670 PangoContext
* wxApp::GetPangoContext()
672 static PangoContext
*s_pangoContext
= NULL
;
673 if ( !s_pangoContext
)
675 Display
*dpy
= wxGlobalDisplay();
677 #ifdef HAVE_PANGO_XFT
678 int xscreen
= DefaultScreen(dpy
);
679 static int use_xft
= -1;
682 wxString val
= wxGetenv( L
"GDK_USE_XFT" );
683 use_xft
= val
== L
"1";
687 s_pangoContext
= pango_xft_get_context(dpy
, xscreen
);
689 #endif // HAVE_PANGO_XFT
690 s_pangoContext
= pango_x_get_context(dpy
);
692 if (!PANGO_IS_CONTEXT(s_pangoContext
))
694 wxLogError( wxT("No pango context.") );
698 return s_pangoContext
;
701 #endif // wxUSE_UNICODE
703 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
705 if (!display
) /* Must be called first with non-NULL display */
706 return m_mainColormap
;
708 int defaultScreen
= DefaultScreen((Display
*) display
);
709 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
711 Colormap c
= DefaultColormapOfScreen(screen
);
714 m_mainColormap
= (WXColormap
) c
;
716 return (WXColormap
) c
;
719 Window
wxGetWindowParent(Window window
)
721 wxASSERT_MSG( window
, wxT("invalid window") );
726 // VMS chokes on unreacheable code
727 Window parent
, root
= 0;
731 unsigned int noChildren
= 0;
733 Window
* children
= NULL
;
735 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
740 XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
741 & children
, & noChildren
);
755 wxAppConsole::Exit();