1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.h"
19 #include "wx/gdicmn.h"
22 #include "wx/cursor.h"
24 #include "wx/palette.h"
26 #include "wx/dialog.h"
27 #include "wx/msgdlg.h"
29 #include "wx/module.h"
30 #include "wx/memory.h"
35 #include "wx/thread.h"
38 #if wxUSE_WX_RESOURCES
39 #include "wx/resource.h"
43 #pragma message disable nosimpint
47 #include <X11/Xutil.h>
48 #include <X11/Xresource.h>
49 #include <X11/Xatom.h>
51 #pragma message enable nosimpint
54 #include "wx/motif/private.h"
58 extern char *wxBuffer
;
59 extern wxList wxPendingDelete
;
61 wxApp
*wxTheApp
= NULL
;
63 wxHashTable
*wxWidgetHashTable
= NULL
;
65 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
67 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
68 EVT_IDLE(wxApp::OnIdle
)
72 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
74 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
76 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
78 // just forward to the default handler for now
79 return gs_pfnXErrorHandler(dpy
, xevent
);
83 long wxApp::sm_lastMessageTime
= 0;
85 bool wxApp::Initialize()
87 wxBuffer
= new char[BUFSIZ
+ 512];
89 wxClassInfo::InitializeClasses();
91 // GL: I'm annoyed ... I don't know where to put this and I don't want to
92 // create a module for that as it's part of the core.
94 wxPendingEventsLocker
= new wxCriticalSection();
97 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
98 wxTheColourDatabase
->Initialize();
100 wxInitializeStockLists();
101 wxInitializeStockObjects();
103 #if wxUSE_WX_RESOURCES
104 wxInitializeResourceSystem();
107 // For PostScript printing
109 /* Done using wxModule now
110 wxInitializePrintSetupData();
111 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
112 wxThePrintPaperDatabase->CreateDatabase();
116 wxBitmap::InitStandardHandlers();
118 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
120 wxModule::RegisterModules();
121 if (!wxModule::InitializeModules()) return FALSE
;
126 void wxApp::CleanUp()
128 delete wxWidgetHashTable
;
129 wxWidgetHashTable
= NULL
;
131 wxModule::CleanUpModules();
133 #if wxUSE_WX_RESOURCES
134 wxCleanUpResourceSystem();
137 wxDeleteStockObjects() ;
139 // Destroy all GDI lists, etc.
141 delete wxTheBrushList
;
142 wxTheBrushList
= NULL
;
147 delete wxTheFontList
;
148 wxTheFontList
= NULL
;
150 delete wxTheBitmapList
;
151 wxTheBitmapList
= NULL
;
153 delete wxTheColourDatabase
;
154 wxTheColourDatabase
= NULL
;
157 /* Done using wxModule now
158 wxInitializePrintSetupData(FALSE);
159 delete wxThePrintPaperDatabase;
160 wxThePrintPaperDatabase = NULL;
164 wxBitmap::CleanUpHandlers();
169 wxClassInfo::CleanUpClasses();
174 // GL: I'm annoyed ... I don't know where to put this and I don't want to
175 // create a module for that as it's part of the core.
177 delete wxPendingEvents
;
178 delete wxPendingEventsLocker
;
181 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
182 // At this point we want to check if there are any memory
183 // blocks that aren't part of the wxDebugContext itself,
184 // as a special case. Then when dumping we need to ignore
185 // wxDebugContext, too.
186 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
188 wxLogDebug("There were memory leaks.\n");
189 wxDebugContext::Dump();
190 wxDebugContext::PrintStatistics();
194 // do it as the very last thing because everything else can log messages
195 wxLog::DontCreateOnDemand();
196 // do it as the very last thing because everything else can log messages
197 delete wxLog::SetActiveTarget(NULL
);
200 int wxEntry( int argc
, char *argv
[] )
202 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
203 // This seems to be necessary since there are 'rogue'
204 // objects present at this point (perhaps global objects?)
205 // Setting a checkpoint will ignore them as far as the
206 // memory checking facility is concerned.
207 // Of course you may argue that memory allocated in globals should be
208 // checked, but this is a reasonable compromise.
209 wxDebugContext::SetCheckpoint();
212 if (!wxApp::Initialize())
217 if (!wxApp::GetInitializerFunction())
219 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
223 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
228 printf( "wxWindows error: wxTheApp == NULL\n" );
232 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
233 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
235 wxTheApp
->argc
= argc
;
236 wxTheApp
->argv
= argv
;
238 // GUI-specific initialization, such as creating an app context.
239 wxTheApp
->OnInitGui();
241 // Here frames insert themselves automatically into wxTopLevelWindows by
242 // getting created in OnInit().
245 if (wxTheApp
->OnInit())
247 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
250 // flush the logged messages if any
251 wxLog
*pLog
= wxLog::GetActiveTarget();
252 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
255 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
256 // for further messages
258 if (wxTheApp
->GetTopWindow())
260 delete wxTheApp
->GetTopWindow();
261 wxTheApp
->SetTopWindow(NULL
);
264 wxTheApp
->DeletePendingObjects();
273 // Static member initialization
274 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
281 m_wantDebugOutput
= TRUE
;
285 m_exitOnFrameDelete
= TRUE
;
287 m_mainColormap
= (WXColormap
) NULL
;
288 m_appContext
= (WXAppContext
) NULL
;
289 m_topLevelWidget
= (WXWidget
) NULL
;
290 m_maxRequestSize
= 0;
291 m_initialDisplay
= (WXDisplay
*) 0;
294 bool wxApp::Initialized()
302 int wxApp::MainLoop()
307 * Sit around forever waiting to process X-events. Property Change
308 * event are handled special, because they have to refer to
309 * the root window rather than to a widget. therefore we can't
310 * use an Xt-eventhandler.
313 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
314 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
319 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
322 XtAppNextEvent( (XtAppContext
) wxTheApp
->GetAppContext(), &event
);
324 ProcessXEvent((WXEvent
*) & event
);
326 if (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) == 0)
331 // leave the main loop to give other threads a chance to
332 // perform their GUI work
345 // Processes an X event.
346 void wxApp::ProcessXEvent(WXEvent
* _event
)
348 XEvent
* event
= (XEvent
*) _event
;
350 if (event
->type
== KeyPress
)
352 #if 0 // def __WXDEBUG__
353 Widget widget
= XtWindowToWidget(event
->xany
.display
, event
->xany
.window
);
354 wxLogDebug("Got key press event for 0x%08x (parent = 0x%08x)",
355 widget
, XtParent(widget
));
358 if (CheckForAccelerator(_event
))
360 // Do nothing! We intercepted and processed the event as an
365 // It seemed before that this hack was redundant and
366 // key down events were being generated by wxCanvasInputEvent.
367 // But no longer - why ???
369 else if (CheckForKeyDown(_event
))
371 // We intercepted and processed the key down event
377 XtDispatchEvent(event
);
381 else if (event
->type
== KeyRelease
)
383 // TODO: work out why we still need this ! -michael
385 if (CheckForKeyUp(_event
))
387 // We intercepted and processed the key up event
392 XtDispatchEvent(event
);
396 else if (event
->type
== PropertyNotify
)
398 HandlePropertyChange(_event
);
401 else if (event
->type
== ResizeRequest
)
403 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
404 * If resize event, don't resize until the last resize event for this
405 * window is recieved. Prevents flicker as windows are resized.
408 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
409 Window win
= event
->xany
.window
;
414 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
416 // TODO: when implementing refresh optimization, we can use
417 // XtAddExposureToRegion to expand the window's paint region.
419 XtDispatchEvent(event
);
423 XtDispatchEvent(event
);
427 // Returns TRUE if more time is needed.
428 bool wxApp::ProcessIdle()
431 event
.SetEventObject(this);
434 return event
.MoreRequested();
437 void wxApp::ExitMainLoop()
442 // Is a message/event pending?
443 bool wxApp::Pending()
445 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
447 // Fix by Doug from STI, to prevent a stall if non-X event
449 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
452 // Dispatch a message.
453 void wxApp::Dispatch()
455 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
458 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
459 ProcessXEvent((WXEvent
*) & event
);
462 // This should be redefined in a derived class for
463 // handling property change events for XAtom IPC.
464 void wxApp::HandlePropertyChange(WXEvent
*event
)
466 // by default do nothing special
467 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
470 void wxApp::OnIdle(wxIdleEvent
& event
)
472 static bool inOnIdle
= FALSE
;
474 // Avoid recursion (via ProcessEvent default case)
480 // 'Garbage' collection of windows deleted with Close().
481 DeletePendingObjects();
484 // Flush pending events.
485 ProcessPendingEvents();
488 // flush the logged messages if any
489 wxLog
*pLog
= wxLog::GetActiveTarget();
490 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
493 // Send OnIdle events to all windows
494 bool needMore
= SendIdleEvents();
497 event
.RequestMore(TRUE
);
504 // **** please implement me! ****
505 // Wake up the idle handler processor, even if it is in another thread...
509 // Send idle event to all top-level windows
510 bool wxApp::SendIdleEvents()
512 bool needMore
= FALSE
;
514 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
517 wxWindow
* win
= node
->GetData();
518 if (SendIdleEvents(win
))
520 node
= node
->GetNext();
526 // Send idle event to window and all subwindows
527 bool wxApp::SendIdleEvents(wxWindow
* win
)
529 bool needMore
= FALSE
;
532 event
.SetEventObject(win
);
533 win
->ProcessEvent(event
);
535 if (event
.MoreRequested())
538 wxNode
* node
= win
->GetChildren().First();
541 wxWindow
* win
= (wxWindow
*) node
->Data();
542 if (SendIdleEvents(win
))
550 void wxApp::DeletePendingObjects()
552 wxNode
*node
= wxPendingDelete
.First();
555 wxObject
*obj
= (wxObject
*)node
->Data();
559 if (wxPendingDelete
.Member(obj
))
562 // Deleting one object may have deleted other pending
563 // objects, so start from beginning of list again.
564 node
= wxPendingDelete
.First();
568 // Create an application context
569 bool wxApp::OnInitGui()
571 XtToolkitInitialize() ;
572 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
573 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
574 (const char*) wxTheApp
->GetClassName(), NULL
, 0,
575 # if XtSpecificationRelease < 5
583 wxString
className(wxTheApp
->GetClassName());
584 wxLogError(_("wxWindows could not open display for '%s': exiting."),
585 (const char*) className
);
588 m_initialDisplay
= (WXDisplay
*) dpy
;
591 // install the X error handler
592 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
593 #endif // __WXDEBUG__
595 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
596 applicationShellWidgetClass
,dpy
,
599 // Add general resize proc
601 rec
.string
= "resize";
602 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
603 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
605 GetMainColormap(dpy
);
606 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
611 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
613 if (!display
) /* Must be called first with non-NULL display */
614 return m_mainColormap
;
616 int defaultScreen
= DefaultScreen((Display
*) display
);
617 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
619 Colormap c
= DefaultColormapOfScreen(screen
);
622 m_mainColormap
= (WXColormap
) c
;
624 return (WXColormap
) c
;
627 // Returns TRUE if an accelerator has been processed
628 bool wxApp::CheckForAccelerator(WXEvent
* event
)
630 XEvent
* xEvent
= (XEvent
*) event
;
631 if (xEvent
->xany
.type
== KeyPress
)
633 // Find a wxWindow for this window
634 // TODO: should get display for the window, not the current display
635 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
636 wxWindow
* win
= NULL
;
638 // Find the first wxWindow that corresponds to this event window
639 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
640 widget
= XtParent(widget
);
645 wxKeyEvent
keyEvent(wxEVT_CHAR
);
646 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
648 // Now we have a wxKeyEvent and we have a wxWindow.
649 // Go up the hierarchy until we find a matching accelerator,
650 // or we get to the top.
653 if (win
->ProcessAccelerator(keyEvent
))
655 win
= win
->GetParent();
662 bool wxApp::CheckForKeyDown(WXEvent
* event
)
664 XEvent
* xEvent
= (XEvent
*) event
;
665 if (xEvent
->xany
.type
== KeyPress
)
667 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
668 xEvent
->xany
.window
);
669 wxWindow
* win
= NULL
;
671 // Find the first wxWindow that corresponds to this event window
672 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
673 widget
= XtParent(widget
);
678 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
679 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
681 return win
->ProcessEvent( keyEvent
);
687 bool wxApp::CheckForKeyUp(WXEvent
* event
)
689 XEvent
* xEvent
= (XEvent
*) event
;
690 if (xEvent
->xany
.type
== KeyRelease
)
692 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
693 xEvent
->xany
.window
);
694 wxWindow
* win
= NULL
;
696 // Find the first wxWindow that corresponds to this event window
697 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
698 widget
= XtParent(widget
);
703 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
704 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
706 return win
->ProcessEvent( keyEvent
);
716 retValue
= wxTheApp
->OnExit();
720 * Exit in some platform-specific way. Not recommended that the app calls this:
721 * only for emergencies.
726 // Yield to other processes
729 while (wxTheApp
&& wxTheApp
->Pending())
730 wxTheApp
->Dispatch();
732 // VZ: is it the same as this (taken from old wxExecute)?
734 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
740 // TODO use XmGetPixmap (?) to get the really standard icons!
742 #include "wx/generic/info.xpm"
743 #include "wx/generic/error.xpm"
744 #include "wx/generic/question.xpm"
745 #include "wx/generic/warning.xpm"
748 wxApp::GetStdIcon(int which
) const
752 case wxICON_INFORMATION
:
753 return wxIcon(info_xpm
);
755 case wxICON_QUESTION
:
756 return wxIcon(question_xpm
);
758 case wxICON_EXCLAMATION
:
759 return wxIcon(warning_xpm
);
762 wxFAIL_MSG("requested non existent standard icon");
763 // still fall through
766 return wxIcon(error_xpm
);
770 // ----------------------------------------------------------------------------
771 // accessors for C modules
772 // ----------------------------------------------------------------------------
774 extern "C" XtAppContext
wxGetAppContext()
776 return (XtAppContext
)wxTheApp
->GetAppContext();