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