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