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