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