]>
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 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
ee31c392 | 13 | #pragma implementation "app.h" |
4bb6408c JS |
14 | #endif |
15 | ||
1248b41f MB |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
bcd055ae JJ |
19 | #ifdef __VMS |
20 | #define XtParent XTPARENT | |
21 | #define XtDisplay XTDISPLAY | |
22 | #endif | |
23 | ||
4bb6408c JS |
24 | #include "wx/app.h" |
25 | #include "wx/utils.h" | |
4bb6408c JS |
26 | #include "wx/module.h" |
27 | #include "wx/memory.h" | |
8bedcdce RR |
28 | #include "wx/log.h" |
29 | #include "wx/intl.h" | |
7e1bcfa8 | 30 | #include "wx/evtloop.h" |
ed39ff57 | 31 | #include "wx/hash.h" |
b412f9be | 32 | |
7bcb11d3 | 33 | #if wxUSE_THREADS |
dfe1eee3 | 34 | #include "wx/thread.h" |
7bcb11d3 | 35 | #endif |
4bb6408c | 36 | |
338dd992 JJ |
37 | #ifdef __VMS__ |
38 | #pragma message disable nosimpint | |
39 | #endif | |
4bb6408c JS |
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> | |
338dd992 JJ |
45 | #ifdef __VMS__ |
46 | #pragma message enable nosimpint | |
47 | #endif | |
4bb6408c JS |
48 | |
49 | #include "wx/motif/private.h" | |
50 | ||
51 | #include <string.h> | |
52 | ||
eb6fa4b4 MB |
53 | struct wxPerDisplayData |
54 | { | |
55 | wxPerDisplayData() | |
5a2e3d8c MB |
56 | { |
57 | m_visualInfo = NULL; | |
58 | m_topLevelWidget = NULL; | |
59 | m_topLevelRealizedWidget = NULL; | |
60 | } | |
eb6fa4b4 MB |
61 | |
62 | wxXVisualInfo* m_visualInfo; | |
5a2e3d8c | 63 | Widget m_topLevelWidget, m_topLevelRealizedWidget; |
eb6fa4b4 MB |
64 | }; |
65 | ||
eb6fa4b4 MB |
66 | static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData, |
67 | XtPointer ptr); | |
68 | static WXWidget wxCreateTopLevelWidget( WXDisplay* display ); | |
9ce8d6a2 | 69 | |
4bb6408c | 70 | extern wxList wxPendingDelete; |
7e1bcfa8 | 71 | extern bool wxAddIdleCallback(); |
7491d644 | 72 | |
4bb6408c JS |
73 | wxHashTable *wxWidgetHashTable = NULL; |
74 | ||
4bb6408c | 75 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) |
9c6e1335 | 76 | |
4bb6408c | 77 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) |
955a9197 | 78 | EVT_IDLE(wxAppBase::OnIdle) |
4bb6408c | 79 | END_EVENT_TABLE() |
4bb6408c | 80 | |
e838cc14 VZ |
81 | #ifdef __WXDEBUG__ |
82 | typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *); | |
83 | ||
84 | XErrorHandlerFunc gs_pfnXErrorHandler = 0; | |
85 | ||
86 | static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent) | |
87 | { | |
88 | // just forward to the default handler for now | |
89 | return gs_pfnXErrorHandler(dpy, xevent); | |
90 | } | |
91 | #endif // __WXDEBUG__ | |
92 | ||
05e2b077 | 93 | bool wxApp::Initialize(int& argc, wxChar **argv) |
4bb6408c | 94 | { |
94826170 VZ |
95 | if ( !wxAppBase::Initialize(argc, argv) ) |
96 | return false; | |
afb74891 | 97 | |
4bb6408c | 98 | wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER); |
afb74891 | 99 | |
9d02485e MB |
100 | #if wxUSE_INTL |
101 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
102 | #endif | |
103 | ||
94826170 | 104 | return true; |
4bb6408c JS |
105 | } |
106 | ||
107 | void wxApp::CleanUp() | |
108 | { | |
8d1bf3c7 MB |
109 | wxAppBase::CleanUp(); |
110 | ||
e6be3e7c MB |
111 | delete wxWidgetHashTable; |
112 | wxWidgetHashTable = NULL; | |
113 | ||
8d1bf3c7 MB |
114 | delete m_mainLoop; |
115 | ||
116 | for( wxPerDisplayDataMap::iterator it = m_perDisplayData->begin(), | |
117 | end = m_perDisplayData->end(); | |
118 | it != end; ++it ) | |
119 | { | |
120 | delete it->second->m_visualInfo; | |
121 | XtDestroyWidget( it->second->m_topLevelWidget ); | |
122 | delete it->second; | |
123 | } | |
124 | ||
125 | delete m_perDisplayData; | |
4bb6408c JS |
126 | } |
127 | ||
e2478fde VZ |
128 | void wxApp::Exit() |
129 | { | |
130 | wxApp::CleanUp(); | |
131 | ||
132 | wxAppConsole::Exit(); | |
133 | } | |
134 | ||
e6be3e7c | 135 | // ============================================================================ |
05e2b077 | 136 | // wxApp |
e6be3e7c MB |
137 | // ============================================================================ |
138 | ||
4bb6408c JS |
139 | wxApp::wxApp() |
140 | { | |
4bb6408c JS |
141 | argc = 0; |
142 | argv = NULL; | |
afb74891 | 143 | |
f15b55ce | 144 | m_mainLoop = new wxEventLoop; |
4bb6408c JS |
145 | m_mainColormap = (WXColormap) NULL; |
146 | m_appContext = (WXAppContext) NULL; | |
47bc1060 | 147 | m_initialDisplay = (WXDisplay*) 0; |
eb6fa4b4 | 148 | m_perDisplayData = new wxPerDisplayDataMap; |
4bb6408c JS |
149 | } |
150 | ||
7e1bcfa8 MB |
151 | wxApp::~wxApp() |
152 | { | |
57bfe3be | 153 | wxApp::SetInstance(NULL); |
7e1bcfa8 MB |
154 | } |
155 | ||
4bb6408c JS |
156 | int wxApp::MainLoop() |
157 | { | |
2d120f83 | 158 | /* |
4bb6408c JS |
159 | * Sit around forever waiting to process X-events. Property Change |
160 | * event are handled special, because they have to refer to | |
161 | * the root window rather than to a widget. therefore we can't | |
162 | * use an Xt-eventhandler. | |
163 | */ | |
afb74891 | 164 | |
4bb6408c | 165 | XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), |
2d120f83 JS |
166 | XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())), |
167 | PropertyChangeMask); | |
afb74891 | 168 | |
f15b55ce | 169 | m_mainLoop->Run(); |
afb74891 | 170 | |
8aa04e8b JS |
171 | return 0; |
172 | } | |
173 | ||
4bb6408c JS |
174 | // This should be redefined in a derived class for |
175 | // handling property change events for XAtom IPC. | |
176 | void wxApp::HandlePropertyChange(WXEvent *event) | |
177 | { | |
178 | // by default do nothing special | |
179 | XtDispatchEvent((XEvent*) event); /* let Motif do the work */ | |
180 | } | |
181 | ||
edc1cd8b JS |
182 | static char *fallbackResources[] = { |
183 | "*menuBar.marginHeight: 0", | |
184 | "*menuBar.shadowThickness: 1", | |
185 | "*background: #c0c0c0", | |
186 | "*foreground: black", | |
187 | NULL | |
188 | }; | |
189 | ||
4bb6408c JS |
190 | // Create an application context |
191 | bool wxApp::OnInitGui() | |
192 | { | |
fd304d98 | 193 | if( !wxAppBase::OnInitGui() ) |
96be256b | 194 | return false; |
fd304d98 | 195 | |
9d02485e | 196 | XtSetLanguageProc(NULL, NULL, NULL); |
4bb6408c | 197 | XtToolkitInitialize() ; |
edc1cd8b JS |
198 | wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext(); |
199 | XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources); | |
200 | ||
4bb6408c | 201 | Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL, |
d3a80c92 | 202 | wxTheApp->GetClassName().c_str(), NULL, 0, |
4bb6408c | 203 | # if XtSpecificationRelease < 5 |
9c6e1335 | 204 | (Cardinal*) &argc, |
4bb6408c | 205 | # else |
9c6e1335 | 206 | &argc, |
4bb6408c | 207 | # endif |
9c6e1335 VZ |
208 | argv); |
209 | ||
4bb6408c | 210 | if (!dpy) { |
c88b736c MB |
211 | // if you don't log to stderr, nothing will be shown... |
212 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
7bcb11d3 | 213 | wxString className(wxTheApp->GetClassName()); |
77ffb593 | 214 | wxLogError(_("wxWidgets could not open display for '%s': exiting."), |
d3a80c92 | 215 | className.c_str()); |
2d120f83 | 216 | exit(-1); |
4bb6408c | 217 | } |
47bc1060 | 218 | m_initialDisplay = (WXDisplay*) dpy; |
afb74891 | 219 | |
e838cc14 VZ |
220 | #ifdef __WXDEBUG__ |
221 | // install the X error handler | |
222 | gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler); | |
223 | #endif // __WXDEBUG__ | |
224 | ||
4bb6408c JS |
225 | // Add general resize proc |
226 | XtActionsRec rec; | |
227 | rec.string = "resize"; | |
228 | rec.proc = (XtActionProc)wxWidgetResizeProc; | |
229 | XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1); | |
afb74891 | 230 | |
4bb6408c | 231 | GetMainColormap(dpy); |
afb74891 | 232 | |
7e1bcfa8 MB |
233 | wxAddIdleCallback(); |
234 | ||
96be256b | 235 | return true; |
4bb6408c JS |
236 | } |
237 | ||
238 | WXColormap wxApp::GetMainColormap(WXDisplay* display) | |
239 | { | |
240 | if (!display) /* Must be called first with non-NULL display */ | |
2d120f83 | 241 | return m_mainColormap; |
a91b47e8 JS |
242 | |
243 | int defaultScreen = DefaultScreen((Display*) display); | |
244 | Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen); | |
afb74891 | 245 | |
a91b47e8 | 246 | Colormap c = DefaultColormapOfScreen(screen); |
afb74891 | 247 | |
4bb6408c | 248 | if (!m_mainColormap) |
2d120f83 | 249 | m_mainColormap = (WXColormap) c; |
afb74891 | 250 | |
4bb6408c JS |
251 | return (WXColormap) c; |
252 | } | |
253 | ||
bdcade0a MB |
254 | static inline wxPerDisplayData& GetOrCreatePerDisplayData |
255 | ( wxPerDisplayDataMap& m, WXDisplay* display ) | |
9ce8d6a2 | 256 | { |
bdcade0a MB |
257 | wxPerDisplayDataMap::iterator it = m.find( display ); |
258 | if( it != m.end() && it->second != NULL ) | |
259 | return *(it->second); | |
260 | ||
261 | wxPerDisplayData* nData = new wxPerDisplayData(); | |
262 | m[display] = nData; | |
263 | ||
264 | return *nData; | |
265 | } | |
9ce8d6a2 | 266 | |
bdcade0a MB |
267 | wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display ) |
268 | { | |
269 | wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData, | |
270 | display ); | |
271 | if( data.m_visualInfo ) | |
272 | return data.m_visualInfo; | |
9ce8d6a2 MB |
273 | |
274 | wxXVisualInfo* vi = new wxXVisualInfo; | |
275 | wxFillXVisualInfo( vi, (Display*)display ); | |
276 | ||
bdcade0a | 277 | data.m_visualInfo = vi; |
9ce8d6a2 MB |
278 | |
279 | return vi; | |
280 | } | |
281 | ||
eb6fa4b4 MB |
282 | static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData, |
283 | XtPointer ptr) | |
284 | { | |
285 | if( wxTheApp ) | |
5a2e3d8c | 286 | { |
eb6fa4b4 MB |
287 | wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w), |
288 | (WXWidget)NULL ); | |
5a2e3d8c MB |
289 | wxTheApp->SetTopLevelRealizedWidget( (WXDisplay*)XtDisplay(w), |
290 | (WXWidget)NULL ); | |
291 | } | |
eb6fa4b4 MB |
292 | } |
293 | ||
294 | WXWidget wxCreateTopLevelWidget( WXDisplay* display ) | |
295 | { | |
296 | Widget tlw = XtAppCreateShell( (String)NULL, | |
297 | wxTheApp->GetClassName().c_str(), | |
298 | applicationShellWidgetClass, | |
299 | (Display*)display, | |
300 | NULL, 0 ); | |
5a2e3d8c MB |
301 | XtVaSetValues( tlw, |
302 | XmNoverrideRedirect, True, | |
303 | NULL ); | |
eb6fa4b4 MB |
304 | |
305 | XtAddCallback( tlw, XmNdestroyCallback, | |
306 | (XtCallbackProc)wxTLWidgetDestroyCallback, | |
307 | (XtPointer)NULL ); | |
308 | ||
309 | return (WXWidget)tlw; | |
310 | } | |
311 | ||
5a2e3d8c MB |
312 | WXWidget wxCreateTopLevelRealizedWidget( WXDisplay* display ) |
313 | { | |
cb81f1a6 | 314 | Widget rTlw = XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass, |
5a2e3d8c | 315 | (Widget)wxTheApp->GetTopLevelWidget(), |
cb81f1a6 | 316 | NULL, 0 ); |
5a2e3d8c MB |
317 | XtSetMappedWhenManaged( rTlw, False ); |
318 | XtRealizeWidget( rTlw ); | |
319 | ||
320 | return (WXWidget)rTlw; | |
321 | } | |
322 | ||
eb6fa4b4 MB |
323 | WXWidget wxApp::GetTopLevelWidget() |
324 | { | |
325 | WXDisplay* display = wxGetDisplay(); | |
bdcade0a MB |
326 | wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData, |
327 | display ); | |
328 | if( data.m_topLevelWidget ) | |
329 | return (WXWidget)data.m_topLevelWidget; | |
eb6fa4b4 MB |
330 | |
331 | WXWidget tlw = wxCreateTopLevelWidget( display ); | |
332 | SetTopLevelWidget( display, tlw ); | |
333 | ||
334 | return tlw; | |
335 | } | |
336 | ||
5a2e3d8c MB |
337 | WXWidget wxApp::GetTopLevelRealizedWidget() |
338 | { | |
339 | WXDisplay* display = wxGetDisplay(); | |
340 | wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display ); | |
341 | ||
342 | if( it != m_perDisplayData->end() && it->second->m_topLevelRealizedWidget ) | |
343 | return (WXWidget)it->second->m_topLevelRealizedWidget; | |
344 | ||
345 | WXWidget rTlw = wxCreateTopLevelRealizedWidget( display ); | |
346 | SetTopLevelRealizedWidget( display, rTlw ); | |
347 | ||
348 | return rTlw; | |
349 | } | |
350 | ||
eb6fa4b4 MB |
351 | void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget) |
352 | { | |
5a2e3d8c MB |
353 | GetOrCreatePerDisplayData( *m_perDisplayData, display ) |
354 | .m_topLevelWidget = (Widget)widget; | |
355 | } | |
356 | ||
357 | void wxApp::SetTopLevelRealizedWidget(WXDisplay* display, WXWidget widget) | |
358 | { | |
359 | GetOrCreatePerDisplayData( *m_perDisplayData, display ) | |
360 | .m_topLevelRealizedWidget = (Widget)widget; | |
eb6fa4b4 MB |
361 | } |
362 | ||
4bb6408c | 363 | // Yield to other processes |
cb2713bf | 364 | |
8461e4c2 | 365 | bool wxApp::Yield(bool onlyIfNeeded) |
4bb6408c | 366 | { |
96be256b | 367 | static bool s_inYield = false; |
8461e4c2 VZ |
368 | |
369 | if ( s_inYield ) | |
370 | { | |
371 | if ( !onlyIfNeeded ) | |
372 | { | |
373 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
374 | } | |
375 | ||
96be256b | 376 | return false; |
8461e4c2 VZ |
377 | } |
378 | ||
96be256b | 379 | s_inYield = true; |
cb2713bf | 380 | |
4bb6408c | 381 | while (wxTheApp && wxTheApp->Pending()) |
2d120f83 | 382 | wxTheApp->Dispatch(); |
518b5d2f | 383 | |
96be256b | 384 | s_inYield = false; |
cb2713bf | 385 | |
96be256b | 386 | return true; |
4bb6408c JS |
387 | } |
388 | ||
d391a345 VZ |
389 | // ---------------------------------------------------------------------------- |
390 | // accessors for C modules | |
391 | // ---------------------------------------------------------------------------- | |
392 | ||
393 | extern "C" XtAppContext wxGetAppContext() | |
394 | { | |
395 | return (XtAppContext)wxTheApp->GetAppContext(); | |
396 | } |