]> git.saurik.com Git - wxWidgets.git/blob - src/motif/app.cpp
regenerated all make/project files to include selstore.cpp/.h
[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/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 wxApp *wxTheApp = NULL;
70
71 wxHashTable *wxWidgetHashTable = NULL;
72
73 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
74
75 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
76 EVT_IDLE(wxApp::OnIdle)
77 END_EVENT_TABLE()
78
79 #ifdef __WXDEBUG__
80 typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *);
81
82 XErrorHandlerFunc gs_pfnXErrorHandler = 0;
83
84 static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent)
85 {
86 // just forward to the default handler for now
87 return gs_pfnXErrorHandler(dpy, xevent);
88 }
89 #endif // __WXDEBUG__
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 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 delete wxWidgetHashTable;
134 wxWidgetHashTable = 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 wxPendingEvents = NULL;
141 delete wxPendingEventsLocker;
142 wxPendingEventsLocker = NULL;
143 #endif
144
145 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
146 // At this point we want to check if there are any memory
147 // blocks that aren't part of the wxDebugContext itself,
148 // as a special case. Then when dumping we need to ignore
149 // wxDebugContext, too.
150 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
151 {
152 wxLogDebug("There were memory leaks.\n");
153 wxDebugContext::Dump();
154 wxDebugContext::PrintStatistics();
155 }
156 #endif
157
158 // do it as the very last thing because everything else can log messages
159 wxLog::DontCreateOnDemand();
160 // do it as the very last thing because everything else can log messages
161 delete wxLog::SetActiveTarget(NULL);
162 }
163
164 // ============================================================================
165 // wxEntry*
166 // ============================================================================
167
168 int wxEntryStart( int argc, char* argv[] )
169 {
170 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
171 // This seems to be necessary since there are 'rogue'
172 // objects present at this point (perhaps global objects?)
173 // Setting a checkpoint will ignore them as far as the
174 // memory checking facility is concerned.
175 // Of course you may argue that memory allocated in globals should be
176 // checked, but this is a reasonable compromise.
177 wxDebugContext::SetCheckpoint();
178 #endif
179
180 if (!wxApp::Initialize())
181 return -1;
182
183 return 0;
184 }
185
186 int wxEntryInitGui()
187 {
188 int retValue = 0;
189
190 // GUI-specific initialization, such as creating an app context.
191 if (!wxTheApp->OnInitGui())
192 retValue = -1;
193
194 return retValue;
195 }
196
197 void wxEntryCleanup()
198 {
199 // So dialog boxes aren't used for further messages
200 delete wxLog::SetActiveTarget(new wxLogStderr);
201
202 // flush the logged messages if any
203 wxLog *pLog = wxLog::GetActiveTarget();
204 if ( pLog != NULL && pLog->HasPendingMessages() )
205 pLog->Flush();
206
207 wxApp::CleanUp();
208 }
209
210 int wxEntry( int argc, char *argv[] )
211 {
212 int retValue = 0;
213
214 retValue = wxEntryStart( argc, argv );
215 if (retValue) return retValue;
216
217 if (!wxTheApp)
218 {
219 if (!wxApp::GetInitializerFunction())
220 {
221 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
222 return 0;
223 };
224
225 wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) ();
226 };
227
228 if (!wxTheApp)
229 {
230 printf( "wxWindows error: wxTheApp == NULL\n" );
231 return 0;
232 };
233
234 wxTheApp->SetClassName(wxFileNameFromPath(argv[0]));
235 wxTheApp->SetAppName(wxFileNameFromPath(argv[0]));
236
237 wxTheApp->argc = argc;
238 wxTheApp->argv = argv;
239
240 // GUI-specific initialization, such as creating an app context.
241 retValue = wxEntryInitGui();
242 if (retValue) return retValue;
243
244 // Here frames insert themselves automatically into wxTopLevelWindows by
245 // getting created in OnInit().
246
247 if (wxTheApp->OnInit())
248 {
249 if (wxTheApp->Initialized())
250 wxTheApp->OnRun();
251 }
252
253 if (wxTheApp->GetTopWindow())
254 {
255 delete wxTheApp->GetTopWindow();
256 wxTheApp->SetTopWindow(NULL);
257 }
258
259 wxTheApp->DeletePendingObjects();
260
261 retValue = wxTheApp->OnExit();
262
263 wxEntryCleanup();
264
265 return retValue;
266 }
267
268 // Static member initialization
269 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
270
271 wxApp::wxApp()
272 {
273 argc = 0;
274 argv = NULL;
275
276 m_eventLoop = new wxEventLoop;
277 m_mainColormap = (WXColormap) NULL;
278 m_appContext = (WXAppContext) NULL;
279 m_initialDisplay = (WXDisplay*) 0;
280 m_perDisplayData = new wxPerDisplayDataMap;
281 }
282
283 wxApp::~wxApp()
284 {
285 delete m_eventLoop;
286
287 for( wxPerDisplayDataMap::iterator it = m_perDisplayData->begin(),
288 end = m_perDisplayData->end();
289 it != end; ++it )
290 {
291 delete it->second.m_visualInfo;
292 XtDestroyWidget( it->second.m_topLevelWidget );
293 }
294
295 delete m_perDisplayData;
296
297 wxTheApp = NULL;
298 }
299
300 bool wxApp::Initialized()
301 {
302 if (GetTopWindow())
303 return TRUE;
304 else
305 return FALSE;
306 }
307
308 int wxApp::MainLoop()
309 {
310 /*
311 * Sit around forever waiting to process X-events. Property Change
312 * event are handled special, because they have to refer to
313 * the root window rather than to a widget. therefore we can't
314 * use an Xt-eventhandler.
315 */
316
317 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
318 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
319 PropertyChangeMask);
320
321 m_eventLoop->Run();
322
323 return 0;
324 }
325
326 // Processes an idle event.
327 // Returns TRUE if more time is needed.
328 bool wxApp::ProcessIdle()
329 {
330 wxIdleEvent event;
331
332 return ProcessEvent(event) && event.MoreRequested();
333 }
334
335 void wxApp::ExitMainLoop()
336 {
337 if( m_eventLoop->IsRunning() )
338 m_eventLoop->Exit();
339 }
340
341 // Is a message/event pending?
342 bool wxApp::Pending()
343 {
344 return m_eventLoop->Pending();
345 #if 0
346 XFlush(XtDisplay( (Widget) wxTheApp->GetTopLevelWidget() ));
347
348 // Fix by Doug from STI, to prevent a stall if non-X event
349 // is found.
350 return ((XtAppPending( (XtAppContext) GetAppContext() ) & XtIMXEvent) != 0) ;
351 #endif
352 }
353
354 // Dispatch a message.
355 void wxApp::Dispatch()
356 {
357 m_eventLoop->Dispatch();
358 }
359
360 // This should be redefined in a derived class for
361 // handling property change events for XAtom IPC.
362 void wxApp::HandlePropertyChange(WXEvent *event)
363 {
364 // by default do nothing special
365 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
366 }
367
368 void wxApp::OnIdle(wxIdleEvent& event)
369 {
370 static bool inOnIdle = FALSE;
371
372 // Avoid recursion (via ProcessEvent default case)
373 if (inOnIdle)
374 return;
375
376 inOnIdle = TRUE;
377
378 // If there are pending events, we must process them: pending events
379 // are either events to the threads other than main or events posted
380 // with wxPostEvent() functions
381 // GRG: I have moved this here so that all pending events are processed
382 // before starting to delete any objects. This behaves better (in
383 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
384 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
385 // Changed Mar/2000 before 2.1.14
386
387 // Flush pending events.
388 ProcessPendingEvents();
389
390 // 'Garbage' collection of windows deleted with Close().
391 DeletePendingObjects();
392
393 // flush the logged messages if any
394 wxLog *pLog = wxLog::GetActiveTarget();
395 if ( pLog != NULL && pLog->HasPendingMessages() )
396 pLog->Flush();
397
398 // Send OnIdle events to all windows
399 bool needMore = SendIdleEvents();
400
401 if (needMore)
402 event.RequestMore(TRUE);
403
404 inOnIdle = FALSE;
405 }
406
407 // Send idle event to all top-level windows
408 bool wxApp::SendIdleEvents()
409 {
410 bool needMore = FALSE;
411
412 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
413 while (node)
414 {
415 wxWindow* win = node->GetData();
416 if (SendIdleEvents(win))
417 needMore = TRUE;
418 node = node->GetNext();
419 }
420
421 return needMore;
422 }
423
424 // Send idle event to window and all subwindows
425 bool wxApp::SendIdleEvents(wxWindow* win)
426 {
427 bool needMore = FALSE;
428
429 wxIdleEvent event;
430 event.SetEventObject(win);
431 win->GetEventHandler()->ProcessEvent(event);
432
433 if (event.MoreRequested())
434 needMore = TRUE;
435
436 wxWindowList::Node* node = win->GetChildren().GetFirst();
437 while (node)
438 {
439 wxWindow* win = node->GetData();
440 if (SendIdleEvents(win))
441 needMore = TRUE;
442
443 node = node->GetNext();
444 }
445 return needMore ;
446 }
447
448 void wxApp::DeletePendingObjects()
449 {
450 wxList::Node *node = wxPendingDelete.GetFirst();
451 while (node)
452 {
453 wxObject *obj = node->GetData();
454
455 delete obj;
456
457 if (wxPendingDelete.Member(obj))
458 delete node;
459
460 // Deleting one object may have deleted other pending
461 // objects, so start from beginning of list again.
462 node = wxPendingDelete.GetFirst();
463 }
464 }
465
466 static char *fallbackResources[] = {
467 "*menuBar.marginHeight: 0",
468 "*menuBar.shadowThickness: 1",
469 "*background: #c0c0c0",
470 "*foreground: black",
471 NULL
472 };
473
474 // Create an application context
475 bool wxApp::OnInitGui()
476 {
477 if( !wxAppBase::OnInitGui() )
478 return FALSE;
479
480 XtToolkitInitialize() ;
481 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext();
482 XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources);
483
484 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
485 wxTheApp->GetClassName().c_str(), NULL, 0,
486 # if XtSpecificationRelease < 5
487 (Cardinal*) &argc,
488 # else
489 &argc,
490 # endif
491 argv);
492
493 if (!dpy) {
494 // if you don't log to stderr, nothing will be shown...
495 delete wxLog::SetActiveTarget(new wxLogStderr);
496 wxString className(wxTheApp->GetClassName());
497 wxLogError(_("wxWindows could not open display for '%s': exiting."),
498 className.c_str());
499 exit(-1);
500 }
501 m_initialDisplay = (WXDisplay*) dpy;
502
503 #ifdef __WXDEBUG__
504 // install the X error handler
505 gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler);
506 #endif // __WXDEBUG__
507
508 // Add general resize proc
509 XtActionsRec rec;
510 rec.string = "resize";
511 rec.proc = (XtActionProc)wxWidgetResizeProc;
512 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
513
514 GetMainColormap(dpy);
515
516 wxAddIdleCallback();
517
518 return TRUE;
519 }
520
521 WXColormap wxApp::GetMainColormap(WXDisplay* display)
522 {
523 if (!display) /* Must be called first with non-NULL display */
524 return m_mainColormap;
525
526 int defaultScreen = DefaultScreen((Display*) display);
527 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
528
529 Colormap c = DefaultColormapOfScreen(screen);
530
531 if (!m_mainColormap)
532 m_mainColormap = (WXColormap) c;
533
534 return (WXColormap) c;
535 }
536
537 wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display )
538 {
539 wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display );
540
541 if( it != m_perDisplayData->end() && it->second.m_visualInfo )
542 return it->second.m_visualInfo;
543
544 wxXVisualInfo* vi = new wxXVisualInfo;
545 wxFillXVisualInfo( vi, (Display*)display );
546
547 (*m_perDisplayData)[display].m_visualInfo = vi;
548
549 return vi;
550 }
551
552 static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData,
553 XtPointer ptr)
554 {
555 if( wxTheApp )
556 wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w),
557 (WXWidget)NULL );
558 }
559
560 WXWidget wxCreateTopLevelWidget( WXDisplay* display )
561 {
562 Widget tlw = XtAppCreateShell( (String)NULL,
563 wxTheApp->GetClassName().c_str(),
564 applicationShellWidgetClass,
565 (Display*)display,
566 NULL, 0 );
567 XtSetMappedWhenManaged( tlw, False );
568 XtRealizeWidget( tlw );
569
570 XtAddCallback( tlw, XmNdestroyCallback,
571 (XtCallbackProc)wxTLWidgetDestroyCallback,
572 (XtPointer)NULL );
573
574 return (WXWidget)tlw;
575 }
576
577 WXWidget wxApp::GetTopLevelWidget()
578 {
579 WXDisplay* display = wxGetDisplay();
580 wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display );
581
582 if( it != m_perDisplayData->end() && it->second.m_topLevelWidget )
583 return (WXWidget)it->second.m_topLevelWidget;
584
585 WXWidget tlw = wxCreateTopLevelWidget( display );
586 SetTopLevelWidget( display, tlw );
587
588 return tlw;
589 }
590
591 void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget)
592 {
593 (*m_perDisplayData)[display].m_topLevelWidget = (Widget)widget;
594 }
595
596 void wxExit()
597 {
598 int retValue = 0;
599 if (wxTheApp)
600 retValue = wxTheApp->OnExit();
601
602 wxApp::CleanUp();
603 /*
604 * Exit in some platform-specific way.
605 * Not recommended that the app calls this:
606 * only for emergencies.
607 */
608 exit(retValue);
609 }
610
611 // Yield to other processes
612
613 bool wxApp::Yield(bool onlyIfNeeded)
614 {
615 bool s_inYield = FALSE;
616
617 if ( s_inYield )
618 {
619 if ( !onlyIfNeeded )
620 {
621 wxFAIL_MSG( wxT("wxYield called recursively" ) );
622 }
623
624 return FALSE;
625 }
626
627 s_inYield = TRUE;
628
629 while (wxTheApp && wxTheApp->Pending())
630 wxTheApp->Dispatch();
631
632 s_inYield = FALSE;
633
634 return TRUE;
635 }
636
637 // ----------------------------------------------------------------------------
638 // accessors for C modules
639 // ----------------------------------------------------------------------------
640
641 extern "C" XtAppContext wxGetAppContext()
642 {
643 return (XtAppContext)wxTheApp->GetAppContext();
644 }