]> git.saurik.com Git - wxWidgets.git/blob - src/motif/app.cpp
More deprecated class mods
[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 #include "wx/hash.h"
40
41 #if wxUSE_THREADS
42 #include "wx/thread.h"
43 #endif
44
45 #ifdef __VMS__
46 #pragma message disable nosimpint
47 #endif
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>
53 #ifdef __VMS__
54 #pragma message enable nosimpint
55 #endif
56
57 #include "wx/motif/private.h"
58
59 #include <string.h>
60
61 extern wxList wxPendingDelete;
62 extern bool wxAddIdleCallback();
63
64 wxApp *wxTheApp = NULL;
65
66 wxHashTable *wxWidgetHashTable = NULL;
67
68 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
69
70 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
71 EVT_IDLE(wxApp::OnIdle)
72 END_EVENT_TABLE()
73
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
86 long wxApp::sm_lastMessageTime = 0;
87
88 bool wxApp::Initialize()
89 {
90 wxClassInfo::InitializeClasses();
91
92 // GL: I'm annoyed ... I don't know where to put this and I don't want to
93 // create a module for that as it's part of the core.
94 #if wxUSE_THREADS
95 wxPendingEventsLocker = new wxCriticalSection();
96 #endif
97
98 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
99 wxTheColourDatabase->Initialize();
100
101 wxInitializeStockLists();
102 wxInitializeStockObjects();
103
104 // For PostScript printing
105 #if wxUSE_POSTSCRIPT
106 /* Done using wxModule now
107 wxInitializePrintSetupData();
108 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
109 wxThePrintPaperDatabase->CreateDatabase();
110 */
111 #endif
112
113 wxBitmap::InitStandardHandlers();
114
115 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
116
117 wxModule::RegisterModules();
118 if (!wxModule::InitializeModules()) return FALSE;
119
120 return TRUE;
121 }
122
123 void wxApp::CleanUp()
124 {
125 delete wxWidgetHashTable;
126 wxWidgetHashTable = NULL;
127
128 wxModule::CleanUpModules();
129
130 wxDeleteStockObjects() ;
131
132 // Destroy all GDI lists, etc.
133
134 wxDeleteStockLists();
135
136 delete wxTheColourDatabase;
137 wxTheColourDatabase = NULL;
138
139 #if wxUSE_POSTSCRIPT
140 /* Done using wxModule now
141 wxInitializePrintSetupData(FALSE);
142 delete wxThePrintPaperDatabase;
143 wxThePrintPaperDatabase = NULL;
144 */
145 #endif
146
147 wxBitmap::CleanUpHandlers();
148
149 wxClassInfo::CleanUpClasses();
150
151 delete wxTheApp;
152 wxTheApp = NULL;
153
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
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.
166 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
167 {
168 wxLogDebug("There were memory leaks.\n");
169 wxDebugContext::Dump();
170 wxDebugContext::PrintStatistics();
171 }
172 #endif
173
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
180 int wxEntry( int argc, char *argv[] )
181 {
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
191
192 if (!wxApp::Initialize())
193 return FALSE;
194
195 if (!wxTheApp)
196 {
197 if (!wxApp::GetInitializerFunction())
198 {
199 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
200 return 0;
201 };
202
203 wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) ();
204 };
205
206 if (!wxTheApp)
207 {
208 printf( "wxWindows error: wxTheApp == NULL\n" );
209 return 0;
210 };
211
212 wxTheApp->SetClassName(wxFileNameFromPath(argv[0]));
213 wxTheApp->SetAppName(wxFileNameFromPath(argv[0]));
214
215 wxTheApp->argc = argc;
216 wxTheApp->argv = argv;
217
218 // GUI-specific initialization, such as creating an app context.
219 wxTheApp->OnInitGui();
220
221 // Here frames insert themselves automatically into wxTopLevelWindows by
222 // getting created in OnInit().
223
224 int retValue = 0;
225 if (wxTheApp->OnInit())
226 {
227 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
228 }
229
230 // flush the logged messages if any
231 wxLog *pLog = wxLog::GetActiveTarget();
232 if ( pLog != NULL && pLog->HasPendingMessages() )
233 pLog->Flush();
234
235 delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used
236 // for further messages
237
238 if (wxTheApp->GetTopWindow())
239 {
240 delete wxTheApp->GetTopWindow();
241 wxTheApp->SetTopWindow(NULL);
242 }
243
244 wxTheApp->DeletePendingObjects();
245
246 wxTheApp->OnExit();
247
248 wxApp::CleanUp();
249
250 return retValue;
251 };
252
253 // Static member initialization
254 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
255
256 wxApp::wxApp()
257 {
258 argc = 0;
259 argv = NULL;
260
261 m_eventLoop = new wxEventLoop;
262 m_mainColormap = (WXColormap) NULL;
263 m_appContext = (WXAppContext) NULL;
264 m_topLevelWidget = (WXWidget) NULL;
265 m_maxRequestSize = 0;
266 m_initialDisplay = (WXDisplay*) 0;
267 }
268
269 wxApp::~wxApp()
270 {
271 delete m_eventLoop;
272 }
273
274 bool wxApp::Initialized()
275 {
276 if (GetTopWindow())
277 return TRUE;
278 else
279 return FALSE;
280 }
281
282 int wxApp::MainLoop()
283 {
284 /*
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 */
290
291 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
292 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
293 PropertyChangeMask);
294
295 m_eventLoop->Run();
296
297 return 0;
298 }
299
300 // Processes an idle event.
301 // Returns TRUE if more time is needed.
302 bool wxApp::ProcessIdle()
303 {
304 wxIdleEvent event;
305
306 return ProcessEvent(event) && event.MoreRequested();
307 }
308
309 void wxApp::ExitMainLoop()
310 {
311 m_eventLoop->Exit();
312 }
313
314 // Is a message/event pending?
315 bool wxApp::Pending()
316 {
317 return m_eventLoop->Pending();
318 #if 0
319 XFlush(XtDisplay( (Widget) wxTheApp->GetTopLevelWidget() ));
320
321 // Fix by Doug from STI, to prevent a stall if non-X event
322 // is found.
323 return ((XtAppPending( (XtAppContext) GetAppContext() ) & XtIMXEvent) != 0) ;
324 #endif
325 }
326
327 // Dispatch a message.
328 void wxApp::Dispatch()
329 {
330 m_eventLoop->Dispatch();
331 }
332
333 // This should be redefined in a derived class for
334 // handling property change events for XAtom IPC.
335 void wxApp::HandlePropertyChange(WXEvent *event)
336 {
337 // by default do nothing special
338 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
339 }
340
341 void wxApp::OnIdle(wxIdleEvent& event)
342 {
343 static bool inOnIdle = FALSE;
344
345 // Avoid recursion (via ProcessEvent default case)
346 if (inOnIdle)
347 return;
348
349 inOnIdle = TRUE;
350
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
359
360 // Flush pending events.
361 ProcessPendingEvents();
362
363 // 'Garbage' collection of windows deleted with Close().
364 DeletePendingObjects();
365
366 // flush the logged messages if any
367 wxLog *pLog = wxLog::GetActiveTarget();
368 if ( pLog != NULL && pLog->HasPendingMessages() )
369 pLog->Flush();
370
371 // Send OnIdle events to all windows
372 bool needMore = SendIdleEvents();
373
374 if (needMore)
375 event.RequestMore(TRUE);
376
377 inOnIdle = FALSE;
378 }
379
380 // Send idle event to all top-level windows
381 bool wxApp::SendIdleEvents()
382 {
383 bool needMore = FALSE;
384
385 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
386 while (node)
387 {
388 wxWindow* win = node->GetData();
389 if (SendIdleEvents(win))
390 needMore = TRUE;
391 node = node->GetNext();
392 }
393
394 return needMore;
395 }
396
397 // Send idle event to window and all subwindows
398 bool wxApp::SendIdleEvents(wxWindow* win)
399 {
400 bool needMore = FALSE;
401
402 wxIdleEvent event;
403 event.SetEventObject(win);
404 win->ProcessEvent(event);
405
406 if (event.MoreRequested())
407 needMore = TRUE;
408
409 wxWindowList::Node* node = win->GetChildren().GetFirst();
410 while (node)
411 {
412 wxWindow* win = node->GetData();
413 if (SendIdleEvents(win))
414 needMore = TRUE;
415
416 node = node->GetNext();
417 }
418 return needMore ;
419 }
420
421 void wxApp::DeletePendingObjects()
422 {
423 wxList::Node *node = wxPendingDelete.GetFirst();
424 while (node)
425 {
426 wxObject *obj = node->GetData();
427
428 delete obj;
429
430 if (wxPendingDelete.Member(obj))
431 delete node;
432
433 // Deleting one object may have deleted other pending
434 // objects, so start from beginning of list again.
435 node = wxPendingDelete.GetFirst();
436 }
437 }
438
439 static char *fallbackResources[] = {
440 "*menuBar.marginHeight: 0",
441 "*menuBar.shadowThickness: 1",
442 "*background: #c0c0c0",
443 "*foreground: black",
444 NULL
445 };
446
447 // Create an application context
448 bool wxApp::OnInitGui()
449 {
450 if( !wxAppBase::OnInitGui() )
451 return FALSE;
452
453 XtToolkitInitialize() ;
454 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext();
455 XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources);
456
457 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
458 wxTheApp->GetClassName().c_str(), NULL, 0,
459 # if XtSpecificationRelease < 5
460 (Cardinal*) &argc,
461 # else
462 &argc,
463 # endif
464 argv);
465
466 if (!dpy) {
467 // if you don't log to stderr, nothing will be shown...
468 delete wxLog::SetActiveTarget(new wxLogStderr);
469 wxString className(wxTheApp->GetClassName());
470 wxLogError(_("wxWindows could not open display for '%s': exiting."),
471 className.c_str());
472 exit(-1);
473 }
474 m_initialDisplay = (WXDisplay*) dpy;
475
476 #ifdef __WXDEBUG__
477 // install the X error handler
478 gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler);
479 #endif // __WXDEBUG__
480
481 wxTheApp->m_topLevelWidget =
482 (WXWidget) XtAppCreateShell((String)NULL,
483 wxTheApp->GetClassName().c_str(),
484 applicationShellWidgetClass,dpy,
485 NULL,0) ;
486
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);
492
493 GetMainColormap(dpy);
494 m_maxRequestSize = XMaxRequestSize((Display*) dpy);
495
496 wxAddIdleCallback();
497
498 return TRUE;
499 }
500
501 WXColormap wxApp::GetMainColormap(WXDisplay* display)
502 {
503 if (!display) /* Must be called first with non-NULL display */
504 return m_mainColormap;
505
506 int defaultScreen = DefaultScreen((Display*) display);
507 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
508
509 Colormap c = DefaultColormapOfScreen(screen);
510
511 if (!m_mainColormap)
512 m_mainColormap = (WXColormap) c;
513
514 return (WXColormap) c;
515 }
516
517 void wxExit()
518 {
519 int retValue = 0;
520 if (wxTheApp)
521 retValue = wxTheApp->OnExit();
522
523 wxApp::CleanUp();
524 /*
525 * Exit in some platform-specific way. Not recommended that the app calls this:
526 * only for emergencies.
527 */
528 exit(retValue);
529 }
530
531 // Yield to other processes
532
533 bool wxApp::Yield(bool onlyIfNeeded)
534 {
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;
548
549 while (wxTheApp && wxTheApp->Pending())
550 wxTheApp->Dispatch();
551
552 s_inYield = FALSE;
553
554 return TRUE;
555 }
556
557 // ----------------------------------------------------------------------------
558 // accessors for C modules
559 // ----------------------------------------------------------------------------
560
561 extern "C" XtAppContext wxGetAppContext()
562 {
563 return (XtAppContext)wxTheApp->GetAppContext();
564 }