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