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