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"
18 #include "wx/gdicmn.h"
20 #include "wx/dialog.h"
22 #include "wx/module.h"
23 #include "wx/memory.h"
26 #include "wx/evtloop.h"
28 #include "wx/filename.h"
31 #include "wx/univ/theme.h"
32 #include "wx/univ/renderer.h"
35 #include "wx/thread.h"
38 #include "wx/x11/private.h"
42 //------------------------------------------------------------------------
44 //------------------------------------------------------------------------
46 extern wxList wxPendingDelete
;
48 wxWindowHash
*wxWidgetHashTable
= NULL
;
49 wxWindowHash
*wxClientWidgetHashTable
= NULL
;
51 static bool g_showIconic
= false;
52 static wxSize g_initialSize
= wxDefaultSize
;
54 // This is required for wxFocusEvent::SetWindow(). It will only
55 // work for focus events which we provoke ourselves (by calling
56 // SetFocus()). It will not work for those events, which X11
58 static wxWindow
*g_nextFocus
= NULL
;
59 static wxWindow
*g_prevFocus
= NULL
;
61 //------------------------------------------------------------------------
63 //------------------------------------------------------------------------
66 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
68 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
70 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
72 // just forward to the default handler for now
73 if (gs_pfnXErrorHandler
)
74 return gs_pfnXErrorHandler(dpy
, xevent
);
80 //------------------------------------------------------------------------
82 //------------------------------------------------------------------------
84 long wxApp::sm_lastMessageTime
= 0;
85 WXDisplay
*wxApp::ms_display
= NULL
;
87 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
89 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
90 EVT_IDLE(wxAppBase::OnIdle
)
93 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
95 #if defined(__WXDEBUG__) && !wxUSE_NANOX
96 // install the X error handler
97 gs_pfnXErrorHandler
= XSetErrorHandler( wxXErrorHandler
);
100 wxString displayName
;
101 bool syncDisplay
= false;
104 for ( int i
= 0; i
< argcOrig
; i
++ )
106 if (wxStrcmp( argv
[i
], _T("-display") ) == 0)
112 displayName
= argv
[i
];
118 else if (wxStrcmp( argv
[i
], _T("-geometry") ) == 0)
125 if (wxSscanf(argv
[i
], _T("%dx%d"), &w
, &h
) != 2)
127 wxLogError( _("Invalid geometry specification '%s'"),
128 wxString(argv
[i
]).c_str() );
132 g_initialSize
= wxSize(w
, h
);
139 else if (wxStrcmp( argv
[i
], _T("-sync") ) == 0)
146 else if (wxStrcmp( argv
[i
], _T("-iconic") ) == 0)
155 if ( argc
!= argcOrig
)
157 // remove the argumens we consumed
158 for ( int i
= 0; i
< argc
; i
++ )
162 memmove(argv
+ i
, argv
+ i
+ 1, argcOrig
- i
);
169 if ( displayName
.empty() )
170 xdisplay
= XOpenDisplay( NULL
);
172 xdisplay
= XOpenDisplay( displayName
.ToAscii() );
175 wxLogError( _("wxWidgets could not open display. Exiting.") );
180 XSynchronize(xdisplay
, True
);
182 ms_display
= (WXDisplay
*) xdisplay
;
184 XSelectInput( xdisplay
, XDefaultRootWindow(xdisplay
), PropertyChangeMask
);
187 wxSetDetectableAutoRepeat( true );
189 if ( !wxAppBase::Initialize(argc
, argv
) )
191 XCloseDisplay(xdisplay
);
197 // Glib's type system required by Pango
202 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
205 wxWidgetHashTable
= new wxWindowHash
;
206 wxClientWidgetHashTable
= new wxWindowHash
;
211 void wxApp::CleanUp()
213 delete wxWidgetHashTable
;
214 wxWidgetHashTable
= NULL
;
215 delete wxClientWidgetHashTable
;
216 wxClientWidgetHashTable
= NULL
;
218 wxAppBase::CleanUp();
223 // TODO: parse the command line
227 m_mainColormap
= (WXColormap
) NULL
;
228 m_topLevelWidget
= (WXWindow
) NULL
;
229 m_maxRequestSize
= 0;
230 m_showIconic
= false;
231 m_initialSize
= wxDefaultSize
;
246 //-----------------------------------------------------------------------
247 // X11 predicate function for exposure compression
248 //-----------------------------------------------------------------------
253 Bool found_non_matching
;
256 static Bool
expose_predicate (Display
*display
, XEvent
*xevent
, XPointer arg
)
258 wxExposeInfo
*info
= (wxExposeInfo
*) arg
;
260 if (info
->found_non_matching
)
263 if (xevent
->xany
.type
!= Expose
)
265 info
->found_non_matching
= true;
269 if (xevent
->xexpose
.window
!= info
->window
)
271 info
->found_non_matching
= true;
280 //-----------------------------------------------------------------------
281 // Processes an X event, returning true if the event was processed.
282 //-----------------------------------------------------------------------
284 bool wxApp::ProcessXEvent(WXEvent
* _event
)
286 XEvent
* event
= (XEvent
*) _event
;
288 wxWindow
* win
= NULL
;
289 Window window
= XEventGetWindow(event
);
291 Window actualWindow
= window
;
294 // Find the first wxWindow that corresponds to this event window
295 // Because we're receiving events after a window
296 // has been destroyed, assume a 1:1 match between
297 // Window and wxWindow, so if it's not in the table,
298 // it must have been destroyed.
300 win
= wxGetWindowFromTable(window
);
303 #if wxUSE_TWO_WINDOWS
304 win
= wxGetClientWindowFromTable(window
);
311 wxString windowClass
= win
->GetClassInfo()->GetClassName();
318 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
319 if (event
->xexpose
.window
!= (Window
)win
->GetClientAreaWindow())
323 info
.window
= event
->xexpose
.window
;
324 info
.found_non_matching
= false;
325 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
327 // Don't worry about optimizing redrawing the border etc.
329 win
->NeedUpdateNcAreaInIdle();
334 win
->GetUpdateRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
335 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
336 win
->GetClearRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
337 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
342 info
.window
= event
->xexpose
.window
;
343 info
.found_non_matching
= false;
344 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
346 win
->GetUpdateRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
347 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
349 win
->GetClearRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
350 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
354 // This simplifies the expose and clear areas to simple
356 win
->GetUpdateRegion() = win
->GetUpdateRegion().GetBox();
357 win
->GetClearRegion() = win
->GetClearRegion().GetBox();
359 // If we only have one X11 window, always indicate
360 // that borders might have to be redrawn.
361 if (win
->GetMainWindow() == win
->GetClientAreaWindow())
362 win
->NeedUpdateNcAreaInIdle();
364 // Only erase background, paint in idle time.
365 win
->SendEraseEvents();
377 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win
->GetName().c_str());
379 win
->GetUpdateRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
380 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
382 win
->GetClearRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
383 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
385 if (event
->xgraphicsexpose
.count
== 0)
387 // Only erase background, paint in idle time.
388 win
->SendEraseEvents();
398 if (!win
->IsEnabled())
401 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
402 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
404 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
406 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
407 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
410 keyEvent
.SetEventType(wxEVT_CHAR
);
411 // Do the translation again, retaining the ASCII
413 wxTranslateKeyEvent(keyEvent
, win
, window
, event
, true);
414 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
417 if ( (keyEvent
.m_keyCode
== WXK_TAB
) &&
418 win
->GetParent() && (win
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
420 wxNavigationKeyEvent new_event
;
421 new_event
.SetEventObject( win
->GetParent() );
422 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
423 new_event
.SetDirection( (keyEvent
.m_keyCode
== WXK_TAB
) );
424 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
425 new_event
.SetWindowChange( keyEvent
.ControlDown() );
426 new_event
.SetCurrentFocus( win
);
427 return win
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
434 if (!win
->IsEnabled())
437 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
438 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
440 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
442 case ConfigureNotify
:
445 if (event
->update
.utype
== GR_UPDATE_SIZE
)
448 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
451 tlw
->SetConfigureGeometry( XConfigureEventGetX(event
), XConfigureEventGetY(event
),
452 XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
) );
455 if ( tlw
&& tlw
->IsShown() )
457 tlw
->SetNeedResizeInIdle();
461 wxSizeEvent
sizeEvent( wxSize(XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
)), win
->GetId() );
462 sizeEvent
.SetEventObject( win
);
464 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
472 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
473 return HandlePropertyChange(_event
);
477 if (!win
->IsEnabled())
480 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);
481 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);
483 if (event
->xclient
.message_type
== wm_protocols
)
485 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
496 printf( "destroy from %s\n", win
->GetName().c_str() );
501 printf( "create from %s\n", win
->GetName().c_str() );
506 printf( "map request from %s\n", win
->GetName().c_str() );
511 printf( "resize request from %s\n", win
->GetName().c_str() );
513 Display
*disp
= (Display
*) wxGetDisplay();
518 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
520 wxSize sz
= win
->GetSize();
521 wxSizeEvent
sizeEvent(sz
, win
->GetId());
522 sizeEvent
.SetEventObject(win
);
524 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
529 case GR_EVENT_TYPE_CLOSE_REQ
:
546 if (!win
->IsEnabled())
549 // Here we check if the top level window is
550 // disabled, which is one aspect of modality.
552 while (tlw
&& !tlw
->IsTopLevel())
553 tlw
= tlw
->GetParent();
554 if (tlw
&& !tlw
->IsEnabled())
557 if (event
->type
== ButtonPress
)
559 if ((win
!= wxWindow::FindFocus()) && win
->AcceptsFocus())
561 // This might actually be done in wxWindow::SetFocus()
562 // and not here. TODO.
563 g_prevFocus
= wxWindow::FindFocus();
566 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
568 // Record the fact that this window is
569 // getting the focus, because we'll need to
570 // check if its parent is getting a bogus
571 // focus and duly ignore it.
572 // TODO: may need to have this code in SetFocus, too.
573 extern wxWindow
* g_GettingFocus
;
574 g_GettingFocus
= win
;
580 if (event
->type
== LeaveNotify
|| event
->type
== EnterNotify
)
582 // Throw out NotifyGrab and NotifyUngrab
583 if (event
->xcrossing
.mode
!= NotifyNormal
)
587 wxMouseEvent wxevent
;
588 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
589 return win
->GetEventHandler()->ProcessEvent( wxevent
);
593 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
594 (event
->xfocus
.mode
== NotifyNormal
))
597 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
599 extern wxWindow
* g_GettingFocus
;
600 if (g_GettingFocus
&& g_GettingFocus
->GetParent() == win
)
602 // Ignore this, this can be a spurious FocusIn
603 // caused by a child having its focus set.
604 g_GettingFocus
= NULL
;
605 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
610 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
611 focusEvent
.SetEventObject(win
);
612 focusEvent
.SetWindow( g_prevFocus
);
615 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
622 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
623 (event
->xfocus
.mode
== NotifyNormal
))
626 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
628 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
629 focusEvent
.SetEventObject(win
);
630 focusEvent
.SetWindow( g_nextFocus
);
632 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
638 //wxString eventName = wxGetXEventName(XEvent& event);
639 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
641 #endif // __WXDEBUG__
647 // This should be redefined in a derived class for
648 // handling property change events for XAtom IPC.
649 bool wxApp::HandlePropertyChange(WXEvent
*event
)
651 // by default do nothing special
652 // TODO: what to do for X11
653 // XtDispatchEvent((XEvent*) event);
657 void wxApp::WakeUpIdle()
659 // TODO: use wxMotif implementation?
661 // Wake up the idle handler processor, even if it is in another thread...
665 // Create display, and other initialization
666 bool wxApp::OnInitGui()
668 // Eventually this line will be removed, but for
669 // now we don't want to try popping up a dialog
670 // for error messages.
671 delete wxLog::SetActiveTarget(new wxLogStderr
);
673 if (!wxAppBase::OnInitGui())
676 GetMainColormap( wxApp::GetDisplay() );
678 m_maxRequestSize
= XMaxRequestSize( (Display
*) wxApp::GetDisplay() );
681 m_visualInfo
= new wxXVisualInfo
;
682 wxFillXVisualInfo( m_visualInfo
, (Display
*) wxApp::GetDisplay() );
690 #include <pango/pango.h>
691 #include <pango/pangox.h>
692 #ifdef HAVE_PANGO_XFT
693 #include <pango/pangoxft.h>
696 PangoContext
* wxApp::GetPangoContext()
698 static PangoContext
*ret
= NULL
;
702 Display
*xdisplay
= (Display
*) wxApp::GetDisplay();
704 #ifdef HAVE_PANGO_XFT
705 int xscreen
= DefaultScreen(xdisplay
);
706 static int use_xft
= -1;
709 wxString val
= wxGetenv( L
"GDK_USE_XFT" );
710 use_xft
= (val
== L
"1");
714 ret
= pango_xft_get_context( xdisplay
, xscreen
);
717 ret
= pango_x_get_context( xdisplay
);
719 if (!PANGO_IS_CONTEXT(ret
))
720 wxLogError( wxT("No pango context.") );
726 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
728 if (!display
) /* Must be called first with non-NULL display */
729 return m_mainColormap
;
731 int defaultScreen
= DefaultScreen((Display
*) display
);
732 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
734 Colormap c
= DefaultColormapOfScreen(screen
);
737 m_mainColormap
= (WXColormap
) c
;
739 return (WXColormap
) c
;
742 Window
wxGetWindowParent(Window window
)
744 wxASSERT_MSG( window
, _T("invalid window") );
749 // VMS chokes on unreacheable code
750 Window parent
, root
= 0;
754 unsigned int noChildren
= 0;
756 Window
* children
= NULL
;
758 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
763 XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
764 & children
, & noChildren
);
778 wxAppConsole::Exit();
781 // Yield to other processes
783 bool wxApp::Yield(bool onlyIfNeeded
)
785 // Sometimes only 2 yields seem
786 // to do the trick, e.g. in the
789 for (i
= 0; i
< 2; i
++)
791 static bool s_inYield
= false;
797 wxFAIL_MSG( wxT("wxYield called recursively" ) );
805 // Make sure we have an event loop object,
806 // or Pending/Dispatch will fail
807 wxEventLoop
* eventLoop
= wxEventLoop::GetActive();
808 wxEventLoop
* newEventLoop
= NULL
;
811 newEventLoop
= new wxEventLoop
;
812 wxEventLoop::SetActive(newEventLoop
);
815 // Call dispatch at least once so that sockets
817 wxTheApp
->Dispatch();
819 while (wxTheApp
&& wxTheApp
->Pending())
820 wxTheApp
->Dispatch();
823 wxTimer::NotifyTimers();
829 wxEventLoop::SetActive(NULL
);
841 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
843 // While the GUI isn't working that well, just print out the
846 wxAppBase::OnAssert(file
, line
, cond
, msg
);
849 msg2
.Printf("At file %s:%d: %s", file
, line
, msg
);
854 #endif // __WXDEBUG__