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"
26 #include "wx/memory.h"
27 #include "wx/gdicmn.h"
28 #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"
38 #include "wx/thread.h"
41 #include "wx/x11/private.h"
45 //------------------------------------------------------------------------
47 //------------------------------------------------------------------------
49 wxWindowHash
*wxWidgetHashTable
= NULL
;
50 wxWindowHash
*wxClientWidgetHashTable
= NULL
;
52 static bool g_showIconic
= false;
53 static wxSize g_initialSize
= wxDefaultSize
;
55 // This is required for wxFocusEvent::SetWindow(). It will only
56 // work for focus events which we provoke ourselves (by calling
57 // SetFocus()). It will not work for those events, which X11
59 static wxWindow
*g_nextFocus
= NULL
;
60 static wxWindow
*g_prevFocus
= NULL
;
62 //------------------------------------------------------------------------
64 //------------------------------------------------------------------------
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
);
81 //------------------------------------------------------------------------
83 //------------------------------------------------------------------------
85 long wxApp::sm_lastMessageTime
= 0;
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
);
167 // open and set up the X11 display
168 if ( !wxSetDisplay(displayName
) )
170 wxLogError(_("wxWidgets could not open display. Exiting."));
174 Display
*dpy
= wxGlobalDisplay();
176 XSynchronize(dpy
, True
);
178 XSelectInput(dpy
, XDefaultRootWindow(dpy
), PropertyChangeMask
);
180 wxSetDetectableAutoRepeat( true );
183 if ( !wxAppBase::Initialize(argC
, argV
) )
187 // Glib's type system required by Pango
192 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
195 wxWidgetHashTable
= new wxWindowHash
;
196 wxClientWidgetHashTable
= new wxWindowHash
;
201 void wxApp::CleanUp()
203 delete wxWidgetHashTable
;
204 wxWidgetHashTable
= NULL
;
205 delete wxClientWidgetHashTable
;
206 wxClientWidgetHashTable
= NULL
;
208 wxAppBase::CleanUp();
213 m_mainColormap
= NULL
;
214 m_topLevelWidget
= NULL
;
215 m_maxRequestSize
= 0;
216 m_showIconic
= false;
217 m_initialSize
= wxDefaultSize
;
233 //-----------------------------------------------------------------------
234 // X11 predicate function for exposure compression
235 //-----------------------------------------------------------------------
240 Bool found_non_matching
;
244 Bool
wxX11ExposePredicate (Display
*display
, XEvent
*xevent
, XPointer arg
)
246 wxExposeInfo
*info
= (wxExposeInfo
*) arg
;
248 if (info
->found_non_matching
)
251 if (xevent
->xany
.type
!= Expose
)
253 info
->found_non_matching
= true;
257 if (xevent
->xexpose
.window
!= info
->window
)
259 info
->found_non_matching
= true;
266 #endif // wxUSE_NANOX
268 //-----------------------------------------------------------------------
269 // Processes an X event, returning true if the event was processed.
270 //-----------------------------------------------------------------------
272 bool wxApp::ProcessXEvent(WXEvent
* _event
)
274 XEvent
* event
= (XEvent
*) _event
;
276 wxWindow
* win
= NULL
;
277 Window window
= XEventGetWindow(event
);
279 Window actualWindow
= window
;
282 // Find the first wxWindow that corresponds to this event window
283 // Because we're receiving events after a window
284 // has been destroyed, assume a 1:1 match between
285 // Window and wxWindow, so if it's not in the table,
286 // it must have been destroyed.
288 win
= wxGetWindowFromTable(window
);
291 #if wxUSE_TWO_WINDOWS
292 win
= wxGetClientWindowFromTable(window
);
299 wxString windowClass
= win
->GetClassInfo()->GetClassName();
306 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
307 if (event
->xexpose
.window
!= (Window
)win
->GetClientAreaWindow())
311 info
.window
= event
->xexpose
.window
;
312 info
.found_non_matching
= false;
313 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, wxX11ExposePredicate
, (XPointer
) &info
))
315 // Don't worry about optimizing redrawing the border etc.
317 win
->NeedUpdateNcAreaInIdle();
322 win
->GetUpdateRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
323 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
324 win
->GetClearRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
325 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
330 info
.window
= event
->xexpose
.window
;
331 info
.found_non_matching
= false;
332 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, wxX11ExposePredicate
, (XPointer
) &info
))
334 win
->GetUpdateRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
335 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
337 win
->GetClearRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
338 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
342 // This simplifies the expose and clear areas to simple
344 win
->GetUpdateRegion() = win
->GetUpdateRegion().GetBox();
345 win
->GetClearRegion() = win
->GetClearRegion().GetBox();
347 // If we only have one X11 window, always indicate
348 // that borders might have to be redrawn.
349 if (win
->GetMainWindow() == win
->GetClientAreaWindow())
350 win
->NeedUpdateNcAreaInIdle();
352 // Only erase background, paint in idle time.
353 win
->SendEraseEvents();
365 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win
->GetName().c_str());
367 win
->GetUpdateRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
368 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
370 win
->GetClearRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
371 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
373 if (event
->xgraphicsexpose
.count
== 0)
375 // Only erase background, paint in idle time.
376 win
->SendEraseEvents();
386 if (!win
->IsEnabled())
389 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
390 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
392 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
394 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
395 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
398 keyEvent
.SetEventType(wxEVT_CHAR
);
399 // Do the translation again, retaining the ASCII
401 if (wxTranslateKeyEvent(keyEvent
, win
, window
, event
, true) &&
402 win
->GetEventHandler()->ProcessEvent( keyEvent
))
405 if ( (keyEvent
.m_keyCode
== WXK_TAB
) &&
406 win
->GetParent() && (win
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
408 wxNavigationKeyEvent new_event
;
409 new_event
.SetEventObject( win
->GetParent() );
410 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
411 new_event
.SetDirection( (keyEvent
.m_keyCode
== WXK_TAB
) );
412 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
413 new_event
.SetWindowChange( keyEvent
.ControlDown() );
414 new_event
.SetCurrentFocus( win
);
415 return win
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
422 if (!win
->IsEnabled())
425 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
426 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
428 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
430 case ConfigureNotify
:
433 if (event
->update
.utype
== GR_UPDATE_SIZE
)
436 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
439 tlw
->SetConfigureGeometry( XConfigureEventGetX(event
), XConfigureEventGetY(event
),
440 XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
) );
443 if ( tlw
&& tlw
->IsShown() )
445 tlw
->SetNeedResizeInIdle();
449 wxSizeEvent
sizeEvent( wxSize(XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
)), win
->GetId() );
450 sizeEvent
.SetEventObject( win
);
452 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
460 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
461 return HandlePropertyChange(_event
);
465 if (!win
->IsEnabled())
468 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);
469 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);
471 if (event
->xclient
.message_type
== wm_protocols
)
473 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
484 printf( "destroy from %s\n", win
->GetName().c_str() );
489 printf( "create from %s\n", win
->GetName().c_str() );
494 printf( "map request from %s\n", win
->GetName().c_str() );
499 printf( "resize request from %s\n", win
->GetName().c_str() );
501 Display
*disp
= (Display
*) wxGetDisplay();
506 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
508 wxSize sz
= win
->GetSize();
509 wxSizeEvent
sizeEvent(sz
, win
->GetId());
510 sizeEvent
.SetEventObject(win
);
512 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
517 case GR_EVENT_TYPE_CLOSE_REQ
:
534 if (!win
->IsEnabled())
537 // Here we check if the top level window is
538 // disabled, which is one aspect of modality.
540 while (tlw
&& !tlw
->IsTopLevel())
541 tlw
= tlw
->GetParent();
542 if (tlw
&& !tlw
->IsEnabled())
545 if (event
->type
== ButtonPress
)
547 if ((win
!= wxWindow::FindFocus()) && win
->CanAcceptFocus())
549 // This might actually be done in wxWindow::SetFocus()
550 // and not here. TODO.
551 g_prevFocus
= wxWindow::FindFocus();
554 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
556 // Record the fact that this window is
557 // getting the focus, because we'll need to
558 // check if its parent is getting a bogus
559 // focus and duly ignore it.
560 // TODO: may need to have this code in SetFocus, too.
561 extern wxWindow
* g_GettingFocus
;
562 g_GettingFocus
= win
;
568 if (event
->type
== LeaveNotify
|| event
->type
== EnterNotify
)
570 // Throw out NotifyGrab and NotifyUngrab
571 if (event
->xcrossing
.mode
!= NotifyNormal
)
575 wxMouseEvent wxevent
;
576 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
577 return win
->GetEventHandler()->ProcessEvent( wxevent
);
581 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
582 (event
->xfocus
.mode
== NotifyNormal
))
585 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
587 extern wxWindow
* g_GettingFocus
;
588 if (g_GettingFocus
&& g_GettingFocus
->GetParent() == win
)
590 // Ignore this, this can be a spurious FocusIn
591 // caused by a child having its focus set.
592 g_GettingFocus
= NULL
;
593 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
598 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
599 focusEvent
.SetEventObject(win
);
600 focusEvent
.SetWindow( g_prevFocus
);
603 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
610 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
611 (event
->xfocus
.mode
== NotifyNormal
))
614 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
616 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
617 focusEvent
.SetEventObject(win
);
618 focusEvent
.SetWindow( g_nextFocus
);
620 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
626 //wxString eventName = wxGetXEventName(XEvent& event);
627 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
629 #endif // __WXDEBUG__
635 // This should be redefined in a derived class for
636 // handling property change events for XAtom IPC.
637 bool wxApp::HandlePropertyChange(WXEvent
*event
)
639 // by default do nothing special
640 // TODO: what to do for X11
641 // XtDispatchEvent((XEvent*) event);
645 void wxApp::WakeUpIdle()
647 // TODO: use wxMotif implementation?
649 // Wake up the idle handler processor, even if it is in another thread...
653 // Create display, and other initialization
654 bool wxApp::OnInitGui()
657 // Eventually this line will be removed, but for
658 // now we don't want to try popping up a dialog
659 // for error messages.
660 delete wxLog::SetActiveTarget(new wxLogStderr
);
663 if (!wxAppBase::OnInitGui())
666 Display
*dpy
= wxGlobalDisplay();
667 GetMainColormap(dpy
);
669 m_maxRequestSize
= XMaxRequestSize(dpy
);
672 m_visualInfo
= new wxXVisualInfo
;
673 wxFillXVisualInfo(m_visualInfo
, dpy
);
681 #include <pango/pango.h>
682 #include <pango/pangox.h>
683 #ifdef HAVE_PANGO_XFT
684 #include <pango/pangoxft.h>
687 PangoContext
* wxApp::GetPangoContext()
689 static PangoContext
*s_pangoContext
= NULL
;
690 if ( !s_pangoContext
)
692 Display
*dpy
= wxGlobalDisplay();
694 #ifdef HAVE_PANGO_XFT
695 int xscreen
= DefaultScreen(dpy
);
696 static int use_xft
= -1;
699 wxString val
= wxGetenv( L
"GDK_USE_XFT" );
700 use_xft
= val
== L
"1";
704 s_pangoContext
= pango_xft_get_context(dpy
, xscreen
);
706 #endif // HAVE_PANGO_XFT
707 s_pangoContext
= pango_x_get_context(dpy
);
709 if (!PANGO_IS_CONTEXT(s_pangoContext
))
710 wxLogError( wxT("No pango context.") );
713 return s_pangoContext
;
716 #endif // wxUSE_UNICODE
718 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
720 if (!display
) /* Must be called first with non-NULL display */
721 return m_mainColormap
;
723 int defaultScreen
= DefaultScreen((Display
*) display
);
724 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
726 Colormap c
= DefaultColormapOfScreen(screen
);
729 m_mainColormap
= (WXColormap
) c
;
731 return (WXColormap
) c
;
734 Window
wxGetWindowParent(Window window
)
736 wxASSERT_MSG( window
, _T("invalid window") );
741 // VMS chokes on unreacheable code
742 Window parent
, root
= 0;
746 unsigned int noChildren
= 0;
748 Window
* children
= NULL
;
750 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
755 XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
756 & children
, & noChildren
);
770 wxAppConsole::Exit();
773 // Yield to other processes
775 bool wxApp::Yield(bool onlyIfNeeded
)
777 // Sometimes only 2 yields seem
778 // to do the trick, e.g. in the
781 for (i
= 0; i
< 2; i
++)
783 static bool s_inYield
= false;
789 wxFAIL_MSG( wxT("wxYield called recursively" ) );
797 // Make sure we have an event loop object,
798 // or Pending/Dispatch will fail
799 wxEventLoop
* eventLoop
= wxEventLoop::GetActive();
800 wxEventLoop
* newEventLoop
= NULL
;
803 newEventLoop
= new wxEventLoop
;
804 wxEventLoop::SetActive(newEventLoop
);
807 // Call dispatch at least once so that sockets
809 wxTheApp
->Dispatch();
811 while (wxTheApp
&& wxTheApp
->Pending())
812 wxTheApp
->Dispatch();
815 wxTimer::NotifyTimers();
821 wxEventLoop::SetActive(NULL
);
833 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
835 // While the GUI isn't working that well, just print out the
838 wxAppBase::OnAssert(file
, line
, cond
, msg
);
841 msg2
.Printf("At file %s:%d: %s", file
, line
, msg
);
846 #endif // __WXDEBUG__