]>
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 | |
e838cc14 | 71 | #ifdef __WXDEBUG__ |
86ac4a22 VZ |
72 | extern "C" |
73 | { | |
e838cc14 | 74 | typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *); |
86ac4a22 | 75 | } |
e838cc14 | 76 | |
86ac4a22 | 77 | XErrorHandlerFunc gs_pfnXErrorHandler = 0; |
e838cc14 | 78 | |
86ac4a22 VZ |
79 | extern "C" |
80 | { | |
81 | ||
82 | static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent) | |
83 | { | |
84 | // just forward to the default handler for now | |
85 | return gs_pfnXErrorHandler(dpy, xevent); | |
86 | } | |
87 | ||
88 | } | |
e838cc14 VZ |
89 | #endif // __WXDEBUG__ |
90 | ||
8ecf7346 | 91 | bool wxApp::Initialize(int& argc_, wxChar **argv_) |
4bb6408c | 92 | { |
105fbe1f MB |
93 | #if wxUSE_INTL |
94 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
95 | #endif | |
96 | ||
8ecf7346 | 97 | if ( !wxAppBase::Initialize(argc_, argv_) ) |
94826170 | 98 | return false; |
afb74891 | 99 | |
4bb6408c | 100 | wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER); |
afb74891 | 101 | |
0fad0dfd VZ |
102 | #ifdef __HPUX__ |
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 | |
107 | // | |
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) | |
112 | // | |
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"); | |
117 | if ( !loc ) | |
118 | { | |
119 | loc = getenv("LC_ALL"); | |
120 | if ( loc ) | |
121 | fixAll = true; | |
122 | } | |
123 | ||
124 | if ( !loc || | |
125 | (loc[0] == 'C' && loc[1] == '\0') || | |
126 | strcmp(loc, "POSIX") == 0 ) | |
127 | { | |
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"); | |
131 | } | |
132 | #endif // __HPUX__ | |
133 | ||
9d02485e | 134 | XtSetLanguageProc(NULL, NULL, NULL); |
4bb6408c | 135 | XtToolkitInitialize() ; |
edc1cd8b | 136 | wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext(); |
8ecf7346 VZ |
137 | |
138 | static char *fallbackResources[] = { | |
139 | // better defaults for CDE under Irix | |
140 | // | |
141 | // TODO: do something similar for the other systems, the hardcoded defaults | |
142 | // below are ugly | |
143 | #ifdef __SGI__ | |
144 | wxMOTIF_STR("*sgiMode: True"), | |
145 | wxMOTIF_STR("*useSchemes: all"), | |
146 | #else // !__SGI__ | |
147 | #if !wxMOTIF_USE_RENDER_TABLE | |
148 | wxMOTIF_STR("*.fontList: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"), | |
149 | #else | |
150 | wxMOTIF_STR("*wxDefaultRendition.fontName: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"), | |
151 | wxMOTIF_STR("*wxDefaultRendition.fontType: FONT_IS_FONTSET"), | |
152 | wxMOTIF_STR("*.renderTable: wxDefaultRendition"), | |
153 | #endif | |
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__ | |
159 | NULL | |
160 | }; | |
edc1cd8b JS |
161 | XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources); |
162 | ||
0b6d76bf VZ |
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() ) | |
170 | clsname = _T("wx"); | |
171 | ||
6991087b VS |
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. | |
176 | #if wxUSE_UNICODE | |
177 | // XtOpenDisplay() wants char*, not wchar_t*, so convert | |
178 | int i; | |
179 | char **argvX11 = new char *[argc + 1]; | |
180 | for ( i = 0; i < argc; i++ ) | |
181 | { | |
8ecf7346 | 182 | argvX11[i] = strdup(wxConvLibc.cWX2MB(argv_[i])); |
6991087b VS |
183 | } |
184 | ||
185 | argvX11[argc] = NULL; | |
186 | ||
187 | int argcX11 = argc; | |
188 | ||
189 | Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext, | |
190 | (String)NULL, | |
191 | appname.c_str(), | |
192 | clsname.c_str(), | |
193 | NULL, 0, // no options | |
194 | # if XtSpecificationRelease < 5 | |
195 | (Cardinal*) &argcX11, | |
196 | # else | |
197 | &argcX11, | |
198 | # endif | |
199 | argvX11); | |
200 | ||
201 | if ( argcX11 != argc ) | |
202 | { | |
203 | // we have to drop the parameters which were consumed by X11+ | |
204 | for ( i = 0; i < argcX11; i++ ) | |
205 | { | |
8ecf7346 | 206 | while ( strcmp(wxConvLibc.cWX2MB(argv_[i]), argvX11[i]) != 0 ) |
6991087b | 207 | { |
8ecf7346 | 208 | memmove(argv_ + i, argv_ + i + 1, (argc - i)*sizeof(*argv_)); |
6991087b VS |
209 | } |
210 | } | |
211 | ||
212 | argc = argcX11; | |
8ecf7346 VZ |
213 | |
214 | // update internal arg[cv] as X11 may have removed processed options: | |
215 | argc = argc_; | |
216 | argv = argv_; | |
6991087b VS |
217 | } |
218 | //else: XtOpenDisplay() didn't modify our parameters | |
219 | ||
220 | // free our copy | |
221 | for ( i = 0; i < argcX11; i++ ) | |
222 | { | |
223 | free(argvX11[i]); | |
224 | } | |
225 | ||
226 | delete [] argvX11; | |
227 | ||
228 | #else // ANSI | |
229 | ||
0b6d76bf VZ |
230 | Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext, |
231 | (String)NULL, | |
232 | appname.c_str(), | |
233 | clsname.c_str(), | |
234 | NULL, 0, // no options | |
4bb6408c | 235 | # if XtSpecificationRelease < 5 |
9c6e1335 | 236 | (Cardinal*) &argc, |
4bb6408c | 237 | # else |
9c6e1335 | 238 | &argc, |
4bb6408c | 239 | # endif |
9c6e1335 VZ |
240 | argv); |
241 | ||
6991087b VS |
242 | #endif // Unicode/ANSI |
243 | ||
4bb6408c | 244 | if (!dpy) { |
c88b736c MB |
245 | // if you don't log to stderr, nothing will be shown... |
246 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
7bcb11d3 | 247 | wxString className(wxTheApp->GetClassName()); |
77ffb593 | 248 | wxLogError(_("wxWidgets could not open display for '%s': exiting."), |
d3a80c92 | 249 | className.c_str()); |
2d120f83 | 250 | exit(-1); |
4bb6408c | 251 | } |
47bc1060 | 252 | m_initialDisplay = (WXDisplay*) dpy; |
afb74891 | 253 | |
e838cc14 VZ |
254 | #ifdef __WXDEBUG__ |
255 | // install the X error handler | |
256 | gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler); | |
257 | #endif // __WXDEBUG__ | |
258 | ||
4bb6408c JS |
259 | // Add general resize proc |
260 | XtActionsRec rec; | |
f1db433a | 261 | rec.string = wxMOTIF_STR("resize"); |
4bb6408c JS |
262 | rec.proc = (XtActionProc)wxWidgetResizeProc; |
263 | XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1); | |
afb74891 | 264 | |
4bb6408c | 265 | GetMainColormap(dpy); |
afb74891 | 266 | |
7e1bcfa8 MB |
267 | wxAddIdleCallback(); |
268 | ||
96be256b | 269 | return true; |
4bb6408c JS |
270 | } |
271 | ||
8ecf7346 VZ |
272 | void wxApp::CleanUp() |
273 | { | |
274 | wxAppBase::CleanUp(); | |
275 | ||
276 | delete wxWidgetHashTable; | |
277 | wxWidgetHashTable = NULL; | |
278 | ||
279 | delete m_mainLoop; | |
280 | ||
281 | for( wxPerDisplayDataMap::iterator it = m_perDisplayData->begin(), | |
282 | end = m_perDisplayData->end(); | |
283 | it != end; ++it ) | |
284 | { | |
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" | |
288 | #ifndef __SUN__ | |
289 | XtDestroyWidget( it->second->m_topLevelWidget ); | |
290 | #endif | |
291 | delete it->second; | |
292 | } | |
293 | } | |
294 | ||
295 | void wxApp::Exit() | |
296 | { | |
297 | wxApp::CleanUp(); | |
298 | ||
299 | wxAppConsole::Exit(); | |
300 | } | |
301 | ||
302 | // ============================================================================ | |
303 | // wxApp | |
304 | // ============================================================================ | |
305 | ||
306 | wxApp::wxApp() | |
307 | { | |
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; | |
313 | } | |
314 | ||
315 | wxApp::~wxApp() | |
316 | { | |
317 | delete m_perDisplayData; | |
318 | } | |
319 | ||
320 | int wxApp::MainLoop() | |
321 | { | |
322 | /* | |
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. | |
327 | */ | |
328 | ||
329 | XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), | |
330 | XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())), | |
331 | PropertyChangeMask); | |
332 | ||
333 | m_mainLoop->Run(); | |
334 | ||
335 | return 0; | |
336 | } | |
337 | ||
338 | // This should be redefined in a derived class for | |
339 | // handling property change events for XAtom IPC. | |
340 | void wxApp::HandlePropertyChange(WXEvent *event) | |
341 | { | |
342 | // by default do nothing special | |
343 | XtDispatchEvent((XEvent*) event); /* let Motif do the work */ | |
344 | } | |
345 | ||
4bb6408c JS |
346 | WXColormap wxApp::GetMainColormap(WXDisplay* display) |
347 | { | |
348 | if (!display) /* Must be called first with non-NULL display */ | |
2d120f83 | 349 | return m_mainColormap; |
a91b47e8 JS |
350 | |
351 | int defaultScreen = DefaultScreen((Display*) display); | |
352 | Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen); | |
afb74891 | 353 | |
a91b47e8 | 354 | Colormap c = DefaultColormapOfScreen(screen); |
afb74891 | 355 | |
4bb6408c | 356 | if (!m_mainColormap) |
2d120f83 | 357 | m_mainColormap = (WXColormap) c; |
afb74891 | 358 | |
4bb6408c JS |
359 | return (WXColormap) c; |
360 | } | |
361 | ||
bdcade0a MB |
362 | static inline wxPerDisplayData& GetOrCreatePerDisplayData |
363 | ( wxPerDisplayDataMap& m, WXDisplay* display ) | |
9ce8d6a2 | 364 | { |
bdcade0a MB |
365 | wxPerDisplayDataMap::iterator it = m.find( display ); |
366 | if( it != m.end() && it->second != NULL ) | |
367 | return *(it->second); | |
368 | ||
369 | wxPerDisplayData* nData = new wxPerDisplayData(); | |
370 | m[display] = nData; | |
371 | ||
372 | return *nData; | |
373 | } | |
9ce8d6a2 | 374 | |
bdcade0a MB |
375 | wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display ) |
376 | { | |
377 | wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData, | |
378 | display ); | |
379 | if( data.m_visualInfo ) | |
380 | return data.m_visualInfo; | |
9ce8d6a2 MB |
381 | |
382 | wxXVisualInfo* vi = new wxXVisualInfo; | |
383 | wxFillXVisualInfo( vi, (Display*)display ); | |
384 | ||
bdcade0a | 385 | data.m_visualInfo = vi; |
9ce8d6a2 MB |
386 | |
387 | return vi; | |
388 | } | |
389 | ||
55034339 WS |
390 | static void wxTLWidgetDestroyCallback(Widget w, XtPointer WXUNUSED(clientData), |
391 | XtPointer WXUNUSED(ptr)) | |
eb6fa4b4 MB |
392 | { |
393 | if( wxTheApp ) | |
5a2e3d8c | 394 | { |
eb6fa4b4 MB |
395 | wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w), |
396 | (WXWidget)NULL ); | |
5a2e3d8c MB |
397 | wxTheApp->SetTopLevelRealizedWidget( (WXDisplay*)XtDisplay(w), |
398 | (WXWidget)NULL ); | |
399 | } | |
eb6fa4b4 MB |
400 | } |
401 | ||
402 | WXWidget wxCreateTopLevelWidget( WXDisplay* display ) | |
403 | { | |
404 | Widget tlw = XtAppCreateShell( (String)NULL, | |
405 | wxTheApp->GetClassName().c_str(), | |
406 | applicationShellWidgetClass, | |
407 | (Display*)display, | |
408 | NULL, 0 ); | |
5a2e3d8c MB |
409 | XtVaSetValues( tlw, |
410 | XmNoverrideRedirect, True, | |
411 | NULL ); | |
eb6fa4b4 MB |
412 | |
413 | XtAddCallback( tlw, XmNdestroyCallback, | |
414 | (XtCallbackProc)wxTLWidgetDestroyCallback, | |
415 | (XtPointer)NULL ); | |
416 | ||
417 | return (WXWidget)tlw; | |
418 | } | |
419 | ||
55034339 | 420 | WXWidget wxCreateTopLevelRealizedWidget( WXDisplay* WXUNUSED(display) ) |
5a2e3d8c | 421 | { |
cb81f1a6 | 422 | Widget rTlw = XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass, |
5a2e3d8c | 423 | (Widget)wxTheApp->GetTopLevelWidget(), |
b09a59f5 | 424 | NULL ); |
5a2e3d8c MB |
425 | XtSetMappedWhenManaged( rTlw, False ); |
426 | XtRealizeWidget( rTlw ); | |
427 | ||
428 | return (WXWidget)rTlw; | |
429 | } | |
430 | ||
eb6fa4b4 MB |
431 | WXWidget wxApp::GetTopLevelWidget() |
432 | { | |
433 | WXDisplay* display = wxGetDisplay(); | |
bdcade0a MB |
434 | wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData, |
435 | display ); | |
436 | if( data.m_topLevelWidget ) | |
437 | return (WXWidget)data.m_topLevelWidget; | |
eb6fa4b4 MB |
438 | |
439 | WXWidget tlw = wxCreateTopLevelWidget( display ); | |
440 | SetTopLevelWidget( display, tlw ); | |
441 | ||
442 | return tlw; | |
443 | } | |
444 | ||
5a2e3d8c MB |
445 | WXWidget wxApp::GetTopLevelRealizedWidget() |
446 | { | |
447 | WXDisplay* display = wxGetDisplay(); | |
448 | wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display ); | |
449 | ||
450 | if( it != m_perDisplayData->end() && it->second->m_topLevelRealizedWidget ) | |
451 | return (WXWidget)it->second->m_topLevelRealizedWidget; | |
452 | ||
453 | WXWidget rTlw = wxCreateTopLevelRealizedWidget( display ); | |
454 | SetTopLevelRealizedWidget( display, rTlw ); | |
455 | ||
456 | return rTlw; | |
457 | } | |
458 | ||
eb6fa4b4 MB |
459 | void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget) |
460 | { | |
5a2e3d8c MB |
461 | GetOrCreatePerDisplayData( *m_perDisplayData, display ) |
462 | .m_topLevelWidget = (Widget)widget; | |
463 | } | |
464 | ||
465 | void wxApp::SetTopLevelRealizedWidget(WXDisplay* display, WXWidget widget) | |
466 | { | |
467 | GetOrCreatePerDisplayData( *m_perDisplayData, display ) | |
468 | .m_topLevelRealizedWidget = (Widget)widget; | |
eb6fa4b4 MB |
469 | } |
470 | ||
4bb6408c | 471 | // Yield to other processes |
cb2713bf | 472 | |
8461e4c2 | 473 | bool wxApp::Yield(bool onlyIfNeeded) |
4bb6408c | 474 | { |
96be256b | 475 | static bool s_inYield = false; |
8461e4c2 VZ |
476 | |
477 | if ( s_inYield ) | |
478 | { | |
479 | if ( !onlyIfNeeded ) | |
480 | { | |
481 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
482 | } | |
483 | ||
96be256b | 484 | return false; |
8461e4c2 VZ |
485 | } |
486 | ||
96be256b | 487 | s_inYield = true; |
cb2713bf | 488 | |
4bb6408c | 489 | while (wxTheApp && wxTheApp->Pending()) |
2d120f83 | 490 | wxTheApp->Dispatch(); |
518b5d2f | 491 | |
96be256b | 492 | s_inYield = false; |
cb2713bf | 493 | |
96be256b | 494 | return true; |
4bb6408c JS |
495 | } |
496 | ||
d391a345 VZ |
497 | // ---------------------------------------------------------------------------- |
498 | // accessors for C modules | |
499 | // ---------------------------------------------------------------------------- | |
500 | ||
501 | extern "C" XtAppContext wxGetAppContext() | |
502 | { | |
503 | return (XtAppContext)wxTheApp->GetAppContext(); | |
504 | } |