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
)
79 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
82 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
87 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
89 // just forward to the default handler for now
90 return gs_pfnXErrorHandler(dpy
, xevent
);
96 bool wxApp::Initialize(int& argcOrig
, wxChar
**argvOrig
)
99 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
102 if ( !wxAppBase::Initialize(argcOrig
, argvOrig
) )
105 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
110 void wxApp::CleanUp()
112 wxAppBase::CleanUp();
114 delete wxWidgetHashTable
;
115 wxWidgetHashTable
= NULL
;
119 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
120 end
= m_perDisplayData
->end();
123 delete it
->second
->m_visualInfo
;
124 // On Solaris 10 calling XtDestroyWidget on the top level widget
125 // dumps core if the locale is set to something other than "C"
127 XtDestroyWidget( it
->second
->m_topLevelWidget
);
137 wxAppConsole::Exit();
140 // ============================================================================
142 // ============================================================================
149 m_mainLoop
= new wxEventLoop
;
150 m_mainColormap
= (WXColormap
) NULL
;
151 m_appContext
= (WXAppContext
) NULL
;
152 m_initialDisplay
= (WXDisplay
*) 0;
153 m_perDisplayData
= new wxPerDisplayDataMap
;
158 delete m_perDisplayData
;
161 int wxApp::MainLoop()
164 * Sit around forever waiting to process X-events. Property Change
165 * event are handled special, because they have to refer to
166 * the root window rather than to a widget. therefore we can't
167 * use an Xt-eventhandler.
170 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
171 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
179 // This should be redefined in a derived class for
180 // handling property change events for XAtom IPC.
181 void wxApp::HandlePropertyChange(WXEvent
*event
)
183 // by default do nothing special
184 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
187 static char *fallbackResources
[] = {
188 // better defaults for CDE under Irix
190 // TODO: do something similar for the other systems, the hardcoded defaults
193 wxMOTIF_STR("*sgiMode: True"),
194 wxMOTIF_STR("*useSchemes: all"),
196 #if !wxMOTIF_USE_RENDER_TABLE
197 wxMOTIF_STR("*.fontList: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"),
199 wxMOTIF_STR("*wxDefaultRendition.fontName: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"),
200 wxMOTIF_STR("*wxDefaultRendition.fontType: FONT_IS_FONTSET"),
201 wxMOTIF_STR("*.renderTable: wxDefaultRendition"),
203 wxMOTIF_STR("*listBox.background: white"),
204 wxMOTIF_STR("*text.background: white"),
205 wxMOTIF_STR("*comboBox.Text.background: white"),
206 wxMOTIF_STR("*comboBox.List.background: white"),
207 #endif // __SGI__/!__SGI__
211 // Create an application context
212 bool wxApp::OnInitGui()
214 if( !wxAppBase::OnInitGui() )
218 // under HP-UX creating XmFontSet fails when the system locale is C and
219 // we're using a remote DISPLAY, presumably because HP-UX uses its own
220 // names for C and ISO locales (roman8 and iso8859n respectively) and so
221 // its Motif libraries have troubles with non-HP X server
223 // whatever the reason, the fact is that without this hack any wxMotif
224 // program crashes on startup because it can't create any font (HP programs
225 // still work but they do spit out messages about failing to create font
226 // sets and failing back on "fixed" font too)
228 // notice that calling setlocale() here is not enough because X(m) init
229 // functions call setlocale() later so we really have to change environment
230 bool fixAll
= false; // tweak LC_ALL (or just LC_CTYPE)?
231 const char *loc
= getenv("LC_CTYPE");
234 loc
= getenv("LC_ALL");
240 (loc
[0] == 'C' && loc
[1] == '\0') ||
241 strcmp(loc
, "POSIX") == 0 )
243 // we're using C locale, "fix" it
244 wxLogDebug(_T("HP-UX fontset hack: forcing locale to en_US.iso88591"));
245 putenv(fixAll
? "LC_ALL=en_US.iso88591" : "LC_CTYPE=en_US.iso88591");
249 XtSetLanguageProc(NULL
, NULL
, NULL
);
250 XtToolkitInitialize() ;
251 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
252 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
254 // we shouldn't pass empty application/class name as it results in
255 // immediate crash inside XOpenIM() (if XIM is used) under IRIX
256 wxString appname
= wxTheApp
->GetAppName();
257 if ( appname
.empty() )
258 appname
= _T("wxapp");
259 wxString clsname
= wxTheApp
->GetClassName();
260 if ( clsname
.empty() )
263 // FIXME-UTF8: This code is taken from wxGTK and duplicated here. This
264 // is just a temporary fix to make wxX11 compile in Unicode
265 // build, the real fix is to change Initialize()'s signature
266 // to use char* on Unix.
268 // XtOpenDisplay() wants char*, not wchar_t*, so convert
270 char **argvX11
= new char *[argc
+ 1];
271 for ( i
= 0; i
< argc
; i
++ )
273 argvX11
[i
] = strdup(wxConvLibc
.cWX2MB(argv
[i
]));
276 argvX11
[argc
] = NULL
;
280 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,
284 NULL
, 0, // no options
285 # if XtSpecificationRelease < 5
286 (Cardinal
*) &argcX11
,
292 if ( argcX11
!= argc
)
294 // we have to drop the parameters which were consumed by X11+
295 for ( i
= 0; i
< argcX11
; i
++ )
297 while ( strcmp(wxConvLibc
.cWX2MB(argv
[i
]), argvX11
[i
]) != 0 )
299 memmove(argv
+ i
, argv
+ i
+ 1, (argc
- i
)*sizeof(*argv
));
305 //else: XtOpenDisplay() didn't modify our parameters
308 for ( i
= 0; i
< argcX11
; i
++ )
317 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,
321 NULL
, 0, // no options
322 # if XtSpecificationRelease < 5
329 #endif // Unicode/ANSI
332 // if you don't log to stderr, nothing will be shown...
333 delete wxLog::SetActiveTarget(new wxLogStderr
);
334 wxString
className(wxTheApp
->GetClassName());
335 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
339 m_initialDisplay
= (WXDisplay
*) dpy
;
342 // install the X error handler
343 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
344 #endif // __WXDEBUG__
346 // Add general resize proc
348 rec
.string
= wxMOTIF_STR("resize");
349 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
350 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
352 GetMainColormap(dpy
);
359 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
361 if (!display
) /* Must be called first with non-NULL display */
362 return m_mainColormap
;
364 int defaultScreen
= DefaultScreen((Display
*) display
);
365 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
367 Colormap c
= DefaultColormapOfScreen(screen
);
370 m_mainColormap
= (WXColormap
) c
;
372 return (WXColormap
) c
;
375 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
376 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
378 wxPerDisplayDataMap::iterator it
= m
.find( display
);
379 if( it
!= m
.end() && it
->second
!= NULL
)
380 return *(it
->second
);
382 wxPerDisplayData
* nData
= new wxPerDisplayData();
388 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
390 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
392 if( data
.m_visualInfo
)
393 return data
.m_visualInfo
;
395 wxXVisualInfo
* vi
= new wxXVisualInfo
;
396 wxFillXVisualInfo( vi
, (Display
*)display
);
398 data
.m_visualInfo
= vi
;
403 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer
WXUNUSED(clientData
),
404 XtPointer
WXUNUSED(ptr
))
408 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
410 wxTheApp
->SetTopLevelRealizedWidget( (WXDisplay
*)XtDisplay(w
),
415 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
417 Widget tlw
= XtAppCreateShell( (String
)NULL
,
418 wxTheApp
->GetClassName().c_str(),
419 applicationShellWidgetClass
,
423 XmNoverrideRedirect
, True
,
426 XtAddCallback( tlw
, XmNdestroyCallback
,
427 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
430 return (WXWidget
)tlw
;
433 WXWidget
wxCreateTopLevelRealizedWidget( WXDisplay
* WXUNUSED(display
) )
435 Widget rTlw
= XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass
,
436 (Widget
)wxTheApp
->GetTopLevelWidget(),
438 XtSetMappedWhenManaged( rTlw
, False
);
439 XtRealizeWidget( rTlw
);
441 return (WXWidget
)rTlw
;
444 WXWidget
wxApp::GetTopLevelWidget()
446 WXDisplay
* display
= wxGetDisplay();
447 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
449 if( data
.m_topLevelWidget
)
450 return (WXWidget
)data
.m_topLevelWidget
;
452 WXWidget tlw
= wxCreateTopLevelWidget( display
);
453 SetTopLevelWidget( display
, tlw
);
458 WXWidget
wxApp::GetTopLevelRealizedWidget()
460 WXDisplay
* display
= wxGetDisplay();
461 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
463 if( it
!= m_perDisplayData
->end() && it
->second
->m_topLevelRealizedWidget
)
464 return (WXWidget
)it
->second
->m_topLevelRealizedWidget
;
466 WXWidget rTlw
= wxCreateTopLevelRealizedWidget( display
);
467 SetTopLevelRealizedWidget( display
, rTlw
);
472 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
474 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
475 .m_topLevelWidget
= (Widget
)widget
;
478 void wxApp::SetTopLevelRealizedWidget(WXDisplay
* display
, WXWidget widget
)
480 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
481 .m_topLevelRealizedWidget
= (Widget
)widget
;
484 // Yield to other processes
486 bool wxApp::Yield(bool onlyIfNeeded
)
488 static bool s_inYield
= false;
494 wxFAIL_MSG( wxT("wxYield called recursively" ) );
502 while (wxTheApp
&& wxTheApp
->Pending())
503 wxTheApp
->Dispatch();
510 // ----------------------------------------------------------------------------
511 // accessors for C modules
512 // ----------------------------------------------------------------------------
514 extern "C" XtAppContext
wxGetAppContext()
516 return (XtAppContext
)wxTheApp
->GetAppContext();