]>
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 | ||
12 | #ifdef __GNUG__ | |
ee31c392 | 13 | #pragma implementation "app.h" |
4bb6408c JS |
14 | #endif |
15 | ||
bcd055ae JJ |
16 | #ifdef __VMS |
17 | #define XtParent XTPARENT | |
18 | #define XtDisplay XTDISPLAY | |
19 | #endif | |
20 | ||
4bb6408c JS |
21 | #include "wx/app.h" |
22 | #include "wx/utils.h" | |
4bb6408c JS |
23 | #include "wx/module.h" |
24 | #include "wx/memory.h" | |
8bedcdce RR |
25 | #include "wx/log.h" |
26 | #include "wx/intl.h" | |
7e1bcfa8 | 27 | #include "wx/evtloop.h" |
ed39ff57 | 28 | #include "wx/hash.h" |
9ce8d6a2 | 29 | #include "wx/hashmap.h" |
b412f9be | 30 | |
7bcb11d3 | 31 | #if wxUSE_THREADS |
dfe1eee3 | 32 | #include "wx/thread.h" |
7bcb11d3 | 33 | #endif |
4bb6408c | 34 | |
338dd992 JJ |
35 | #ifdef __VMS__ |
36 | #pragma message disable nosimpint | |
37 | #endif | |
4bb6408c JS |
38 | #include <Xm/Xm.h> |
39 | #include <X11/Xlib.h> | |
40 | #include <X11/Xutil.h> | |
41 | #include <X11/Xresource.h> | |
42 | #include <X11/Xatom.h> | |
338dd992 JJ |
43 | #ifdef __VMS__ |
44 | #pragma message enable nosimpint | |
45 | #endif | |
4bb6408c JS |
46 | |
47 | #include "wx/motif/private.h" | |
48 | ||
49 | #include <string.h> | |
50 | ||
eb6fa4b4 MB |
51 | struct wxPerDisplayData |
52 | { | |
53 | wxPerDisplayData() | |
54 | { m_visualInfo = NULL; m_topLevelWidget = NULL; } | |
55 | ||
56 | wxXVisualInfo* m_visualInfo; | |
57 | Widget m_topLevelWidget; | |
58 | }; | |
59 | ||
60 | WX_DECLARE_VOIDPTR_HASH_MAP( wxPerDisplayData, wxPerDisplayDataMap ); | |
61 | ||
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) |
9c6e1335 | 74 | EVT_IDLE(wxApp::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 | |
7e1bcfa8 | 123 | m_eventLoop = 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 | { | |
132 | delete m_eventLoop; | |
9ce8d6a2 | 133 | |
eb6fa4b4 MB |
134 | for( wxPerDisplayDataMap::iterator it = m_perDisplayData->begin(), |
135 | end = m_perDisplayData->end(); | |
9ce8d6a2 MB |
136 | it != end; ++it ) |
137 | { | |
eb6fa4b4 MB |
138 | delete it->second.m_visualInfo; |
139 | XtDestroyWidget( it->second.m_topLevelWidget ); | |
9ce8d6a2 MB |
140 | } |
141 | ||
eb6fa4b4 MB |
142 | delete m_perDisplayData; |
143 | ||
144 | wxTheApp = NULL; | |
7e1bcfa8 MB |
145 | } |
146 | ||
4bb6408c JS |
147 | bool wxApp::Initialized() |
148 | { | |
149 | if (GetTopWindow()) | |
2d120f83 | 150 | return TRUE; |
4bb6408c | 151 | else |
2d120f83 | 152 | return FALSE; |
4bb6408c JS |
153 | } |
154 | ||
155 | int wxApp::MainLoop() | |
156 | { | |
2d120f83 | 157 | /* |
4bb6408c JS |
158 | * Sit around forever waiting to process X-events. Property Change |
159 | * event are handled special, because they have to refer to | |
160 | * the root window rather than to a widget. therefore we can't | |
161 | * use an Xt-eventhandler. | |
162 | */ | |
afb74891 | 163 | |
4bb6408c | 164 | XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), |
2d120f83 JS |
165 | XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())), |
166 | PropertyChangeMask); | |
afb74891 | 167 | |
7e1bcfa8 | 168 | m_eventLoop->Run(); |
afb74891 | 169 | |
8aa04e8b JS |
170 | return 0; |
171 | } | |
172 | ||
4bb6408c JS |
173 | void wxApp::ExitMainLoop() |
174 | { | |
e6be3e7c MB |
175 | if( m_eventLoop->IsRunning() ) |
176 | m_eventLoop->Exit(); | |
4bb6408c JS |
177 | } |
178 | ||
179 | // Is a message/event pending? | |
180 | bool wxApp::Pending() | |
181 | { | |
ba2fcb07 MB |
182 | return m_eventLoop->Pending(); |
183 | #if 0 | |
4bb6408c | 184 | XFlush(XtDisplay( (Widget) wxTheApp->GetTopLevelWidget() )); |
afb74891 | 185 | |
8aa04e8b JS |
186 | // Fix by Doug from STI, to prevent a stall if non-X event |
187 | // is found. | |
188 | return ((XtAppPending( (XtAppContext) GetAppContext() ) & XtIMXEvent) != 0) ; | |
ba2fcb07 | 189 | #endif |
4bb6408c JS |
190 | } |
191 | ||
192 | // Dispatch a message. | |
193 | void wxApp::Dispatch() | |
194 | { | |
7e1bcfa8 | 195 | m_eventLoop->Dispatch(); |
4bb6408c JS |
196 | } |
197 | ||
198 | // This should be redefined in a derived class for | |
199 | // handling property change events for XAtom IPC. | |
200 | void wxApp::HandlePropertyChange(WXEvent *event) | |
201 | { | |
202 | // by default do nothing special | |
203 | XtDispatchEvent((XEvent*) event); /* let Motif do the work */ | |
204 | } | |
205 | ||
206 | void wxApp::OnIdle(wxIdleEvent& event) | |
207 | { | |
208 | static bool inOnIdle = FALSE; | |
afb74891 | 209 | |
4bb6408c JS |
210 | // Avoid recursion (via ProcessEvent default case) |
211 | if (inOnIdle) | |
2d120f83 | 212 | return; |
afb74891 | 213 | |
4bb6408c | 214 | inOnIdle = TRUE; |
afb74891 | 215 | |
0ca580f6 GRG |
216 | // If there are pending events, we must process them: pending events |
217 | // are either events to the threads other than main or events posted | |
218 | // with wxPostEvent() functions | |
219 | // GRG: I have moved this here so that all pending events are processed | |
220 | // before starting to delete any objects. This behaves better (in | |
221 | // particular, wrt wxPostEvent) and is coherent with wxGTK's current | |
222 | // behaviour. Also removed the '#if wxUSE_THREADS' around it. | |
223 | // Changed Mar/2000 before 2.1.14 | |
afb74891 | 224 | |
7214297d GL |
225 | // Flush pending events. |
226 | ProcessPendingEvents(); | |
0ca580f6 GRG |
227 | |
228 | // 'Garbage' collection of windows deleted with Close(). | |
229 | DeletePendingObjects(); | |
7214297d | 230 | |
4bb6408c JS |
231 | // flush the logged messages if any |
232 | wxLog *pLog = wxLog::GetActiveTarget(); | |
233 | if ( pLog != NULL && pLog->HasPendingMessages() ) | |
2d120f83 | 234 | pLog->Flush(); |
afb74891 | 235 | |
4bb6408c JS |
236 | // Send OnIdle events to all windows |
237 | bool needMore = SendIdleEvents(); | |
afb74891 | 238 | |
4bb6408c | 239 | if (needMore) |
2d120f83 | 240 | event.RequestMore(TRUE); |
afb74891 | 241 | |
4bb6408c JS |
242 | inOnIdle = FALSE; |
243 | } | |
244 | ||
edc1cd8b JS |
245 | static char *fallbackResources[] = { |
246 | "*menuBar.marginHeight: 0", | |
247 | "*menuBar.shadowThickness: 1", | |
248 | "*background: #c0c0c0", | |
249 | "*foreground: black", | |
250 | NULL | |
251 | }; | |
252 | ||
4bb6408c JS |
253 | // Create an application context |
254 | bool wxApp::OnInitGui() | |
255 | { | |
fd304d98 MB |
256 | if( !wxAppBase::OnInitGui() ) |
257 | return FALSE; | |
258 | ||
4bb6408c | 259 | XtToolkitInitialize() ; |
edc1cd8b JS |
260 | wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext(); |
261 | XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources); | |
262 | ||
4bb6408c | 263 | Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL, |
d3a80c92 | 264 | wxTheApp->GetClassName().c_str(), NULL, 0, |
4bb6408c | 265 | # if XtSpecificationRelease < 5 |
9c6e1335 | 266 | (Cardinal*) &argc, |
4bb6408c | 267 | # else |
9c6e1335 | 268 | &argc, |
4bb6408c | 269 | # endif |
9c6e1335 VZ |
270 | argv); |
271 | ||
4bb6408c | 272 | if (!dpy) { |
c88b736c MB |
273 | // if you don't log to stderr, nothing will be shown... |
274 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
7bcb11d3 | 275 | wxString className(wxTheApp->GetClassName()); |
9c6e1335 | 276 | wxLogError(_("wxWindows could not open display for '%s': exiting."), |
d3a80c92 | 277 | className.c_str()); |
2d120f83 | 278 | exit(-1); |
4bb6408c | 279 | } |
47bc1060 | 280 | m_initialDisplay = (WXDisplay*) dpy; |
afb74891 | 281 | |
e838cc14 VZ |
282 | #ifdef __WXDEBUG__ |
283 | // install the X error handler | |
284 | gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler); | |
285 | #endif // __WXDEBUG__ | |
286 | ||
4bb6408c JS |
287 | // Add general resize proc |
288 | XtActionsRec rec; | |
289 | rec.string = "resize"; | |
290 | rec.proc = (XtActionProc)wxWidgetResizeProc; | |
291 | XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1); | |
afb74891 | 292 | |
4bb6408c | 293 | GetMainColormap(dpy); |
afb74891 | 294 | |
7e1bcfa8 MB |
295 | wxAddIdleCallback(); |
296 | ||
4bb6408c JS |
297 | return TRUE; |
298 | } | |
299 | ||
300 | WXColormap wxApp::GetMainColormap(WXDisplay* display) | |
301 | { | |
302 | if (!display) /* Must be called first with non-NULL display */ | |
2d120f83 | 303 | return m_mainColormap; |
a91b47e8 JS |
304 | |
305 | int defaultScreen = DefaultScreen((Display*) display); | |
306 | Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen); | |
afb74891 | 307 | |
a91b47e8 | 308 | Colormap c = DefaultColormapOfScreen(screen); |
afb74891 | 309 | |
4bb6408c | 310 | if (!m_mainColormap) |
2d120f83 | 311 | m_mainColormap = (WXColormap) c; |
afb74891 | 312 | |
4bb6408c JS |
313 | return (WXColormap) c; |
314 | } | |
315 | ||
9ce8d6a2 MB |
316 | wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display ) |
317 | { | |
eb6fa4b4 | 318 | wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display ); |
9ce8d6a2 | 319 | |
eb6fa4b4 MB |
320 | if( it != m_perDisplayData->end() && it->second.m_visualInfo ) |
321 | return it->second.m_visualInfo; | |
9ce8d6a2 MB |
322 | |
323 | wxXVisualInfo* vi = new wxXVisualInfo; | |
324 | wxFillXVisualInfo( vi, (Display*)display ); | |
325 | ||
eb6fa4b4 | 326 | (*m_perDisplayData)[display].m_visualInfo = vi; |
9ce8d6a2 MB |
327 | |
328 | return vi; | |
329 | } | |
330 | ||
eb6fa4b4 MB |
331 | static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData, |
332 | XtPointer ptr) | |
333 | { | |
334 | if( wxTheApp ) | |
335 | wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w), | |
336 | (WXWidget)NULL ); | |
337 | } | |
338 | ||
339 | WXWidget wxCreateTopLevelWidget( WXDisplay* display ) | |
340 | { | |
341 | Widget tlw = XtAppCreateShell( (String)NULL, | |
342 | wxTheApp->GetClassName().c_str(), | |
343 | applicationShellWidgetClass, | |
344 | (Display*)display, | |
345 | NULL, 0 ); | |
732c0bfd | 346 | XtSetMappedWhenManaged( tlw, False ); |
dd38c875 | 347 | XtRealizeWidget( tlw ); |
eb6fa4b4 MB |
348 | |
349 | XtAddCallback( tlw, XmNdestroyCallback, | |
350 | (XtCallbackProc)wxTLWidgetDestroyCallback, | |
351 | (XtPointer)NULL ); | |
352 | ||
353 | return (WXWidget)tlw; | |
354 | } | |
355 | ||
356 | WXWidget wxApp::GetTopLevelWidget() | |
357 | { | |
358 | WXDisplay* display = wxGetDisplay(); | |
359 | wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display ); | |
360 | ||
361 | if( it != m_perDisplayData->end() && it->second.m_topLevelWidget ) | |
362 | return (WXWidget)it->second.m_topLevelWidget; | |
363 | ||
364 | WXWidget tlw = wxCreateTopLevelWidget( display ); | |
365 | SetTopLevelWidget( display, tlw ); | |
366 | ||
367 | return tlw; | |
368 | } | |
369 | ||
370 | void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget) | |
371 | { | |
372 | (*m_perDisplayData)[display].m_topLevelWidget = (Widget)widget; | |
373 | } | |
374 | ||
4bb6408c | 375 | // Yield to other processes |
cb2713bf | 376 | |
8461e4c2 | 377 | bool wxApp::Yield(bool onlyIfNeeded) |
4bb6408c | 378 | { |
8461e4c2 VZ |
379 | bool s_inYield = FALSE; |
380 | ||
381 | if ( s_inYield ) | |
382 | { | |
383 | if ( !onlyIfNeeded ) | |
384 | { | |
385 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
386 | } | |
387 | ||
388 | return FALSE; | |
389 | } | |
390 | ||
391 | s_inYield = TRUE; | |
cb2713bf | 392 | |
4bb6408c | 393 | while (wxTheApp && wxTheApp->Pending()) |
2d120f83 | 394 | wxTheApp->Dispatch(); |
518b5d2f | 395 | |
8461e4c2 | 396 | s_inYield = FALSE; |
cb2713bf | 397 | |
4bb6408c JS |
398 | return TRUE; |
399 | } | |
400 | ||
d391a345 VZ |
401 | // ---------------------------------------------------------------------------- |
402 | // accessors for C modules | |
403 | // ---------------------------------------------------------------------------- | |
404 | ||
405 | extern "C" XtAppContext wxGetAppContext() | |
406 | { | |
407 | return (XtAppContext)wxTheApp->GetAppContext(); | |
408 | } |