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