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