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