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