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