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
;
246 bool wxApp::Initialized()
255 //-----------------------------------------------------------------------
256 // X11 predicate function for exposure compression
257 //-----------------------------------------------------------------------
262 Bool found_non_matching
;
265 static Bool
expose_predicate (Display
*display
, XEvent
*xevent
, XPointer arg
)
267 wxExposeInfo
*info
= (wxExposeInfo
*) arg
;
269 if (info
->found_non_matching
)
272 if (xevent
->xany
.type
!= Expose
)
274 info
->found_non_matching
= TRUE
;
278 if (xevent
->xexpose
.window
!= info
->window
)
280 info
->found_non_matching
= TRUE
;
289 //-----------------------------------------------------------------------
290 // Processes an X event, returning TRUE if the event was processed.
291 //-----------------------------------------------------------------------
293 bool wxApp::ProcessXEvent(WXEvent
* _event
)
295 XEvent
* event
= (XEvent
*) _event
;
297 wxWindow
* win
= NULL
;
298 Window window
= XEventGetWindow(event
);
300 Window actualWindow
= window
;
303 // Find the first wxWindow that corresponds to this event window
304 // Because we're receiving events after a window
305 // has been destroyed, assume a 1:1 match between
306 // Window and wxWindow, so if it's not in the table,
307 // it must have been destroyed.
309 win
= wxGetWindowFromTable(window
);
312 #if wxUSE_TWO_WINDOWS
313 win
= wxGetClientWindowFromTable(window
);
320 wxString windowClass
= win
->GetClassInfo()->GetClassName();
327 #if wxUSE_TWO_WINDOWS && !wxUSE_NANOX
328 if (event
->xexpose
.window
!= (Window
)win
->GetClientAreaWindow())
332 info
.window
= event
->xexpose
.window
;
333 info
.found_non_matching
= FALSE
;
334 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
336 // Don't worry about optimizing redrawing the border etc.
338 win
->NeedUpdateNcAreaInIdle();
343 win
->GetUpdateRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
344 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
345 win
->GetClearRegion().Union( XExposeEventGetX(event
), XExposeEventGetY(event
),
346 XExposeEventGetWidth(event
), XExposeEventGetHeight(event
));
351 info
.window
= event
->xexpose
.window
;
352 info
.found_non_matching
= FALSE
;
353 while (XCheckIfEvent( wxGlobalDisplay(), &tmp_event
, expose_predicate
, (XPointer
) &info
))
355 win
->GetUpdateRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
356 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
358 win
->GetClearRegion().Union( tmp_event
.xexpose
.x
, tmp_event
.xexpose
.y
,
359 tmp_event
.xexpose
.width
, tmp_event
.xexpose
.height
);
363 // This simplifies the expose and clear areas to simple
365 win
->GetUpdateRegion() = win
->GetUpdateRegion().GetBox();
366 win
->GetClearRegion() = win
->GetClearRegion().GetBox();
368 // If we only have one X11 window, always indicate
369 // that borders might have to be redrawn.
370 if (win
->GetMainWindow() == win
->GetClientAreaWindow())
371 win
->NeedUpdateNcAreaInIdle();
373 // Only erase background, paint in idle time.
374 win
->SendEraseEvents();
386 printf( "GraphicExpose event\n" );
388 wxLogTrace( _T("expose"), _T("GraphicsExpose from %s"), win
->GetName().c_str());
390 win
->GetUpdateRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
391 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
393 win
->GetClearRegion().Union( event
->xgraphicsexpose
.x
, event
->xgraphicsexpose
.y
,
394 event
->xgraphicsexpose
.width
, event
->xgraphicsexpose
.height
);
396 if (event
->xgraphicsexpose
.count
== 0)
398 // Only erase background, paint in idle time.
399 win
->SendEraseEvents();
409 if (!win
->IsEnabled())
412 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
413 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
415 // wxLogDebug( "OnKey from %s", win->GetName().c_str() );
417 // We didn't process wxEVT_KEY_DOWN, so send wxEVT_CHAR
418 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
421 keyEvent
.SetEventType(wxEVT_CHAR
);
422 // Do the translation again, retaining the ASCII
424 wxTranslateKeyEvent(keyEvent
, win
, window
, event
, TRUE
);
425 if (win
->GetEventHandler()->ProcessEvent( keyEvent
))
428 if ( (keyEvent
.m_keyCode
== WXK_TAB
) &&
429 win
->GetParent() && (win
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
431 wxNavigationKeyEvent new_event
;
432 new_event
.SetEventObject( win
->GetParent() );
433 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
434 new_event
.SetDirection( (keyEvent
.m_keyCode
== WXK_TAB
) );
435 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
436 new_event
.SetWindowChange( keyEvent
.ControlDown() );
437 new_event
.SetCurrentFocus( win
);
438 return win
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
445 if (!win
->IsEnabled())
448 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
449 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
451 return win
->GetEventHandler()->ProcessEvent( keyEvent
);
453 case ConfigureNotify
:
456 if (event
->update
.utype
== GR_UPDATE_SIZE
)
459 if (win
->IsTopLevel())
461 wxTopLevelWindow
*tlw
= (wxTopLevelWindow
*) win
;
462 tlw
->SetConfigureGeometry( XConfigureEventGetX(event
), XConfigureEventGetY(event
),
463 XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
) );
466 if (win
->IsTopLevel() && win
->IsShown())
468 wxTopLevelWindowX11
*tlw
= (wxTopLevelWindowX11
*) win
;
469 tlw
->SetNeedResizeInIdle();
473 wxSizeEvent
sizeEvent( wxSize(XConfigureEventGetWidth(event
), XConfigureEventGetHeight(event
)), win
->GetId() );
474 sizeEvent
.SetEventObject( win
);
476 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
485 //wxLogDebug("PropertyNotify: %s", windowClass.c_str());
486 return HandlePropertyChange(_event
);
490 if (!win
->IsEnabled())
493 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);
494 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);
496 if (event
->xclient
.message_type
== wm_protocols
)
498 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
509 printf( "destroy from %s\n", win
->GetName().c_str() );
514 printf( "create from %s\n", win
->GetName().c_str() );
519 printf( "map request from %s\n", win
->GetName().c_str() );
524 printf( "resize request from %s\n", win
->GetName().c_str() );
526 Display
*disp
= (Display
*) wxGetDisplay();
531 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
533 wxSize sz
= win
->GetSize();
534 wxSizeEvent
sizeEvent(sz
, win
->GetId());
535 sizeEvent
.SetEventObject(win
);
537 return win
->GetEventHandler()->ProcessEvent( sizeEvent
);
542 case GR_EVENT_TYPE_CLOSE_REQ
:
559 if (!win
->IsEnabled())
562 // Here we check if the top level window is
563 // disabled, which is one aspect of modality.
565 while (tlw
&& !tlw
->IsTopLevel())
566 tlw
= tlw
->GetParent();
567 if (tlw
&& !tlw
->IsEnabled())
570 if (event
->type
== ButtonPress
)
572 if ((win
!= wxWindow::FindFocus()) && win
->AcceptsFocus())
574 // This might actually be done in wxWindow::SetFocus()
575 // and not here. TODO.
576 g_prevFocus
= wxWindow::FindFocus();
579 wxLogTrace( _T("focus"), _T("About to call SetFocus on %s of type %s due to button press"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
581 // Record the fact that this window is
582 // getting the focus, because we'll need to
583 // check if its parent is getting a bogus
584 // focus and duly ignore it.
585 // TODO: may need to have this code in SetFocus, too.
586 extern wxWindow
* g_GettingFocus
;
587 g_GettingFocus
= win
;
593 if (event
->type
== LeaveNotify
|| event
->type
== EnterNotify
)
595 // Throw out NotifyGrab and NotifyUngrab
596 if (event
->xcrossing
.mode
!= NotifyNormal
)
600 wxMouseEvent wxevent
;
601 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
602 return win
->GetEventHandler()->ProcessEvent( wxevent
);
606 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
607 (event
->xfocus
.mode
== NotifyNormal
))
610 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
612 extern wxWindow
* g_GettingFocus
;
613 if (g_GettingFocus
&& g_GettingFocus
->GetParent() == win
)
615 // Ignore this, this can be a spurious FocusIn
616 // caused by a child having its focus set.
617 g_GettingFocus
= NULL
;
618 wxLogTrace( _T("focus"), _T("FocusIn from %s of type %s being deliberately ignored"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
623 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
624 focusEvent
.SetEventObject(win
);
625 focusEvent
.SetWindow( g_prevFocus
);
628 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
635 if ((event
->xfocus
.detail
!= NotifyPointer
) &&
636 (event
->xfocus
.mode
== NotifyNormal
))
639 wxLogTrace( _T("focus"), _T("FocusOut from %s of type %s"), win
->GetName().c_str(), win
->GetClassInfo()->GetClassName() );
641 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
642 focusEvent
.SetEventObject(win
);
643 focusEvent
.SetWindow( g_nextFocus
);
645 return win
->GetEventHandler()->ProcessEvent(focusEvent
);
651 //wxString eventName = wxGetXEventName(XEvent& event);
652 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
654 #endif // __WXDEBUG__
660 // This should be redefined in a derived class for
661 // handling property change events for XAtom IPC.
662 bool wxApp::HandlePropertyChange(WXEvent
*event
)
664 // by default do nothing special
665 // TODO: what to do for X11
666 // XtDispatchEvent((XEvent*) event);
670 void wxApp::WakeUpIdle()
672 // TODO: use wxMotif implementation?
674 // Wake up the idle handler processor, even if it is in another thread...
678 // Create display, and other initialization
679 bool wxApp::OnInitGui()
681 // Eventually this line will be removed, but for
682 // now we don't want to try popping up a dialog
683 // for error messages.
684 delete wxLog::SetActiveTarget(new wxLogStderr
);
686 if (!wxAppBase::OnInitGui())
689 GetMainColormap( wxApp::GetDisplay() );
691 m_maxRequestSize
= XMaxRequestSize( (Display
*) wxApp::GetDisplay() );
694 m_visualInfo
= new wxXVisualInfo
;
695 wxFillXVisualInfo( m_visualInfo
, (Display
*) wxApp::GetDisplay() );
703 #include <pango/pango.h>
704 #include <pango/pangox.h>
705 #include <pango/pangoxft.h>
707 PangoContext
* wxApp::GetPangoContext()
709 static PangoContext
*ret
= NULL
;
713 Display
*xdisplay
= (Display
*) wxApp::GetDisplay();
716 int xscreen
= DefaultScreen(xdisplay
);
717 static int use_xft
= -1;
720 wxString val
= wxGetenv( L
"GDK_USE_XFT" );
721 use_xft
= (val
== L
"1");
725 ret
= pango_xft_get_context( xdisplay
, xscreen
);
728 ret
= pango_x_get_context( xdisplay
);
730 if (!PANGO_IS_CONTEXT(ret
))
731 wxLogError( wxT("No pango context.") );
737 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
739 if (!display
) /* Must be called first with non-NULL display */
740 return m_mainColormap
;
742 int defaultScreen
= DefaultScreen((Display
*) display
);
743 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
745 Colormap c
= DefaultColormapOfScreen(screen
);
748 m_mainColormap
= (WXColormap
) c
;
750 return (WXColormap
) c
;
753 Window
wxGetWindowParent(Window window
)
755 wxASSERT_MSG( window
, _T("invalid window") );
759 Window parent
, root
= 0;
763 unsigned int noChildren
= 0;
765 Window
* children
= NULL
;
767 // #define XQueryTree(d,w,r,p,c,nc) GrQueryTree(w,p,c,nc)
772 XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
773 & children
, & noChildren
);
786 wxAppConsole::Exit();
789 // Yield to other processes
791 bool wxApp::Yield(bool onlyIfNeeded
)
793 // Sometimes only 2 yields seem
794 // to do the trick, e.g. in the
797 for (i
= 0; i
< 2; i
++)
799 bool s_inYield
= FALSE
;
805 wxFAIL_MSG( wxT("wxYield called recursively" ) );
813 // Make sure we have an event loop object,
814 // or Pending/Dispatch will fail
815 wxEventLoop
* eventLoop
= wxEventLoop::GetActive();
816 wxEventLoop
* newEventLoop
= NULL
;
819 newEventLoop
= new wxEventLoop
;
820 wxEventLoop::SetActive(newEventLoop
);
823 // Call dispatch at least once so that sockets
825 wxTheApp
->Dispatch();
827 while (wxTheApp
&& wxTheApp
->Pending())
828 wxTheApp
->Dispatch();
831 wxTimer::NotifyTimers();
837 wxEventLoop::SetActive(NULL
);
849 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
851 // While the GUI isn't working that well, just print out the
854 wxAppBase::OnAssert(file
, line
, cond
, msg
);
857 msg2
.Printf("At file %s:%d: %s", file
, line
, msg
);
862 #endif // __WXDEBUG__