]> git.saurik.com Git - wxWidgets.git/blame - src/motif/app.cpp
replaced wxGetOsVersion() with wxGUIAppTraits::GetOSVersion()
[wxWidgets.git] / src / motif / app.cpp
CommitLineData
4bb6408c
JS
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
afb74891 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
ee31c392 13 #pragma implementation "app.h"
4bb6408c
JS
14#endif
15
bcd055ae
JJ
16#ifdef __VMS
17#define XtParent XTPARENT
18#define XtDisplay XTDISPLAY
19#endif
20
4bb6408c
JS
21#include "wx/app.h"
22#include "wx/utils.h"
4bb6408c
JS
23#include "wx/module.h"
24#include "wx/memory.h"
8bedcdce
RR
25#include "wx/log.h"
26#include "wx/intl.h"
7e1bcfa8 27#include "wx/evtloop.h"
ed39ff57 28#include "wx/hash.h"
9ce8d6a2 29#include "wx/hashmap.h"
b412f9be 30
7bcb11d3 31#if wxUSE_THREADS
dfe1eee3 32 #include "wx/thread.h"
7bcb11d3 33#endif
4bb6408c 34
338dd992
JJ
35#ifdef __VMS__
36#pragma message disable nosimpint
37#endif
4bb6408c
JS
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>
338dd992
JJ
43#ifdef __VMS__
44#pragma message enable nosimpint
45#endif
4bb6408c
JS
46
47#include "wx/motif/private.h"
48
49#include <string.h>
50
eb6fa4b4
MB
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 );
9ce8d6a2 65
4bb6408c 66extern wxList wxPendingDelete;
7e1bcfa8 67extern bool wxAddIdleCallback();
7491d644 68
4bb6408c
JS
69wxHashTable *wxWidgetHashTable = NULL;
70
4bb6408c 71IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
9c6e1335 72
4bb6408c 73BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
9c6e1335 74 EVT_IDLE(wxApp::OnIdle)
4bb6408c 75END_EVENT_TABLE()
4bb6408c 76
e838cc14
VZ
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
4bb6408c
JS
89bool wxApp::Initialize()
90{
4bb6408c 91 wxClassInfo::InitializeClasses();
afb74891 92
ee31c392 93 // GL: I'm annoyed ... I don't know where to put this and I don't want to
4d3a259a
GL
94 // create a module for that as it's part of the core.
95#if wxUSE_THREADS
4d3a259a
GL
96 wxPendingEventsLocker = new wxCriticalSection();
97#endif
98
4bb6408c
JS
99 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
100 wxTheColourDatabase->Initialize();
afb74891 101
4b5f3fe6 102 wxInitializeStockLists();
4bb6408c 103 wxInitializeStockObjects();
afb74891 104
4bb6408c 105 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
afb74891 106
4bb6408c 107 wxModule::RegisterModules();
aaa38880 108 if (!wxModule::InitializeModules()) return FALSE;
afb74891 109
4bb6408c
JS
110 return TRUE;
111}
112
113void wxApp::CleanUp()
114{
4bb6408c 115 wxModule::CleanUpModules();
afb74891 116
4bb6408c 117 wxDeleteStockObjects() ;
afb74891 118
4bb6408c 119 // Destroy all GDI lists, etc.
afb74891 120
7ecb8b06 121 wxDeleteStockLists();
afb74891 122
4bb6408c
JS
123 delete wxTheColourDatabase;
124 wxTheColourDatabase = NULL;
afb74891 125
4bb6408c 126 wxClassInfo::CleanUpClasses();
afb74891 127
184b5d99
JS
128 delete wxTheApp;
129 wxTheApp = NULL;
afb74891 130
e6be3e7c
MB
131 delete wxWidgetHashTable;
132 wxWidgetHashTable = NULL;
133
4d3a259a
GL
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;
b0feedc5 138 wxPendingEvents = NULL;
4d3a259a 139 delete wxPendingEventsLocker;
b0feedc5 140 wxPendingEventsLocker = NULL;
4d3a259a
GL
141#endif
142
184b5d99
JS
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.
4fabb575 148 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
184b5d99 149 {
2d120f83
JS
150 wxLogDebug("There were memory leaks.\n");
151 wxDebugContext::Dump();
152 wxDebugContext::PrintStatistics();
184b5d99 153 }
184b5d99 154#endif
afb74891 155
4bb6408c
JS
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
e2478fde
VZ
162void wxApp::Exit()
163{
164 wxApp::CleanUp();
165
166 wxAppConsole::Exit();
167}
168
e6be3e7c
MB
169// ============================================================================
170// wxEntry*
171// ============================================================================
172
173int wxEntryStart( int argc, char* argv[] )
4bb6408c 174{
4fabb575
JS
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
afb74891 184
4bb6408c 185 if (!wxApp::Initialize())
e6be3e7c
MB
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;
afb74891 221
4bb6408c
JS
222 if (!wxTheApp)
223 {
2d120f83
JS
224 if (!wxApp::GetInitializerFunction())
225 {
226 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
227 return 0;
228 };
afb74891 229
2d120f83 230 wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) ();
4bb6408c 231 };
afb74891 232
4bb6408c
JS
233 if (!wxTheApp)
234 {
2d120f83
JS
235 printf( "wxWindows error: wxTheApp == NULL\n" );
236 return 0;
4bb6408c 237 };
afb74891 238
4bb6408c
JS
239 wxTheApp->SetClassName(wxFileNameFromPath(argv[0]));
240 wxTheApp->SetAppName(wxFileNameFromPath(argv[0]));
afb74891 241
4bb6408c
JS
242 wxTheApp->argc = argc;
243 wxTheApp->argv = argv;
afb74891 244
4bb6408c 245 // GUI-specific initialization, such as creating an app context.
e6be3e7c
MB
246 retValue = wxEntryInitGui();
247 if (retValue) return retValue;
afb74891 248
e146b8c8
VZ
249 // Here frames insert themselves automatically into wxTopLevelWindows by
250 // getting created in OnInit().
afb74891 251
4fabb575
JS
252 if (wxTheApp->OnInit())
253 {
e6be3e7c
MB
254 if (wxTheApp->Initialized())
255 wxTheApp->OnRun();
4fabb575 256 }
afb74891 257
4bb6408c
JS
258 if (wxTheApp->GetTopWindow())
259 {
2d120f83
JS
260 delete wxTheApp->GetTopWindow();
261 wxTheApp->SetTopWindow(NULL);
4bb6408c 262 }
afb74891 263
4bb6408c 264 wxTheApp->DeletePendingObjects();
afb74891 265
e6be3e7c 266 retValue = wxTheApp->OnExit();
afb74891 267
e6be3e7c 268 wxEntryCleanup();
afb74891 269
4bb6408c 270 return retValue;
e6be3e7c 271}
4bb6408c 272
4bb6408c
JS
273wxApp::wxApp()
274{
4bb6408c
JS
275 argc = 0;
276 argv = NULL;
afb74891 277
7e1bcfa8 278 m_eventLoop = new wxEventLoop;
4bb6408c
JS
279 m_mainColormap = (WXColormap) NULL;
280 m_appContext = (WXAppContext) NULL;
47bc1060 281 m_initialDisplay = (WXDisplay*) 0;
eb6fa4b4 282 m_perDisplayData = new wxPerDisplayDataMap;
4bb6408c
JS
283}
284
7e1bcfa8
MB
285wxApp::~wxApp()
286{
287 delete m_eventLoop;
9ce8d6a2 288
eb6fa4b4
MB
289 for( wxPerDisplayDataMap::iterator it = m_perDisplayData->begin(),
290 end = m_perDisplayData->end();
9ce8d6a2
MB
291 it != end; ++it )
292 {
eb6fa4b4
MB
293 delete it->second.m_visualInfo;
294 XtDestroyWidget( it->second.m_topLevelWidget );
9ce8d6a2
MB
295 }
296
eb6fa4b4
MB
297 delete m_perDisplayData;
298
299 wxTheApp = NULL;
7e1bcfa8
MB
300}
301
4bb6408c
JS
302bool wxApp::Initialized()
303{
304 if (GetTopWindow())
2d120f83 305 return TRUE;
4bb6408c 306 else
2d120f83 307 return FALSE;
4bb6408c
JS
308}
309
310int wxApp::MainLoop()
311{
2d120f83 312 /*
4bb6408c
JS
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 */
afb74891 318
4bb6408c 319 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
2d120f83
JS
320 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
321 PropertyChangeMask);
afb74891 322
7e1bcfa8 323 m_eventLoop->Run();
afb74891 324
8aa04e8b
JS
325 return 0;
326}
327
7e1bcfa8 328// Processes an idle event.
4bb6408c
JS
329// Returns TRUE if more time is needed.
330bool wxApp::ProcessIdle()
331{
332 wxIdleEvent event;
afb74891 333
7e1bcfa8 334 return ProcessEvent(event) && event.MoreRequested();
4bb6408c
JS
335}
336
337void wxApp::ExitMainLoop()
338{
e6be3e7c
MB
339 if( m_eventLoop->IsRunning() )
340 m_eventLoop->Exit();
4bb6408c
JS
341}
342
343// Is a message/event pending?
344bool wxApp::Pending()
345{
ba2fcb07
MB
346 return m_eventLoop->Pending();
347#if 0
4bb6408c 348 XFlush(XtDisplay( (Widget) wxTheApp->GetTopLevelWidget() ));
afb74891 349
8aa04e8b
JS
350 // Fix by Doug from STI, to prevent a stall if non-X event
351 // is found.
352 return ((XtAppPending( (XtAppContext) GetAppContext() ) & XtIMXEvent) != 0) ;
ba2fcb07 353#endif
4bb6408c
JS
354}
355
356// Dispatch a message.
357void wxApp::Dispatch()
358{
7e1bcfa8 359 m_eventLoop->Dispatch();
4bb6408c
JS
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;
afb74891 373
4bb6408c
JS
374 // Avoid recursion (via ProcessEvent default case)
375 if (inOnIdle)
2d120f83 376 return;
afb74891 377
4bb6408c 378 inOnIdle = TRUE;
afb74891 379
0ca580f6
GRG
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
afb74891 388
7214297d
GL
389 // Flush pending events.
390 ProcessPendingEvents();
0ca580f6
GRG
391
392 // 'Garbage' collection of windows deleted with Close().
393 DeletePendingObjects();
7214297d 394
4bb6408c
JS
395 // flush the logged messages if any
396 wxLog *pLog = wxLog::GetActiveTarget();
397 if ( pLog != NULL && pLog->HasPendingMessages() )
2d120f83 398 pLog->Flush();
afb74891 399
4bb6408c
JS
400 // Send OnIdle events to all windows
401 bool needMore = SendIdleEvents();
afb74891 402
4bb6408c 403 if (needMore)
2d120f83 404 event.RequestMore(TRUE);
afb74891 405
4bb6408c
JS
406 inOnIdle = FALSE;
407}
408
409// Send idle event to all top-level windows
410bool wxApp::SendIdleEvents()
411{
412 bool needMore = FALSE;
e146b8c8
VZ
413
414 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
2d120f83
JS
415 while (node)
416 {
e146b8c8 417 wxWindow* win = node->GetData();
2d120f83 418 if (SendIdleEvents(win))
4bb6408c 419 needMore = TRUE;
e146b8c8 420 node = node->GetNext();
2d120f83 421 }
e146b8c8 422
4bb6408c
JS
423 return needMore;
424}
425
426// Send idle event to window and all subwindows
427bool wxApp::SendIdleEvents(wxWindow* win)
428{
429 bool needMore = FALSE;
afb74891 430
2d120f83
JS
431 wxIdleEvent event;
432 event.SetEventObject(win);
44f5b995 433 win->GetEventHandler()->ProcessEvent(event);
afb74891 434
4bb6408c
JS
435 if (event.MoreRequested())
436 needMore = TRUE;
afb74891 437
fd304d98 438 wxWindowList::Node* node = win->GetChildren().GetFirst();
2d120f83
JS
439 while (node)
440 {
fd304d98 441 wxWindow* win = node->GetData();
2d120f83 442 if (SendIdleEvents(win))
4bb6408c 443 needMore = TRUE;
afb74891 444
fd304d98 445 node = node->GetNext();
2d120f83 446 }
4bb6408c
JS
447 return needMore ;
448}
449
450void wxApp::DeletePendingObjects()
451{
7e1bcfa8 452 wxList::Node *node = wxPendingDelete.GetFirst();
4bb6408c
JS
453 while (node)
454 {
fd304d98 455 wxObject *obj = node->GetData();
afb74891 456
2d120f83 457 delete obj;
afb74891 458
2d120f83
JS
459 if (wxPendingDelete.Member(obj))
460 delete node;
afb74891 461
2d120f83
JS
462 // Deleting one object may have deleted other pending
463 // objects, so start from beginning of list again.
fd304d98 464 node = wxPendingDelete.GetFirst();
4bb6408c
JS
465 }
466}
467
edc1cd8b
JS
468static char *fallbackResources[] = {
469 "*menuBar.marginHeight: 0",
470 "*menuBar.shadowThickness: 1",
471 "*background: #c0c0c0",
472 "*foreground: black",
473 NULL
474};
475
4bb6408c
JS
476// Create an application context
477bool wxApp::OnInitGui()
478{
fd304d98
MB
479 if( !wxAppBase::OnInitGui() )
480 return FALSE;
481
4bb6408c 482 XtToolkitInitialize() ;
edc1cd8b
JS
483 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext();
484 XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources);
485
4bb6408c 486 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
d3a80c92 487 wxTheApp->GetClassName().c_str(), NULL, 0,
4bb6408c 488# if XtSpecificationRelease < 5
9c6e1335 489 (Cardinal*) &argc,
4bb6408c 490# else
9c6e1335 491 &argc,
4bb6408c 492# endif
9c6e1335
VZ
493 argv);
494
4bb6408c 495 if (!dpy) {
c88b736c
MB
496 // if you don't log to stderr, nothing will be shown...
497 delete wxLog::SetActiveTarget(new wxLogStderr);
7bcb11d3 498 wxString className(wxTheApp->GetClassName());
9c6e1335 499 wxLogError(_("wxWindows could not open display for '%s': exiting."),
d3a80c92 500 className.c_str());
2d120f83 501 exit(-1);
4bb6408c 502 }
47bc1060 503 m_initialDisplay = (WXDisplay*) dpy;
afb74891 504
e838cc14
VZ
505#ifdef __WXDEBUG__
506 // install the X error handler
507 gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler);
508#endif // __WXDEBUG__
509
4bb6408c
JS
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);
afb74891 515
4bb6408c 516 GetMainColormap(dpy);
afb74891 517
7e1bcfa8
MB
518 wxAddIdleCallback();
519
4bb6408c
JS
520 return TRUE;
521}
522
523WXColormap wxApp::GetMainColormap(WXDisplay* display)
524{
525 if (!display) /* Must be called first with non-NULL display */
2d120f83 526 return m_mainColormap;
a91b47e8
JS
527
528 int defaultScreen = DefaultScreen((Display*) display);
529 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
afb74891 530
a91b47e8 531 Colormap c = DefaultColormapOfScreen(screen);
afb74891 532
4bb6408c 533 if (!m_mainColormap)
2d120f83 534 m_mainColormap = (WXColormap) c;
afb74891 535
4bb6408c
JS
536 return (WXColormap) c;
537}
538
9ce8d6a2
MB
539wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display )
540{
eb6fa4b4 541 wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display );
9ce8d6a2 542
eb6fa4b4
MB
543 if( it != m_perDisplayData->end() && it->second.m_visualInfo )
544 return it->second.m_visualInfo;
9ce8d6a2
MB
545
546 wxXVisualInfo* vi = new wxXVisualInfo;
547 wxFillXVisualInfo( vi, (Display*)display );
548
eb6fa4b4 549 (*m_perDisplayData)[display].m_visualInfo = vi;
9ce8d6a2
MB
550
551 return vi;
552}
553
eb6fa4b4
MB
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 );
732c0bfd 569 XtSetMappedWhenManaged( tlw, False );
dd38c875 570 XtRealizeWidget( tlw );
eb6fa4b4
MB
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
4bb6408c 598// Yield to other processes
cb2713bf 599
8461e4c2 600bool wxApp::Yield(bool onlyIfNeeded)
4bb6408c 601{
8461e4c2
VZ
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;
cb2713bf 615
4bb6408c 616 while (wxTheApp && wxTheApp->Pending())
2d120f83 617 wxTheApp->Dispatch();
518b5d2f 618
8461e4c2 619 s_inYield = FALSE;
cb2713bf 620
4bb6408c
JS
621 return TRUE;
622}
623
d391a345
VZ
624// ----------------------------------------------------------------------------
625// accessors for C modules
626// ----------------------------------------------------------------------------
627
628extern "C" XtAppContext wxGetAppContext()
629{
630 return (XtAppContext)wxTheApp->GetAppContext();
631}