]> git.saurik.com Git - wxWidgets.git/blob - src/motif/app.cpp
Remove unnecessary restriction for sizer parent
[wxWidgets.git] / src / motif / app.cpp
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 ( !wxAppBase::Initialize(argcOrig, argvOrig) )
103 return false;
104
105 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
106
107 #if wxUSE_INTL
108 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
109 #endif
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 delete m_perDisplayData;
137 }
138
139 void wxApp::Exit()
140 {
141 wxApp::CleanUp();
142
143 wxAppConsole::Exit();
144 }
145
146 // ============================================================================
147 // wxApp
148 // ============================================================================
149
150 wxApp::wxApp()
151 {
152 argc = 0;
153 argv = NULL;
154
155 m_mainLoop = new wxEventLoop;
156 m_mainColormap = (WXColormap) NULL;
157 m_appContext = (WXAppContext) NULL;
158 m_initialDisplay = (WXDisplay*) 0;
159 m_perDisplayData = new wxPerDisplayDataMap;
160 }
161
162 wxApp::~wxApp()
163 {
164 wxApp::SetInstance(NULL);
165 }
166
167 int wxApp::MainLoop()
168 {
169 /*
170 * Sit around forever waiting to process X-events. Property Change
171 * event are handled special, because they have to refer to
172 * the root window rather than to a widget. therefore we can't
173 * use an Xt-eventhandler.
174 */
175
176 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
177 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
178 PropertyChangeMask);
179
180 m_mainLoop->Run();
181
182 return 0;
183 }
184
185 // This should be redefined in a derived class for
186 // handling property change events for XAtom IPC.
187 void wxApp::HandlePropertyChange(WXEvent *event)
188 {
189 // by default do nothing special
190 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
191 }
192
193 static char *fallbackResources[] = {
194 // better defaults for CDE under Irix
195 //
196 // TODO: do something similar for the other systems, the hardcoded defaults
197 // below are ugly
198 #ifdef __SGI__
199 wxMOTIF_STR("*sgiMode: True"),
200 wxMOTIF_STR("*useSchemes: all"),
201 #else // !__SGI__
202 wxMOTIF_STR("*menuBar.marginHeight: 0"),
203 wxMOTIF_STR("*menuBar.shadowThickness: 1"),
204 wxMOTIF_STR("*background: #c0c0c0"),
205 wxMOTIF_STR("*foreground: black"),
206 #endif // __SGI__/!__SGI__
207 NULL
208 };
209
210 // Create an application context
211 bool wxApp::OnInitGui()
212 {
213 if( !wxAppBase::OnInitGui() )
214 return false;
215
216 #ifdef __HPUX__
217 // under HP-UX creating XmFontSet fails when the system locale is C and
218 // we're using a remote DISPLAY, presumably because HP-UX uses its own
219 // names for C and ISO locales (roman8 and iso8859n respectively) and so
220 // its Motif libraries have troubles with non-HP X server
221 //
222 // whatever the reason, the fact is that without this hack any wxMotif
223 // program crashes on startup because it can't create any font (HP programs
224 // still work but they do spit out messages about failing to create font
225 // sets and failing back on "fixed" font too)
226 //
227 // notice that calling setlocale() here is not enough because X(m) init
228 // functions call setlocale() later so we really have to change environment
229 bool fixAll = false; // tweak LC_ALL (or just LC_CTYPE)?
230 const char *loc = getenv("LC_CTYPE");
231 if ( !loc )
232 {
233 loc = getenv("LC_ALL");
234 if ( loc )
235 fixAll = true;
236 }
237
238 if ( !loc ||
239 (loc[0] == 'C' && loc[1] == '\0') ||
240 strcmp(loc, "POSIX") == 0 )
241 {
242 // we're using C locale, "fix" it
243 wxLogDebug(_T("HP-UX fontset hack: forcing locale to en_US.iso88591"));
244 putenv(fixAll ? "LC_ALL=en_US.iso88591" : "LC_CTYPE=en_US.iso88591");
245 }
246 #endif // __HPUX__
247
248 XtSetLanguageProc(NULL, NULL, NULL);
249 XtToolkitInitialize() ;
250 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext();
251 XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources);
252
253 // we shouldn't pass empty application/class name as it results in
254 // immediate crash inside XOpenIM() (if XIM is used) under IRIX
255 wxString appname = wxTheApp->GetAppName();
256 if ( appname.empty() )
257 appname = _T("wxapp");
258 wxString clsname = wxTheApp->GetClassName();
259 if ( clsname.empty() )
260 clsname = _T("wx");
261
262 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,
263 (String)NULL,
264 appname.c_str(),
265 clsname.c_str(),
266 NULL, 0, // no options
267 # if XtSpecificationRelease < 5
268 (Cardinal*) &argc,
269 # else
270 &argc,
271 # endif
272 argv);
273
274 if (!dpy) {
275 // if you don't log to stderr, nothing will be shown...
276 delete wxLog::SetActiveTarget(new wxLogStderr);
277 wxString className(wxTheApp->GetClassName());
278 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
279 className.c_str());
280 exit(-1);
281 }
282 m_initialDisplay = (WXDisplay*) dpy;
283
284 #ifdef __WXDEBUG__
285 // install the X error handler
286 gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler);
287 #endif // __WXDEBUG__
288
289 // Add general resize proc
290 XtActionsRec rec;
291 rec.string = wxMOTIF_STR("resize");
292 rec.proc = (XtActionProc)wxWidgetResizeProc;
293 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
294
295 GetMainColormap(dpy);
296
297 wxAddIdleCallback();
298
299 return true;
300 }
301
302 WXColormap wxApp::GetMainColormap(WXDisplay* display)
303 {
304 if (!display) /* Must be called first with non-NULL display */
305 return m_mainColormap;
306
307 int defaultScreen = DefaultScreen((Display*) display);
308 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
309
310 Colormap c = DefaultColormapOfScreen(screen);
311
312 if (!m_mainColormap)
313 m_mainColormap = (WXColormap) c;
314
315 return (WXColormap) c;
316 }
317
318 static inline wxPerDisplayData& GetOrCreatePerDisplayData
319 ( wxPerDisplayDataMap& m, WXDisplay* display )
320 {
321 wxPerDisplayDataMap::iterator it = m.find( display );
322 if( it != m.end() && it->second != NULL )
323 return *(it->second);
324
325 wxPerDisplayData* nData = new wxPerDisplayData();
326 m[display] = nData;
327
328 return *nData;
329 }
330
331 wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display )
332 {
333 wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData,
334 display );
335 if( data.m_visualInfo )
336 return data.m_visualInfo;
337
338 wxXVisualInfo* vi = new wxXVisualInfo;
339 wxFillXVisualInfo( vi, (Display*)display );
340
341 data.m_visualInfo = vi;
342
343 return vi;
344 }
345
346 static void wxTLWidgetDestroyCallback(Widget w, XtPointer WXUNUSED(clientData),
347 XtPointer WXUNUSED(ptr))
348 {
349 if( wxTheApp )
350 {
351 wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w),
352 (WXWidget)NULL );
353 wxTheApp->SetTopLevelRealizedWidget( (WXDisplay*)XtDisplay(w),
354 (WXWidget)NULL );
355 }
356 }
357
358 WXWidget wxCreateTopLevelWidget( WXDisplay* display )
359 {
360 Widget tlw = XtAppCreateShell( (String)NULL,
361 wxTheApp->GetClassName().c_str(),
362 applicationShellWidgetClass,
363 (Display*)display,
364 NULL, 0 );
365 XtVaSetValues( tlw,
366 XmNoverrideRedirect, True,
367 NULL );
368
369 XtAddCallback( tlw, XmNdestroyCallback,
370 (XtCallbackProc)wxTLWidgetDestroyCallback,
371 (XtPointer)NULL );
372
373 return (WXWidget)tlw;
374 }
375
376 WXWidget wxCreateTopLevelRealizedWidget( WXDisplay* WXUNUSED(display) )
377 {
378 Widget rTlw = XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass,
379 (Widget)wxTheApp->GetTopLevelWidget(),
380 NULL );
381 XtSetMappedWhenManaged( rTlw, False );
382 XtRealizeWidget( rTlw );
383
384 return (WXWidget)rTlw;
385 }
386
387 WXWidget wxApp::GetTopLevelWidget()
388 {
389 WXDisplay* display = wxGetDisplay();
390 wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData,
391 display );
392 if( data.m_topLevelWidget )
393 return (WXWidget)data.m_topLevelWidget;
394
395 WXWidget tlw = wxCreateTopLevelWidget( display );
396 SetTopLevelWidget( display, tlw );
397
398 return tlw;
399 }
400
401 WXWidget wxApp::GetTopLevelRealizedWidget()
402 {
403 WXDisplay* display = wxGetDisplay();
404 wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display );
405
406 if( it != m_perDisplayData->end() && it->second->m_topLevelRealizedWidget )
407 return (WXWidget)it->second->m_topLevelRealizedWidget;
408
409 WXWidget rTlw = wxCreateTopLevelRealizedWidget( display );
410 SetTopLevelRealizedWidget( display, rTlw );
411
412 return rTlw;
413 }
414
415 void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget)
416 {
417 GetOrCreatePerDisplayData( *m_perDisplayData, display )
418 .m_topLevelWidget = (Widget)widget;
419 }
420
421 void wxApp::SetTopLevelRealizedWidget(WXDisplay* display, WXWidget widget)
422 {
423 GetOrCreatePerDisplayData( *m_perDisplayData, display )
424 .m_topLevelRealizedWidget = (Widget)widget;
425 }
426
427 // Yield to other processes
428
429 bool wxApp::Yield(bool onlyIfNeeded)
430 {
431 static bool s_inYield = false;
432
433 if ( s_inYield )
434 {
435 if ( !onlyIfNeeded )
436 {
437 wxFAIL_MSG( wxT("wxYield called recursively" ) );
438 }
439
440 return false;
441 }
442
443 s_inYield = true;
444
445 while (wxTheApp && wxTheApp->Pending())
446 wxTheApp->Dispatch();
447
448 s_inYield = false;
449
450 return true;
451 }
452
453 // ----------------------------------------------------------------------------
454 // accessors for C modules
455 // ----------------------------------------------------------------------------
456
457 extern "C" XtAppContext wxGetAppContext()
458 {
459 return (XtAppContext)wxTheApp->GetAppContext();
460 }