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"
25 #include "wx/gdicmn.h"
27 #include "wx/dialog.h"
28 #include "wx/module.h"
29 #include "wx/memory.h"
30 #include "wx/evtloop.h"
32 #include "wx/filename.h"
34 #include "wx/univ/theme.h"
35 #include "wx/univ/renderer.h"
38 #include "wx/thread.h"
41 #include "wx/x11/private.h"
45 //------------------------------------------------------------------------
47 //------------------------------------------------------------------------
49 extern wxList wxPendingDelete
;
51 wxWindowHash
*wxWidgetHashTable
= NULL
;
52 wxWindowHash
*wxClientWidgetHashTable
= NULL
;
54 static bool g_showIconic
= false;
55 static wxSize g_initialSize
= wxDefaultSize
;
57 // This is required for wxFocusEvent::SetWindow(). It will only
58 // work for focus events which we provoke ourselves (by calling
59 // SetFocus()). It will not work for those events, which X11
61 static wxWindow
*g_nextFocus
= NULL
;
62 static wxWindow
*g_prevFocus
= NULL
;
64 //------------------------------------------------------------------------
66 //------------------------------------------------------------------------
69 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
71 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
73 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
75 // just forward to the default handler for now
76 if (gs_pfnXErrorHandler
)
77 return gs_pfnXErrorHandler(dpy
, xevent
);
83 //------------------------------------------------------------------------
85 //------------------------------------------------------------------------
87 long wxApp::sm_lastMessageTime
= 0;
88 WXDisplay
*wxApp::ms_display
= NULL
;
90 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
92 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
93 EVT_IDLE(wxAppBase::OnIdle
)
96 bool wxApp::Initialize(int& argC
, wxChar
**argV
)
98 #if defined(__WXDEBUG__) && !wxUSE_NANOX
99 // install the X error handler
100 gs_pfnXErrorHandler
= XSetErrorHandler( wxXErrorHandler
);
101 #endif // __WXDEBUG__
103 wxString displayName
;
104 bool syncDisplay
= false;
107 for ( int i
= 0; i
< argCOrig
; i
++ )
109 if (wxStrcmp( argV
[i
], _T("-display") ) == 0)
115 displayName
= argV
[i
];
121 else if (wxStrcmp( argV
[i
], _T("-geometry") ) == 0)
128 if (wxSscanf(argV
[i
], _T("%dx%d"), &w
, &h
) != 2)
130 wxLogError( _("Invalid geometry specification '%s'"),
131 wxString(argV
[i
]).c_str() );
135 g_initialSize
= wxSize(w
, h
);
142 else if (wxStrcmp( argV
[i
], _T("-sync") ) == 0)
149 else if (wxStrcmp( argV
[i
], _T("-iconic") ) == 0)
158 if ( argC
!= argCOrig
)
160 // remove the argumens we consumed
161 for ( int i
= 0; i
< argC
; i
++ )
165 memmove(argV
+ i
, argV
+ i
+ 1, argCOrig
- i
);
172 if ( displayName
.empty() )
173 xdisplay
= XOpenDisplay( NULL
);
175 xdisplay
= XOpenDisplay( displayName
.ToAscii() );
178 wxLogError( _("wxWidgets could not open display. Exiting.") );
183 XSynchronize(xdisplay
, True
);
185 ms_display
= (WXDisplay
*) xdisplay
;
187 XSelectInput( xdisplay
, XDefaultRootWindow(xdisplay
), PropertyChangeMask
);
190 wxSetDetectableAutoRepeat( true );
192 if ( !wxAppBase::Initialize(argC
, argV
) )
194 XCloseDisplay(xdisplay
);
200 // Glib's type system required by Pango
205 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
208 wxWidgetHashTable
= new wxWindowHash
;
209 wxClientWidgetHashTable
= new wxWindowHash
;
214 void wxApp::CleanUp()
216 delete wxWidgetHashTable
;
217 wxWidgetHashTable
= NULL
;
218 delete wxClientWidgetHashTable
;
219 wxClientWidgetHashTable
= NULL
;
221 wxAppBase::CleanUp();
226 // TODO: parse the command line
230 m_mainColormap
= (WXColormap
) NULL
;
231 m_topLevelWidget
= (WXWindow
) NULL
;
232 m_maxRequestSize
= 0;
233 m_showIconic
= false;
234 m_initialSize
= wxDefaultSize
;
250 //-----------------------------------------------------------------------
251 // X11 predicate function for exposure compression
252 //-----------------------------------------------------------------------
257 Bool found_non_matching
;
261 Bool
wxX11ExposePredicate (Display
*display
, XEvent
*xevent
, XPointer arg
)
263 wxExposeInfo
*info
= (wxExposeInfo
*) arg
;
265 if (info
->found_non_matching
)
268 if (xevent
->xany
.type
!= Expose
)
270 info
->found_non_matching
= true;
274 if (xevent
->xexpose
.window
!= info
->window
)
276 info
->found_non_matching
= true;
283 #endif // wxUSE_NANOX
285 //-----------------------------------------------------------------------
286 // Processes an X event, returning true if the event was processed.
287 //-----------------------------------------------------------------------
289 bool wxApp::ProcessXEvent(WXEvent
* _event
)
291 XEvent
* event
= (XEvent
*) _event
;
293 wxWindow
* win
= NULL
;
294 Window window
= XEventGetWindow(event
);
296 Window actualWindow
= window
;
299 // Find the first wxWindow that corresponds to this event window
300 // Because we're receiving events after a window
301 // has been destroyed, assume a 1:1 match between
302 // Window and wxWindow, so if it's not in the table,
303 // it must have been destroyed.
305 win
= wxGetWindowFromTable(window
);
308 #if wxUSE_TWO_WINDOWS
309 win
= wxGetClientWindowFromTable(window
);
316 wxString windowClass
= win
->GetClassInfo()->GetClassName();
323 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
324 if (event
->xexpose
.window
!= (Window
)win
->GetClientAreaWindow())
328 info
.window
= event
->xexpose
.window
;
329 info
.found_non_matching
= false;
330 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, wxX11ExposePredicate
, (XPointer
) &info
))
332 // Don't worry about optimizing redrawing the border etc.
334 win
->NeedUpdateNcAreaInIdle();
339 win
->GetUpdateRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
340 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
341 win
->GetClearRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
342 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
347 info
.window
= event
->xexpose
.window
;
348 info
.found_non_matching
= false;
349 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, wxX11ExposePredicate
, (XPointer
) &info
))
351 win
->GetUpdateRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
352 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
354 win
->GetClearRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
355 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
359 // This simplifies the expose and clear areas to simple
361 win
->GetUpdateRegion() = win
->GetUpdateRegion().GetBox();
362 win
->GetClearRegion() = win
->GetClearRegion().GetBox();
364 // If we only have one X11 window, always indicate
365 // that borders might have to be redrawn.
366 if (win
->GetMainWindow() == win
->GetClientAreaWindow())
367 win
->NeedUpdateNcAreaInIdle();
369 // Only erase background, paint in idle time.
370 win
->SendEraseEvents();
382 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win
->GetName().c_str());
384 win
->GetUpdateRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
385 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
387 win
->GetClearRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
388 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
390 if (event
->xgraphicsexpose
.count
== 0)
392 // Only erase background, paint in idle time.
393 win
->SendEraseEvents();
403 if (!win
->IsEnabled())
406 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
407 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
409 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
411 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
412 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
415 keyEvent
.SetEventType(wxEVT_CHAR
);
416 // Do the translation again, retaining the ASCII
418 wxTranslateKeyEvent(keyEvent
, win
, window
, event
, true);
419 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
422 if ( (keyEvent
.m_keyCode
== WXK_TAB
) &&
423 win
->GetParent() && (win
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
425 wxNavigationKeyEvent new_event
;
426 new_event
.SetEventObject( win
->GetParent() );
427 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
428 new_event
.SetDirection( (keyEvent
.m_keyCode
== WXK_TAB
) );
429 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
430 new_event
.SetWindowChange( keyEvent
.ControlDown() );
431 new_event
.SetCurrentFocus( win
);
432 return win
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
439 if (!win
->IsEnabled())
442 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
443 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
445 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
447 case ConfigureNotify
:
450 if (event
->update
.utype
== GR_UPDATE_SIZE
)
453 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
456 tlw
->SetConfigureGeometry( XConfigureEventGetX(event
), XConfigureEventGetY(event
),
457 XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
) );
460 if ( tlw
&& tlw
->IsShown() )
462 tlw
->SetNeedResizeInIdle();
466 wxSizeEvent
sizeEvent( wxSize(XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
)), win
->GetId() );
467 sizeEvent
.SetEventObject( win
);
469 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
477 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
478 return HandlePropertyChange(_event
);
482 if (!win
->IsEnabled())
485 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);
486 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);
488 if (event
->xclient
.message_type
== wm_protocols
)
490 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
501 printf( "destroy from %s\n", win
->GetName().c_str() );
506 printf( "create from %s\n", win
->GetName().c_str() );
511 printf( "map request from %s\n", win
->GetName().c_str() );
516 printf( "resize request from %s\n", win
->GetName().c_str() );
518 Display
*disp
= (Display
*) wxGetDisplay();
523 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
525 wxSize sz
= win
->GetSize();
526 wxSizeEvent
sizeEvent(sz
, win
->GetId());
527 sizeEvent
.SetEventObject(win
);
529 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
534 case GR_EVENT_TYPE_CLOSE_REQ
:
551 if (!win
->IsEnabled())
554 // Here we check if the top level window is
555 // disabled, which is one aspect of modality.
557 while (tlw
&& !tlw
->IsTopLevel())
558 tlw
= tlw
->GetParent();
559 if (tlw
&& !tlw
->IsEnabled())
562 if (event
->type
== ButtonPress
)
564 if ((win
!= wxWindow::FindFocus()) && win
->AcceptsFocus())
566 // This might actually be done in wxWindow::SetFocus()
567 // and not here. TODO.
568 g_prevFocus
= wxWindow::FindFocus();
571 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
573 // Record the fact that this window is
574 // getting the focus, because we'll need to
575 // check if its parent is getting a bogus
576 // focus and duly ignore it.
577 // TODO: may need to have this code in SetFocus, too.
578 extern wxWindow
* g_GettingFocus
;
579 g_GettingFocus
= win
;
585 if (event
->type
== LeaveNotify
|| event
->type
== EnterNotify
)
587 // Throw out NotifyGrab and NotifyUngrab
588 if (event
->xcrossing
.mode
!= NotifyNormal
)
592 wxMouseEvent wxevent
;
593 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
594 return win
->GetEventHandler()->ProcessEvent( wxevent
);
598 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
599 (event
->xfocus
.mode
== NotifyNormal
))
602 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
604 extern wxWindow
* g_GettingFocus
;
605 if (g_GettingFocus
&& g_GettingFocus
->GetParent() == win
)
607 // Ignore this, this can be a spurious FocusIn
608 // caused by a child having its focus set.
609 g_GettingFocus
= NULL
;
610 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
615 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
616 focusEvent
.SetEventObject(win
);
617 focusEvent
.SetWindow( g_prevFocus
);
620 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
627 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
628 (event
->xfocus
.mode
== NotifyNormal
))
631 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
633 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
634 focusEvent
.SetEventObject(win
);
635 focusEvent
.SetWindow( g_nextFocus
);
637 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
643 //wxString eventName = wxGetXEventName(XEvent& event);
644 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
646 #endif // __WXDEBUG__
652 // This should be redefined in a derived class for
653 // handling property change events for XAtom IPC.
654 bool wxApp::HandlePropertyChange(WXEvent
*event
)
656 // by default do nothing special
657 // TODO: what to do for X11
658 // XtDispatchEvent((XEvent*) event);
662 void wxApp::WakeUpIdle()
664 // TODO: use wxMotif implementation?
666 // Wake up the idle handler processor, even if it is in another thread...
670 // Create display, and other initialization
671 bool wxApp::OnInitGui()
673 // Eventually this line will be removed, but for
674 // now we don't want to try popping up a dialog
675 // for error messages.
676 delete wxLog::SetActiveTarget(new wxLogStderr
);
678 if (!wxAppBase::OnInitGui())
681 GetMainColormap( wxApp::GetDisplay() );
683 m_maxRequestSize
= XMaxRequestSize( (Display
*) wxApp::GetDisplay() );
686 m_visualInfo
= new wxXVisualInfo
;
687 wxFillXVisualInfo( m_visualInfo
, (Display
*) wxApp::GetDisplay() );
695 #include <pango/pango.h>
696 #include <pango/pangox.h>
697 #ifdef HAVE_PANGO_XFT
698 #include <pango/pangoxft.h>
701 PangoContext
* wxApp::GetPangoContext()
703 static PangoContext
*ret
= NULL
;
707 Display
*xdisplay
= (Display
*) wxApp::GetDisplay();
709 #ifdef HAVE_PANGO_XFT
710 int xscreen
= DefaultScreen(xdisplay
);
711 static int use_xft
= -1;
714 wxString val
= wxGetenv( L
"GDK_USE_XFT" );
715 use_xft
= (val
== L
"1");
719 ret
= pango_xft_get_context( xdisplay
, xscreen
);
722 ret
= pango_x_get_context( xdisplay
);
724 if (!PANGO_IS_CONTEXT(ret
))
725 wxLogError( wxT("No pango context.") );
731 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
733 if (!display
) /* Must be called first with non-NULL display */
734 return m_mainColormap
;
736 int defaultScreen
= DefaultScreen((Display
*) display
);
737 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
739 Colormap c
= DefaultColormapOfScreen(screen
);
742 m_mainColormap
= (WXColormap
) c
;
744 return (WXColormap
) c
;
747 Window
wxGetWindowParent(Window window
)
749 wxASSERT_MSG( window
, _T("invalid window") );
754 // VMS chokes on unreacheable code
755 Window parent
, root
= 0;
759 unsigned int noChildren
= 0;
761 Window
* children
= NULL
;
763 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
768 XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
769 & children
, & noChildren
);
783 wxAppConsole::Exit();
786 // Yield to other processes
788 bool wxApp::Yield(bool onlyIfNeeded
)
790 // Sometimes only 2 yields seem
791 // to do the trick, e.g. in the
794 for (i
= 0; i
< 2; i
++)
796 static bool s_inYield
= false;
802 wxFAIL_MSG( wxT("wxYield called recursively" ) );
810 // Make sure we have an event loop object,
811 // or Pending/Dispatch will fail
812 wxEventLoop
* eventLoop
= wxEventLoop::GetActive();
813 wxEventLoop
* newEventLoop
= NULL
;
816 newEventLoop
= new wxEventLoop
;
817 wxEventLoop::SetActive(newEventLoop
);
820 // Call dispatch at least once so that sockets
822 wxTheApp
->Dispatch();
824 while (wxTheApp
&& wxTheApp
->Pending())
825 wxTheApp
->Dispatch();
828 wxTimer::NotifyTimers();
834 wxEventLoop::SetActive(NULL
);
846 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
848 // While the GUI isn't working that well, just print out the
851 wxAppBase::OnAssert(file
, line
, cond
, msg
);
854 msg2
.Printf("At file %s:%d: %s", file
, line
, msg
);
859 #endif // __WXDEBUG__