Remove unnecessary header dependencies. Fix resulting compilation
[wxWidgets.git] / src / motif / app.cpp
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/frame.h"
22 #include "wx/app.h"
23 #include "wx/utils.h"
24 #include "wx/gdicmn.h"
25 #include "wx/pen.h"
26 #include "wx/brush.h"
27 #include "wx/cursor.h"
28 #include "wx/icon.h"
29 #include "wx/palette.h"
30 #include "wx/dc.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.h"
33 #include "wx/log.h"
34 #include "wx/module.h"
35 #include "wx/memory.h"
36 #include "wx/log.h"
37 #include "wx/intl.h"
38 #include "wx/evtloop.h"
39 #include "wx/hash.h"
40
41 #if wxUSE_THREADS
42 #include "wx/thread.h"
43 #endif
44
45 #if wxUSE_WX_RESOURCES
46 #include "wx/resource.h"
47 #endif
48
49 #ifdef __VMS__
50 #pragma message disable nosimpint
51 #endif
52 #include <Xm/Xm.h>
53 #include <X11/Xlib.h>
54 #include <X11/Xutil.h>
55 #include <X11/Xresource.h>
56 #include <X11/Xatom.h>
57 #ifdef __VMS__
58 #pragma message enable nosimpint
59 #endif
60
61 #include "wx/motif/private.h"
62
63 #include <string.h>
64
65 extern wxList wxPendingDelete;
66 extern bool wxAddIdleCallback();
67
68 wxApp *wxTheApp = NULL;
69
70 wxHashTable *wxWidgetHashTable = NULL;
71
72 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
73
74 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
75 EVT_IDLE(wxApp::OnIdle)
76 END_EVENT_TABLE()
77
78 #ifdef __WXDEBUG__
79 typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *);
80
81 XErrorHandlerFunc gs_pfnXErrorHandler = 0;
82
83 static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent)
84 {
85 // just forward to the default handler for now
86 return gs_pfnXErrorHandler(dpy, xevent);
87 }
88 #endif // __WXDEBUG__
89
90 long wxApp::sm_lastMessageTime = 0;
91
92 bool wxApp::Initialize()
93 {
94 wxClassInfo::InitializeClasses();
95
96 // GL: I'm annoyed ... I don't know where to put this and I don't want to
97 // create a module for that as it's part of the core.
98 #if wxUSE_THREADS
99 wxPendingEventsLocker = new wxCriticalSection();
100 #endif
101
102 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
103 wxTheColourDatabase->Initialize();
104
105 wxInitializeStockLists();
106 wxInitializeStockObjects();
107
108 #if wxUSE_WX_RESOURCES
109 wxInitializeResourceSystem();
110 #endif
111
112 // For PostScript printing
113 #if wxUSE_POSTSCRIPT
114 /* Done using wxModule now
115 wxInitializePrintSetupData();
116 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
117 wxThePrintPaperDatabase->CreateDatabase();
118 */
119 #endif
120
121 wxBitmap::InitStandardHandlers();
122
123 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
124
125 wxModule::RegisterModules();
126 if (!wxModule::InitializeModules()) return FALSE;
127
128 return TRUE;
129 }
130
131 void wxApp::CleanUp()
132 {
133 delete wxWidgetHashTable;
134 wxWidgetHashTable = NULL;
135
136 wxModule::CleanUpModules();
137
138 #if wxUSE_WX_RESOURCES
139 wxCleanUpResourceSystem();
140 #endif
141
142 wxDeleteStockObjects() ;
143
144 // Destroy all GDI lists, etc.
145
146 wxDeleteStockLists();
147
148 delete wxTheColourDatabase;
149 wxTheColourDatabase = NULL;
150
151 #if wxUSE_POSTSCRIPT
152 /* Done using wxModule now
153 wxInitializePrintSetupData(FALSE);
154 delete wxThePrintPaperDatabase;
155 wxThePrintPaperDatabase = NULL;
156 */
157 #endif
158
159 wxBitmap::CleanUpHandlers();
160
161 wxClassInfo::CleanUpClasses();
162
163 delete wxTheApp;
164 wxTheApp = NULL;
165
166 // GL: I'm annoyed ... I don't know where to put this and I don't want to
167 // create a module for that as it's part of the core.
168 #if wxUSE_THREADS
169 delete wxPendingEvents;
170 delete wxPendingEventsLocker;
171 #endif
172
173 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
174 // At this point we want to check if there are any memory
175 // blocks that aren't part of the wxDebugContext itself,
176 // as a special case. Then when dumping we need to ignore
177 // wxDebugContext, too.
178 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
179 {
180 wxLogDebug("There were memory leaks.\n");
181 wxDebugContext::Dump();
182 wxDebugContext::PrintStatistics();
183 }
184 #endif
185
186 // do it as the very last thing because everything else can log messages
187 wxLog::DontCreateOnDemand();
188 // do it as the very last thing because everything else can log messages
189 delete wxLog::SetActiveTarget(NULL);
190 }
191
192 int wxEntry( int argc, char *argv[] )
193 {
194 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
195 // This seems to be necessary since there are 'rogue'
196 // objects present at this point (perhaps global objects?)
197 // Setting a checkpoint will ignore them as far as the
198 // memory checking facility is concerned.
199 // Of course you may argue that memory allocated in globals should be
200 // checked, but this is a reasonable compromise.
201 wxDebugContext::SetCheckpoint();
202 #endif
203
204 if (!wxApp::Initialize())
205 return FALSE;
206
207 if (!wxTheApp)
208 {
209 if (!wxApp::GetInitializerFunction())
210 {
211 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
212 return 0;
213 };
214
215 wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) ();
216 };
217
218 if (!wxTheApp)
219 {
220 printf( "wxWindows error: wxTheApp == NULL\n" );
221 return 0;
222 };
223
224 wxTheApp->SetClassName(wxFileNameFromPath(argv[0]));
225 wxTheApp->SetAppName(wxFileNameFromPath(argv[0]));
226
227 wxTheApp->argc = argc;
228 wxTheApp->argv = argv;
229
230 // GUI-specific initialization, such as creating an app context.
231 wxTheApp->OnInitGui();
232
233 // Here frames insert themselves automatically into wxTopLevelWindows by
234 // getting created in OnInit().
235
236 int retValue = 0;
237 if (wxTheApp->OnInit())
238 {
239 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
240 }
241
242 // flush the logged messages if any
243 wxLog *pLog = wxLog::GetActiveTarget();
244 if ( pLog != NULL && pLog->HasPendingMessages() )
245 pLog->Flush();
246
247 delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used
248 // for further messages
249
250 if (wxTheApp->GetTopWindow())
251 {
252 delete wxTheApp->GetTopWindow();
253 wxTheApp->SetTopWindow(NULL);
254 }
255
256 wxTheApp->DeletePendingObjects();
257
258 wxTheApp->OnExit();
259
260 wxApp::CleanUp();
261
262 return retValue;
263 };
264
265 // Static member initialization
266 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
267
268 wxApp::wxApp()
269 {
270 argc = 0;
271 argv = NULL;
272
273 m_eventLoop = new wxEventLoop;
274 m_mainColormap = (WXColormap) NULL;
275 m_appContext = (WXAppContext) NULL;
276 m_topLevelWidget = (WXWidget) NULL;
277 m_maxRequestSize = 0;
278 m_initialDisplay = (WXDisplay*) 0;
279 }
280
281 wxApp::~wxApp()
282 {
283 delete m_eventLoop;
284 }
285
286 bool wxApp::Initialized()
287 {
288 if (GetTopWindow())
289 return TRUE;
290 else
291 return FALSE;
292 }
293
294 int wxApp::MainLoop()
295 {
296 /*
297 * Sit around forever waiting to process X-events. Property Change
298 * event are handled special, because they have to refer to
299 * the root window rather than to a widget. therefore we can't
300 * use an Xt-eventhandler.
301 */
302
303 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
304 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
305 PropertyChangeMask);
306
307 m_eventLoop->Run();
308
309 return 0;
310 }
311
312 // Processes an idle event.
313 // Returns TRUE if more time is needed.
314 bool wxApp::ProcessIdle()
315 {
316 wxIdleEvent event;
317
318 return ProcessEvent(event) && event.MoreRequested();
319 }
320
321 void wxApp::ExitMainLoop()
322 {
323 m_eventLoop->Exit();
324 }
325
326 // Is a message/event pending?
327 bool wxApp::Pending()
328 {
329 XFlush(XtDisplay( (Widget) wxTheApp->GetTopLevelWidget() ));
330
331 // Fix by Doug from STI, to prevent a stall if non-X event
332 // is found.
333 return ((XtAppPending( (XtAppContext) GetAppContext() ) & XtIMXEvent) != 0) ;
334 }
335
336 // Dispatch a message.
337 void wxApp::Dispatch()
338 {
339 m_eventLoop->Dispatch();
340 }
341
342 // This should be redefined in a derived class for
343 // handling property change events for XAtom IPC.
344 void wxApp::HandlePropertyChange(WXEvent *event)
345 {
346 // by default do nothing special
347 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
348 }
349
350 void wxApp::OnIdle(wxIdleEvent& event)
351 {
352 static bool inOnIdle = FALSE;
353
354 // Avoid recursion (via ProcessEvent default case)
355 if (inOnIdle)
356 return;
357
358 inOnIdle = TRUE;
359
360 // If there are pending events, we must process them: pending events
361 // are either events to the threads other than main or events posted
362 // with wxPostEvent() functions
363 // GRG: I have moved this here so that all pending events are processed
364 // before starting to delete any objects. This behaves better (in
365 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
366 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
367 // Changed Mar/2000 before 2.1.14
368
369 // Flush pending events.
370 ProcessPendingEvents();
371
372 // 'Garbage' collection of windows deleted with Close().
373 DeletePendingObjects();
374
375 // flush the logged messages if any
376 wxLog *pLog = wxLog::GetActiveTarget();
377 if ( pLog != NULL && pLog->HasPendingMessages() )
378 pLog->Flush();
379
380 // Send OnIdle events to all windows
381 bool needMore = SendIdleEvents();
382
383 if (needMore)
384 event.RequestMore(TRUE);
385
386 inOnIdle = FALSE;
387 }
388
389 // Send idle event to all top-level windows
390 bool wxApp::SendIdleEvents()
391 {
392 bool needMore = FALSE;
393
394 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
395 while (node)
396 {
397 wxWindow* win = node->GetData();
398 if (SendIdleEvents(win))
399 needMore = TRUE;
400 node = node->GetNext();
401 }
402
403 return needMore;
404 }
405
406 // Send idle event to window and all subwindows
407 bool wxApp::SendIdleEvents(wxWindow* win)
408 {
409 bool needMore = FALSE;
410
411 wxIdleEvent event;
412 event.SetEventObject(win);
413 win->ProcessEvent(event);
414
415 if (event.MoreRequested())
416 needMore = TRUE;
417
418 wxWindowList::Node* node = win->GetChildren().GetFirst();
419 while (node)
420 {
421 wxWindow* win = node->GetData();
422 if (SendIdleEvents(win))
423 needMore = TRUE;
424
425 node = node->GetNext();
426 }
427 return needMore ;
428 }
429
430 void wxApp::DeletePendingObjects()
431 {
432 wxList::Node *node = wxPendingDelete.GetFirst();
433 while (node)
434 {
435 wxObject *obj = node->GetData();
436
437 delete obj;
438
439 if (wxPendingDelete.Member(obj))
440 delete node;
441
442 // Deleting one object may have deleted other pending
443 // objects, so start from beginning of list again.
444 node = wxPendingDelete.GetFirst();
445 }
446 }
447
448 static char *fallbackResources[] = {
449 "*menuBar.marginHeight: 0",
450 "*menuBar.shadowThickness: 1",
451 "*background: #c0c0c0",
452 "*foreground: black",
453 NULL
454 };
455
456 // Create an application context
457 bool wxApp::OnInitGui()
458 {
459 if( !wxAppBase::OnInitGui() )
460 return FALSE;
461
462 XtToolkitInitialize() ;
463 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext();
464 XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources);
465
466 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
467 (const char*) wxTheApp->GetClassName(), NULL, 0,
468 # if XtSpecificationRelease < 5
469 (Cardinal*) &argc,
470 # else
471 &argc,
472 # endif
473 argv);
474
475 if (!dpy) {
476 // if you don't log to stderr, nothing will be shown...
477 delete wxLog::SetActiveTarget(new wxLogStderr);
478 wxString className(wxTheApp->GetClassName());
479 wxLogError(_("wxWindows could not open display for '%s': exiting."),
480 (const char*) className);
481 exit(-1);
482 }
483 m_initialDisplay = (WXDisplay*) dpy;
484
485 #ifdef __WXDEBUG__
486 // install the X error handler
487 gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler);
488 #endif // __WXDEBUG__
489
490 wxTheApp->m_topLevelWidget = (WXWidget) XtAppCreateShell((String)NULL, (const char*) wxTheApp->GetClassName(),
491 applicationShellWidgetClass,dpy,
492 NULL,0) ;
493
494 // Add general resize proc
495 XtActionsRec rec;
496 rec.string = "resize";
497 rec.proc = (XtActionProc)wxWidgetResizeProc;
498 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
499
500 GetMainColormap(dpy);
501 m_maxRequestSize = XMaxRequestSize((Display*) dpy);
502
503 wxAddIdleCallback();
504
505 return TRUE;
506 }
507
508 WXColormap wxApp::GetMainColormap(WXDisplay* display)
509 {
510 if (!display) /* Must be called first with non-NULL display */
511 return m_mainColormap;
512
513 int defaultScreen = DefaultScreen((Display*) display);
514 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
515
516 Colormap c = DefaultColormapOfScreen(screen);
517
518 if (!m_mainColormap)
519 m_mainColormap = (WXColormap) c;
520
521 return (WXColormap) c;
522 }
523
524 void wxExit()
525 {
526 int retValue = 0;
527 if (wxTheApp)
528 retValue = wxTheApp->OnExit();
529
530 wxApp::CleanUp();
531 /*
532 * Exit in some platform-specific way. Not recommended that the app calls this:
533 * only for emergencies.
534 */
535 exit(retValue);
536 }
537
538 // Yield to other processes
539
540 bool wxApp::Yield(bool onlyIfNeeded)
541 {
542 bool s_inYield = FALSE;
543
544 if ( s_inYield )
545 {
546 if ( !onlyIfNeeded )
547 {
548 wxFAIL_MSG( wxT("wxYield called recursively" ) );
549 }
550
551 return FALSE;
552 }
553
554 s_inYield = TRUE;
555
556 while (wxTheApp && wxTheApp->Pending())
557 wxTheApp->Dispatch();
558
559 s_inYield = FALSE;
560
561 return TRUE;
562 }
563
564 // ----------------------------------------------------------------------------
565 // accessors for C modules
566 // ----------------------------------------------------------------------------
567
568 extern "C" XtAppContext wxGetAppContext()
569 {
570 return (XtAppContext)wxTheApp->GetAppContext();
571 }