]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: 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 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
13 | #pragma implementation "app.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __VMS | |
20 | #define XtParent XTPARENT | |
21 | #define XtDisplay XTDISPLAY | |
22 | #endif | |
23 | ||
24 | #include "wx/app.h" | |
25 | #include "wx/utils.h" | |
26 | #include "wx/module.h" | |
27 | #include "wx/memory.h" | |
28 | #include "wx/log.h" | |
29 | #include "wx/intl.h" | |
30 | #include "wx/evtloop.h" | |
31 | #include "wx/hash.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 wxList wxPendingDelete; | |
71 | extern bool wxAddIdleCallback(); | |
72 | ||
73 | wxHashTable *wxWidgetHashTable = NULL; | |
74 | ||
75 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) | |
76 | ||
77 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
78 | EVT_IDLE(wxAppBase::OnIdle) | |
79 | END_EVENT_TABLE() | |
80 | ||
81 | #ifdef __WXDEBUG__ | |
82 | extern "C" | |
83 | { | |
84 | typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *); | |
85 | } | |
86 | ||
87 | XErrorHandlerFunc gs_pfnXErrorHandler = 0; | |
88 | ||
89 | extern "C" | |
90 | { | |
91 | ||
92 | static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent) | |
93 | { | |
94 | // just forward to the default handler for now | |
95 | return gs_pfnXErrorHandler(dpy, xevent); | |
96 | } | |
97 | ||
98 | } | |
99 | #endif // __WXDEBUG__ | |
100 | ||
101 | bool wxApp::Initialize(int& argcOrig, wxChar **argvOrig) | |
102 | { | |
103 | if ( !wxAppBase::Initialize(argcOrig, argvOrig) ) | |
104 | return false; | |
105 | ||
106 | wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER); | |
107 | ||
108 | #if wxUSE_INTL | |
109 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
110 | #endif | |
111 | ||
112 | return true; | |
113 | } | |
114 | ||
115 | void wxApp::CleanUp() | |
116 | { | |
117 | wxAppBase::CleanUp(); | |
118 | ||
119 | delete wxWidgetHashTable; | |
120 | wxWidgetHashTable = NULL; | |
121 | ||
122 | delete m_mainLoop; | |
123 | ||
124 | for( wxPerDisplayDataMap::iterator it = m_perDisplayData->begin(), | |
125 | end = m_perDisplayData->end(); | |
126 | it != end; ++it ) | |
127 | { | |
128 | delete it->second->m_visualInfo; | |
129 | XtDestroyWidget( it->second->m_topLevelWidget ); | |
130 | delete it->second; | |
131 | } | |
132 | ||
133 | delete m_perDisplayData; | |
134 | } | |
135 | ||
136 | void wxApp::Exit() | |
137 | { | |
138 | wxApp::CleanUp(); | |
139 | ||
140 | wxAppConsole::Exit(); | |
141 | } | |
142 | ||
143 | // ============================================================================ | |
144 | // wxApp | |
145 | // ============================================================================ | |
146 | ||
147 | wxApp::wxApp() | |
148 | { | |
149 | argc = 0; | |
150 | argv = NULL; | |
151 | ||
152 | m_mainLoop = new wxEventLoop; | |
153 | m_mainColormap = (WXColormap) NULL; | |
154 | m_appContext = (WXAppContext) NULL; | |
155 | m_initialDisplay = (WXDisplay*) 0; | |
156 | m_perDisplayData = new wxPerDisplayDataMap; | |
157 | } | |
158 | ||
159 | wxApp::~wxApp() | |
160 | { | |
161 | wxApp::SetInstance(NULL); | |
162 | } | |
163 | ||
164 | int wxApp::MainLoop() | |
165 | { | |
166 | /* | |
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 | */ | |
172 | ||
173 | XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), | |
174 | XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())), | |
175 | PropertyChangeMask); | |
176 | ||
177 | m_mainLoop->Run(); | |
178 | ||
179 | return 0; | |
180 | } | |
181 | ||
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 | ||
190 | static char *fallbackResources[] = { | |
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__ | |
196 | wxMOTIF_STR("*sgiMode: True"), | |
197 | wxMOTIF_STR("*useSchemes: all"), | |
198 | #else // !__SGI__ | |
199 | wxMOTIF_STR("*menuBar.marginHeight: 0"), | |
200 | wxMOTIF_STR("*menuBar.shadowThickness: 1"), | |
201 | wxMOTIF_STR("*background: #c0c0c0"), | |
202 | wxMOTIF_STR("*foreground: black"), | |
203 | #endif // __SGI__/!__SGI__ | |
204 | NULL | |
205 | }; | |
206 | ||
207 | // Create an application context | |
208 | bool wxApp::OnInitGui() | |
209 | { | |
210 | if( !wxAppBase::OnInitGui() ) | |
211 | return false; | |
212 | ||
213 | XtSetLanguageProc(NULL, NULL, NULL); | |
214 | XtToolkitInitialize() ; | |
215 | wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext(); | |
216 | XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources); | |
217 | ||
218 | Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL, | |
219 | wxTheApp->GetClassName().c_str(), NULL, 0, | |
220 | # if XtSpecificationRelease < 5 | |
221 | (Cardinal*) &argc, | |
222 | # else | |
223 | &argc, | |
224 | # endif | |
225 | argv); | |
226 | ||
227 | if (!dpy) { | |
228 | // if you don't log to stderr, nothing will be shown... | |
229 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
230 | wxString className(wxTheApp->GetClassName()); | |
231 | wxLogError(_("wxWidgets could not open display for '%s': exiting."), | |
232 | className.c_str()); | |
233 | exit(-1); | |
234 | } | |
235 | m_initialDisplay = (WXDisplay*) dpy; | |
236 | ||
237 | #ifdef __WXDEBUG__ | |
238 | // install the X error handler | |
239 | gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler); | |
240 | #endif // __WXDEBUG__ | |
241 | ||
242 | // Add general resize proc | |
243 | XtActionsRec rec; | |
244 | rec.string = wxMOTIF_STR("resize"); | |
245 | rec.proc = (XtActionProc)wxWidgetResizeProc; | |
246 | XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1); | |
247 | ||
248 | GetMainColormap(dpy); | |
249 | ||
250 | wxAddIdleCallback(); | |
251 | ||
252 | return true; | |
253 | } | |
254 | ||
255 | WXColormap wxApp::GetMainColormap(WXDisplay* display) | |
256 | { | |
257 | if (!display) /* Must be called first with non-NULL display */ | |
258 | return m_mainColormap; | |
259 | ||
260 | int defaultScreen = DefaultScreen((Display*) display); | |
261 | Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen); | |
262 | ||
263 | Colormap c = DefaultColormapOfScreen(screen); | |
264 | ||
265 | if (!m_mainColormap) | |
266 | m_mainColormap = (WXColormap) c; | |
267 | ||
268 | return (WXColormap) c; | |
269 | } | |
270 | ||
271 | static inline wxPerDisplayData& GetOrCreatePerDisplayData | |
272 | ( wxPerDisplayDataMap& m, WXDisplay* display ) | |
273 | { | |
274 | wxPerDisplayDataMap::iterator it = m.find( display ); | |
275 | if( it != m.end() && it->second != NULL ) | |
276 | return *(it->second); | |
277 | ||
278 | wxPerDisplayData* nData = new wxPerDisplayData(); | |
279 | m[display] = nData; | |
280 | ||
281 | return *nData; | |
282 | } | |
283 | ||
284 | wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display ) | |
285 | { | |
286 | wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData, | |
287 | display ); | |
288 | if( data.m_visualInfo ) | |
289 | return data.m_visualInfo; | |
290 | ||
291 | wxXVisualInfo* vi = new wxXVisualInfo; | |
292 | wxFillXVisualInfo( vi, (Display*)display ); | |
293 | ||
294 | data.m_visualInfo = vi; | |
295 | ||
296 | return vi; | |
297 | } | |
298 | ||
299 | static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData, | |
300 | XtPointer ptr) | |
301 | { | |
302 | if( wxTheApp ) | |
303 | { | |
304 | wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w), | |
305 | (WXWidget)NULL ); | |
306 | wxTheApp->SetTopLevelRealizedWidget( (WXDisplay*)XtDisplay(w), | |
307 | (WXWidget)NULL ); | |
308 | } | |
309 | } | |
310 | ||
311 | WXWidget wxCreateTopLevelWidget( WXDisplay* display ) | |
312 | { | |
313 | Widget tlw = XtAppCreateShell( (String)NULL, | |
314 | wxTheApp->GetClassName().c_str(), | |
315 | applicationShellWidgetClass, | |
316 | (Display*)display, | |
317 | NULL, 0 ); | |
318 | XtVaSetValues( tlw, | |
319 | XmNoverrideRedirect, True, | |
320 | NULL ); | |
321 | ||
322 | XtAddCallback( tlw, XmNdestroyCallback, | |
323 | (XtCallbackProc)wxTLWidgetDestroyCallback, | |
324 | (XtPointer)NULL ); | |
325 | ||
326 | return (WXWidget)tlw; | |
327 | } | |
328 | ||
329 | WXWidget wxCreateTopLevelRealizedWidget( WXDisplay* display ) | |
330 | { | |
331 | Widget rTlw = XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass, | |
332 | (Widget)wxTheApp->GetTopLevelWidget(), | |
333 | NULL, 0 ); | |
334 | XtSetMappedWhenManaged( rTlw, False ); | |
335 | XtRealizeWidget( rTlw ); | |
336 | ||
337 | return (WXWidget)rTlw; | |
338 | } | |
339 | ||
340 | WXWidget wxApp::GetTopLevelWidget() | |
341 | { | |
342 | WXDisplay* display = wxGetDisplay(); | |
343 | wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData, | |
344 | display ); | |
345 | if( data.m_topLevelWidget ) | |
346 | return (WXWidget)data.m_topLevelWidget; | |
347 | ||
348 | WXWidget tlw = wxCreateTopLevelWidget( display ); | |
349 | SetTopLevelWidget( display, tlw ); | |
350 | ||
351 | return tlw; | |
352 | } | |
353 | ||
354 | WXWidget wxApp::GetTopLevelRealizedWidget() | |
355 | { | |
356 | WXDisplay* display = wxGetDisplay(); | |
357 | wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display ); | |
358 | ||
359 | if( it != m_perDisplayData->end() && it->second->m_topLevelRealizedWidget ) | |
360 | return (WXWidget)it->second->m_topLevelRealizedWidget; | |
361 | ||
362 | WXWidget rTlw = wxCreateTopLevelRealizedWidget( display ); | |
363 | SetTopLevelRealizedWidget( display, rTlw ); | |
364 | ||
365 | return rTlw; | |
366 | } | |
367 | ||
368 | void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget) | |
369 | { | |
370 | GetOrCreatePerDisplayData( *m_perDisplayData, display ) | |
371 | .m_topLevelWidget = (Widget)widget; | |
372 | } | |
373 | ||
374 | void wxApp::SetTopLevelRealizedWidget(WXDisplay* display, WXWidget widget) | |
375 | { | |
376 | GetOrCreatePerDisplayData( *m_perDisplayData, display ) | |
377 | .m_topLevelRealizedWidget = (Widget)widget; | |
378 | } | |
379 | ||
380 | // Yield to other processes | |
381 | ||
382 | bool wxApp::Yield(bool onlyIfNeeded) | |
383 | { | |
384 | static bool s_inYield = false; | |
385 | ||
386 | if ( s_inYield ) | |
387 | { | |
388 | if ( !onlyIfNeeded ) | |
389 | { | |
390 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
391 | } | |
392 | ||
393 | return false; | |
394 | } | |
395 | ||
396 | s_inYield = true; | |
397 | ||
398 | while (wxTheApp && wxTheApp->Pending()) | |
399 | wxTheApp->Dispatch(); | |
400 | ||
401 | s_inYield = false; | |
402 | ||
403 | return true; | |
404 | } | |
405 | ||
406 | // ---------------------------------------------------------------------------- | |
407 | // accessors for C modules | |
408 | // ---------------------------------------------------------------------------- | |
409 | ||
410 | extern "C" XtAppContext wxGetAppContext() | |
411 | { | |
412 | return (XtAppContext)wxTheApp->GetAppContext(); | |
413 | } |