1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #include "wx/gdicmn.h"
17 #include "wx/dialog.h"
19 #include "wx/module.h"
20 #include "wx/memory.h"
23 #include "wx/evtloop.h"
25 #include "wx/filename.h"
28 #include "wx/univ/theme.h"
29 #include "wx/univ/renderer.h"
32 #include "wx/thread.h"
35 #include "wx/x11/private.h"
39 //------------------------------------------------------------------------
41 //------------------------------------------------------------------------
43 extern wxList wxPendingDelete
;
45 wxWindowHash
*wxWidgetHashTable
= NULL
;
46 wxWindowHash
*wxClientWidgetHashTable
= NULL
;
48 static bool g_showIconic
= FALSE
;
49 static wxSize g_initialSize
= wxDefaultSize
;
51 // This is required for wxFocusEvent::SetWindow(). It will only
52 // work for focus events which we provoke ourselves (by calling
53 // SetFocus()). It will not work for those events, which X11
55 static wxWindow
*g_nextFocus
= NULL
;
56 static wxWindow
*g_prevFocus
= NULL
;
58 //------------------------------------------------------------------------
60 //------------------------------------------------------------------------
63 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
65 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
67 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
69 // just forward to the default handler for now
70 if (gs_pfnXErrorHandler
)
71 return gs_pfnXErrorHandler(dpy
, xevent
);
77 //------------------------------------------------------------------------
79 //------------------------------------------------------------------------
81 long wxApp::sm_lastMessageTime
= 0;
82 WXDisplay
*wxApp::ms_display
= NULL
;
84 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
86 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
87 EVT_IDLE(wxAppBase::OnIdle
)
90 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
92 #if defined(__WXDEBUG__) && !wxUSE_NANOX
93 // install the X error handler
94 gs_pfnXErrorHandler
= XSetErrorHandler( wxXErrorHandler
);
98 bool syncDisplay
= FALSE
;
101 for ( int i
= 0; i
< argcOrig
; i
++ )
103 if (wxStrcmp( argv
[i
], _T("-display") ) == 0)
109 displayName
= argv
[i
];
115 else if (wxStrcmp( argv
[i
], _T("-geometry") ) == 0)
122 if (wxSscanf(argv
[i
], _T("%dx%d"), &w
, &h
) != 2)
124 wxLogError( _("Invalid geometry specification '%s'"),
125 wxString(argv
[i
]).c_str() );
129 g_initialSize
= wxSize(w
, h
);
136 else if (wxStrcmp( argv
[i
], _T("-sync") ) == 0)
143 else if (wxStrcmp( argv
[i
], _T("-iconic") ) == 0)
152 if ( argc
!= argcOrig
)
154 // remove the argumens we consumed
155 for ( int i
= 0; i
< argc
; i
++ )
159 memmove(argv
+ i
, argv
+ i
+ 1, argcOrig
- i
);
166 if ( displayName
.empty() )
167 xdisplay
= XOpenDisplay( NULL
);
169 xdisplay
= XOpenDisplay( displayName
.ToAscii() );
172 wxLogError( _("wxWidgets could not open display. Exiting.") );
177 XSynchronize(xdisplay
, True
);
179 ms_display
= (WXDisplay
*) xdisplay
;
181 XSelectInput( xdisplay
, XDefaultRootWindow(xdisplay
), PropertyChangeMask
);
184 wxSetDetectableAutoRepeat( TRUE
);
186 if ( !wxAppBase::Initialize(argc
, argv
) )
188 XCloseDisplay(xdisplay
);
194 // Glib's type system required by Pango
199 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
202 wxWidgetHashTable
= new wxWindowHash
;
203 wxClientWidgetHashTable
= new wxWindowHash
;
208 void wxApp::CleanUp()
210 delete wxWidgetHashTable
;
211 wxWidgetHashTable
= NULL
;
212 delete wxClientWidgetHashTable
;
213 wxClientWidgetHashTable
= NULL
;
215 wxAppBase::CleanUp();
220 // TODO: parse the command line
224 m_mainColormap
= (WXColormap
) NULL
;
225 m_topLevelWidget
= (WXWindow
) NULL
;
226 m_maxRequestSize
= 0;
227 m_showIconic
= FALSE
;
228 m_initialSize
= wxDefaultSize
;
243 //-----------------------------------------------------------------------
244 // X11 predicate function for exposure compression
245 //-----------------------------------------------------------------------
250 Bool found_non_matching
;
253 static Bool
expose_predicate (Display
*display
, XEvent
*xevent
, XPointer arg
)
255 wxExposeInfo
*info
= (wxExposeInfo
*) arg
;
257 if (info
->found_non_matching
)
260 if (xevent
->xany
.type
!= Expose
)
262 info
->found_non_matching
= TRUE
;
266 if (xevent
->xexpose
.window
!= info
->window
)
268 info
->found_non_matching
= TRUE
;
277 //-----------------------------------------------------------------------
278 // Processes an X event, returning TRUE if the event was processed.
279 //-----------------------------------------------------------------------
281 bool wxApp::ProcessXEvent(WXEvent
* _event
)
283 XEvent
* event
= (XEvent
*) _event
;
285 wxWindow
* win
= NULL
;
286 Window window
= XEventGetWindow(event
);
288 Window actualWindow
= window
;
291 // Find the first wxWindow that corresponds to this event window
292 // Because we're receiving events after a window
293 // has been destroyed, assume a 1:1 match between
294 // Window and wxWindow, so if it's not in the table,
295 // it must have been destroyed.
297 win
= wxGetWindowFromTable(window
);
300 #if wxUSE_TWO_WINDOWS
301 win
= wxGetClientWindowFromTable(window
);
308 wxString windowClass
= win
->GetClassInfo()->GetClassName();
315 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
316 if (event
->xexpose
.window
!= (Window
)win
->GetClientAreaWindow())
320 info
.window
= event
->xexpose
.window
;
321 info
.found_non_matching
= FALSE
;
322 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
324 // Don't worry about optimizing redrawing the border etc.
326 win
->NeedUpdateNcAreaInIdle();
331 win
->GetUpdateRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
332 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
333 win
->GetClearRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
334 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
339 info
.window
= event
->xexpose
.window
;
340 info
.found_non_matching
= FALSE
;
341 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
343 win
->GetUpdateRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
344 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
346 win
->GetClearRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
347 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
351 // This simplifies the expose and clear areas to simple
353 win
->GetUpdateRegion() = win
->GetUpdateRegion().GetBox();
354 win
->GetClearRegion() = win
->GetClearRegion().GetBox();
356 // If we only have one X11 window, always indicate
357 // that borders might have to be redrawn.
358 if (win
->GetMainWindow() == win
->GetClientAreaWindow())
359 win
->NeedUpdateNcAreaInIdle();
361 // Only erase background, paint in idle time.
362 win
->SendEraseEvents();
374 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win
->GetName().c_str());
376 win
->GetUpdateRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
377 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
379 win
->GetClearRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
380 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
382 if (event
->xgraphicsexpose
.count
== 0)
384 // Only erase background, paint in idle time.
385 win
->SendEraseEvents();
395 if (!win
->IsEnabled())
398 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
399 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
401 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
403 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
404 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
407 keyEvent
.SetEventType(wxEVT_CHAR
);
408 // Do the translation again, retaining the ASCII
410 wxTranslateKeyEvent(keyEvent
, win
, window
, event
, TRUE
);
411 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
414 if ( (keyEvent
.m_keyCode
== WXK_TAB
) &&
415 win
->GetParent() && (win
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
417 wxNavigationKeyEvent new_event
;
418 new_event
.SetEventObject( win
->GetParent() );
419 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
420 new_event
.SetDirection( (keyEvent
.m_keyCode
== WXK_TAB
) );
421 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
422 new_event
.SetWindowChange( keyEvent
.ControlDown() );
423 new_event
.SetCurrentFocus( win
);
424 return win
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
431 if (!win
->IsEnabled())
434 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
435 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
437 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
439 case ConfigureNotify
:
442 if (event
->update
.utype
== GR_UPDATE_SIZE
)
445 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
448 tlw
->SetConfigureGeometry( XConfigureEventGetX(event
), XConfigureEventGetY(event
),
449 XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
) );
452 if ( tlw
&& tlw
->IsShown() )
454 tlw
->SetNeedResizeInIdle();
458 wxSizeEvent
sizeEvent( wxSize(XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
)), win
->GetId() );
459 sizeEvent
.SetEventObject( win
);
461 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
469 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
470 return HandlePropertyChange(_event
);
474 if (!win
->IsEnabled())
477 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);
478 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);
480 if (event
->xclient
.message_type
== wm_protocols
)
482 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
493 printf( "destroy from %s\n", win
->GetName().c_str() );
498 printf( "create from %s\n", win
->GetName().c_str() );
503 printf( "map request from %s\n", win
->GetName().c_str() );
508 printf( "resize request from %s\n", win
->GetName().c_str() );
510 Display
*disp
= (Display
*) wxGetDisplay();
515 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
517 wxSize sz
= win
->GetSize();
518 wxSizeEvent
sizeEvent(sz
, win
->GetId());
519 sizeEvent
.SetEventObject(win
);
521 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
526 case GR_EVENT_TYPE_CLOSE_REQ
:
543 if (!win
->IsEnabled())
546 // Here we check if the top level window is
547 // disabled, which is one aspect of modality.
549 while (tlw
&& !tlw
->IsTopLevel())
550 tlw
= tlw
->GetParent();
551 if (tlw
&& !tlw
->IsEnabled())
554 if (event
->type
== ButtonPress
)
556 if ((win
!= wxWindow::FindFocus()) && win
->AcceptsFocus())
558 // This might actually be done in wxWindow::SetFocus()
559 // and not here. TODO.
560 g_prevFocus
= wxWindow::FindFocus();
563 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
565 // Record the fact that this window is
566 // getting the focus, because we'll need to
567 // check if its parent is getting a bogus
568 // focus and duly ignore it.
569 // TODO: may need to have this code in SetFocus, too.
570 extern wxWindow
* g_GettingFocus
;
571 g_GettingFocus
= win
;
577 if (event
->type
== LeaveNotify
|| event
->type
== EnterNotify
)
579 // Throw out NotifyGrab and NotifyUngrab
580 if (event
->xcrossing
.mode
!= NotifyNormal
)
584 wxMouseEvent wxevent
;
585 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
586 return win
->GetEventHandler()->ProcessEvent( wxevent
);
590 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
591 (event
->xfocus
.mode
== NotifyNormal
))
594 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
596 extern wxWindow
* g_GettingFocus
;
597 if (g_GettingFocus
&& g_GettingFocus
->GetParent() == win
)
599 // Ignore this, this can be a spurious FocusIn
600 // caused by a child having its focus set.
601 g_GettingFocus
= NULL
;
602 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
607 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
608 focusEvent
.SetEventObject(win
);
609 focusEvent
.SetWindow( g_prevFocus
);
612 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
619 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
620 (event
->xfocus
.mode
== NotifyNormal
))
623 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
625 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
626 focusEvent
.SetEventObject(win
);
627 focusEvent
.SetWindow( g_nextFocus
);
629 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
635 //wxString eventName = wxGetXEventName(XEvent& event);
636 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
638 #endif // __WXDEBUG__
644 // This should be redefined in a derived class for
645 // handling property change events for XAtom IPC.
646 bool wxApp::HandlePropertyChange(WXEvent
*event
)
648 // by default do nothing special
649 // TODO: what to do for X11
650 // XtDispatchEvent((XEvent*) event);
654 void wxApp::WakeUpIdle()
656 // TODO: use wxMotif implementation?
658 // Wake up the idle handler processor, even if it is in another thread...
662 // Create display, and other initialization
663 bool wxApp::OnInitGui()
665 // Eventually this line will be removed, but for
666 // now we don't want to try popping up a dialog
667 // for error messages.
668 delete wxLog::SetActiveTarget(new wxLogStderr
);
670 if (!wxAppBase::OnInitGui())
673 GetMainColormap( wxApp::GetDisplay() );
675 m_maxRequestSize
= XMaxRequestSize( (Display
*) wxApp::GetDisplay() );
678 m_visualInfo
= new wxXVisualInfo
;
679 wxFillXVisualInfo( m_visualInfo
, (Display
*) wxApp::GetDisplay() );
687 #include <pango/pango.h>
688 #include <pango/pangox.h>
689 #ifdef HAVE_PANGO_XFT
690 #include <pango/pangoxft.h>
693 PangoContext
* wxApp::GetPangoContext()
695 static PangoContext
*ret
= NULL
;
699 Display
*xdisplay
= (Display
*) wxApp::GetDisplay();
701 #ifdef HAVE_PANGO_XFT
702 int xscreen
= DefaultScreen(xdisplay
);
703 static int use_xft
= -1;
706 wxString val
= wxGetenv( L
"GDK_USE_XFT" );
707 use_xft
= (val
== L
"1");
711 ret
= pango_xft_get_context( xdisplay
, xscreen
);
714 ret
= pango_x_get_context( xdisplay
);
716 if (!PANGO_IS_CONTEXT(ret
))
717 wxLogError( wxT("No pango context.") );
723 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
725 if (!display
) /* Must be called first with non-NULL display */
726 return m_mainColormap
;
728 int defaultScreen
= DefaultScreen((Display
*) display
);
729 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
731 Colormap c
= DefaultColormapOfScreen(screen
);
734 m_mainColormap
= (WXColormap
) c
;
736 return (WXColormap
) c
;
739 Window
wxGetWindowParent(Window window
)
741 wxASSERT_MSG( window
, _T("invalid window") );
746 // VMS chokes on unreacheable code
747 Window parent
, root
= 0;
751 unsigned int noChildren
= 0;
753 Window
* children
= NULL
;
755 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
760 XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
761 & children
, & noChildren
);
775 wxAppConsole::Exit();
778 // Yield to other processes
780 bool wxApp::Yield(bool onlyIfNeeded
)
782 // Sometimes only 2 yields seem
783 // to do the trick, e.g. in the
786 for (i
= 0; i
< 2; i
++)
788 static bool s_inYield
= FALSE
;
794 wxFAIL_MSG( wxT("wxYield called recursively" ) );
802 // Make sure we have an event loop object,
803 // or Pending/Dispatch will fail
804 wxEventLoop
* eventLoop
= wxEventLoop::GetActive();
805 wxEventLoop
* newEventLoop
= NULL
;
808 newEventLoop
= new wxEventLoop
;
809 wxEventLoop::SetActive(newEventLoop
);
812 // Call dispatch at least once so that sockets
814 wxTheApp
->Dispatch();
816 while (wxTheApp
&& wxTheApp
->Pending())
817 wxTheApp
->Dispatch();
820 wxTimer::NotifyTimers();
826 wxEventLoop::SetActive(NULL
);
838 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
840 // While the GUI isn't working that well, just print out the
843 wxAppBase::OnAssert(file
, line
, cond
, msg
);
846 msg2
.Printf("At file %s:%d: %s", file
, line
, msg
);
851 #endif // __WXDEBUG__