]>
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 | ||
bcd055ae JJ |
15 | #ifdef __VMS |
16 | #define XtParent XTPARENT | |
17 | #define XtDisplay XTDISPLAY | |
18 | #endif | |
19 | ||
88a7a4e1 WS |
20 | #include "wx/app.h" |
21 | ||
32d4c30a WS |
22 | #ifndef WX_PRECOMP |
23 | #include "wx/hash.h" | |
88a7a4e1 | 24 | #include "wx/intl.h" |
e4db172a | 25 | #include "wx/log.h" |
de6185e2 | 26 | #include "wx/utils.h" |
5b56bffb | 27 | #include "wx/memory.h" |
73216ce6 | 28 | #include "wx/font.h" |
32d4c30a WS |
29 | #endif |
30 | ||
7e1bcfa8 | 31 | #include "wx/evtloop.h" |
b412f9be | 32 | |
7bcb11d3 | 33 | #if wxUSE_THREADS |
dfe1eee3 | 34 | #include "wx/thread.h" |
7bcb11d3 | 35 | #endif |
4bb6408c | 36 | |
338dd992 | 37 | #ifdef __VMS__ |
02761f6c | 38 | #pragma message disable nosimpint |
338dd992 | 39 | #endif |
4bb6408c JS |
40 | #include <Xm/Xm.h> |
41 | #include <X11/Xlib.h> | |
42 | #include <X11/Xutil.h> | |
43 | #include <X11/Xresource.h> | |
44 | #include <X11/Xatom.h> | |
338dd992 | 45 | #ifdef __VMS__ |
02761f6c | 46 | #pragma message enable nosimpint |
338dd992 | 47 | #endif |
4bb6408c JS |
48 | |
49 | #include "wx/motif/private.h" | |
50 | ||
51 | #include <string.h> | |
52 | ||
eb6fa4b4 MB |
53 | struct wxPerDisplayData |
54 | { | |
55 | wxPerDisplayData() | |
5a2e3d8c MB |
56 | { |
57 | m_visualInfo = NULL; | |
58 | m_topLevelWidget = NULL; | |
59 | m_topLevelRealizedWidget = NULL; | |
60 | } | |
eb6fa4b4 MB |
61 | |
62 | wxXVisualInfo* m_visualInfo; | |
5a2e3d8c | 63 | Widget m_topLevelWidget, m_topLevelRealizedWidget; |
eb6fa4b4 MB |
64 | }; |
65 | ||
eb6fa4b4 MB |
66 | static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData, |
67 | XtPointer ptr); | |
68 | static WXWidget wxCreateTopLevelWidget( WXDisplay* display ); | |
9ce8d6a2 | 69 | |
7e1bcfa8 | 70 | extern bool wxAddIdleCallback(); |
7491d644 | 71 | |
4bb6408c JS |
72 | wxHashTable *wxWidgetHashTable = NULL; |
73 | ||
4bb6408c | 74 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) |
9c6e1335 | 75 | |
4bb6408c | 76 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) |
955a9197 | 77 | EVT_IDLE(wxAppBase::OnIdle) |
4bb6408c | 78 | END_EVENT_TABLE() |
4bb6408c | 79 | |
e838cc14 | 80 | #ifdef __WXDEBUG__ |
86ac4a22 VZ |
81 | extern "C" |
82 | { | |
e838cc14 | 83 | typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *); |
86ac4a22 | 84 | } |
e838cc14 | 85 | |
86ac4a22 | 86 | XErrorHandlerFunc gs_pfnXErrorHandler = 0; |
e838cc14 | 87 | |
86ac4a22 VZ |
88 | extern "C" |
89 | { | |
90 | ||
91 | static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent) | |
92 | { | |
93 | // just forward to the default handler for now | |
94 | return gs_pfnXErrorHandler(dpy, xevent); | |
95 | } | |
96 | ||
97 | } | |
e838cc14 VZ |
98 | #endif // __WXDEBUG__ |
99 | ||
86ac4a22 | 100 | bool wxApp::Initialize(int& argcOrig, wxChar **argvOrig) |
4bb6408c | 101 | { |
105fbe1f MB |
102 | #if wxUSE_INTL |
103 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
104 | #endif | |
105 | ||
86ac4a22 | 106 | if ( !wxAppBase::Initialize(argcOrig, argvOrig) ) |
94826170 | 107 | return false; |
afb74891 | 108 | |
4bb6408c | 109 | wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER); |
afb74891 | 110 | |
94826170 | 111 | return true; |
4bb6408c JS |
112 | } |
113 | ||
114 | void wxApp::CleanUp() | |
115 | { | |
8d1bf3c7 MB |
116 | wxAppBase::CleanUp(); |
117 | ||
e6be3e7c MB |
118 | delete wxWidgetHashTable; |
119 | wxWidgetHashTable = NULL; | |
120 | ||
8d1bf3c7 MB |
121 | delete m_mainLoop; |
122 | ||
123 | for( wxPerDisplayDataMap::iterator it = m_perDisplayData->begin(), | |
124 | end = m_perDisplayData->end(); | |
125 | it != end; ++it ) | |
126 | { | |
127 | delete it->second->m_visualInfo; | |
df7e062c MW |
128 | // On Solaris 10 calling XtDestroyWidget on the top level widget |
129 | // dumps core if the locale is set to something other than "C" | |
130 | #ifndef __SUN__ | |
8d1bf3c7 | 131 | XtDestroyWidget( it->second->m_topLevelWidget ); |
df7e062c | 132 | #endif |
8d1bf3c7 MB |
133 | delete it->second; |
134 | } | |
4bb6408c JS |
135 | } |
136 | ||
e2478fde VZ |
137 | void wxApp::Exit() |
138 | { | |
139 | wxApp::CleanUp(); | |
140 | ||
141 | wxAppConsole::Exit(); | |
142 | } | |
143 | ||
e6be3e7c | 144 | // ============================================================================ |
05e2b077 | 145 | // wxApp |
e6be3e7c MB |
146 | // ============================================================================ |
147 | ||
4bb6408c JS |
148 | wxApp::wxApp() |
149 | { | |
4bb6408c JS |
150 | argc = 0; |
151 | argv = NULL; | |
afb74891 | 152 | |
f15b55ce | 153 | m_mainLoop = new wxEventLoop; |
4bb6408c JS |
154 | m_mainColormap = (WXColormap) NULL; |
155 | m_appContext = (WXAppContext) NULL; | |
47bc1060 | 156 | m_initialDisplay = (WXDisplay*) 0; |
eb6fa4b4 | 157 | m_perDisplayData = new wxPerDisplayDataMap; |
4bb6408c JS |
158 | } |
159 | ||
7e1bcfa8 MB |
160 | wxApp::~wxApp() |
161 | { | |
a6a68df8 | 162 | delete m_perDisplayData; |
7e1bcfa8 MB |
163 | } |
164 | ||
4bb6408c JS |
165 | int wxApp::MainLoop() |
166 | { | |
2d120f83 | 167 | /* |
4bb6408c JS |
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. | |
172 | */ | |
afb74891 | 173 | |
4bb6408c | 174 | XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), |
2d120f83 JS |
175 | XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())), |
176 | PropertyChangeMask); | |
afb74891 | 177 | |
f15b55ce | 178 | m_mainLoop->Run(); |
afb74891 | 179 | |
8aa04e8b JS |
180 | return 0; |
181 | } | |
182 | ||
4bb6408c JS |
183 | // This should be redefined in a derived class for |
184 | // handling property change events for XAtom IPC. | |
185 | void wxApp::HandlePropertyChange(WXEvent *event) | |
186 | { | |
187 | // by default do nothing special | |
188 | XtDispatchEvent((XEvent*) event); /* let Motif do the work */ | |
189 | } | |
190 | ||
edc1cd8b | 191 | static char *fallbackResources[] = { |
0ab82ca9 VZ |
192 | // better defaults for CDE under Irix |
193 | // | |
194 | // TODO: do something similar for the other systems, the hardcoded defaults | |
195 | // below are ugly | |
196 | #ifdef __SGI__ | |
f1db433a VZ |
197 | wxMOTIF_STR("*sgiMode: True"), |
198 | wxMOTIF_STR("*useSchemes: all"), | |
0ab82ca9 | 199 | #else // !__SGI__ |
105fbe1f MB |
200 | #if !wxMOTIF_USE_RENDER_TABLE |
201 | wxMOTIF_STR("*.fontList: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"), | |
202 | #else | |
203 | wxMOTIF_STR("*wxDefaultRendition.fontName: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"), | |
204 | wxMOTIF_STR("*wxDefaultRendition.fontType: FONT_IS_FONTSET"), | |
205 | wxMOTIF_STR("*.renderTable: wxDefaultRendition"), | |
206 | #endif | |
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"), | |
0ab82ca9 | 211 | #endif // __SGI__/!__SGI__ |
edc1cd8b JS |
212 | NULL |
213 | }; | |
214 | ||
4bb6408c JS |
215 | // Create an application context |
216 | bool wxApp::OnInitGui() | |
217 | { | |
fd304d98 | 218 | if( !wxAppBase::OnInitGui() ) |
96be256b | 219 | return false; |
fd304d98 | 220 | |
0fad0dfd VZ |
221 | #ifdef __HPUX__ |
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 | |
226 | // | |
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) | |
231 | // | |
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"); | |
236 | if ( !loc ) | |
237 | { | |
238 | loc = getenv("LC_ALL"); | |
239 | if ( loc ) | |
240 | fixAll = true; | |
241 | } | |
242 | ||
243 | if ( !loc || | |
244 | (loc[0] == 'C' && loc[1] == '\0') || | |
245 | strcmp(loc, "POSIX") == 0 ) | |
246 | { | |
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"); | |
250 | } | |
251 | #endif // __HPUX__ | |
252 | ||
9d02485e | 253 | XtSetLanguageProc(NULL, NULL, NULL); |
4bb6408c | 254 | XtToolkitInitialize() ; |
edc1cd8b JS |
255 | wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext(); |
256 | XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources); | |
257 | ||
0b6d76bf VZ |
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() ) | |
265 | clsname = _T("wx"); | |
266 | ||
267 | Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext, | |
268 | (String)NULL, | |
269 | appname.c_str(), | |
270 | clsname.c_str(), | |
271 | NULL, 0, // no options | |
4bb6408c | 272 | # if XtSpecificationRelease < 5 |
9c6e1335 | 273 | (Cardinal*) &argc, |
4bb6408c | 274 | # else |
9c6e1335 | 275 | &argc, |
4bb6408c | 276 | # endif |
9c6e1335 VZ |
277 | argv); |
278 | ||
4bb6408c | 279 | if (!dpy) { |
c88b736c MB |
280 | // if you don't log to stderr, nothing will be shown... |
281 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
7bcb11d3 | 282 | wxString className(wxTheApp->GetClassName()); |
77ffb593 | 283 | wxLogError(_("wxWidgets could not open display for '%s': exiting."), |
d3a80c92 | 284 | className.c_str()); |
2d120f83 | 285 | exit(-1); |
4bb6408c | 286 | } |
47bc1060 | 287 | m_initialDisplay = (WXDisplay*) dpy; |
afb74891 | 288 | |
e838cc14 VZ |
289 | #ifdef __WXDEBUG__ |
290 | // install the X error handler | |
291 | gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler); | |
292 | #endif // __WXDEBUG__ | |
293 | ||
4bb6408c JS |
294 | // Add general resize proc |
295 | XtActionsRec rec; | |
f1db433a | 296 | rec.string = wxMOTIF_STR("resize"); |
4bb6408c JS |
297 | rec.proc = (XtActionProc)wxWidgetResizeProc; |
298 | XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1); | |
afb74891 | 299 | |
4bb6408c | 300 | GetMainColormap(dpy); |
afb74891 | 301 | |
7e1bcfa8 MB |
302 | wxAddIdleCallback(); |
303 | ||
96be256b | 304 | return true; |
4bb6408c JS |
305 | } |
306 | ||
307 | WXColormap wxApp::GetMainColormap(WXDisplay* display) | |
308 | { | |
309 | if (!display) /* Must be called first with non-NULL display */ | |
2d120f83 | 310 | return m_mainColormap; |
a91b47e8 JS |
311 | |
312 | int defaultScreen = DefaultScreen((Display*) display); | |
313 | Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen); | |
afb74891 | 314 | |
a91b47e8 | 315 | Colormap c = DefaultColormapOfScreen(screen); |
afb74891 | 316 | |
4bb6408c | 317 | if (!m_mainColormap) |
2d120f83 | 318 | m_mainColormap = (WXColormap) c; |
afb74891 | 319 | |
4bb6408c JS |
320 | return (WXColormap) c; |
321 | } | |
322 | ||
bdcade0a MB |
323 | static inline wxPerDisplayData& GetOrCreatePerDisplayData |
324 | ( wxPerDisplayDataMap& m, WXDisplay* display ) | |
9ce8d6a2 | 325 | { |
bdcade0a MB |
326 | wxPerDisplayDataMap::iterator it = m.find( display ); |
327 | if( it != m.end() && it->second != NULL ) | |
328 | return *(it->second); | |
329 | ||
330 | wxPerDisplayData* nData = new wxPerDisplayData(); | |
331 | m[display] = nData; | |
332 | ||
333 | return *nData; | |
334 | } | |
9ce8d6a2 | 335 | |
bdcade0a MB |
336 | wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display ) |
337 | { | |
338 | wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData, | |
339 | display ); | |
340 | if( data.m_visualInfo ) | |
341 | return data.m_visualInfo; | |
9ce8d6a2 MB |
342 | |
343 | wxXVisualInfo* vi = new wxXVisualInfo; | |
344 | wxFillXVisualInfo( vi, (Display*)display ); | |
345 | ||
bdcade0a | 346 | data.m_visualInfo = vi; |
9ce8d6a2 MB |
347 | |
348 | return vi; | |
349 | } | |
350 | ||
55034339 WS |
351 | static void wxTLWidgetDestroyCallback(Widget w, XtPointer WXUNUSED(clientData), |
352 | XtPointer WXUNUSED(ptr)) | |
eb6fa4b4 MB |
353 | { |
354 | if( wxTheApp ) | |
5a2e3d8c | 355 | { |
eb6fa4b4 MB |
356 | wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w), |
357 | (WXWidget)NULL ); | |
5a2e3d8c MB |
358 | wxTheApp->SetTopLevelRealizedWidget( (WXDisplay*)XtDisplay(w), |
359 | (WXWidget)NULL ); | |
360 | } | |
eb6fa4b4 MB |
361 | } |
362 | ||
363 | WXWidget wxCreateTopLevelWidget( WXDisplay* display ) | |
364 | { | |
365 | Widget tlw = XtAppCreateShell( (String)NULL, | |
366 | wxTheApp->GetClassName().c_str(), | |
367 | applicationShellWidgetClass, | |
368 | (Display*)display, | |
369 | NULL, 0 ); | |
5a2e3d8c MB |
370 | XtVaSetValues( tlw, |
371 | XmNoverrideRedirect, True, | |
372 | NULL ); | |
eb6fa4b4 MB |
373 | |
374 | XtAddCallback( tlw, XmNdestroyCallback, | |
375 | (XtCallbackProc)wxTLWidgetDestroyCallback, | |
376 | (XtPointer)NULL ); | |
377 | ||
378 | return (WXWidget)tlw; | |
379 | } | |
380 | ||
55034339 | 381 | WXWidget wxCreateTopLevelRealizedWidget( WXDisplay* WXUNUSED(display) ) |
5a2e3d8c | 382 | { |
cb81f1a6 | 383 | Widget rTlw = XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass, |
5a2e3d8c | 384 | (Widget)wxTheApp->GetTopLevelWidget(), |
b09a59f5 | 385 | NULL ); |
5a2e3d8c MB |
386 | XtSetMappedWhenManaged( rTlw, False ); |
387 | XtRealizeWidget( rTlw ); | |
388 | ||
389 | return (WXWidget)rTlw; | |
390 | } | |
391 | ||
eb6fa4b4 MB |
392 | WXWidget wxApp::GetTopLevelWidget() |
393 | { | |
394 | WXDisplay* display = wxGetDisplay(); | |
bdcade0a MB |
395 | wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData, |
396 | display ); | |
397 | if( data.m_topLevelWidget ) | |
398 | return (WXWidget)data.m_topLevelWidget; | |
eb6fa4b4 MB |
399 | |
400 | WXWidget tlw = wxCreateTopLevelWidget( display ); | |
401 | SetTopLevelWidget( display, tlw ); | |
402 | ||
403 | return tlw; | |
404 | } | |
405 | ||
5a2e3d8c MB |
406 | WXWidget wxApp::GetTopLevelRealizedWidget() |
407 | { | |
408 | WXDisplay* display = wxGetDisplay(); | |
409 | wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display ); | |
410 | ||
411 | if( it != m_perDisplayData->end() && it->second->m_topLevelRealizedWidget ) | |
412 | return (WXWidget)it->second->m_topLevelRealizedWidget; | |
413 | ||
414 | WXWidget rTlw = wxCreateTopLevelRealizedWidget( display ); | |
415 | SetTopLevelRealizedWidget( display, rTlw ); | |
416 | ||
417 | return rTlw; | |
418 | } | |
419 | ||
eb6fa4b4 MB |
420 | void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget) |
421 | { | |
5a2e3d8c MB |
422 | GetOrCreatePerDisplayData( *m_perDisplayData, display ) |
423 | .m_topLevelWidget = (Widget)widget; | |
424 | } | |
425 | ||
426 | void wxApp::SetTopLevelRealizedWidget(WXDisplay* display, WXWidget widget) | |
427 | { | |
428 | GetOrCreatePerDisplayData( *m_perDisplayData, display ) | |
429 | .m_topLevelRealizedWidget = (Widget)widget; | |
eb6fa4b4 MB |
430 | } |
431 | ||
4bb6408c | 432 | // Yield to other processes |
cb2713bf | 433 | |
8461e4c2 | 434 | bool wxApp::Yield(bool onlyIfNeeded) |
4bb6408c | 435 | { |
96be256b | 436 | static bool s_inYield = false; |
8461e4c2 VZ |
437 | |
438 | if ( s_inYield ) | |
439 | { | |
440 | if ( !onlyIfNeeded ) | |
441 | { | |
442 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
443 | } | |
444 | ||
96be256b | 445 | return false; |
8461e4c2 VZ |
446 | } |
447 | ||
96be256b | 448 | s_inYield = true; |
cb2713bf | 449 | |
4bb6408c | 450 | while (wxTheApp && wxTheApp->Pending()) |
2d120f83 | 451 | wxTheApp->Dispatch(); |
518b5d2f | 452 | |
96be256b | 453 | s_inYield = false; |
cb2713bf | 454 | |
96be256b | 455 | return true; |
4bb6408c JS |
456 | } |
457 | ||
d391a345 VZ |
458 | // ---------------------------------------------------------------------------- |
459 | // accessors for C modules | |
460 | // ---------------------------------------------------------------------------- | |
461 | ||
462 | extern "C" XtAppContext wxGetAppContext() | |
463 | { | |
464 | return (XtAppContext)wxTheApp->GetAppContext(); | |
465 | } |