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