]>
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 | |
afb74891 | 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() | |
56 | { m_visualInfo = NULL; m_topLevelWidget = NULL; } | |
57 | ||
58 | wxXVisualInfo* m_visualInfo; | |
59 | Widget m_topLevelWidget; | |
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 VZ |
77 | #ifdef __WXDEBUG__ |
78 | typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *); | |
79 | ||
80 | XErrorHandlerFunc gs_pfnXErrorHandler = 0; | |
81 | ||
82 | static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent) | |
83 | { | |
84 | // just forward to the default handler for now | |
85 | return gs_pfnXErrorHandler(dpy, xevent); | |
86 | } | |
87 | #endif // __WXDEBUG__ | |
88 | ||
05e2b077 | 89 | bool wxApp::Initialize(int& argc, wxChar **argv) |
4bb6408c | 90 | { |
94826170 VZ |
91 | if ( !wxAppBase::Initialize(argc, argv) ) |
92 | return false; | |
afb74891 | 93 | |
4bb6408c | 94 | wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER); |
afb74891 | 95 | |
94826170 | 96 | return true; |
4bb6408c JS |
97 | } |
98 | ||
99 | void wxApp::CleanUp() | |
100 | { | |
e6be3e7c MB |
101 | delete wxWidgetHashTable; |
102 | wxWidgetHashTable = NULL; | |
103 | ||
94826170 | 104 | wxAppBase::CleanUp(); |
4bb6408c JS |
105 | } |
106 | ||
e2478fde VZ |
107 | void wxApp::Exit() |
108 | { | |
109 | wxApp::CleanUp(); | |
110 | ||
111 | wxAppConsole::Exit(); | |
112 | } | |
113 | ||
e6be3e7c | 114 | // ============================================================================ |
05e2b077 | 115 | // wxApp |
e6be3e7c MB |
116 | // ============================================================================ |
117 | ||
4bb6408c JS |
118 | wxApp::wxApp() |
119 | { | |
4bb6408c JS |
120 | argc = 0; |
121 | argv = NULL; | |
afb74891 | 122 | |
f15b55ce | 123 | m_mainLoop = new wxEventLoop; |
4bb6408c JS |
124 | m_mainColormap = (WXColormap) NULL; |
125 | m_appContext = (WXAppContext) NULL; | |
47bc1060 | 126 | m_initialDisplay = (WXDisplay*) 0; |
eb6fa4b4 | 127 | m_perDisplayData = new wxPerDisplayDataMap; |
4bb6408c JS |
128 | } |
129 | ||
7e1bcfa8 MB |
130 | wxApp::~wxApp() |
131 | { | |
f15b55ce | 132 | delete m_mainLoop; |
9ce8d6a2 | 133 | |
eb6fa4b4 MB |
134 | for( wxPerDisplayDataMap::iterator it = m_perDisplayData->begin(), |
135 | end = m_perDisplayData->end(); | |
9ce8d6a2 MB |
136 | it != end; ++it ) |
137 | { | |
bdcade0a MB |
138 | delete it->second->m_visualInfo; |
139 | XtDestroyWidget( it->second->m_topLevelWidget ); | |
140 | delete it->second; | |
9ce8d6a2 MB |
141 | } |
142 | ||
eb6fa4b4 MB |
143 | delete m_perDisplayData; |
144 | ||
57bfe3be | 145 | wxApp::SetInstance(NULL); |
7e1bcfa8 MB |
146 | } |
147 | ||
4bb6408c JS |
148 | int wxApp::MainLoop() |
149 | { | |
2d120f83 | 150 | /* |
4bb6408c JS |
151 | * Sit around forever waiting to process X-events. Property Change |
152 | * event are handled special, because they have to refer to | |
153 | * the root window rather than to a widget. therefore we can't | |
154 | * use an Xt-eventhandler. | |
155 | */ | |
afb74891 | 156 | |
4bb6408c | 157 | XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), |
2d120f83 JS |
158 | XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())), |
159 | PropertyChangeMask); | |
afb74891 | 160 | |
f15b55ce | 161 | m_mainLoop->Run(); |
afb74891 | 162 | |
8aa04e8b JS |
163 | return 0; |
164 | } | |
165 | ||
4bb6408c JS |
166 | // This should be redefined in a derived class for |
167 | // handling property change events for XAtom IPC. | |
168 | void wxApp::HandlePropertyChange(WXEvent *event) | |
169 | { | |
170 | // by default do nothing special | |
171 | XtDispatchEvent((XEvent*) event); /* let Motif do the work */ | |
172 | } | |
173 | ||
edc1cd8b JS |
174 | static char *fallbackResources[] = { |
175 | "*menuBar.marginHeight: 0", | |
176 | "*menuBar.shadowThickness: 1", | |
177 | "*background: #c0c0c0", | |
178 | "*foreground: black", | |
179 | NULL | |
180 | }; | |
181 | ||
4bb6408c JS |
182 | // Create an application context |
183 | bool wxApp::OnInitGui() | |
184 | { | |
fd304d98 MB |
185 | if( !wxAppBase::OnInitGui() ) |
186 | return FALSE; | |
187 | ||
4bb6408c | 188 | XtToolkitInitialize() ; |
edc1cd8b JS |
189 | wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext(); |
190 | XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources); | |
191 | ||
4bb6408c | 192 | Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL, |
d3a80c92 | 193 | wxTheApp->GetClassName().c_str(), NULL, 0, |
4bb6408c | 194 | # if XtSpecificationRelease < 5 |
9c6e1335 | 195 | (Cardinal*) &argc, |
4bb6408c | 196 | # else |
9c6e1335 | 197 | &argc, |
4bb6408c | 198 | # endif |
9c6e1335 VZ |
199 | argv); |
200 | ||
4bb6408c | 201 | if (!dpy) { |
c88b736c MB |
202 | // if you don't log to stderr, nothing will be shown... |
203 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
7bcb11d3 | 204 | wxString className(wxTheApp->GetClassName()); |
9c6e1335 | 205 | wxLogError(_("wxWindows could not open display for '%s': exiting."), |
d3a80c92 | 206 | className.c_str()); |
2d120f83 | 207 | exit(-1); |
4bb6408c | 208 | } |
47bc1060 | 209 | m_initialDisplay = (WXDisplay*) dpy; |
afb74891 | 210 | |
e838cc14 VZ |
211 | #ifdef __WXDEBUG__ |
212 | // install the X error handler | |
213 | gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler); | |
214 | #endif // __WXDEBUG__ | |
215 | ||
4bb6408c JS |
216 | // Add general resize proc |
217 | XtActionsRec rec; | |
218 | rec.string = "resize"; | |
219 | rec.proc = (XtActionProc)wxWidgetResizeProc; | |
220 | XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1); | |
afb74891 | 221 | |
4bb6408c | 222 | GetMainColormap(dpy); |
afb74891 | 223 | |
7e1bcfa8 MB |
224 | wxAddIdleCallback(); |
225 | ||
4bb6408c JS |
226 | return TRUE; |
227 | } | |
228 | ||
229 | WXColormap wxApp::GetMainColormap(WXDisplay* display) | |
230 | { | |
231 | if (!display) /* Must be called first with non-NULL display */ | |
2d120f83 | 232 | return m_mainColormap; |
a91b47e8 JS |
233 | |
234 | int defaultScreen = DefaultScreen((Display*) display); | |
235 | Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen); | |
afb74891 | 236 | |
a91b47e8 | 237 | Colormap c = DefaultColormapOfScreen(screen); |
afb74891 | 238 | |
4bb6408c | 239 | if (!m_mainColormap) |
2d120f83 | 240 | m_mainColormap = (WXColormap) c; |
afb74891 | 241 | |
4bb6408c JS |
242 | return (WXColormap) c; |
243 | } | |
244 | ||
bdcade0a MB |
245 | static inline wxPerDisplayData& GetOrCreatePerDisplayData |
246 | ( wxPerDisplayDataMap& m, WXDisplay* display ) | |
9ce8d6a2 | 247 | { |
bdcade0a MB |
248 | wxPerDisplayDataMap::iterator it = m.find( display ); |
249 | if( it != m.end() && it->second != NULL ) | |
250 | return *(it->second); | |
251 | ||
252 | wxPerDisplayData* nData = new wxPerDisplayData(); | |
253 | m[display] = nData; | |
254 | ||
255 | return *nData; | |
256 | } | |
9ce8d6a2 | 257 | |
bdcade0a MB |
258 | wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display ) |
259 | { | |
260 | wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData, | |
261 | display ); | |
262 | if( data.m_visualInfo ) | |
263 | return data.m_visualInfo; | |
9ce8d6a2 MB |
264 | |
265 | wxXVisualInfo* vi = new wxXVisualInfo; | |
266 | wxFillXVisualInfo( vi, (Display*)display ); | |
267 | ||
bdcade0a | 268 | data.m_visualInfo = vi; |
9ce8d6a2 MB |
269 | |
270 | return vi; | |
271 | } | |
272 | ||
eb6fa4b4 MB |
273 | static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData, |
274 | XtPointer ptr) | |
275 | { | |
276 | if( wxTheApp ) | |
277 | wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w), | |
278 | (WXWidget)NULL ); | |
279 | } | |
280 | ||
281 | WXWidget wxCreateTopLevelWidget( WXDisplay* display ) | |
282 | { | |
283 | Widget tlw = XtAppCreateShell( (String)NULL, | |
284 | wxTheApp->GetClassName().c_str(), | |
285 | applicationShellWidgetClass, | |
286 | (Display*)display, | |
287 | NULL, 0 ); | |
732c0bfd | 288 | XtSetMappedWhenManaged( tlw, False ); |
dd38c875 | 289 | XtRealizeWidget( tlw ); |
eb6fa4b4 MB |
290 | |
291 | XtAddCallback( tlw, XmNdestroyCallback, | |
292 | (XtCallbackProc)wxTLWidgetDestroyCallback, | |
293 | (XtPointer)NULL ); | |
294 | ||
295 | return (WXWidget)tlw; | |
296 | } | |
297 | ||
298 | WXWidget wxApp::GetTopLevelWidget() | |
299 | { | |
300 | WXDisplay* display = wxGetDisplay(); | |
bdcade0a MB |
301 | wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData, |
302 | display ); | |
303 | if( data.m_topLevelWidget ) | |
304 | return (WXWidget)data.m_topLevelWidget; | |
eb6fa4b4 MB |
305 | |
306 | WXWidget tlw = wxCreateTopLevelWidget( display ); | |
307 | SetTopLevelWidget( display, tlw ); | |
308 | ||
309 | return tlw; | |
310 | } | |
311 | ||
312 | void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget) | |
313 | { | |
bdcade0a | 314 | (*m_perDisplayData)[display]->m_topLevelWidget = (Widget)widget; |
eb6fa4b4 MB |
315 | } |
316 | ||
4bb6408c | 317 | // Yield to other processes |
cb2713bf | 318 | |
8461e4c2 | 319 | bool wxApp::Yield(bool onlyIfNeeded) |
4bb6408c | 320 | { |
f6afb56a | 321 | static bool s_inYield = FALSE; |
8461e4c2 VZ |
322 | |
323 | if ( s_inYield ) | |
324 | { | |
325 | if ( !onlyIfNeeded ) | |
326 | { | |
327 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
328 | } | |
329 | ||
330 | return FALSE; | |
331 | } | |
332 | ||
333 | s_inYield = TRUE; | |
cb2713bf | 334 | |
4bb6408c | 335 | while (wxTheApp && wxTheApp->Pending()) |
2d120f83 | 336 | wxTheApp->Dispatch(); |
518b5d2f | 337 | |
8461e4c2 | 338 | s_inYield = FALSE; |
cb2713bf | 339 | |
4bb6408c JS |
340 | return TRUE; |
341 | } | |
342 | ||
d391a345 VZ |
343 | // ---------------------------------------------------------------------------- |
344 | // accessors for C modules | |
345 | // ---------------------------------------------------------------------------- | |
346 | ||
347 | extern "C" XtAppContext wxGetAppContext() | |
348 | { | |
349 | return (XtAppContext)wxTheApp->GetAppContext(); | |
350 | } |