1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "app.h"
19 #include "wx/gdicmn.h"
21 #include "wx/dialog.h"
23 #include "wx/module.h"
24 #include "wx/memory.h"
27 #include "wx/evtloop.h"
29 #include "wx/filename.h"
32 #include "wx/univ/theme.h"
33 #include "wx/univ/renderer.h"
36 #include "wx/thread.h"
39 #include "wx/x11/private.h"
43 //------------------------------------------------------------------------
45 //------------------------------------------------------------------------
47 extern wxList wxPendingDelete
;
49 wxHashTable
*wxWidgetHashTable
= NULL
;
50 wxHashTable
*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;
86 WXDisplay
*wxApp::ms_display
= NULL
;
88 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
90 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
91 EVT_IDLE(wxAppBase::OnIdle
)
94 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
96 #if defined(__WXDEBUG__) && !wxUSE_NANOX
97 // install the X error handler
98 gs_pfnXErrorHandler
= XSetErrorHandler( wxXErrorHandler
);
101 wxString displayName
;
102 bool syncDisplay
= FALSE
;
105 for ( int i
= 0; i
< argcOrig
; i
++ )
107 if (wxStrcmp( argv
[i
], _T("-display") ) == 0)
113 displayName
= argv
[i
];
119 else if (wxStrcmp( argv
[i
], _T("-geometry") ) == 0)
126 if (wxSscanf(argv
[i
], _T("%dx%d"), &w
, &h
) != 2)
128 wxLogError( _("Invalid geometry specification '%s'"),
129 wxString(argv
[i
]).c_str() );
133 g_initialSize
= wxSize(w
, h
);
140 else if (wxStrcmp( argv
[i
], _T("-sync") ) == 0)
147 else if (wxStrcmp( argv
[i
], _T("-iconic") ) == 0)
156 if ( argc
!= argcOrig
)
158 // remove the argumens we consumed
159 for ( int i
= 0; i
< argc
; i
++ )
163 memmove(argv
+ i
, argv
+ i
+ 1, argcOrig
- i
);
170 if ( displayName
.empty() )
171 xdisplay
= XOpenDisplay( NULL
);
173 xdisplay
= XOpenDisplay( displayName
.ToAscii() );
176 wxLogError( _("wxWindows could not open display. Exiting.") );
181 XSynchronize(xdisplay
, True
);
183 ms_display
= (WXDisplay
*) xdisplay
;
185 XSelectInput( xdisplay
, XDefaultRootWindow(xdisplay
), PropertyChangeMask
);
188 wxSetDetectableAutoRepeat( TRUE
);
190 if ( !wxAppBase::Initialize(argc
, argv
) )
192 XCloseDisplay(xdisplay
);
198 // Glib's type system required by Pango
203 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
206 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
207 wxClientWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
212 void wxApp::CleanUp()
214 delete wxWidgetHashTable
;
215 wxWidgetHashTable
= NULL
;
216 delete wxClientWidgetHashTable
;
217 wxClientWidgetHashTable
= NULL
;
219 wxAppBase::CleanUp();
224 // TODO: parse the command line
228 m_mainColormap
= (WXColormap
) NULL
;
229 m_topLevelWidget
= (WXWindow
) NULL
;
230 m_maxRequestSize
= 0;
232 m_showIconic
= FALSE
;
233 m_initialSize
= wxDefaultSize
;
247 bool wxApp::Initialized()
255 int wxApp::MainLoop()
258 m_mainLoop
= new wxEventLoop
;
260 rt
= m_mainLoop
->Run();
268 //-----------------------------------------------------------------------
269 // X11 predicate function for exposure compression
270 //-----------------------------------------------------------------------
275 Bool found_non_matching
;
278 static Bool
expose_predicate (Display
*display
, XEvent
*xevent
, XPointer arg
)
280 wxExposeInfo
*info
= (wxExposeInfo
*) arg
;
282 if (info
->found_non_matching
)
285 if (xevent
->xany
.type
!= Expose
)
287 info
->found_non_matching
= TRUE
;
291 if (xevent
->xexpose
.window
!= info
->window
)
293 info
->found_non_matching
= TRUE
;
302 //-----------------------------------------------------------------------
303 // Processes an X event, returning TRUE if the event was processed.
304 //-----------------------------------------------------------------------
306 bool wxApp::ProcessXEvent(WXEvent
* _event
)
308 XEvent
* event
= (XEvent
*) _event
;
310 wxWindow
* win
= NULL
;
311 Window window
= XEventGetWindow(event
);
313 Window actualWindow
= window
;
316 // Find the first wxWindow that corresponds to this event window
317 // Because we're receiving events after a window
318 // has been destroyed, assume a 1:1 match between
319 // Window and wxWindow, so if it's not in the table,
320 // it must have been destroyed.
322 win
= wxGetWindowFromTable(window
);
325 #if wxUSE_TWO_WINDOWS
326 win
= wxGetClientWindowFromTable(window
);
333 wxString windowClass
= win
->GetClassInfo()->GetClassName();
340 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
341 if (event
->xexpose
.window
!= (Window
)win
->GetClientAreaWindow())
345 info
.window
= event
->xexpose
.window
;
346 info
.found_non_matching
= FALSE
;
347 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
349 // Don't worry about optimizing redrawing the border etc.
351 win
->NeedUpdateNcAreaInIdle();
356 win
->GetUpdateRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
357 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
358 win
->GetClearRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
359 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
364 info
.window
= event
->xexpose
.window
;
365 info
.found_non_matching
= FALSE
;
366 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
368 win
->GetUpdateRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
369 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
371 win
->GetClearRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
372 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
376 // This simplifies the expose and clear areas to simple
378 win
->GetUpdateRegion() = win
->GetUpdateRegion().GetBox();
379 win
->GetClearRegion() = win
->GetClearRegion().GetBox();
381 // If we only have one X11 window, always indicate
382 // that borders might have to be redrawn.
383 if (win
->GetMainWindow() == win
->GetClientAreaWindow())
384 win
->NeedUpdateNcAreaInIdle();
386 // Only erase background, paint in idle time.
387 win
->SendEraseEvents();
399 printf( "GraphicExpose event\n" );
401 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win
->GetName().c_str());
403 win
->GetUpdateRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
404 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
406 win
->GetClearRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
407 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
409 if (event
->xgraphicsexpose
.count
== 0)
411 // Only erase background, paint in idle time.
412 win
->SendEraseEvents();
422 if (!win
->IsEnabled())
425 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
426 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
428 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
430 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
431 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
434 keyEvent
.SetEventType(wxEVT_CHAR
);
435 // Do the translation again, retaining the ASCII
437 wxTranslateKeyEvent(keyEvent
, win
, window
, event
, TRUE
);
438 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
441 if ( (keyEvent
.m_keyCode
== WXK_TAB
) &&
442 win
->GetParent() && (win
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
444 wxNavigationKeyEvent new_event
;
445 new_event
.SetEventObject( win
->GetParent() );
446 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
447 new_event
.SetDirection( (keyEvent
.m_keyCode
== WXK_TAB
) );
448 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
449 new_event
.SetWindowChange( keyEvent
.ControlDown() );
450 new_event
.SetCurrentFocus( win
);
451 return win
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
458 if (!win
->IsEnabled())
461 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
462 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
464 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
466 case ConfigureNotify
:
469 if (event
->update
.utype
== GR_UPDATE_SIZE
)
472 if (win
->IsTopLevel())
474 wxTopLevelWindow
*tlw
= (wxTopLevelWindow
*) win
;
475 tlw
->SetConfigureGeometry( XConfigureEventGetX(event
), XConfigureEventGetY(event
),
476 XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
) );
479 if (win
->IsTopLevel() && win
->IsShown())
481 wxTopLevelWindowX11
*tlw
= (wxTopLevelWindowX11
*) win
;
482 tlw
->SetNeedResizeInIdle();
486 wxSizeEvent
sizeEvent( wxSize(XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
)), win
->GetId() );
487 sizeEvent
.SetEventObject( win
);
489 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
498 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
499 return HandlePropertyChange(_event
);
503 if (!win
->IsEnabled())
506 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);
507 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);
509 if (event
->xclient
.message_type
== wm_protocols
)
511 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
522 printf( "destroy from %s\n", win
->GetName().c_str() );
527 printf( "create from %s\n", win
->GetName().c_str() );
532 printf( "map request from %s\n", win
->GetName().c_str() );
537 printf( "resize request from %s\n", win
->GetName().c_str() );
539 Display
*disp
= (Display
*) wxGetDisplay();
544 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
546 wxSize sz
= win
->GetSize();
547 wxSizeEvent
sizeEvent(sz
, win
->GetId());
548 sizeEvent
.SetEventObject(win
);
550 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
555 case GR_EVENT_TYPE_CLOSE_REQ
:
572 if (!win
->IsEnabled())
575 // Here we check if the top level window is
576 // disabled, which is one aspect of modality.
578 while (tlw
&& !tlw
->IsTopLevel())
579 tlw
= tlw
->GetParent();
580 if (tlw
&& !tlw
->IsEnabled())
583 if (event
->type
== ButtonPress
)
585 if ((win
!= wxWindow::FindFocus()) && win
->AcceptsFocus())
587 // This might actually be done in wxWindow::SetFocus()
588 // and not here. TODO.
589 g_prevFocus
= wxWindow::FindFocus();
592 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
594 // Record the fact that this window is
595 // getting the focus, because we'll need to
596 // check if its parent is getting a bogus
597 // focus and duly ignore it.
598 // TODO: may need to have this code in SetFocus, too.
599 extern wxWindow
* g_GettingFocus
;
600 g_GettingFocus
= win
;
606 if (event
->type
== LeaveNotify
|| event
->type
== EnterNotify
)
608 // Throw out NotifyGrab and NotifyUngrab
609 if (event
->xcrossing
.mode
!= NotifyNormal
)
613 wxMouseEvent wxevent
;
614 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
615 return win
->GetEventHandler()->ProcessEvent( wxevent
);
620 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
621 (event
->xfocus
.mode
== NotifyNormal
))
624 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
626 extern wxWindow
* g_GettingFocus
;
627 if (g_GettingFocus
&& g_GettingFocus
->GetParent() == win
)
629 // Ignore this, this can be a spurious FocusIn
630 // caused by a child having its focus set.
631 g_GettingFocus
= NULL
;
632 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
637 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
638 focusEvent
.SetEventObject(win
);
639 focusEvent
.SetWindow( g_prevFocus
);
642 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
651 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
652 (event
->xfocus
.mode
== NotifyNormal
))
655 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
657 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
658 focusEvent
.SetEventObject(win
);
659 focusEvent
.SetWindow( g_nextFocus
);
661 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
669 //wxString eventName = wxGetXEventName(XEvent& event);
670 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
679 void wxApp::ExitMainLoop()
685 // Is a message/event pending?
686 bool wxApp::Pending()
688 return wxEventLoop::GetActive()->Pending();
691 // Dispatch a message.
692 void wxApp::Dispatch()
694 wxEventLoop::GetActive()->Dispatch();
697 // This should be redefined in a derived class for
698 // handling property change events for XAtom IPC.
699 bool wxApp::HandlePropertyChange(WXEvent
*event
)
701 // by default do nothing special
702 // TODO: what to do for X11
703 // XtDispatchEvent((XEvent*) event);
707 void wxApp::WakeUpIdle()
709 // TODO: use wxMotif implementation?
711 // Wake up the idle handler processor, even if it is in another thread...
715 // Create display, and other initialization
716 bool wxApp::OnInitGui()
718 // Eventually this line will be removed, but for
719 // now we don't want to try popping up a dialog
720 // for error messages.
721 delete wxLog::SetActiveTarget(new wxLogStderr
);
723 if (!wxAppBase::OnInitGui())
726 GetMainColormap( wxApp::GetDisplay() );
728 m_maxRequestSize
= XMaxRequestSize( (Display
*) wxApp::GetDisplay() );
731 m_visualInfo
= new wxXVisualInfo
;
732 wxFillXVisualInfo( m_visualInfo
, (Display
*) wxApp::GetDisplay() );
740 #include <pango/pango.h>
741 #include <pango/pangox.h>
742 #include <pango/pangoxft.h>
744 PangoContext
* wxApp::GetPangoContext()
746 static PangoContext
*ret
= NULL
;
750 Display
*xdisplay
= (Display
*) wxApp::GetDisplay();
753 int xscreen
= DefaultScreen(xdisplay
);
754 static int use_xft
= -1;
757 wxString val
= wxGetenv( L
"GDK_USE_XFT" );
758 use_xft
= (val
== L
"1");
762 ret
= pango_xft_get_context( xdisplay
, xscreen
);
765 ret
= pango_x_get_context( xdisplay
);
767 if (!PANGO_IS_CONTEXT(ret
))
768 wxLogError( wxT("No pango context.") );
774 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
776 if (!display
) /* Must be called first with non-NULL display */
777 return m_mainColormap
;
779 int defaultScreen
= DefaultScreen((Display
*) display
);
780 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
782 Colormap c
= DefaultColormapOfScreen(screen
);
785 m_mainColormap
= (WXColormap
) c
;
787 return (WXColormap
) c
;
790 Window
wxGetWindowParent(Window window
)
792 wxASSERT_MSG( window
, _T("invalid window") );
796 Window parent
, root
= 0;
800 unsigned int noChildren
= 0;
802 Window
* children
= NULL
;
804 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
809 XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
810 & children
, & noChildren
);
823 wxAppConsole::Exit();
826 // Yield to other processes
828 bool wxApp::Yield(bool onlyIfNeeded
)
830 // Sometimes only 2 yields seem
831 // to do the trick, e.g. in the
834 for (i
= 0; i
< 2; i
++)
836 bool s_inYield
= FALSE
;
842 wxFAIL_MSG( wxT("wxYield called recursively" ) );
850 // Make sure we have an event loop object,
851 // or Pending/Dispatch will fail
852 wxEventLoop
* eventLoop
= wxEventLoop::GetActive();
853 wxEventLoop
* newEventLoop
= NULL
;
856 newEventLoop
= new wxEventLoop
;
857 wxEventLoop::SetActive(newEventLoop
);
860 // Call dispatch at least once so that sockets
862 wxTheApp
->Dispatch();
864 while (wxTheApp
&& wxTheApp
->Pending())
865 wxTheApp
->Dispatch();
868 wxTimer::NotifyTimers();
874 wxEventLoop::SetActive(NULL
);
886 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
888 // While the GUI isn't working that well, just print out the
891 wxAppBase::OnAssert(file
, line
, cond
, msg
);
894 msg2
.Printf("At file %s:%d: %s", file
, line
, msg
);
899 #endif // __WXDEBUG__