]>
Commit | Line | Data |
---|---|---|
4bb6408c | 1 | ///////////////////////////////////////////////////////////////////////////// |
55034339 | 2 | // Name: src/motif/app.cpp |
4bb6408c JS |
3 | // Purpose: wxApp |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
4bb6408c JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
1248b41f MB |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
88a7a4e1 WS |
15 | #include "wx/app.h" |
16 | ||
32d4c30a WS |
17 | #ifndef WX_PRECOMP |
18 | #include "wx/hash.h" | |
88a7a4e1 | 19 | #include "wx/intl.h" |
e4db172a | 20 | #include "wx/log.h" |
de6185e2 | 21 | #include "wx/utils.h" |
5b56bffb | 22 | #include "wx/memory.h" |
73216ce6 | 23 | #include "wx/font.h" |
32d4c30a WS |
24 | #endif |
25 | ||
7e1bcfa8 | 26 | #include "wx/evtloop.h" |
b412f9be | 27 | |
7bcb11d3 | 28 | #if wxUSE_THREADS |
dfe1eee3 | 29 | #include "wx/thread.h" |
7bcb11d3 | 30 | #endif |
4bb6408c | 31 | |
338dd992 | 32 | #ifdef __VMS__ |
02761f6c | 33 | #pragma message disable nosimpint |
338dd992 | 34 | #endif |
4bb6408c JS |
35 | #include <Xm/Xm.h> |
36 | #include <X11/Xlib.h> | |
37 | #include <X11/Xutil.h> | |
38 | #include <X11/Xresource.h> | |
39 | #include <X11/Xatom.h> | |
338dd992 | 40 | #ifdef __VMS__ |
02761f6c | 41 | #pragma message enable nosimpint |
338dd992 | 42 | #endif |
4bb6408c JS |
43 | |
44 | #include "wx/motif/private.h" | |
45 | ||
46 | #include <string.h> | |
47 | ||
eb6fa4b4 MB |
48 | struct wxPerDisplayData |
49 | { | |
50 | wxPerDisplayData() | |
5a2e3d8c MB |
51 | { |
52 | m_visualInfo = NULL; | |
53 | m_topLevelWidget = NULL; | |
54 | m_topLevelRealizedWidget = NULL; | |
55 | } | |
eb6fa4b4 MB |
56 | |
57 | wxXVisualInfo* m_visualInfo; | |
5a2e3d8c | 58 | Widget m_topLevelWidget, m_topLevelRealizedWidget; |
eb6fa4b4 MB |
59 | }; |
60 | ||
eb6fa4b4 MB |
61 | static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData, |
62 | XtPointer ptr); | |
63 | static WXWidget wxCreateTopLevelWidget( WXDisplay* display ); | |
9ce8d6a2 | 64 | |
7e1bcfa8 | 65 | extern bool wxAddIdleCallback(); |
7491d644 | 66 | |
4bb6408c JS |
67 | wxHashTable *wxWidgetHashTable = NULL; |
68 | ||
4bb6408c | 69 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) |
9c6e1335 | 70 | |
86ac4a22 VZ |
71 | extern "C" |
72 | { | |
e838cc14 | 73 | typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *); |
86ac4a22 | 74 | } |
e838cc14 | 75 | |
86ac4a22 | 76 | XErrorHandlerFunc gs_pfnXErrorHandler = 0; |
e838cc14 | 77 | |
86ac4a22 VZ |
78 | extern "C" |
79 | { | |
80 | ||
81 | static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent) | |
82 | { | |
83 | // just forward to the default handler for now | |
84 | return gs_pfnXErrorHandler(dpy, xevent); | |
85 | } | |
86 | ||
87 | } | |
e838cc14 | 88 | |
8ecf7346 | 89 | bool wxApp::Initialize(int& argc_, wxChar **argv_) |
4bb6408c | 90 | { |
105fbe1f MB |
91 | #if wxUSE_INTL |
92 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
93 | #endif | |
94 | ||
8ecf7346 | 95 | if ( !wxAppBase::Initialize(argc_, argv_) ) |
94826170 | 96 | return false; |
afb74891 | 97 | |
4bb6408c | 98 | wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER); |
afb74891 | 99 | |
0fad0dfd VZ |
100 | #ifdef __HPUX__ |
101 | // under HP-UX creating XmFontSet fails when the system locale is C and | |
102 | // we're using a remote DISPLAY, presumably because HP-UX uses its own | |
103 | // names for C and ISO locales (roman8 and iso8859n respectively) and so | |
104 | // its Motif libraries have troubles with non-HP X server | |
105 | // | |
106 | // whatever the reason, the fact is that without this hack any wxMotif | |
107 | // program crashes on startup because it can't create any font (HP programs | |
108 | // still work but they do spit out messages about failing to create font | |
109 | // sets and failing back on "fixed" font too) | |
110 | // | |
111 | // notice that calling setlocale() here is not enough because X(m) init | |
112 | // functions call setlocale() later so we really have to change environment | |
113 | bool fixAll = false; // tweak LC_ALL (or just LC_CTYPE)? | |
114 | const char *loc = getenv("LC_CTYPE"); | |
115 | if ( !loc ) | |
116 | { | |
117 | loc = getenv("LC_ALL"); | |
118 | if ( loc ) | |
119 | fixAll = true; | |
120 | } | |
121 | ||
122 | if ( !loc || | |
123 | (loc[0] == 'C' && loc[1] == '\0') || | |
124 | strcmp(loc, "POSIX") == 0 ) | |
125 | { | |
126 | // we're using C locale, "fix" it | |
9a83f860 | 127 | wxLogDebug(wxT("HP-UX fontset hack: forcing locale to en_US.iso88591")); |
0fad0dfd VZ |
128 | putenv(fixAll ? "LC_ALL=en_US.iso88591" : "LC_CTYPE=en_US.iso88591"); |
129 | } | |
130 | #endif // __HPUX__ | |
131 | ||
9d02485e | 132 | XtSetLanguageProc(NULL, NULL, NULL); |
4bb6408c | 133 | XtToolkitInitialize() ; |
edc1cd8b | 134 | wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext(); |
8ecf7346 VZ |
135 | |
136 | static char *fallbackResources[] = { | |
137 | // better defaults for CDE under Irix | |
138 | // | |
139 | // TODO: do something similar for the other systems, the hardcoded defaults | |
140 | // below are ugly | |
141 | #ifdef __SGI__ | |
142 | wxMOTIF_STR("*sgiMode: True"), | |
143 | wxMOTIF_STR("*useSchemes: all"), | |
144 | #else // !__SGI__ | |
145 | #if !wxMOTIF_USE_RENDER_TABLE | |
146 | wxMOTIF_STR("*.fontList: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"), | |
147 | #else | |
148 | wxMOTIF_STR("*wxDefaultRendition.fontName: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"), | |
149 | wxMOTIF_STR("*wxDefaultRendition.fontType: FONT_IS_FONTSET"), | |
150 | wxMOTIF_STR("*.renderTable: wxDefaultRendition"), | |
151 | #endif | |
152 | wxMOTIF_STR("*listBox.background: white"), | |
153 | wxMOTIF_STR("*text.background: white"), | |
154 | wxMOTIF_STR("*comboBox.Text.background: white"), | |
155 | wxMOTIF_STR("*comboBox.List.background: white"), | |
156 | #endif // __SGI__/!__SGI__ | |
157 | NULL | |
158 | }; | |
edc1cd8b JS |
159 | XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources); |
160 | ||
0b6d76bf VZ |
161 | // we shouldn't pass empty application/class name as it results in |
162 | // immediate crash inside XOpenIM() (if XIM is used) under IRIX | |
163 | wxString appname = wxTheApp->GetAppName(); | |
164 | if ( appname.empty() ) | |
9a83f860 | 165 | appname = wxT("wxapp"); |
0b6d76bf VZ |
166 | wxString clsname = wxTheApp->GetClassName(); |
167 | if ( clsname.empty() ) | |
9a83f860 | 168 | clsname = wxT("wx"); |
0b6d76bf | 169 | |
6991087b VS |
170 | // FIXME-UTF8: This code is taken from wxGTK and duplicated here. This |
171 | // is just a temporary fix to make wxX11 compile in Unicode | |
172 | // build, the real fix is to change Initialize()'s signature | |
173 | // to use char* on Unix. | |
174 | #if wxUSE_UNICODE | |
175 | // XtOpenDisplay() wants char*, not wchar_t*, so convert | |
176 | int i; | |
177 | char **argvX11 = new char *[argc + 1]; | |
178 | for ( i = 0; i < argc; i++ ) | |
179 | { | |
8ecf7346 | 180 | argvX11[i] = strdup(wxConvLibc.cWX2MB(argv_[i])); |
6991087b VS |
181 | } |
182 | ||
183 | argvX11[argc] = NULL; | |
184 | ||
185 | int argcX11 = argc; | |
186 | ||
187 | Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext, | |
188 | (String)NULL, | |
189 | appname.c_str(), | |
190 | clsname.c_str(), | |
191 | NULL, 0, // no options | |
192 | # if XtSpecificationRelease < 5 | |
193 | (Cardinal*) &argcX11, | |
194 | # else | |
195 | &argcX11, | |
196 | # endif | |
197 | argvX11); | |
198 | ||
199 | if ( argcX11 != argc ) | |
200 | { | |
201 | // we have to drop the parameters which were consumed by X11+ | |
202 | for ( i = 0; i < argcX11; i++ ) | |
203 | { | |
8ecf7346 | 204 | while ( strcmp(wxConvLibc.cWX2MB(argv_[i]), argvX11[i]) != 0 ) |
6991087b | 205 | { |
8ecf7346 | 206 | memmove(argv_ + i, argv_ + i + 1, (argc - i)*sizeof(*argv_)); |
6991087b VS |
207 | } |
208 | } | |
209 | ||
210 | argc = argcX11; | |
8ecf7346 VZ |
211 | |
212 | // update internal arg[cv] as X11 may have removed processed options: | |
213 | argc = argc_; | |
214 | argv = argv_; | |
6991087b VS |
215 | } |
216 | //else: XtOpenDisplay() didn't modify our parameters | |
217 | ||
218 | // free our copy | |
219 | for ( i = 0; i < argcX11; i++ ) | |
220 | { | |
221 | free(argvX11[i]); | |
222 | } | |
223 | ||
224 | delete [] argvX11; | |
225 | ||
226 | #else // ANSI | |
227 | ||
0b6d76bf VZ |
228 | Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext, |
229 | (String)NULL, | |
230 | appname.c_str(), | |
231 | clsname.c_str(), | |
232 | NULL, 0, // no options | |
4bb6408c | 233 | # if XtSpecificationRelease < 5 |
9c6e1335 | 234 | (Cardinal*) &argc, |
4bb6408c | 235 | # else |
9c6e1335 | 236 | &argc, |
4bb6408c | 237 | # endif |
9c6e1335 VZ |
238 | argv); |
239 | ||
6991087b VS |
240 | #endif // Unicode/ANSI |
241 | ||
4bb6408c | 242 | if (!dpy) { |
c88b736c MB |
243 | // if you don't log to stderr, nothing will be shown... |
244 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
7bcb11d3 | 245 | wxString className(wxTheApp->GetClassName()); |
77ffb593 | 246 | wxLogError(_("wxWidgets could not open display for '%s': exiting."), |
d3a80c92 | 247 | className.c_str()); |
2d120f83 | 248 | exit(-1); |
4bb6408c | 249 | } |
47bc1060 | 250 | m_initialDisplay = (WXDisplay*) dpy; |
afb74891 | 251 | |
e838cc14 VZ |
252 | // install the X error handler |
253 | gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler); | |
e838cc14 | 254 | |
4bb6408c JS |
255 | // Add general resize proc |
256 | XtActionsRec rec; | |
f1db433a | 257 | rec.string = wxMOTIF_STR("resize"); |
4bb6408c JS |
258 | rec.proc = (XtActionProc)wxWidgetResizeProc; |
259 | XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1); | |
afb74891 | 260 | |
4bb6408c | 261 | GetMainColormap(dpy); |
afb74891 | 262 | |
7e1bcfa8 MB |
263 | wxAddIdleCallback(); |
264 | ||
96be256b | 265 | return true; |
4bb6408c JS |
266 | } |
267 | ||
8ecf7346 VZ |
268 | void wxApp::CleanUp() |
269 | { | |
270 | wxAppBase::CleanUp(); | |
271 | ||
272 | delete wxWidgetHashTable; | |
273 | wxWidgetHashTable = NULL; | |
274 | ||
275 | delete m_mainLoop; | |
276 | ||
277 | for( wxPerDisplayDataMap::iterator it = m_perDisplayData->begin(), | |
278 | end = m_perDisplayData->end(); | |
279 | it != end; ++it ) | |
280 | { | |
281 | delete it->second->m_visualInfo; | |
282 | // On Solaris 10 calling XtDestroyWidget on the top level widget | |
283 | // dumps core if the locale is set to something other than "C" | |
284 | #ifndef __SUN__ | |
285 | XtDestroyWidget( it->second->m_topLevelWidget ); | |
286 | #endif | |
287 | delete it->second; | |
288 | } | |
289 | } | |
290 | ||
291 | void wxApp::Exit() | |
292 | { | |
293 | wxApp::CleanUp(); | |
294 | ||
295 | wxAppConsole::Exit(); | |
296 | } | |
297 | ||
298 | // ============================================================================ | |
299 | // wxApp | |
300 | // ============================================================================ | |
301 | ||
302 | wxApp::wxApp() | |
303 | { | |
304 | m_mainLoop = new wxEventLoop; | |
305 | m_mainColormap = (WXColormap) NULL; | |
306 | m_appContext = (WXAppContext) NULL; | |
307 | m_initialDisplay = (WXDisplay*) 0; | |
308 | m_perDisplayData = new wxPerDisplayDataMap; | |
309 | } | |
310 | ||
311 | wxApp::~wxApp() | |
312 | { | |
313 | delete m_perDisplayData; | |
314 | } | |
315 | ||
316 | int wxApp::MainLoop() | |
317 | { | |
318 | /* | |
319 | * Sit around forever waiting to process X-events. Property Change | |
320 | * event are handled special, because they have to refer to | |
321 | * the root window rather than to a widget. therefore we can't | |
322 | * use an Xt-eventhandler. | |
323 | */ | |
324 | ||
325 | XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), | |
326 | XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())), | |
327 | PropertyChangeMask); | |
328 | ||
329 | m_mainLoop->Run(); | |
330 | ||
331 | return 0; | |
332 | } | |
333 | ||
334 | // This should be redefined in a derived class for | |
335 | // handling property change events for XAtom IPC. | |
336 | void wxApp::HandlePropertyChange(WXEvent *event) | |
337 | { | |
338 | // by default do nothing special | |
339 | XtDispatchEvent((XEvent*) event); /* let Motif do the work */ | |
340 | } | |
341 | ||
4bb6408c JS |
342 | WXColormap wxApp::GetMainColormap(WXDisplay* display) |
343 | { | |
344 | if (!display) /* Must be called first with non-NULL display */ | |
2d120f83 | 345 | return m_mainColormap; |
a91b47e8 JS |
346 | |
347 | int defaultScreen = DefaultScreen((Display*) display); | |
348 | Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen); | |
afb74891 | 349 | |
a91b47e8 | 350 | Colormap c = DefaultColormapOfScreen(screen); |
afb74891 | 351 | |
4bb6408c | 352 | if (!m_mainColormap) |
2d120f83 | 353 | m_mainColormap = (WXColormap) c; |
afb74891 | 354 | |
4bb6408c JS |
355 | return (WXColormap) c; |
356 | } | |
357 | ||
bdcade0a MB |
358 | static inline wxPerDisplayData& GetOrCreatePerDisplayData |
359 | ( wxPerDisplayDataMap& m, WXDisplay* display ) | |
9ce8d6a2 | 360 | { |
bdcade0a MB |
361 | wxPerDisplayDataMap::iterator it = m.find( display ); |
362 | if( it != m.end() && it->second != NULL ) | |
363 | return *(it->second); | |
364 | ||
365 | wxPerDisplayData* nData = new wxPerDisplayData(); | |
366 | m[display] = nData; | |
367 | ||
368 | return *nData; | |
369 | } | |
9ce8d6a2 | 370 | |
bdcade0a MB |
371 | wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display ) |
372 | { | |
373 | wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData, | |
374 | display ); | |
375 | if( data.m_visualInfo ) | |
376 | return data.m_visualInfo; | |
9ce8d6a2 MB |
377 | |
378 | wxXVisualInfo* vi = new wxXVisualInfo; | |
379 | wxFillXVisualInfo( vi, (Display*)display ); | |
380 | ||
bdcade0a | 381 | data.m_visualInfo = vi; |
9ce8d6a2 MB |
382 | |
383 | return vi; | |
384 | } | |
385 | ||
55034339 WS |
386 | static void wxTLWidgetDestroyCallback(Widget w, XtPointer WXUNUSED(clientData), |
387 | XtPointer WXUNUSED(ptr)) | |
eb6fa4b4 MB |
388 | { |
389 | if( wxTheApp ) | |
5a2e3d8c | 390 | { |
eb6fa4b4 MB |
391 | wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w), |
392 | (WXWidget)NULL ); | |
5a2e3d8c MB |
393 | wxTheApp->SetTopLevelRealizedWidget( (WXDisplay*)XtDisplay(w), |
394 | (WXWidget)NULL ); | |
395 | } | |
eb6fa4b4 MB |
396 | } |
397 | ||
398 | WXWidget wxCreateTopLevelWidget( WXDisplay* display ) | |
399 | { | |
400 | Widget tlw = XtAppCreateShell( (String)NULL, | |
401 | wxTheApp->GetClassName().c_str(), | |
402 | applicationShellWidgetClass, | |
403 | (Display*)display, | |
404 | NULL, 0 ); | |
5a2e3d8c MB |
405 | XtVaSetValues( tlw, |
406 | XmNoverrideRedirect, True, | |
407 | NULL ); | |
eb6fa4b4 MB |
408 | |
409 | XtAddCallback( tlw, XmNdestroyCallback, | |
410 | (XtCallbackProc)wxTLWidgetDestroyCallback, | |
411 | (XtPointer)NULL ); | |
412 | ||
413 | return (WXWidget)tlw; | |
414 | } | |
415 | ||
55034339 | 416 | WXWidget wxCreateTopLevelRealizedWidget( WXDisplay* WXUNUSED(display) ) |
5a2e3d8c | 417 | { |
cb81f1a6 | 418 | Widget rTlw = XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass, |
5a2e3d8c | 419 | (Widget)wxTheApp->GetTopLevelWidget(), |
b09a59f5 | 420 | NULL ); |
5a2e3d8c MB |
421 | XtSetMappedWhenManaged( rTlw, False ); |
422 | XtRealizeWidget( rTlw ); | |
423 | ||
424 | return (WXWidget)rTlw; | |
425 | } | |
426 | ||
eb6fa4b4 MB |
427 | WXWidget wxApp::GetTopLevelWidget() |
428 | { | |
429 | WXDisplay* display = wxGetDisplay(); | |
bdcade0a MB |
430 | wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData, |
431 | display ); | |
432 | if( data.m_topLevelWidget ) | |
433 | return (WXWidget)data.m_topLevelWidget; | |
eb6fa4b4 MB |
434 | |
435 | WXWidget tlw = wxCreateTopLevelWidget( display ); | |
436 | SetTopLevelWidget( display, tlw ); | |
437 | ||
438 | return tlw; | |
439 | } | |
440 | ||
5a2e3d8c MB |
441 | WXWidget wxApp::GetTopLevelRealizedWidget() |
442 | { | |
443 | WXDisplay* display = wxGetDisplay(); | |
444 | wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display ); | |
445 | ||
446 | if( it != m_perDisplayData->end() && it->second->m_topLevelRealizedWidget ) | |
447 | return (WXWidget)it->second->m_topLevelRealizedWidget; | |
448 | ||
449 | WXWidget rTlw = wxCreateTopLevelRealizedWidget( display ); | |
450 | SetTopLevelRealizedWidget( display, rTlw ); | |
451 | ||
452 | return rTlw; | |
453 | } | |
454 | ||
eb6fa4b4 MB |
455 | void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget) |
456 | { | |
5a2e3d8c MB |
457 | GetOrCreatePerDisplayData( *m_perDisplayData, display ) |
458 | .m_topLevelWidget = (Widget)widget; | |
459 | } | |
460 | ||
461 | void wxApp::SetTopLevelRealizedWidget(WXDisplay* display, WXWidget widget) | |
462 | { | |
463 | GetOrCreatePerDisplayData( *m_perDisplayData, display ) | |
464 | .m_topLevelRealizedWidget = (Widget)widget; | |
eb6fa4b4 MB |
465 | } |
466 | ||
4bb6408c | 467 | |
d391a345 VZ |
468 | // ---------------------------------------------------------------------------- |
469 | // accessors for C modules | |
470 | // ---------------------------------------------------------------------------- | |
471 | ||
472 | extern "C" XtAppContext wxGetAppContext() | |
473 | { | |
474 | return (XtAppContext)wxTheApp->GetAppContext(); | |
475 | } |