]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
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 | |
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; | |
125 | XtDestroyWidget( it->second->m_topLevelWidget ); | |
126 | delete it->second; | |
127 | } | |
128 | ||
129 | delete m_perDisplayData; | |
4bb6408c JS |
130 | } |
131 | ||
e2478fde VZ |
132 | void wxApp::Exit() |
133 | { | |
134 | wxApp::CleanUp(); | |
135 | ||
136 | wxAppConsole::Exit(); | |
137 | } | |
138 | ||
e6be3e7c | 139 | // ============================================================================ |
05e2b077 | 140 | // wxApp |
e6be3e7c MB |
141 | // ============================================================================ |
142 | ||
4bb6408c JS |
143 | wxApp::wxApp() |
144 | { | |
4bb6408c JS |
145 | argc = 0; |
146 | argv = NULL; | |
afb74891 | 147 | |
f15b55ce | 148 | m_mainLoop = new wxEventLoop; |
4bb6408c JS |
149 | m_mainColormap = (WXColormap) NULL; |
150 | m_appContext = (WXAppContext) NULL; | |
47bc1060 | 151 | m_initialDisplay = (WXDisplay*) 0; |
eb6fa4b4 | 152 | m_perDisplayData = new wxPerDisplayDataMap; |
4bb6408c JS |
153 | } |
154 | ||
7e1bcfa8 MB |
155 | wxApp::~wxApp() |
156 | { | |
57bfe3be | 157 | wxApp::SetInstance(NULL); |
7e1bcfa8 MB |
158 | } |
159 | ||
4bb6408c JS |
160 | int wxApp::MainLoop() |
161 | { | |
2d120f83 | 162 | /* |
4bb6408c JS |
163 | * Sit around forever waiting to process X-events. Property Change |
164 | * event are handled special, because they have to refer to | |
165 | * the root window rather than to a widget. therefore we can't | |
166 | * use an Xt-eventhandler. | |
167 | */ | |
afb74891 | 168 | |
4bb6408c | 169 | XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), |
2d120f83 JS |
170 | XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())), |
171 | PropertyChangeMask); | |
afb74891 | 172 | |
f15b55ce | 173 | m_mainLoop->Run(); |
afb74891 | 174 | |
8aa04e8b JS |
175 | return 0; |
176 | } | |
177 | ||
4bb6408c JS |
178 | // This should be redefined in a derived class for |
179 | // handling property change events for XAtom IPC. | |
180 | void wxApp::HandlePropertyChange(WXEvent *event) | |
181 | { | |
182 | // by default do nothing special | |
183 | XtDispatchEvent((XEvent*) event); /* let Motif do the work */ | |
184 | } | |
185 | ||
edc1cd8b | 186 | static char *fallbackResources[] = { |
0ab82ca9 VZ |
187 | // better defaults for CDE under Irix |
188 | // | |
189 | // TODO: do something similar for the other systems, the hardcoded defaults | |
190 | // below are ugly | |
191 | #ifdef __SGI__ | |
f1db433a VZ |
192 | wxMOTIF_STR("*sgiMode: True"), |
193 | wxMOTIF_STR("*useSchemes: all"), | |
0ab82ca9 | 194 | #else // !__SGI__ |
f1db433a VZ |
195 | wxMOTIF_STR("*menuBar.marginHeight: 0"), |
196 | wxMOTIF_STR("*menuBar.shadowThickness: 1"), | |
197 | wxMOTIF_STR("*background: #c0c0c0"), | |
198 | wxMOTIF_STR("*foreground: black"), | |
0ab82ca9 | 199 | #endif // __SGI__/!__SGI__ |
edc1cd8b JS |
200 | NULL |
201 | }; | |
202 | ||
4bb6408c JS |
203 | // Create an application context |
204 | bool wxApp::OnInitGui() | |
205 | { | |
fd304d98 | 206 | if( !wxAppBase::OnInitGui() ) |
96be256b | 207 | return false; |
fd304d98 | 208 | |
9d02485e | 209 | XtSetLanguageProc(NULL, NULL, NULL); |
4bb6408c | 210 | XtToolkitInitialize() ; |
edc1cd8b JS |
211 | wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext(); |
212 | XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources); | |
213 | ||
4bb6408c | 214 | Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL, |
d3a80c92 | 215 | wxTheApp->GetClassName().c_str(), NULL, 0, |
4bb6408c | 216 | # if XtSpecificationRelease < 5 |
9c6e1335 | 217 | (Cardinal*) &argc, |
4bb6408c | 218 | # else |
9c6e1335 | 219 | &argc, |
4bb6408c | 220 | # endif |
9c6e1335 VZ |
221 | argv); |
222 | ||
4bb6408c | 223 | if (!dpy) { |
c88b736c MB |
224 | // if you don't log to stderr, nothing will be shown... |
225 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
7bcb11d3 | 226 | wxString className(wxTheApp->GetClassName()); |
77ffb593 | 227 | wxLogError(_("wxWidgets could not open display for '%s': exiting."), |
d3a80c92 | 228 | className.c_str()); |
2d120f83 | 229 | exit(-1); |
4bb6408c | 230 | } |
47bc1060 | 231 | m_initialDisplay = (WXDisplay*) dpy; |
afb74891 | 232 | |
e838cc14 VZ |
233 | #ifdef __WXDEBUG__ |
234 | // install the X error handler | |
235 | gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler); | |
236 | #endif // __WXDEBUG__ | |
237 | ||
4bb6408c JS |
238 | // Add general resize proc |
239 | XtActionsRec rec; | |
f1db433a | 240 | rec.string = wxMOTIF_STR("resize"); |
4bb6408c JS |
241 | rec.proc = (XtActionProc)wxWidgetResizeProc; |
242 | XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1); | |
afb74891 | 243 | |
4bb6408c | 244 | GetMainColormap(dpy); |
afb74891 | 245 | |
7e1bcfa8 MB |
246 | wxAddIdleCallback(); |
247 | ||
96be256b | 248 | return true; |
4bb6408c JS |
249 | } |
250 | ||
251 | WXColormap wxApp::GetMainColormap(WXDisplay* display) | |
252 | { | |
253 | if (!display) /* Must be called first with non-NULL display */ | |
2d120f83 | 254 | return m_mainColormap; |
a91b47e8 JS |
255 | |
256 | int defaultScreen = DefaultScreen((Display*) display); | |
257 | Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen); | |
afb74891 | 258 | |
a91b47e8 | 259 | Colormap c = DefaultColormapOfScreen(screen); |
afb74891 | 260 | |
4bb6408c | 261 | if (!m_mainColormap) |
2d120f83 | 262 | m_mainColormap = (WXColormap) c; |
afb74891 | 263 | |
4bb6408c JS |
264 | return (WXColormap) c; |
265 | } | |
266 | ||
bdcade0a MB |
267 | static inline wxPerDisplayData& GetOrCreatePerDisplayData |
268 | ( wxPerDisplayDataMap& m, WXDisplay* display ) | |
9ce8d6a2 | 269 | { |
bdcade0a MB |
270 | wxPerDisplayDataMap::iterator it = m.find( display ); |
271 | if( it != m.end() && it->second != NULL ) | |
272 | return *(it->second); | |
273 | ||
274 | wxPerDisplayData* nData = new wxPerDisplayData(); | |
275 | m[display] = nData; | |
276 | ||
277 | return *nData; | |
278 | } | |
9ce8d6a2 | 279 | |
bdcade0a MB |
280 | wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display ) |
281 | { | |
282 | wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData, | |
283 | display ); | |
284 | if( data.m_visualInfo ) | |
285 | return data.m_visualInfo; | |
9ce8d6a2 MB |
286 | |
287 | wxXVisualInfo* vi = new wxXVisualInfo; | |
288 | wxFillXVisualInfo( vi, (Display*)display ); | |
289 | ||
bdcade0a | 290 | data.m_visualInfo = vi; |
9ce8d6a2 MB |
291 | |
292 | return vi; | |
293 | } | |
294 | ||
eb6fa4b4 MB |
295 | static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData, |
296 | XtPointer ptr) | |
297 | { | |
298 | if( wxTheApp ) | |
5a2e3d8c | 299 | { |
eb6fa4b4 MB |
300 | wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w), |
301 | (WXWidget)NULL ); | |
5a2e3d8c MB |
302 | wxTheApp->SetTopLevelRealizedWidget( (WXDisplay*)XtDisplay(w), |
303 | (WXWidget)NULL ); | |
304 | } | |
eb6fa4b4 MB |
305 | } |
306 | ||
307 | WXWidget wxCreateTopLevelWidget( WXDisplay* display ) | |
308 | { | |
309 | Widget tlw = XtAppCreateShell( (String)NULL, | |
310 | wxTheApp->GetClassName().c_str(), | |
311 | applicationShellWidgetClass, | |
312 | (Display*)display, | |
313 | NULL, 0 ); | |
5a2e3d8c MB |
314 | XtVaSetValues( tlw, |
315 | XmNoverrideRedirect, True, | |
316 | NULL ); | |
eb6fa4b4 MB |
317 | |
318 | XtAddCallback( tlw, XmNdestroyCallback, | |
319 | (XtCallbackProc)wxTLWidgetDestroyCallback, | |
320 | (XtPointer)NULL ); | |
321 | ||
322 | return (WXWidget)tlw; | |
323 | } | |
324 | ||
5a2e3d8c MB |
325 | WXWidget wxCreateTopLevelRealizedWidget( WXDisplay* display ) |
326 | { | |
cb81f1a6 | 327 | Widget rTlw = XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass, |
5a2e3d8c | 328 | (Widget)wxTheApp->GetTopLevelWidget(), |
cb81f1a6 | 329 | NULL, 0 ); |
5a2e3d8c MB |
330 | XtSetMappedWhenManaged( rTlw, False ); |
331 | XtRealizeWidget( rTlw ); | |
332 | ||
333 | return (WXWidget)rTlw; | |
334 | } | |
335 | ||
eb6fa4b4 MB |
336 | WXWidget wxApp::GetTopLevelWidget() |
337 | { | |
338 | WXDisplay* display = wxGetDisplay(); | |
bdcade0a MB |
339 | wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData, |
340 | display ); | |
341 | if( data.m_topLevelWidget ) | |
342 | return (WXWidget)data.m_topLevelWidget; | |
eb6fa4b4 MB |
343 | |
344 | WXWidget tlw = wxCreateTopLevelWidget( display ); | |
345 | SetTopLevelWidget( display, tlw ); | |
346 | ||
347 | return tlw; | |
348 | } | |
349 | ||
5a2e3d8c MB |
350 | WXWidget wxApp::GetTopLevelRealizedWidget() |
351 | { | |
352 | WXDisplay* display = wxGetDisplay(); | |
353 | wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display ); | |
354 | ||
355 | if( it != m_perDisplayData->end() && it->second->m_topLevelRealizedWidget ) | |
356 | return (WXWidget)it->second->m_topLevelRealizedWidget; | |
357 | ||
358 | WXWidget rTlw = wxCreateTopLevelRealizedWidget( display ); | |
359 | SetTopLevelRealizedWidget( display, rTlw ); | |
360 | ||
361 | return rTlw; | |
362 | } | |
363 | ||
eb6fa4b4 MB |
364 | void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget) |
365 | { | |
5a2e3d8c MB |
366 | GetOrCreatePerDisplayData( *m_perDisplayData, display ) |
367 | .m_topLevelWidget = (Widget)widget; | |
368 | } | |
369 | ||
370 | void wxApp::SetTopLevelRealizedWidget(WXDisplay* display, WXWidget widget) | |
371 | { | |
372 | GetOrCreatePerDisplayData( *m_perDisplayData, display ) | |
373 | .m_topLevelRealizedWidget = (Widget)widget; | |
eb6fa4b4 MB |
374 | } |
375 | ||
4bb6408c | 376 | // Yield to other processes |
cb2713bf | 377 | |
8461e4c2 | 378 | bool wxApp::Yield(bool onlyIfNeeded) |
4bb6408c | 379 | { |
96be256b | 380 | static bool s_inYield = false; |
8461e4c2 VZ |
381 | |
382 | if ( s_inYield ) | |
383 | { | |
384 | if ( !onlyIfNeeded ) | |
385 | { | |
386 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
387 | } | |
388 | ||
96be256b | 389 | return false; |
8461e4c2 VZ |
390 | } |
391 | ||
96be256b | 392 | s_inYield = true; |
cb2713bf | 393 | |
4bb6408c | 394 | while (wxTheApp && wxTheApp->Pending()) |
2d120f83 | 395 | wxTheApp->Dispatch(); |
518b5d2f | 396 | |
96be256b | 397 | s_inYield = false; |
cb2713bf | 398 | |
96be256b | 399 | return true; |
4bb6408c JS |
400 | } |
401 | ||
d391a345 VZ |
402 | // ---------------------------------------------------------------------------- |
403 | // accessors for C modules | |
404 | // ---------------------------------------------------------------------------- | |
405 | ||
406 | extern "C" XtAppContext wxGetAppContext() | |
407 | { | |
408 | return (XtAppContext)wxTheApp->GetAppContext(); | |
409 | } |