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
;
247 //-----------------------------------------------------------------------
248 // X11 predicate function for exposure compression
249 //-----------------------------------------------------------------------
254 Bool found_non_matching
;
258 Bool
wxX11ExposePredicate (Display
*display
, XEvent
*xevent
, XPointer arg
)
260 wxExposeInfo
*info
= (wxExposeInfo
*) arg
;
262 if (info
->found_non_matching
)
265 if (xevent
->xany
.type
!= Expose
)
267 info
->found_non_matching
= true;
271 if (xevent
->xexpose
.window
!= info
->window
)
273 info
->found_non_matching
= true;
280 #endif // wxUSE_NANOX
282 //-----------------------------------------------------------------------
283 // Processes an X event, returning true if the event was processed.
284 //-----------------------------------------------------------------------
286 bool wxApp::ProcessXEvent(WXEvent
* _event
)
288 XEvent
* event
= (XEvent
*) _event
;
290 wxWindow
* win
= NULL
;
291 Window window
= XEventGetWindow(event
);
293 Window actualWindow
= window
;
296 // Find the first wxWindow that corresponds to this event window
297 // Because we're receiving events after a window
298 // has been destroyed, assume a 1:1 match between
299 // Window and wxWindow, so if it's not in the table,
300 // it must have been destroyed.
302 win
= wxGetWindowFromTable(window
);
305 #if wxUSE_TWO_WINDOWS
306 win
= wxGetClientWindowFromTable(window
);
313 wxString windowClass
= win
->GetClassInfo()->GetClassName();
320 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
321 if (event
->xexpose
.window
!= (Window
)win
->GetClientAreaWindow())
325 info
.window
= event
->xexpose
.window
;
326 info
.found_non_matching
= false;
327 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, wxX11ExposePredicate
, (XPointer
) &info
))
329 // Don't worry about optimizing redrawing the border etc.
331 win
->NeedUpdateNcAreaInIdle();
336 win
->GetUpdateRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
337 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
338 win
->GetClearRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
339 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
344 info
.window
= event
->xexpose
.window
;
345 info
.found_non_matching
= false;
346 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, wxX11ExposePredicate
, (XPointer
) &info
))
348 win
->GetUpdateRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
349 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
351 win
->GetClearRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
352 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
356 // This simplifies the expose and clear areas to simple
358 win
->GetUpdateRegion() = win
->GetUpdateRegion().GetBox();
359 win
->GetClearRegion() = win
->GetClearRegion().GetBox();
361 // If we only have one X11 window, always indicate
362 // that borders might have to be redrawn.
363 if (win
->GetMainWindow() == win
->GetClientAreaWindow())
364 win
->NeedUpdateNcAreaInIdle();
366 // Only erase background, paint in idle time.
367 win
->SendEraseEvents();
379 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win
->GetName().c_str());
381 win
->GetUpdateRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
382 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
384 win
->GetClearRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
385 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
387 if (event
->xgraphicsexpose
.count
== 0)
389 // Only erase background, paint in idle time.
390 win
->SendEraseEvents();
400 if (!win
->IsEnabled())
403 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
404 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
406 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
408 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
409 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
412 keyEvent
.SetEventType(wxEVT_CHAR
);
413 // Do the translation again, retaining the ASCII
415 wxTranslateKeyEvent(keyEvent
, win
, window
, event
, true);
416 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
419 if ( (keyEvent
.m_keyCode
== WXK_TAB
) &&
420 win
->GetParent() && (win
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
422 wxNavigationKeyEvent new_event
;
423 new_event
.SetEventObject( win
->GetParent() );
424 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
425 new_event
.SetDirection( (keyEvent
.m_keyCode
== WXK_TAB
) );
426 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
427 new_event
.SetWindowChange( keyEvent
.ControlDown() );
428 new_event
.SetCurrentFocus( win
);
429 return win
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
436 if (!win
->IsEnabled())
439 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
440 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
442 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
444 case ConfigureNotify
:
447 if (event
->update
.utype
== GR_UPDATE_SIZE
)
450 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
453 tlw
->SetConfigureGeometry( XConfigureEventGetX(event
), XConfigureEventGetY(event
),
454 XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
) );
457 if ( tlw
&& tlw
->IsShown() )
459 tlw
->SetNeedResizeInIdle();
463 wxSizeEvent
sizeEvent( wxSize(XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
)), win
->GetId() );
464 sizeEvent
.SetEventObject( win
);
466 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
474 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
475 return HandlePropertyChange(_event
);
479 if (!win
->IsEnabled())
482 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);
483 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);
485 if (event
->xclient
.message_type
== wm_protocols
)
487 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
498 printf( "destroy from %s\n", win
->GetName().c_str() );
503 printf( "create from %s\n", win
->GetName().c_str() );
508 printf( "map request from %s\n", win
->GetName().c_str() );
513 printf( "resize request from %s\n", win
->GetName().c_str() );
515 Display
*disp
= (Display
*) wxGetDisplay();
520 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
522 wxSize sz
= win
->GetSize();
523 wxSizeEvent
sizeEvent(sz
, win
->GetId());
524 sizeEvent
.SetEventObject(win
);
526 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
531 case GR_EVENT_TYPE_CLOSE_REQ
:
548 if (!win
->IsEnabled())
551 // Here we check if the top level window is
552 // disabled, which is one aspect of modality.
554 while (tlw
&& !tlw
->IsTopLevel())
555 tlw
= tlw
->GetParent();
556 if (tlw
&& !tlw
->IsEnabled())
559 if (event
->type
== ButtonPress
)
561 if ((win
!= wxWindow::FindFocus()) && win
->AcceptsFocus())
563 // This might actually be done in wxWindow::SetFocus()
564 // and not here. TODO.
565 g_prevFocus
= wxWindow::FindFocus();
568 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
570 // Record the fact that this window is
571 // getting the focus, because we'll need to
572 // check if its parent is getting a bogus
573 // focus and duly ignore it.
574 // TODO: may need to have this code in SetFocus, too.
575 extern wxWindow
* g_GettingFocus
;
576 g_GettingFocus
= win
;
582 if (event
->type
== LeaveNotify
|| event
->type
== EnterNotify
)
584 // Throw out NotifyGrab and NotifyUngrab
585 if (event
->xcrossing
.mode
!= NotifyNormal
)
589 wxMouseEvent wxevent
;
590 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
591 return win
->GetEventHandler()->ProcessEvent( wxevent
);
595 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
596 (event
->xfocus
.mode
== NotifyNormal
))
599 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
601 extern wxWindow
* g_GettingFocus
;
602 if (g_GettingFocus
&& g_GettingFocus
->GetParent() == win
)
604 // Ignore this, this can be a spurious FocusIn
605 // caused by a child having its focus set.
606 g_GettingFocus
= NULL
;
607 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
612 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
613 focusEvent
.SetEventObject(win
);
614 focusEvent
.SetWindow( g_prevFocus
);
617 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
624 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
625 (event
->xfocus
.mode
== NotifyNormal
))
628 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
630 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
631 focusEvent
.SetEventObject(win
);
632 focusEvent
.SetWindow( g_nextFocus
);
634 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
640 //wxString eventName = wxGetXEventName(XEvent& event);
641 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
643 #endif // __WXDEBUG__
649 // This should be redefined in a derived class for
650 // handling property change events for XAtom IPC.
651 bool wxApp::HandlePropertyChange(WXEvent
*event
)
653 // by default do nothing special
654 // TODO: what to do for X11
655 // XtDispatchEvent((XEvent*) event);
659 void wxApp::WakeUpIdle()
661 // TODO: use wxMotif implementation?
663 // Wake up the idle handler processor, even if it is in another thread...
667 // Create display, and other initialization
668 bool wxApp::OnInitGui()
670 // Eventually this line will be removed, but for
671 // now we don't want to try popping up a dialog
672 // for error messages.
673 delete wxLog::SetActiveTarget(new wxLogStderr
);
675 if (!wxAppBase::OnInitGui())
678 GetMainColormap( wxApp::GetDisplay() );
680 m_maxRequestSize
= XMaxRequestSize( (Display
*) wxApp::GetDisplay() );
683 m_visualInfo
= new wxXVisualInfo
;
684 wxFillXVisualInfo( m_visualInfo
, (Display
*) wxApp::GetDisplay() );
692 #include <pango/pango.h>
693 #include <pango/pangox.h>
694 #ifdef HAVE_PANGO_XFT
695 #include <pango/pangoxft.h>
698 PangoContext
* wxApp::GetPangoContext()
700 static PangoContext
*ret
= NULL
;
704 Display
*xdisplay
= (Display
*) wxApp::GetDisplay();
706 #ifdef HAVE_PANGO_XFT
707 int xscreen
= DefaultScreen(xdisplay
);
708 static int use_xft
= -1;
711 wxString val
= wxGetenv( L
"GDK_USE_XFT" );
712 use_xft
= (val
== L
"1");
716 ret
= pango_xft_get_context( xdisplay
, xscreen
);
719 ret
= pango_x_get_context( xdisplay
);
721 if (!PANGO_IS_CONTEXT(ret
))
722 wxLogError( wxT("No pango context.") );
728 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
730 if (!display
) /* Must be called first with non-NULL display */
731 return m_mainColormap
;
733 int defaultScreen
= DefaultScreen((Display
*) display
);
734 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
736 Colormap c
= DefaultColormapOfScreen(screen
);
739 m_mainColormap
= (WXColormap
) c
;
741 return (WXColormap
) c
;
744 Window
wxGetWindowParent(Window window
)
746 wxASSERT_MSG( window
, _T("invalid window") );
751 // VMS chokes on unreacheable code
752 Window parent
, root
= 0;
756 unsigned int noChildren
= 0;
758 Window
* children
= NULL
;
760 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
765 XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
766 & children
, & noChildren
);
780 wxAppConsole::Exit();
783 // Yield to other processes
785 bool wxApp::Yield(bool onlyIfNeeded
)
787 // Sometimes only 2 yields seem
788 // to do the trick, e.g. in the
791 for (i
= 0; i
< 2; i
++)
793 static bool s_inYield
= false;
799 wxFAIL_MSG( wxT("wxYield called recursively" ) );
807 // Make sure we have an event loop object,
808 // or Pending/Dispatch will fail
809 wxEventLoop
* eventLoop
= wxEventLoop::GetActive();
810 wxEventLoop
* newEventLoop
= NULL
;
813 newEventLoop
= new wxEventLoop
;
814 wxEventLoop::SetActive(newEventLoop
);
817 // Call dispatch at least once so that sockets
819 wxTheApp
->Dispatch();
821 while (wxTheApp
&& wxTheApp
->Pending())
822 wxTheApp
->Dispatch();
825 wxTimer::NotifyTimers();
831 wxEventLoop::SetActive(NULL
);
843 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
845 // While the GUI isn't working that well, just print out the
848 wxAppBase::OnAssert(file
, line
, cond
, msg
);
851 msg2
.Printf("At file %s:%d: %s", file
, line
, msg
);
856 #endif // __WXDEBUG__