]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/motif/app.cpp | |
3 | // Purpose: wxApp | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __VMS | |
16 | #define XtParent XTPARENT | |
17 | #define XtDisplay XTDISPLAY | |
18 | #endif | |
19 | ||
20 | #include "wx/app.h" | |
21 | ||
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/hash.h" | |
24 | #include "wx/intl.h" | |
25 | #include "wx/log.h" | |
26 | #include "wx/utils.h" | |
27 | #include "wx/memory.h" | |
28 | #include "wx/font.h" | |
29 | #endif | |
30 | ||
31 | #include "wx/evtloop.h" | |
32 | ||
33 | #if wxUSE_THREADS | |
34 | #include "wx/thread.h" | |
35 | #endif | |
36 | ||
37 | #ifdef __VMS__ | |
38 | #pragma message disable nosimpint | |
39 | #endif | |
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> | |
45 | #ifdef __VMS__ | |
46 | #pragma message enable nosimpint | |
47 | #endif | |
48 | ||
49 | #include "wx/motif/private.h" | |
50 | ||
51 | #include <string.h> | |
52 | ||
53 | struct wxPerDisplayData | |
54 | { | |
55 | wxPerDisplayData() | |
56 | { | |
57 | m_visualInfo = NULL; | |
58 | m_topLevelWidget = NULL; | |
59 | m_topLevelRealizedWidget = NULL; | |
60 | } | |
61 | ||
62 | wxXVisualInfo* m_visualInfo; | |
63 | Widget m_topLevelWidget, m_topLevelRealizedWidget; | |
64 | }; | |
65 | ||
66 | static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData, | |
67 | XtPointer ptr); | |
68 | static WXWidget wxCreateTopLevelWidget( WXDisplay* display ); | |
69 | ||
70 | extern bool wxAddIdleCallback(); | |
71 | ||
72 | wxHashTable *wxWidgetHashTable = NULL; | |
73 | ||
74 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) | |
75 | ||
76 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
77 | EVT_IDLE(wxAppBase::OnIdle) | |
78 | END_EVENT_TABLE() | |
79 | ||
80 | #ifdef __WXDEBUG__ | |
81 | extern "C" | |
82 | { | |
83 | typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *); | |
84 | } | |
85 | ||
86 | XErrorHandlerFunc gs_pfnXErrorHandler = 0; | |
87 | ||
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 | } | |
98 | #endif // __WXDEBUG__ | |
99 | ||
100 | bool wxApp::Initialize(int& argcOrig, wxChar **argvOrig) | |
101 | { | |
102 | #if wxUSE_INTL | |
103 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
104 | #endif | |
105 | ||
106 | if ( !wxAppBase::Initialize(argcOrig, argvOrig) ) | |
107 | return false; | |
108 | ||
109 | wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER); | |
110 | ||
111 | return true; | |
112 | } | |
113 | ||
114 | void wxApp::CleanUp() | |
115 | { | |
116 | wxAppBase::CleanUp(); | |
117 | ||
118 | delete wxWidgetHashTable; | |
119 | wxWidgetHashTable = NULL; | |
120 | ||
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; | |
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__ | |
131 | XtDestroyWidget( it->second->m_topLevelWidget ); | |
132 | #endif | |
133 | delete it->second; | |
134 | } | |
135 | } | |
136 | ||
137 | void wxApp::Exit() | |
138 | { | |
139 | wxApp::CleanUp(); | |
140 | ||
141 | wxAppConsole::Exit(); | |
142 | } | |
143 | ||
144 | // ============================================================================ | |
145 | // wxApp | |
146 | // ============================================================================ | |
147 | ||
148 | wxApp::wxApp() | |
149 | { | |
150 | argc = 0; | |
151 | argv = NULL; | |
152 | ||
153 | m_mainLoop = new wxEventLoop; | |
154 | m_mainColormap = (WXColormap) NULL; | |
155 | m_appContext = (WXAppContext) NULL; | |
156 | m_initialDisplay = (WXDisplay*) 0; | |
157 | m_perDisplayData = new wxPerDisplayDataMap; | |
158 | } | |
159 | ||
160 | wxApp::~wxApp() | |
161 | { | |
162 | delete m_perDisplayData; | |
163 | } | |
164 | ||
165 | int wxApp::MainLoop() | |
166 | { | |
167 | /* | |
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 | */ | |
173 | ||
174 | XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), | |
175 | XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())), | |
176 | PropertyChangeMask); | |
177 | ||
178 | m_mainLoop->Run(); | |
179 | ||
180 | return 0; | |
181 | } | |
182 | ||
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 | ||
191 | static char *fallbackResources[] = { | |
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__ | |
197 | wxMOTIF_STR("*sgiMode: True"), | |
198 | wxMOTIF_STR("*useSchemes: all"), | |
199 | #else // !__SGI__ | |
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"), | |
211 | #endif // __SGI__/!__SGI__ | |
212 | NULL | |
213 | }; | |
214 | ||
215 | // Create an application context | |
216 | bool wxApp::OnInitGui() | |
217 | { | |
218 | if( !wxAppBase::OnInitGui() ) | |
219 | return false; | |
220 | ||
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 | ||
253 | XtSetLanguageProc(NULL, NULL, NULL); | |
254 | XtToolkitInitialize() ; | |
255 | wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext(); | |
256 | XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources); | |
257 | ||
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 | |
272 | # if XtSpecificationRelease < 5 | |
273 | (Cardinal*) &argc, | |
274 | # else | |
275 | &argc, | |
276 | # endif | |
277 | argv); | |
278 | ||
279 | if (!dpy) { | |
280 | // if you don't log to stderr, nothing will be shown... | |
281 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
282 | wxString className(wxTheApp->GetClassName()); | |
283 | wxLogError(_("wxWidgets could not open display for '%s': exiting."), | |
284 | className.c_str()); | |
285 | exit(-1); | |
286 | } | |
287 | m_initialDisplay = (WXDisplay*) dpy; | |
288 | ||
289 | #ifdef __WXDEBUG__ | |
290 | // install the X error handler | |
291 | gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler); | |
292 | #endif // __WXDEBUG__ | |
293 | ||
294 | // Add general resize proc | |
295 | XtActionsRec rec; | |
296 | rec.string = wxMOTIF_STR("resize"); | |
297 | rec.proc = (XtActionProc)wxWidgetResizeProc; | |
298 | XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1); | |
299 | ||
300 | GetMainColormap(dpy); | |
301 | ||
302 | wxAddIdleCallback(); | |
303 | ||
304 | return true; | |
305 | } | |
306 | ||
307 | WXColormap wxApp::GetMainColormap(WXDisplay* display) | |
308 | { | |
309 | if (!display) /* Must be called first with non-NULL display */ | |
310 | return m_mainColormap; | |
311 | ||
312 | int defaultScreen = DefaultScreen((Display*) display); | |
313 | Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen); | |
314 | ||
315 | Colormap c = DefaultColormapOfScreen(screen); | |
316 | ||
317 | if (!m_mainColormap) | |
318 | m_mainColormap = (WXColormap) c; | |
319 | ||
320 | return (WXColormap) c; | |
321 | } | |
322 | ||
323 | static inline wxPerDisplayData& GetOrCreatePerDisplayData | |
324 | ( wxPerDisplayDataMap& m, WXDisplay* display ) | |
325 | { | |
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 | } | |
335 | ||
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; | |
342 | ||
343 | wxXVisualInfo* vi = new wxXVisualInfo; | |
344 | wxFillXVisualInfo( vi, (Display*)display ); | |
345 | ||
346 | data.m_visualInfo = vi; | |
347 | ||
348 | return vi; | |
349 | } | |
350 | ||
351 | static void wxTLWidgetDestroyCallback(Widget w, XtPointer WXUNUSED(clientData), | |
352 | XtPointer WXUNUSED(ptr)) | |
353 | { | |
354 | if( wxTheApp ) | |
355 | { | |
356 | wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w), | |
357 | (WXWidget)NULL ); | |
358 | wxTheApp->SetTopLevelRealizedWidget( (WXDisplay*)XtDisplay(w), | |
359 | (WXWidget)NULL ); | |
360 | } | |
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 ); | |
370 | XtVaSetValues( tlw, | |
371 | XmNoverrideRedirect, True, | |
372 | NULL ); | |
373 | ||
374 | XtAddCallback( tlw, XmNdestroyCallback, | |
375 | (XtCallbackProc)wxTLWidgetDestroyCallback, | |
376 | (XtPointer)NULL ); | |
377 | ||
378 | return (WXWidget)tlw; | |
379 | } | |
380 | ||
381 | WXWidget wxCreateTopLevelRealizedWidget( WXDisplay* WXUNUSED(display) ) | |
382 | { | |
383 | Widget rTlw = XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass, | |
384 | (Widget)wxTheApp->GetTopLevelWidget(), | |
385 | NULL ); | |
386 | XtSetMappedWhenManaged( rTlw, False ); | |
387 | XtRealizeWidget( rTlw ); | |
388 | ||
389 | return (WXWidget)rTlw; | |
390 | } | |
391 | ||
392 | WXWidget wxApp::GetTopLevelWidget() | |
393 | { | |
394 | WXDisplay* display = wxGetDisplay(); | |
395 | wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData, | |
396 | display ); | |
397 | if( data.m_topLevelWidget ) | |
398 | return (WXWidget)data.m_topLevelWidget; | |
399 | ||
400 | WXWidget tlw = wxCreateTopLevelWidget( display ); | |
401 | SetTopLevelWidget( display, tlw ); | |
402 | ||
403 | return tlw; | |
404 | } | |
405 | ||
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 | ||
420 | void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget) | |
421 | { | |
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; | |
430 | } | |
431 | ||
432 | // Yield to other processes | |
433 | ||
434 | bool wxApp::Yield(bool onlyIfNeeded) | |
435 | { | |
436 | static bool s_inYield = false; | |
437 | ||
438 | if ( s_inYield ) | |
439 | { | |
440 | if ( !onlyIfNeeded ) | |
441 | { | |
442 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
443 | } | |
444 | ||
445 | return false; | |
446 | } | |
447 | ||
448 | s_inYield = true; | |
449 | ||
450 | while (wxTheApp && wxTheApp->Pending()) | |
451 | wxTheApp->Dispatch(); | |
452 | ||
453 | s_inYield = false; | |
454 | ||
455 | return true; | |
456 | } | |
457 | ||
458 | // ---------------------------------------------------------------------------- | |
459 | // accessors for C modules | |
460 | // ---------------------------------------------------------------------------- | |
461 | ||
462 | extern "C" XtAppContext wxGetAppContext() | |
463 | { | |
464 | return (XtAppContext)wxTheApp->GetAppContext(); | |
465 | } |