]> git.saurik.com Git - wxWidgets.git/blob - src/motif/app.cpp
ba8f0da0121f0f2e36e60a4c44a75db924c2f46f
[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 #include "wx/frame.h"
17 #include "wx/app.h"
18 #include "wx/utils.h"
19 #include "wx/gdicmn.h"
20 #include "wx/pen.h"
21 #include "wx/brush.h"
22 #include "wx/cursor.h"
23 #include "wx/icon.h"
24 #include "wx/palette.h"
25 #include "wx/dc.h"
26 #include "wx/dialog.h"
27 #include "wx/msgdlg.h"
28 #include "wx/log.h"
29 #include "wx/module.h"
30 #include "wx/memory.h"
31
32 #if wxUSE_THREADS
33 #include "wx/thread.h"
34 #endif
35
36 #if wxUSE_WX_RESOURCES
37 #include "wx/resource.h"
38 #endif
39
40 #include <Xm/Xm.h>
41 #include <X11/Xlib.h>
42 #include <X11/Xutil.h>
43 #include <X11/Xresource.h>
44 #include <X11/Xatom.h>
45
46 #include "wx/motif/private.h"
47
48 #include <string.h>
49
50 extern char *wxBuffer;
51 extern wxList wxPendingDelete;
52
53 wxApp *wxTheApp = NULL;
54
55 wxHashTable *wxWidgetHashTable = NULL;
56
57 #if !USE_SHARED_LIBRARY
58 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
59
60 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
61 EVT_IDLE(wxApp::OnIdle)
62 END_EVENT_TABLE()
63 #endif
64
65 long wxApp::sm_lastMessageTime = 0;
66
67 bool wxApp::Initialize()
68 {
69 wxBuffer = new char[BUFSIZ + 512];
70
71 wxClassInfo::InitializeClasses();
72
73 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
74 wxTheColourDatabase->Initialize();
75
76 wxInitializeStockLists();
77 wxInitializeStockObjects();
78
79 #if wxUSE_WX_RESOURCES
80 wxInitializeResourceSystem();
81 #endif
82
83 // For PostScript printing
84 #if wxUSE_POSTSCRIPT
85 /* Done using wxModule now
86 wxInitializePrintSetupData();
87 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
88 wxThePrintPaperDatabase->CreateDatabase();
89 */
90 #endif
91
92 wxBitmap::InitStandardHandlers();
93
94 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
95
96 wxModule::RegisterModules();
97 if (!wxModule::InitializeModules()) return FALSE;
98
99 return TRUE;
100 }
101
102 void wxApp::CleanUp()
103 {
104 delete wxWidgetHashTable;
105 wxWidgetHashTable = NULL;
106
107 wxModule::CleanUpModules();
108
109 #if wxUSE_WX_RESOURCES
110 wxCleanUpResourceSystem();
111 #endif
112
113 wxDeleteStockObjects() ;
114
115 // Destroy all GDI lists, etc.
116
117 delete wxTheBrushList;
118 wxTheBrushList = NULL;
119
120 delete wxThePenList;
121 wxThePenList = NULL;
122
123 delete wxTheFontList;
124 wxTheFontList = NULL;
125
126 delete wxTheBitmapList;
127 wxTheBitmapList = NULL;
128
129 delete wxTheColourDatabase;
130 wxTheColourDatabase = NULL;
131
132 #if wxUSE_POSTSCRIPT
133 /* Done using wxModule now
134 wxInitializePrintSetupData(FALSE);
135 delete wxThePrintPaperDatabase;
136 wxThePrintPaperDatabase = NULL;
137 */
138 #endif
139
140 wxBitmap::CleanUpHandlers();
141
142 delete[] wxBuffer;
143 wxBuffer = NULL;
144
145 wxClassInfo::CleanUpClasses();
146
147 delete wxTheApp;
148 wxTheApp = NULL;
149
150 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
151 // At this point we want to check if there are any memory
152 // blocks that aren't part of the wxDebugContext itself,
153 // as a special case. Then when dumping we need to ignore
154 // wxDebugContext, too.
155 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
156 {
157 wxLogDebug("There were memory leaks.\n");
158 wxDebugContext::Dump();
159 wxDebugContext::PrintStatistics();
160 }
161 #endif
162
163 // do it as the very last thing because everything else can log messages
164 wxLog::DontCreateOnDemand();
165 // do it as the very last thing because everything else can log messages
166 delete wxLog::SetActiveTarget(NULL);
167 }
168
169 int wxEntry( int argc, char *argv[] )
170 {
171 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
172 // This seems to be necessary since there are 'rogue'
173 // objects present at this point (perhaps global objects?)
174 // Setting a checkpoint will ignore them as far as the
175 // memory checking facility is concerned.
176 // Of course you may argue that memory allocated in globals should be
177 // checked, but this is a reasonable compromise.
178 wxDebugContext::SetCheckpoint();
179 #endif
180
181 if (!wxApp::Initialize())
182 return FALSE;
183
184 if (!wxTheApp)
185 {
186 if (!wxApp::GetInitializerFunction())
187 {
188 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
189 return 0;
190 };
191
192 wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) ();
193 };
194
195 if (!wxTheApp)
196 {
197 printf( "wxWindows error: wxTheApp == NULL\n" );
198 return 0;
199 };
200
201 wxTheApp->SetClassName(wxFileNameFromPath(argv[0]));
202 wxTheApp->SetAppName(wxFileNameFromPath(argv[0]));
203
204 wxTheApp->argc = argc;
205 wxTheApp->argv = argv;
206
207 // GUI-specific initialization, such as creating an app context.
208 wxTheApp->OnInitGui();
209
210 // Here frames insert themselves automatically
211 // into wxTopLevelWindows by getting created
212 // in OnInit().
213
214 int retValue = 0;
215 if (wxTheApp->OnInit())
216 {
217 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
218 }
219
220 // flush the logged messages if any
221 wxLog *pLog = wxLog::GetActiveTarget();
222 if ( pLog != NULL && pLog->HasPendingMessages() )
223 pLog->Flush();
224
225 delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used
226 // for further messages
227
228 if (wxTheApp->GetTopWindow())
229 {
230 delete wxTheApp->GetTopWindow();
231 wxTheApp->SetTopWindow(NULL);
232 }
233
234 wxTheApp->DeletePendingObjects();
235
236 wxTheApp->OnExit();
237
238 wxApp::CleanUp();
239
240 return retValue;
241 };
242
243 // Static member initialization
244 wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
245
246 wxApp::wxApp()
247 {
248 m_topWindow = NULL;
249 wxTheApp = this;
250 m_className = "";
251 m_wantDebugOutput = TRUE ;
252 m_appName = "";
253 argc = 0;
254 argv = NULL;
255 m_printMode = wxPRINT_POSTSCRIPT;
256 m_exitOnFrameDelete = TRUE;
257 m_auto3D = TRUE;
258
259 m_mainColormap = (WXColormap) NULL;
260 m_appContext = (WXAppContext) NULL;
261 m_topLevelWidget = (WXWidget) NULL;
262 m_maxRequestSize = 0;
263 m_initialDisplay = (WXDisplay*) 0;
264 }
265
266 bool wxApp::Initialized()
267 {
268 if (GetTopWindow())
269 return TRUE;
270 else
271 return FALSE;
272 }
273
274 int wxApp::MainLoop()
275 {
276 m_keepGoing = TRUE;
277
278 /*
279 * Sit around forever waiting to process X-events. Property Change
280 * event are handled special, because they have to refer to
281 * the root window rather than to a widget. therefore we can't
282 * use an Xt-eventhandler.
283 */
284
285 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
286 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
287 PropertyChangeMask);
288
289 XEvent event;
290
291 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
292 while (m_keepGoing)
293 {
294 XtAppNextEvent( (XtAppContext) wxTheApp->GetAppContext(), &event);
295
296 ProcessXEvent((WXEvent*) & event);
297
298 if (XtAppPending( (XtAppContext) wxTheApp->GetAppContext() ) == 0)
299 {
300 if (!ProcessIdle())
301 {
302 #if wxUSE_THREADS
303 // leave the main loop to give other threads a chance to
304 // perform their GUI work
305 wxMutexGuiLeave();
306 wxUsleep(20);
307 wxMutexGuiEnter();
308 #endif
309 }
310 }
311
312 }
313
314 return 0;
315 }
316
317 // Processes an X event.
318 void wxApp::ProcessXEvent(WXEvent* _event)
319 {
320 XEvent* event = (XEvent*) _event;
321
322 if ((event->type == KeyPress) && CheckForAccelerator(_event))
323 {
324 // Do nothing! We intercepted and processed the event as an accelerator.
325 return;
326 }
327 else if (event->type == PropertyNotify)
328 {
329 HandlePropertyChange(_event);
330 return;
331 }
332 else if (event->type == ResizeRequest)
333 {
334 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
335 * If resize event, don't resize until the last resize event for this
336 * window is recieved. Prevents flicker as windows are resized.
337 */
338
339 Display *disp = XtDisplay((Widget) wxTheApp->GetTopLevelWidget());
340 Window win = event->xany.window;
341 XEvent report;
342
343 // to avoid flicker
344 report = * event;
345 while( XCheckTypedWindowEvent (disp, win, ResizeRequest, &report));
346
347 // TODO: when implementing refresh optimization, we can use
348 // XtAddExposureToRegion to expand the window's paint region.
349
350 XtDispatchEvent(event);
351 }
352 else
353 {
354 XtDispatchEvent(event);
355 }
356 }
357
358 // Returns TRUE if more time is needed.
359 bool wxApp::ProcessIdle()
360 {
361 wxIdleEvent event;
362 event.SetEventObject(this);
363 ProcessEvent(event);
364
365 return event.MoreRequested();
366 }
367
368 void wxApp::ExitMainLoop()
369 {
370 m_keepGoing = FALSE;
371 }
372
373 // Is a message/event pending?
374 bool wxApp::Pending()
375 {
376 XFlush(XtDisplay( (Widget) wxTheApp->GetTopLevelWidget() ));
377
378 // Fix by Doug from STI, to prevent a stall if non-X event
379 // is found.
380 return ((XtAppPending( (XtAppContext) GetAppContext() ) & XtIMXEvent) != 0) ;
381 }
382
383 // Dispatch a message.
384 void wxApp::Dispatch()
385 {
386 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
387
388 XEvent event;
389 XtAppNextEvent((XtAppContext) GetAppContext(), &event);
390 ProcessXEvent((WXEvent*) & event);
391 }
392
393 // This should be redefined in a derived class for
394 // handling property change events for XAtom IPC.
395 void wxApp::HandlePropertyChange(WXEvent *event)
396 {
397 // by default do nothing special
398 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
399 }
400
401 void wxApp::OnIdle(wxIdleEvent& event)
402 {
403 static bool inOnIdle = FALSE;
404
405 // Avoid recursion (via ProcessEvent default case)
406 if (inOnIdle)
407 return;
408
409 inOnIdle = TRUE;
410
411 // 'Garbage' collection of windows deleted with Close().
412 DeletePendingObjects();
413
414 // flush the logged messages if any
415 wxLog *pLog = wxLog::GetActiveTarget();
416 if ( pLog != NULL && pLog->HasPendingMessages() )
417 pLog->Flush();
418
419 // Send OnIdle events to all windows
420 bool needMore = SendIdleEvents();
421
422 if (needMore)
423 event.RequestMore(TRUE);
424
425 inOnIdle = FALSE;
426 }
427
428 // Send idle event to all top-level windows
429 bool wxApp::SendIdleEvents()
430 {
431 bool needMore = FALSE;
432 wxNode* node = wxTopLevelWindows.First();
433 while (node)
434 {
435 wxWindow* win = (wxWindow*) node->Data();
436 if (SendIdleEvents(win))
437 needMore = TRUE;
438
439 node = node->Next();
440 }
441 return needMore;
442 }
443
444 // Send idle event to window and all subwindows
445 bool wxApp::SendIdleEvents(wxWindow* win)
446 {
447 bool needMore = FALSE;
448
449 wxIdleEvent event;
450 event.SetEventObject(win);
451 win->ProcessEvent(event);
452
453 if (event.MoreRequested())
454 needMore = TRUE;
455
456 wxNode* node = win->GetChildren().First();
457 while (node)
458 {
459 wxWindow* win = (wxWindow*) node->Data();
460 if (SendIdleEvents(win))
461 needMore = TRUE;
462
463 node = node->Next();
464 }
465 return needMore ;
466 }
467
468 void wxApp::DeletePendingObjects()
469 {
470 wxNode *node = wxPendingDelete.First();
471 while (node)
472 {
473 wxObject *obj = (wxObject *)node->Data();
474
475 delete obj;
476
477 if (wxPendingDelete.Member(obj))
478 delete node;
479
480 // Deleting one object may have deleted other pending
481 // objects, so start from beginning of list again.
482 node = wxPendingDelete.First();
483 }
484 }
485
486 wxLog* wxApp::CreateLogTarget()
487 {
488 return new wxLogGui;
489 }
490
491 wxWindow* wxApp::GetTopWindow() const
492 {
493 if (m_topWindow)
494 return m_topWindow;
495 else if (wxTopLevelWindows.Number() > 0)
496 return (wxWindow*) wxTopLevelWindows.First()->Data();
497 else
498 return NULL;
499 }
500
501 // Create an application context
502 bool wxApp::OnInitGui()
503 {
504 XtToolkitInitialize() ;
505 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext() ;
506 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
507 (const char*) wxTheApp->GetClassName(), NULL, 0,
508 # if XtSpecificationRelease < 5
509 (Cardinal*) &argc,
510 # else
511 &argc,
512 # endif
513 argv);
514
515 if (!dpy) {
516 wxString className(wxTheApp->GetClassName());
517 wxLogError(_("wxWindows could not open display for '%s': exiting."),
518 (const char*) className);
519 exit(-1);
520 }
521 m_initialDisplay = (WXDisplay*) dpy;
522
523 wxTheApp->m_topLevelWidget = (WXWidget) XtAppCreateShell((String)NULL, (const char*) wxTheApp->GetClassName(),
524 applicationShellWidgetClass,dpy,
525 NULL,0) ;
526
527 // Add general resize proc
528 XtActionsRec rec;
529 rec.string = "resize";
530 rec.proc = (XtActionProc)wxWidgetResizeProc;
531 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
532
533 GetMainColormap(dpy);
534 m_maxRequestSize = XMaxRequestSize((Display*) dpy);
535
536 return TRUE;
537 }
538
539 WXColormap wxApp::GetMainColormap(WXDisplay* display)
540 {
541 if (!display) /* Must be called first with non-NULL display */
542 return m_mainColormap;
543
544 int defaultScreen = DefaultScreen((Display*) display);
545 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
546
547 Colormap c = DefaultColormapOfScreen(screen);
548
549 if (!m_mainColormap)
550 m_mainColormap = (WXColormap) c;
551
552 return (WXColormap) c;
553 }
554
555 // Returns TRUE if an accelerator has been processed
556 bool wxApp::CheckForAccelerator(WXEvent* event)
557 {
558 XEvent* xEvent = (XEvent*) event;
559 if (xEvent->xany.type == KeyPress)
560 {
561 // Find a wxWindow for this window
562 // TODO: should get display for the window, not the current display
563 Widget widget = XtWindowToWidget((Display*) wxGetDisplay(), xEvent->xany.window);
564 wxWindow* win = NULL;
565
566 // Find the first wxWindow that corresponds to this event window
567 while (widget && !(win = wxGetWindowFromTable(widget)))
568 widget = XtParent(widget);
569
570 if (!widget || !win)
571 return FALSE;
572
573 wxKeyEvent keyEvent(wxEVT_CHAR);
574 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent);
575
576 // Now we have a wxKeyEvent and we have a wxWindow.
577 // Go up the hierarchy until we find a matching accelerator,
578 // or we get to the top.
579 while (win)
580 {
581 if (win->ProcessAccelerator(keyEvent))
582 return TRUE;
583 win = win->GetParent();
584 }
585 return FALSE;
586 }
587 return FALSE;
588 }
589
590 void wxExit()
591 {
592 int retValue = 0;
593 if (wxTheApp)
594 retValue = wxTheApp->OnExit();
595
596 wxApp::CleanUp();
597 /*
598 * Exit in some platform-specific way. Not recommended that the app calls this:
599 * only for emergencies.
600 */
601 exit(retValue);
602 }
603
604 // Yield to other processes
605 bool wxYield()
606 {
607 while (wxTheApp && wxTheApp->Pending())
608 wxTheApp->Dispatch();
609
610 // VZ: is it the same as this (taken from old wxExecute)?
611 #if 0
612 XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
613 #endif
614
615 return TRUE;
616 }
617