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;
231 m_showIconic
= FALSE
;
232 m_initialSize
= wxDefaultSize
;
247 //-----------------------------------------------------------------------
248 // X11 predicate function for exposure compression
249 //-----------------------------------------------------------------------
254 Bool found_non_matching
;
257 static Bool
expose_predicate (Display
*display
, XEvent
*xevent
, XPointer arg
)
259 wxExposeInfo
*info
= (wxExposeInfo
*) arg
;
261 if (info
->found_non_matching
)
264 if (xevent
->xany
.type
!= Expose
)
266 info
->found_non_matching
= TRUE
;
270 if (xevent
->xexpose
.window
!= info
->window
)
272 info
->found_non_matching
= TRUE
;
281 //-----------------------------------------------------------------------
282 // Processes an X event, returning TRUE if the event was processed.
283 //-----------------------------------------------------------------------
285 bool wxApp::ProcessXEvent(WXEvent
* _event
)
287 XEvent
* event
= (XEvent
*) _event
;
289 wxWindow
* win
= NULL
;
290 Window window
= XEventGetWindow(event
);
292 Window actualWindow
= window
;
295 // Find the first wxWindow that corresponds to this event window
296 // Because we're receiving events after a window
297 // has been destroyed, assume a 1:1 match between
298 // Window and wxWindow, so if it's not in the table,
299 // it must have been destroyed.
301 win
= wxGetWindowFromTable(window
);
304 #if wxUSE_TWO_WINDOWS
305 win
= wxGetClientWindowFromTable(window
);
312 wxString windowClass
= win
->GetClassInfo()->GetClassName();
319 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
320 if (event
->xexpose
.window
!= (Window
)win
->GetClientAreaWindow())
324 info
.window
= event
->xexpose
.window
;
325 info
.found_non_matching
= FALSE
;
326 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
328 // Don't worry about optimizing redrawing the border etc.
330 win
->NeedUpdateNcAreaInIdle();
335 win
->GetUpdateRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
336 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
337 win
->GetClearRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
338 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
343 info
.window
= event
->xexpose
.window
;
344 info
.found_non_matching
= FALSE
;
345 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
347 win
->GetUpdateRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
348 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
350 win
->GetClearRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
351 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
355 // This simplifies the expose and clear areas to simple
357 win
->GetUpdateRegion() = win
->GetUpdateRegion().GetBox();
358 win
->GetClearRegion() = win
->GetClearRegion().GetBox();
360 // If we only have one X11 window, always indicate
361 // that borders might have to be redrawn.
362 if (win
->GetMainWindow() == win
->GetClientAreaWindow())
363 win
->NeedUpdateNcAreaInIdle();
365 // Only erase background, paint in idle time.
366 win
->SendEraseEvents();
378 printf( "GraphicExpose event\n" );
380 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win
->GetName().c_str());
382 win
->GetUpdateRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
383 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
385 win
->GetClearRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
386 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
388 if (event
->xgraphicsexpose
.count
== 0)
390 // Only erase background, paint in idle time.
391 win
->SendEraseEvents();
401 if (!win
->IsEnabled())
404 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
405 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
407 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
409 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
410 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
413 keyEvent
.SetEventType(wxEVT_CHAR
);
414 // Do the translation again, retaining the ASCII
416 wxTranslateKeyEvent(keyEvent
, win
, window
, event
, TRUE
);
417 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
420 if ( (keyEvent
.m_keyCode
== WXK_TAB
) &&
421 win
->GetParent() && (win
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
423 wxNavigationKeyEvent new_event
;
424 new_event
.SetEventObject( win
->GetParent() );
425 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
426 new_event
.SetDirection( (keyEvent
.m_keyCode
== WXK_TAB
) );
427 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
428 new_event
.SetWindowChange( keyEvent
.ControlDown() );
429 new_event
.SetCurrentFocus( win
);
430 return win
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
437 if (!win
->IsEnabled())
440 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
441 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
443 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
445 case ConfigureNotify
:
448 if (event
->update
.utype
== GR_UPDATE_SIZE
)
451 if (win
->IsTopLevel())
453 wxTopLevelWindow
*tlw
= (wxTopLevelWindow
*) win
;
454 tlw
->SetConfigureGeometry( XConfigureEventGetX(event
), XConfigureEventGetY(event
),
455 XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
) );
458 if (win
->IsTopLevel() && win
->IsShown())
460 wxTopLevelWindowX11
*tlw
= (wxTopLevelWindowX11
*) win
;
461 tlw
->SetNeedResizeInIdle();
465 wxSizeEvent
sizeEvent( wxSize(XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
)), win
->GetId() );
466 sizeEvent
.SetEventObject( win
);
468 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
477 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
478 return HandlePropertyChange(_event
);
482 if (!win
->IsEnabled())
485 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);
486 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);
488 if (event
->xclient
.message_type
== wm_protocols
)
490 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
501 printf( "destroy from %s\n", win
->GetName().c_str() );
506 printf( "create from %s\n", win
->GetName().c_str() );
511 printf( "map request from %s\n", win
->GetName().c_str() );
516 printf( "resize request from %s\n", win
->GetName().c_str() );
518 Display
*disp
= (Display
*) wxGetDisplay();
523 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
525 wxSize sz
= win
->GetSize();
526 wxSizeEvent
sizeEvent(sz
, win
->GetId());
527 sizeEvent
.SetEventObject(win
);
529 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
534 case GR_EVENT_TYPE_CLOSE_REQ
:
551 if (!win
->IsEnabled())
554 // Here we check if the top level window is
555 // disabled, which is one aspect of modality.
557 while (tlw
&& !tlw
->IsTopLevel())
558 tlw
= tlw
->GetParent();
559 if (tlw
&& !tlw
->IsEnabled())
562 if (event
->type
== ButtonPress
)
564 if ((win
!= wxWindow::FindFocus()) && win
->AcceptsFocus())
566 // This might actually be done in wxWindow::SetFocus()
567 // and not here. TODO.
568 g_prevFocus
= wxWindow::FindFocus();
571 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
573 // Record the fact that this window is
574 // getting the focus, because we'll need to
575 // check if its parent is getting a bogus
576 // focus and duly ignore it.
577 // TODO: may need to have this code in SetFocus, too.
578 extern wxWindow
* g_GettingFocus
;
579 g_GettingFocus
= win
;
585 if (event
->type
== LeaveNotify
|| event
->type
== EnterNotify
)
587 // Throw out NotifyGrab and NotifyUngrab
588 if (event
->xcrossing
.mode
!= NotifyNormal
)
592 wxMouseEvent wxevent
;
593 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
594 return win
->GetEventHandler()->ProcessEvent( wxevent
);
598 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
599 (event
->xfocus
.mode
== NotifyNormal
))
602 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
604 extern wxWindow
* g_GettingFocus
;
605 if (g_GettingFocus
&& g_GettingFocus
->GetParent() == win
)
607 // Ignore this, this can be a spurious FocusIn
608 // caused by a child having its focus set.
609 g_GettingFocus
= NULL
;
610 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
615 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
616 focusEvent
.SetEventObject(win
);
617 focusEvent
.SetWindow( g_prevFocus
);
620 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
627 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
628 (event
->xfocus
.mode
== NotifyNormal
))
631 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
633 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
634 focusEvent
.SetEventObject(win
);
635 focusEvent
.SetWindow( g_nextFocus
);
637 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
643 //wxString eventName = wxGetXEventName(XEvent& event);
644 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
646 #endif // __WXDEBUG__
652 // This should be redefined in a derived class for
653 // handling property change events for XAtom IPC.
654 bool wxApp::HandlePropertyChange(WXEvent
*event
)
656 // by default do nothing special
657 // TODO: what to do for X11
658 // XtDispatchEvent((XEvent*) event);
662 void wxApp::WakeUpIdle()
664 // TODO: use wxMotif implementation?
666 // Wake up the idle handler processor, even if it is in another thread...
670 // Create display, and other initialization
671 bool wxApp::OnInitGui()
673 // Eventually this line will be removed, but for
674 // now we don't want to try popping up a dialog
675 // for error messages.
676 delete wxLog::SetActiveTarget(new wxLogStderr
);
678 if (!wxAppBase::OnInitGui())
681 GetMainColormap( wxApp::GetDisplay() );
683 m_maxRequestSize
= XMaxRequestSize( (Display
*) wxApp::GetDisplay() );
686 m_visualInfo
= new wxXVisualInfo
;
687 wxFillXVisualInfo( m_visualInfo
, (Display
*) wxApp::GetDisplay() );
695 #include <pango/pango.h>
696 #include <pango/pangox.h>
697 #include <pango/pangoxft.h>
699 PangoContext
* wxApp::GetPangoContext()
701 static PangoContext
*ret
= NULL
;
705 Display
*xdisplay
= (Display
*) wxApp::GetDisplay();
708 int xscreen
= DefaultScreen(xdisplay
);
709 static int use_xft
= -1;
712 wxString val
= wxGetenv( L
"GDK_USE_XFT" );
713 use_xft
= (val
== L
"1");
717 ret
= pango_xft_get_context( xdisplay
, xscreen
);
720 ret
= pango_x_get_context( xdisplay
);
722 if (!PANGO_IS_CONTEXT(ret
))
723 wxLogError( wxT("No pango context.") );
729 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
731 if (!display
) /* Must be called first with non-NULL display */
732 return m_mainColormap
;
734 int defaultScreen
= DefaultScreen((Display
*) display
);
735 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
737 Colormap c
= DefaultColormapOfScreen(screen
);
740 m_mainColormap
= (WXColormap
) c
;
742 return (WXColormap
) c
;
745 Window
wxGetWindowParent(Window window
)
747 wxASSERT_MSG( window
, _T("invalid window") );
751 Window parent
, root
= 0;
755 unsigned int noChildren
= 0;
757 Window
* children
= NULL
;
759 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
764 XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
765 & 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 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__