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