]>
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 | #ifdef __GNUG__ | |
13 | #pragma implementation "app.h" | |
14 | #endif | |
15 | ||
16 | #ifdef __VMS | |
17 | #define XtParent XTPARENT | |
18 | #define XtDisplay XTDISPLAY | |
19 | #endif | |
20 | ||
21 | #include "wx/app.h" | |
22 | #include "wx/utils.h" | |
23 | #include "wx/module.h" | |
24 | #include "wx/memory.h" | |
25 | #include "wx/log.h" | |
26 | #include "wx/intl.h" | |
27 | #include "wx/evtloop.h" | |
28 | #include "wx/hash.h" | |
29 | #include "wx/hashmap.h" | |
30 | ||
31 | #if wxUSE_THREADS | |
32 | #include "wx/thread.h" | |
33 | #endif | |
34 | ||
35 | #ifdef __VMS__ | |
36 | #pragma message disable nosimpint | |
37 | #endif | |
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> | |
43 | #ifdef __VMS__ | |
44 | #pragma message enable nosimpint | |
45 | #endif | |
46 | ||
47 | #include "wx/motif/private.h" | |
48 | ||
49 | #include <string.h> | |
50 | ||
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 ); | |
65 | ||
66 | extern wxList wxPendingDelete; | |
67 | extern bool wxAddIdleCallback(); | |
68 | ||
69 | wxHashTable *wxWidgetHashTable = NULL; | |
70 | ||
71 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) | |
72 | ||
73 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
74 | EVT_IDLE(wxApp::OnIdle) | |
75 | END_EVENT_TABLE() | |
76 | ||
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 | ||
89 | bool wxApp::Initialize(int& argc, wxChar **argv) | |
90 | { | |
91 | if ( !wxAppBase::Initialize(argc, argv) ) | |
92 | return false; | |
93 | ||
94 | wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER); | |
95 | ||
96 | return true; | |
97 | } | |
98 | ||
99 | void wxApp::CleanUp() | |
100 | { | |
101 | delete wxWidgetHashTable; | |
102 | wxWidgetHashTable = NULL; | |
103 | ||
104 | wxAppBase::CleanUp(); | |
105 | } | |
106 | ||
107 | void wxApp::Exit() | |
108 | { | |
109 | wxApp::CleanUp(); | |
110 | ||
111 | wxAppConsole::Exit(); | |
112 | } | |
113 | ||
114 | // ============================================================================ | |
115 | // wxApp | |
116 | // ============================================================================ | |
117 | ||
118 | wxApp::wxApp() | |
119 | { | |
120 | argc = 0; | |
121 | argv = NULL; | |
122 | ||
123 | m_eventLoop = new wxEventLoop; | |
124 | m_mainColormap = (WXColormap) NULL; | |
125 | m_appContext = (WXAppContext) NULL; | |
126 | m_initialDisplay = (WXDisplay*) 0; | |
127 | m_perDisplayData = new wxPerDisplayDataMap; | |
128 | } | |
129 | ||
130 | wxApp::~wxApp() | |
131 | { | |
132 | delete m_eventLoop; | |
133 | ||
134 | for( wxPerDisplayDataMap::iterator it = m_perDisplayData->begin(), | |
135 | end = m_perDisplayData->end(); | |
136 | it != end; ++it ) | |
137 | { | |
138 | delete it->second.m_visualInfo; | |
139 | XtDestroyWidget( it->second.m_topLevelWidget ); | |
140 | } | |
141 | ||
142 | delete m_perDisplayData; | |
143 | ||
144 | wxTheApp = NULL; | |
145 | } | |
146 | ||
147 | bool wxApp::Initialized() | |
148 | { | |
149 | if (GetTopWindow()) | |
150 | return TRUE; | |
151 | else | |
152 | return FALSE; | |
153 | } | |
154 | ||
155 | int wxApp::MainLoop() | |
156 | { | |
157 | /* | |
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 | */ | |
163 | ||
164 | XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), | |
165 | XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())), | |
166 | PropertyChangeMask); | |
167 | ||
168 | m_eventLoop->Run(); | |
169 | ||
170 | return 0; | |
171 | } | |
172 | ||
173 | // Processes an idle event. | |
174 | // Returns TRUE if more time is needed. | |
175 | bool wxApp::ProcessIdle() | |
176 | { | |
177 | wxIdleEvent event; | |
178 | ||
179 | bool processed = ProcessEvent(event); | |
180 | ||
181 | wxUpdateUIEvent::ResetUpdateTime(); | |
182 | ||
183 | return processed && event.MoreRequested(); | |
184 | } | |
185 | ||
186 | void wxApp::ExitMainLoop() | |
187 | { | |
188 | if( m_eventLoop->IsRunning() ) | |
189 | m_eventLoop->Exit(); | |
190 | } | |
191 | ||
192 | // Is a message/event pending? | |
193 | bool wxApp::Pending() | |
194 | { | |
195 | return m_eventLoop->Pending(); | |
196 | #if 0 | |
197 | XFlush(XtDisplay( (Widget) wxTheApp->GetTopLevelWidget() )); | |
198 | ||
199 | // Fix by Doug from STI, to prevent a stall if non-X event | |
200 | // is found. | |
201 | return ((XtAppPending( (XtAppContext) GetAppContext() ) & XtIMXEvent) != 0) ; | |
202 | #endif | |
203 | } | |
204 | ||
205 | // Dispatch a message. | |
206 | void wxApp::Dispatch() | |
207 | { | |
208 | m_eventLoop->Dispatch(); | |
209 | } | |
210 | ||
211 | // This should be redefined in a derived class for | |
212 | // handling property change events for XAtom IPC. | |
213 | void wxApp::HandlePropertyChange(WXEvent *event) | |
214 | { | |
215 | // by default do nothing special | |
216 | XtDispatchEvent((XEvent*) event); /* let Motif do the work */ | |
217 | } | |
218 | ||
219 | void wxApp::OnIdle(wxIdleEvent& event) | |
220 | { | |
221 | static bool inOnIdle = FALSE; | |
222 | ||
223 | // Avoid recursion (via ProcessEvent default case) | |
224 | if (inOnIdle) | |
225 | return; | |
226 | ||
227 | inOnIdle = TRUE; | |
228 | ||
229 | // If there are pending events, we must process them: pending events | |
230 | // are either events to the threads other than main or events posted | |
231 | // with wxPostEvent() functions | |
232 | // GRG: I have moved this here so that all pending events are processed | |
233 | // before starting to delete any objects. This behaves better (in | |
234 | // particular, wrt wxPostEvent) and is coherent with wxGTK's current | |
235 | // behaviour. Also removed the '#if wxUSE_THREADS' around it. | |
236 | // Changed Mar/2000 before 2.1.14 | |
237 | ||
238 | // Flush pending events. | |
239 | ProcessPendingEvents(); | |
240 | ||
241 | // 'Garbage' collection of windows deleted with Close(). | |
242 | DeletePendingObjects(); | |
243 | ||
244 | // flush the logged messages if any | |
245 | wxLog *pLog = wxLog::GetActiveTarget(); | |
246 | if ( pLog != NULL && pLog->HasPendingMessages() ) | |
247 | pLog->Flush(); | |
248 | ||
249 | // Send OnIdle events to all windows | |
250 | bool needMore = SendIdleEvents(); | |
251 | ||
252 | if (needMore) | |
253 | event.RequestMore(TRUE); | |
254 | ||
255 | inOnIdle = FALSE; | |
256 | } | |
257 | ||
258 | // Send idle event to all top-level windows | |
259 | bool wxApp::SendIdleEvents() | |
260 | { | |
261 | bool needMore = FALSE; | |
262 | ||
263 | wxWindowList::Node* node = wxTopLevelWindows.GetFirst(); | |
264 | while (node) | |
265 | { | |
266 | wxWindow* win = node->GetData(); | |
267 | if (SendIdleEvents(win)) | |
268 | needMore = TRUE; | |
269 | node = node->GetNext(); | |
270 | } | |
271 | ||
272 | return needMore; | |
273 | } | |
274 | ||
275 | // Send idle event to window and all subwindows | |
276 | bool wxApp::SendIdleEvents(wxWindow* win) | |
277 | { | |
278 | bool needMore = FALSE; | |
279 | ||
280 | wxIdleEvent event; | |
281 | event.SetEventObject(win); | |
282 | win->GetEventHandler()->ProcessEvent(event); | |
283 | ||
284 | if (event.MoreRequested()) | |
285 | needMore = TRUE; | |
286 | ||
287 | wxWindowList::Node* node = win->GetChildren().GetFirst(); | |
288 | while (node) | |
289 | { | |
290 | wxWindow* win = node->GetData(); | |
291 | if (SendIdleEvents(win)) | |
292 | needMore = TRUE; | |
293 | ||
294 | node = node->GetNext(); | |
295 | } | |
296 | return needMore ; | |
297 | } | |
298 | ||
299 | static char *fallbackResources[] = { | |
300 | "*menuBar.marginHeight: 0", | |
301 | "*menuBar.shadowThickness: 1", | |
302 | "*background: #c0c0c0", | |
303 | "*foreground: black", | |
304 | NULL | |
305 | }; | |
306 | ||
307 | // Create an application context | |
308 | bool wxApp::OnInitGui() | |
309 | { | |
310 | if( !wxAppBase::OnInitGui() ) | |
311 | return FALSE; | |
312 | ||
313 | XtToolkitInitialize() ; | |
314 | wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext(); | |
315 | XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources); | |
316 | ||
317 | Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL, | |
318 | wxTheApp->GetClassName().c_str(), NULL, 0, | |
319 | # if XtSpecificationRelease < 5 | |
320 | (Cardinal*) &argc, | |
321 | # else | |
322 | &argc, | |
323 | # endif | |
324 | argv); | |
325 | ||
326 | if (!dpy) { | |
327 | // if you don't log to stderr, nothing will be shown... | |
328 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
329 | wxString className(wxTheApp->GetClassName()); | |
330 | wxLogError(_("wxWindows could not open display for '%s': exiting."), | |
331 | className.c_str()); | |
332 | exit(-1); | |
333 | } | |
334 | m_initialDisplay = (WXDisplay*) dpy; | |
335 | ||
336 | #ifdef __WXDEBUG__ | |
337 | // install the X error handler | |
338 | gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler); | |
339 | #endif // __WXDEBUG__ | |
340 | ||
341 | // Add general resize proc | |
342 | XtActionsRec rec; | |
343 | rec.string = "resize"; | |
344 | rec.proc = (XtActionProc)wxWidgetResizeProc; | |
345 | XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1); | |
346 | ||
347 | GetMainColormap(dpy); | |
348 | ||
349 | wxAddIdleCallback(); | |
350 | ||
351 | return TRUE; | |
352 | } | |
353 | ||
354 | WXColormap wxApp::GetMainColormap(WXDisplay* display) | |
355 | { | |
356 | if (!display) /* Must be called first with non-NULL display */ | |
357 | return m_mainColormap; | |
358 | ||
359 | int defaultScreen = DefaultScreen((Display*) display); | |
360 | Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen); | |
361 | ||
362 | Colormap c = DefaultColormapOfScreen(screen); | |
363 | ||
364 | if (!m_mainColormap) | |
365 | m_mainColormap = (WXColormap) c; | |
366 | ||
367 | return (WXColormap) c; | |
368 | } | |
369 | ||
370 | wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display ) | |
371 | { | |
372 | wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display ); | |
373 | ||
374 | if( it != m_perDisplayData->end() && it->second.m_visualInfo ) | |
375 | return it->second.m_visualInfo; | |
376 | ||
377 | wxXVisualInfo* vi = new wxXVisualInfo; | |
378 | wxFillXVisualInfo( vi, (Display*)display ); | |
379 | ||
380 | (*m_perDisplayData)[display].m_visualInfo = vi; | |
381 | ||
382 | return vi; | |
383 | } | |
384 | ||
385 | static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData, | |
386 | XtPointer ptr) | |
387 | { | |
388 | if( wxTheApp ) | |
389 | wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w), | |
390 | (WXWidget)NULL ); | |
391 | } | |
392 | ||
393 | WXWidget wxCreateTopLevelWidget( WXDisplay* display ) | |
394 | { | |
395 | Widget tlw = XtAppCreateShell( (String)NULL, | |
396 | wxTheApp->GetClassName().c_str(), | |
397 | applicationShellWidgetClass, | |
398 | (Display*)display, | |
399 | NULL, 0 ); | |
400 | XtSetMappedWhenManaged( tlw, False ); | |
401 | XtRealizeWidget( tlw ); | |
402 | ||
403 | XtAddCallback( tlw, XmNdestroyCallback, | |
404 | (XtCallbackProc)wxTLWidgetDestroyCallback, | |
405 | (XtPointer)NULL ); | |
406 | ||
407 | return (WXWidget)tlw; | |
408 | } | |
409 | ||
410 | WXWidget wxApp::GetTopLevelWidget() | |
411 | { | |
412 | WXDisplay* display = wxGetDisplay(); | |
413 | wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display ); | |
414 | ||
415 | if( it != m_perDisplayData->end() && it->second.m_topLevelWidget ) | |
416 | return (WXWidget)it->second.m_topLevelWidget; | |
417 | ||
418 | WXWidget tlw = wxCreateTopLevelWidget( display ); | |
419 | SetTopLevelWidget( display, tlw ); | |
420 | ||
421 | return tlw; | |
422 | } | |
423 | ||
424 | void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget) | |
425 | { | |
426 | (*m_perDisplayData)[display].m_topLevelWidget = (Widget)widget; | |
427 | } | |
428 | ||
429 | // Yield to other processes | |
430 | ||
431 | bool wxApp::Yield(bool onlyIfNeeded) | |
432 | { | |
433 | bool s_inYield = FALSE; | |
434 | ||
435 | if ( s_inYield ) | |
436 | { | |
437 | if ( !onlyIfNeeded ) | |
438 | { | |
439 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
440 | } | |
441 | ||
442 | return FALSE; | |
443 | } | |
444 | ||
445 | s_inYield = TRUE; | |
446 | ||
447 | while (wxTheApp && wxTheApp->Pending()) | |
448 | wxTheApp->Dispatch(); | |
449 | ||
450 | s_inYield = FALSE; | |
451 | ||
452 | return TRUE; | |
453 | } | |
454 | ||
455 | // ---------------------------------------------------------------------------- | |
456 | // accessors for C modules | |
457 | // ---------------------------------------------------------------------------- | |
458 | ||
459 | extern "C" XtAppContext wxGetAppContext() | |
460 | { | |
461 | return (XtAppContext)wxTheApp->GetAppContext(); | |
462 | } |