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 wxDELETE(wxWidgetHashTable
);
199 wxDELETE(wxClientWidgetHashTable
);
201 wxAppBase::CleanUp();
206 m_mainColormap
= NULL
;
207 m_topLevelWidget
= NULL
;
208 m_maxRequestSize
= 0;
209 m_showIconic
= false;
210 m_initialSize
= wxDefaultSize
;
226 //-----------------------------------------------------------------------
227 // X11 predicate function for exposure compression
228 //-----------------------------------------------------------------------
233 Bool found_non_matching
;
237 Bool
wxX11ExposePredicate (Display
*WXUNUSED(display
), XEvent
*xevent
, XPointer arg
)
239 wxExposeInfo
*info
= (wxExposeInfo
*) arg
;
241 if (info
->found_non_matching
)
244 if (xevent
->xany
.type
!= Expose
)
246 info
->found_non_matching
= true;
250 if (xevent
->xexpose
.window
!= info
->window
)
252 info
->found_non_matching
= true;
259 #endif // wxUSE_NANOX
261 //-----------------------------------------------------------------------
262 // Processes an X event, returning true if the event was processed.
263 //-----------------------------------------------------------------------
265 bool wxApp::ProcessXEvent(WXEvent
* _event
)
267 XEvent
* event
= (XEvent
*) _event
;
269 wxWindow
* win
= NULL
;
270 Window window
= XEventGetWindow(event
);
272 Window actualWindow
= window
;
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.
281 win
= wxGetWindowFromTable(window
);
284 #if wxUSE_TWO_WINDOWS
285 win
= wxGetClientWindowFromTable(window
);
296 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
297 if (event
->xexpose
.window
!= (Window
)win
->GetClientAreaWindow())
301 info
.window
= event
->xexpose
.window
;
302 info
.found_non_matching
= false;
303 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, wxX11ExposePredicate
, (XPointer
) &info
))
305 // Don't worry about optimizing redrawing the border etc.
307 win
->NeedUpdateNcAreaInIdle();
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
));
320 info
.window
= event
->xexpose
.window
;
321 info
.found_non_matching
= false;
322 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, wxX11ExposePredicate
, (XPointer
) &info
))
324 win
->GetUpdateRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
325 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
327 win
->GetClearRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
328 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
332 // This simplifies the expose and clear areas to simple
334 win
->GetUpdateRegion() = win
->GetUpdateRegion().GetBox();
335 win
->GetClearRegion() = win
->GetClearRegion().GetBox();
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();
342 // Only erase background, paint in idle time.
343 win
->SendEraseEvents();
355 wxLogTrace( wxT("expose"), wxT("GraphicsExpose from %s"), win
->GetName().c_str());
357 win
->GetUpdateRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
358 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
360 win
->GetClearRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
361 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
363 if (event
->xgraphicsexpose
.count
== 0)
365 // Only erase background, paint in idle time.
366 win
->SendEraseEvents();
376 if (!win
->IsEnabled())
379 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
380 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
382 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
384 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
385 if (win
->HandleWindowEvent( keyEvent
))
388 keyEvent
.SetEventType(wxEVT_CHAR
);
389 // Do the translation again, retaining the ASCII
391 if (wxTranslateKeyEvent(keyEvent
, win
, window
, event
, true) &&
392 win
->HandleWindowEvent( keyEvent
))
395 if ( (keyEvent
.m_keyCode
== WXK_TAB
) &&
396 win
->GetParent() && (win
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
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
);
412 if (!win
->IsEnabled())
415 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
416 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
418 return win
->HandleWindowEvent( keyEvent
);
420 case ConfigureNotify
:
423 if (event
->update
.utype
== GR_UPDATE_SIZE
)
426 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
429 tlw
->SetConfigureGeometry( XConfigureEventGetX(event
), XConfigureEventGetY(event
),
430 XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
) );
433 if ( tlw
&& tlw
->IsShown() )
435 tlw
->SetNeedResizeInIdle();
439 wxSizeEvent
sizeEvent( wxSize(XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
)), win
->GetId() );
440 sizeEvent
.SetEventObject( win
);
442 return win
->HandleWindowEvent( sizeEvent
);
449 return HandlePropertyChange(_event
);
453 if (!win
->IsEnabled())
456 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);
457 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);
459 if (event
->xclient
.message_type
== wm_protocols
)
461 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
472 printf( "destroy from %s\n", win
->GetName().c_str() );
477 printf( "create from %s\n", win
->GetName().c_str() );
482 printf( "map request from %s\n", win
->GetName().c_str() );
487 printf( "resize request from %s\n", win
->GetName().c_str() );
489 Display
*disp
= (Display
*) wxGetDisplay();
494 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
496 wxSize sz
= win
->GetSize();
497 wxSizeEvent
sizeEvent(sz
, win
->GetId());
498 sizeEvent
.SetEventObject(win
);
500 return win
->HandleWindowEvent( sizeEvent
);
505 case GR_EVENT_TYPE_CLOSE_REQ
:
522 if (!win
->IsEnabled())
525 // Here we check if the top level window is
526 // disabled, which is one aspect of modality.
528 while (tlw
&& !tlw
->IsTopLevel())
529 tlw
= tlw
->GetParent();
530 if (tlw
&& !tlw
->IsEnabled())
533 if (event
->type
== ButtonPress
)
535 if ((win
!= wxWindow::FindFocus()) && win
->CanAcceptFocus())
537 // This might actually be done in wxWindow::SetFocus()
538 // and not here. TODO.
539 g_prevFocus
= wxWindow::FindFocus();
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() );
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
;
556 if (event
->type
== LeaveNotify
|| event
->type
== EnterNotify
)
558 // Throw out NotifyGrab and NotifyUngrab
559 if (event
->xcrossing
.mode
!= NotifyNormal
)
563 wxMouseEvent wxevent
;
564 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
565 return win
->HandleWindowEvent( wxevent
);
569 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
570 (event
->xfocus
.mode
== NotifyNormal
))
573 wxLogTrace( wxT("focus"), wxT("FocusIn from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
575 extern wxWindow
* g_GettingFocus
;
576 if (g_GettingFocus
&& g_GettingFocus
->GetParent() == win
)
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() );
586 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
587 focusEvent
.SetEventObject(win
);
588 focusEvent
.SetWindow( g_prevFocus
);
591 return win
->HandleWindowEvent(focusEvent
);
598 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
599 (event
->xfocus
.mode
== NotifyNormal
))
602 wxLogTrace( wxT("focus"), wxT("FocusOut from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
604 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
605 focusEvent
.SetEventObject(win
);
606 focusEvent
.SetWindow( g_nextFocus
);
608 return win
->HandleWindowEvent(focusEvent
);
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
))
620 // by default do nothing special
621 // TODO: what to do for X11
622 // XtDispatchEvent((XEvent*) event);
626 void wxApp::WakeUpIdle()
628 // TODO: use wxMotif implementation?
630 // Wake up the idle handler processor, even if it is in another thread...
634 // Create display, and other initialization
635 bool wxApp::OnInitGui()
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
);
644 if (!wxAppBase::OnInitGui())
647 Display
*dpy
= wxGlobalDisplay();
648 GetMainColormap(dpy
);
650 m_maxRequestSize
= XMaxRequestSize(dpy
);
653 m_visualInfo
= new wxXVisualInfo
;
654 wxFillXVisualInfo(m_visualInfo
, dpy
);
662 #include <pango/pango.h>
663 #include <pango/pangox.h>
664 #ifdef HAVE_PANGO_XFT
665 #include <pango/pangoxft.h>
668 PangoContext
* wxApp::GetPangoContext()
670 static PangoContext
*s_pangoContext
= NULL
;
671 if ( !s_pangoContext
)
673 Display
*dpy
= wxGlobalDisplay();
675 #ifdef HAVE_PANGO_XFT
676 int xscreen
= DefaultScreen(dpy
);
677 static int use_xft
= -1;
680 wxString val
= wxGetenv( L
"GDK_USE_XFT" );
681 use_xft
= val
== L
"1";
685 s_pangoContext
= pango_xft_get_context(dpy
, xscreen
);
687 #endif // HAVE_PANGO_XFT
688 s_pangoContext
= pango_x_get_context(dpy
);
690 if (!PANGO_IS_CONTEXT(s_pangoContext
))
692 wxLogError( wxT("No pango context.") );
696 return s_pangoContext
;
699 #endif // wxUSE_UNICODE
701 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
703 if (!display
) /* Must be called first with non-NULL display */
704 return m_mainColormap
;
706 int defaultScreen
= DefaultScreen((Display
*) display
);
707 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
709 Colormap c
= DefaultColormapOfScreen(screen
);
712 m_mainColormap
= (WXColormap
) c
;
714 return (WXColormap
) c
;
717 Window
wxGetWindowParent(Window window
)
719 wxASSERT_MSG( window
, wxT("invalid window") );
724 // VMS chokes on unreacheable code
725 Window parent
, root
= 0;
729 unsigned int noChildren
= 0;
731 Window
* children
= NULL
;
733 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
738 XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
739 & children
, & noChildren
);
753 wxAppConsole::Exit();