1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/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"
16 #define XtParent XTPARENT
17 #define XtDisplay XTDISPLAY
27 #include "wx/memory.h"
31 #include "wx/evtloop.h"
34 #include "wx/thread.h"
38 #pragma message disable nosimpint
42 #include <X11/Xutil.h>
43 #include <X11/Xresource.h>
44 #include <X11/Xatom.h>
46 #pragma message enable nosimpint
49 #include "wx/motif/private.h"
53 struct wxPerDisplayData
58 m_topLevelWidget
= NULL
;
59 m_topLevelRealizedWidget
= NULL
;
62 wxXVisualInfo
* m_visualInfo
;
63 Widget m_topLevelWidget
, m_topLevelRealizedWidget
;
66 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
68 static WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
);
70 extern bool wxAddIdleCallback();
72 wxHashTable
*wxWidgetHashTable
= NULL
;
74 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
76 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
77 EVT_IDLE(wxAppBase::OnIdle
)
83 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
86 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
91 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
93 // just forward to the default handler for now
94 return gs_pfnXErrorHandler(dpy
, xevent
);
100 bool wxApp::Initialize(int& argcOrig
, wxChar
**argvOrig
)
103 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
106 if ( !wxAppBase::Initialize(argcOrig
, argvOrig
) )
109 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
114 void wxApp::CleanUp()
116 wxAppBase::CleanUp();
118 delete wxWidgetHashTable
;
119 wxWidgetHashTable
= NULL
;
123 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
124 end
= m_perDisplayData
->end();
127 delete it
->second
->m_visualInfo
;
128 // On Solaris 10 calling XtDestroyWidget on the top level widget
129 // dumps core if the locale is set to something other than "C"
131 XtDestroyWidget( it
->second
->m_topLevelWidget
);
141 wxAppConsole::Exit();
144 // ============================================================================
146 // ============================================================================
153 m_mainLoop
= new wxEventLoop
;
154 m_mainColormap
= (WXColormap
) NULL
;
155 m_appContext
= (WXAppContext
) NULL
;
156 m_initialDisplay
= (WXDisplay
*) 0;
157 m_perDisplayData
= new wxPerDisplayDataMap
;
162 delete m_perDisplayData
;
165 int wxApp::MainLoop()
168 * Sit around forever waiting to process X-events. Property Change
169 * event are handled special, because they have to refer to
170 * the root window rather than to a widget. therefore we can't
171 * use an Xt-eventhandler.
174 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
175 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
183 // This should be redefined in a derived class for
184 // handling property change events for XAtom IPC.
185 void wxApp::HandlePropertyChange(WXEvent
*event
)
187 // by default do nothing special
188 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
191 static char *fallbackResources
[] = {
192 // better defaults for CDE under Irix
194 // TODO: do something similar for the other systems, the hardcoded defaults
197 wxMOTIF_STR("*sgiMode: True"),
198 wxMOTIF_STR("*useSchemes: all"),
200 #if !wxMOTIF_USE_RENDER_TABLE
201 wxMOTIF_STR("*.fontList: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"),
203 wxMOTIF_STR("*wxDefaultRendition.fontName: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"),
204 wxMOTIF_STR("*wxDefaultRendition.fontType: FONT_IS_FONTSET"),
205 wxMOTIF_STR("*.renderTable: wxDefaultRendition"),
207 wxMOTIF_STR("*listBox.background: white"),
208 wxMOTIF_STR("*text.background: white"),
209 wxMOTIF_STR("*comboBox.Text.background: white"),
210 wxMOTIF_STR("*comboBox.List.background: white"),
211 #endif // __SGI__/!__SGI__
215 // Create an application context
216 bool wxApp::OnInitGui()
218 if( !wxAppBase::OnInitGui() )
222 // under HP-UX creating XmFontSet fails when the system locale is C and
223 // we're using a remote DISPLAY, presumably because HP-UX uses its own
224 // names for C and ISO locales (roman8 and iso8859n respectively) and so
225 // its Motif libraries have troubles with non-HP X server
227 // whatever the reason, the fact is that without this hack any wxMotif
228 // program crashes on startup because it can't create any font (HP programs
229 // still work but they do spit out messages about failing to create font
230 // sets and failing back on "fixed" font too)
232 // notice that calling setlocale() here is not enough because X(m) init
233 // functions call setlocale() later so we really have to change environment
234 bool fixAll
= false; // tweak LC_ALL (or just LC_CTYPE)?
235 const char *loc
= getenv("LC_CTYPE");
238 loc
= getenv("LC_ALL");
244 (loc
[0] == 'C' && loc
[1] == '\0') ||
245 strcmp(loc
, "POSIX") == 0 )
247 // we're using C locale, "fix" it
248 wxLogDebug(_T("HP-UX fontset hack: forcing locale to en_US.iso88591"));
249 putenv(fixAll
? "LC_ALL=en_US.iso88591" : "LC_CTYPE=en_US.iso88591");
253 XtSetLanguageProc(NULL
, NULL
, NULL
);
254 XtToolkitInitialize() ;
255 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
256 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
258 // we shouldn't pass empty application/class name as it results in
259 // immediate crash inside XOpenIM() (if XIM is used) under IRIX
260 wxString appname
= wxTheApp
->GetAppName();
261 if ( appname
.empty() )
262 appname
= _T("wxapp");
263 wxString clsname
= wxTheApp
->GetClassName();
264 if ( clsname
.empty() )
267 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,
271 NULL
, 0, // no options
272 # if XtSpecificationRelease < 5
280 // if you don't log to stderr, nothing will be shown...
281 delete wxLog::SetActiveTarget(new wxLogStderr
);
282 wxString
className(wxTheApp
->GetClassName());
283 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
287 m_initialDisplay
= (WXDisplay
*) dpy
;
290 // install the X error handler
291 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
292 #endif // __WXDEBUG__
294 // Add general resize proc
296 rec
.string
= wxMOTIF_STR("resize");
297 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
298 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
300 GetMainColormap(dpy
);
307 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
309 if (!display
) /* Must be called first with non-NULL display */
310 return m_mainColormap
;
312 int defaultScreen
= DefaultScreen((Display
*) display
);
313 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
315 Colormap c
= DefaultColormapOfScreen(screen
);
318 m_mainColormap
= (WXColormap
) c
;
320 return (WXColormap
) c
;
323 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
324 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
326 wxPerDisplayDataMap::iterator it
= m
.find( display
);
327 if( it
!= m
.end() && it
->second
!= NULL
)
328 return *(it
->second
);
330 wxPerDisplayData
* nData
= new wxPerDisplayData();
336 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
338 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
340 if( data
.m_visualInfo
)
341 return data
.m_visualInfo
;
343 wxXVisualInfo
* vi
= new wxXVisualInfo
;
344 wxFillXVisualInfo( vi
, (Display
*)display
);
346 data
.m_visualInfo
= vi
;
351 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer
WXUNUSED(clientData
),
352 XtPointer
WXUNUSED(ptr
))
356 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
358 wxTheApp
->SetTopLevelRealizedWidget( (WXDisplay
*)XtDisplay(w
),
363 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
365 Widget tlw
= XtAppCreateShell( (String
)NULL
,
366 wxTheApp
->GetClassName().c_str(),
367 applicationShellWidgetClass
,
371 XmNoverrideRedirect
, True
,
374 XtAddCallback( tlw
, XmNdestroyCallback
,
375 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
378 return (WXWidget
)tlw
;
381 WXWidget
wxCreateTopLevelRealizedWidget( WXDisplay
* WXUNUSED(display
) )
383 Widget rTlw
= XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass
,
384 (Widget
)wxTheApp
->GetTopLevelWidget(),
386 XtSetMappedWhenManaged( rTlw
, False
);
387 XtRealizeWidget( rTlw
);
389 return (WXWidget
)rTlw
;
392 WXWidget
wxApp::GetTopLevelWidget()
394 WXDisplay
* display
= wxGetDisplay();
395 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
397 if( data
.m_topLevelWidget
)
398 return (WXWidget
)data
.m_topLevelWidget
;
400 WXWidget tlw
= wxCreateTopLevelWidget( display
);
401 SetTopLevelWidget( display
, tlw
);
406 WXWidget
wxApp::GetTopLevelRealizedWidget()
408 WXDisplay
* display
= wxGetDisplay();
409 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
411 if( it
!= m_perDisplayData
->end() && it
->second
->m_topLevelRealizedWidget
)
412 return (WXWidget
)it
->second
->m_topLevelRealizedWidget
;
414 WXWidget rTlw
= wxCreateTopLevelRealizedWidget( display
);
415 SetTopLevelRealizedWidget( display
, rTlw
);
420 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
422 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
423 .m_topLevelWidget
= (Widget
)widget
;
426 void wxApp::SetTopLevelRealizedWidget(WXDisplay
* display
, WXWidget widget
)
428 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
429 .m_topLevelRealizedWidget
= (Widget
)widget
;
432 // Yield to other processes
434 bool wxApp::Yield(bool onlyIfNeeded
)
436 static bool s_inYield
= false;
442 wxFAIL_MSG( wxT("wxYield called recursively" ) );
450 while (wxTheApp
&& wxTheApp
->Pending())
451 wxTheApp
->Dispatch();
458 // ----------------------------------------------------------------------------
459 // accessors for C modules
460 // ----------------------------------------------------------------------------
462 extern "C" XtAppContext
wxGetAppContext()
464 return (XtAppContext
)wxTheApp
->GetAppContext();