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"
22 #include "wx/memory.h"
26 #include "wx/evtloop.h"
29 #include "wx/thread.h"
33 #pragma message disable nosimpint
37 #include <X11/Xutil.h>
38 #include <X11/Xresource.h>
39 #include <X11/Xatom.h>
41 #pragma message enable nosimpint
44 #include "wx/motif/private.h"
48 struct wxPerDisplayData
53 m_topLevelWidget
= NULL
;
54 m_topLevelRealizedWidget
= NULL
;
57 wxXVisualInfo
* m_visualInfo
;
58 Widget m_topLevelWidget
, m_topLevelRealizedWidget
;
61 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
63 static WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
);
65 extern bool wxAddIdleCallback();
67 wxHashTable
*wxWidgetHashTable
= NULL
;
69 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
74 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
77 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
82 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
84 // just forward to the default handler for now
85 return gs_pfnXErrorHandler(dpy
, xevent
);
91 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
94 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
97 if ( !wxAppBase::Initialize(argc_
, argv_
) )
100 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
103 // under HP-UX creating XmFontSet fails when the system locale is C and
104 // we're using a remote DISPLAY, presumably because HP-UX uses its own
105 // names for C and ISO locales (roman8 and iso8859n respectively) and so
106 // its Motif libraries have troubles with non-HP X server
108 // whatever the reason, the fact is that without this hack any wxMotif
109 // program crashes on startup because it can't create any font (HP programs
110 // still work but they do spit out messages about failing to create font
111 // sets and failing back on "fixed" font too)
113 // notice that calling setlocale() here is not enough because X(m) init
114 // functions call setlocale() later so we really have to change environment
115 bool fixAll
= false; // tweak LC_ALL (or just LC_CTYPE)?
116 const char *loc
= getenv("LC_CTYPE");
119 loc
= getenv("LC_ALL");
125 (loc
[0] == 'C' && loc
[1] == '\0') ||
126 strcmp(loc
, "POSIX") == 0 )
128 // we're using C locale, "fix" it
129 wxLogDebug(_T("HP-UX fontset hack: forcing locale to en_US.iso88591"));
130 putenv(fixAll
? "LC_ALL=en_US.iso88591" : "LC_CTYPE=en_US.iso88591");
134 XtSetLanguageProc(NULL
, NULL
, NULL
);
135 XtToolkitInitialize() ;
136 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
138 static char *fallbackResources
[] = {
139 // better defaults for CDE under Irix
141 // TODO: do something similar for the other systems, the hardcoded defaults
144 wxMOTIF_STR("*sgiMode: True"),
145 wxMOTIF_STR("*useSchemes: all"),
147 #if !wxMOTIF_USE_RENDER_TABLE
148 wxMOTIF_STR("*.fontList: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"),
150 wxMOTIF_STR("*wxDefaultRendition.fontName: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"),
151 wxMOTIF_STR("*wxDefaultRendition.fontType: FONT_IS_FONTSET"),
152 wxMOTIF_STR("*.renderTable: wxDefaultRendition"),
154 wxMOTIF_STR("*listBox.background: white"),
155 wxMOTIF_STR("*text.background: white"),
156 wxMOTIF_STR("*comboBox.Text.background: white"),
157 wxMOTIF_STR("*comboBox.List.background: white"),
158 #endif // __SGI__/!__SGI__
161 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
163 // we shouldn't pass empty application/class name as it results in
164 // immediate crash inside XOpenIM() (if XIM is used) under IRIX
165 wxString appname
= wxTheApp
->GetAppName();
166 if ( appname
.empty() )
167 appname
= _T("wxapp");
168 wxString clsname
= wxTheApp
->GetClassName();
169 if ( clsname
.empty() )
172 // FIXME-UTF8: This code is taken from wxGTK and duplicated here. This
173 // is just a temporary fix to make wxX11 compile in Unicode
174 // build, the real fix is to change Initialize()'s signature
175 // to use char* on Unix.
177 // XtOpenDisplay() wants char*, not wchar_t*, so convert
179 char **argvX11
= new char *[argc
+ 1];
180 for ( i
= 0; i
< argc
; i
++ )
182 argvX11
[i
] = strdup(wxConvLibc
.cWX2MB(argv_
[i
]));
185 argvX11
[argc
] = NULL
;
189 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,
193 NULL
, 0, // no options
194 # if XtSpecificationRelease < 5
195 (Cardinal
*) &argcX11
,
201 if ( argcX11
!= argc
)
203 // we have to drop the parameters which were consumed by X11+
204 for ( i
= 0; i
< argcX11
; i
++ )
206 while ( strcmp(wxConvLibc
.cWX2MB(argv_
[i
]), argvX11
[i
]) != 0 )
208 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc
- i
)*sizeof(*argv_
));
214 // update internal arg[cv] as X11 may have removed processed options:
218 //else: XtOpenDisplay() didn't modify our parameters
221 for ( i
= 0; i
< argcX11
; i
++ )
230 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,
234 NULL
, 0, // no options
235 # if XtSpecificationRelease < 5
242 #endif // Unicode/ANSI
245 // if you don't log to stderr, nothing will be shown...
246 delete wxLog::SetActiveTarget(new wxLogStderr
);
247 wxString
className(wxTheApp
->GetClassName());
248 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
252 m_initialDisplay
= (WXDisplay
*) dpy
;
255 // install the X error handler
256 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
257 #endif // __WXDEBUG__
259 // Add general resize proc
261 rec
.string
= wxMOTIF_STR("resize");
262 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
263 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
265 GetMainColormap(dpy
);
272 void wxApp::CleanUp()
274 wxAppBase::CleanUp();
276 delete wxWidgetHashTable
;
277 wxWidgetHashTable
= NULL
;
281 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
282 end
= m_perDisplayData
->end();
285 delete it
->second
->m_visualInfo
;
286 // On Solaris 10 calling XtDestroyWidget on the top level widget
287 // dumps core if the locale is set to something other than "C"
289 XtDestroyWidget( it
->second
->m_topLevelWidget
);
299 wxAppConsole::Exit();
302 // ============================================================================
304 // ============================================================================
308 m_mainLoop
= new wxEventLoop
;
309 m_mainColormap
= (WXColormap
) NULL
;
310 m_appContext
= (WXAppContext
) NULL
;
311 m_initialDisplay
= (WXDisplay
*) 0;
312 m_perDisplayData
= new wxPerDisplayDataMap
;
317 delete m_perDisplayData
;
320 int wxApp::MainLoop()
323 * Sit around forever waiting to process X-events. Property Change
324 * event are handled special, because they have to refer to
325 * the root window rather than to a widget. therefore we can't
326 * use an Xt-eventhandler.
329 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
330 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
338 // This should be redefined in a derived class for
339 // handling property change events for XAtom IPC.
340 void wxApp::HandlePropertyChange(WXEvent
*event
)
342 // by default do nothing special
343 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
346 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
348 if (!display
) /* Must be called first with non-NULL display */
349 return m_mainColormap
;
351 int defaultScreen
= DefaultScreen((Display
*) display
);
352 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
354 Colormap c
= DefaultColormapOfScreen(screen
);
357 m_mainColormap
= (WXColormap
) c
;
359 return (WXColormap
) c
;
362 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
363 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
365 wxPerDisplayDataMap::iterator it
= m
.find( display
);
366 if( it
!= m
.end() && it
->second
!= NULL
)
367 return *(it
->second
);
369 wxPerDisplayData
* nData
= new wxPerDisplayData();
375 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
377 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
379 if( data
.m_visualInfo
)
380 return data
.m_visualInfo
;
382 wxXVisualInfo
* vi
= new wxXVisualInfo
;
383 wxFillXVisualInfo( vi
, (Display
*)display
);
385 data
.m_visualInfo
= vi
;
390 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer
WXUNUSED(clientData
),
391 XtPointer
WXUNUSED(ptr
))
395 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
397 wxTheApp
->SetTopLevelRealizedWidget( (WXDisplay
*)XtDisplay(w
),
402 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
404 Widget tlw
= XtAppCreateShell( (String
)NULL
,
405 wxTheApp
->GetClassName().c_str(),
406 applicationShellWidgetClass
,
410 XmNoverrideRedirect
, True
,
413 XtAddCallback( tlw
, XmNdestroyCallback
,
414 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
417 return (WXWidget
)tlw
;
420 WXWidget
wxCreateTopLevelRealizedWidget( WXDisplay
* WXUNUSED(display
) )
422 Widget rTlw
= XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass
,
423 (Widget
)wxTheApp
->GetTopLevelWidget(),
425 XtSetMappedWhenManaged( rTlw
, False
);
426 XtRealizeWidget( rTlw
);
428 return (WXWidget
)rTlw
;
431 WXWidget
wxApp::GetTopLevelWidget()
433 WXDisplay
* display
= wxGetDisplay();
434 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
436 if( data
.m_topLevelWidget
)
437 return (WXWidget
)data
.m_topLevelWidget
;
439 WXWidget tlw
= wxCreateTopLevelWidget( display
);
440 SetTopLevelWidget( display
, tlw
);
445 WXWidget
wxApp::GetTopLevelRealizedWidget()
447 WXDisplay
* display
= wxGetDisplay();
448 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
450 if( it
!= m_perDisplayData
->end() && it
->second
->m_topLevelRealizedWidget
)
451 return (WXWidget
)it
->second
->m_topLevelRealizedWidget
;
453 WXWidget rTlw
= wxCreateTopLevelRealizedWidget( display
);
454 SetTopLevelRealizedWidget( display
, rTlw
);
459 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
461 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
462 .m_topLevelWidget
= (Widget
)widget
;
465 void wxApp::SetTopLevelRealizedWidget(WXDisplay
* display
, WXWidget widget
)
467 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
468 .m_topLevelRealizedWidget
= (Widget
)widget
;
471 // Yield to other processes
473 bool wxApp::DoYield(bool onlyIfNeeded
, long eventsToProcess
)
475 if ( m_isInsideYield
)
479 wxFAIL_MSG( wxT("wxYield called recursively" ) );
485 m_isInsideYield
= true;
486 m_eventsToProcessInsideYield
= eventsToProcess
;
488 wxEventLoopGuarantor dummyLoopIfNeeded
;
489 while (wxTheApp
&& wxTheApp
->Pending())
490 // TODO: implement event filtering using the eventsToProcess mask
491 wxTheApp
->Dispatch();
493 m_isInsideYield
= false;
498 // ----------------------------------------------------------------------------
499 // accessors for C modules
500 // ----------------------------------------------------------------------------
502 extern "C" XtAppContext
wxGetAppContext()
504 return (XtAppContext
)wxTheApp
->GetAppContext();