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