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