1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.h"
14 #pragma implementation "appbase.h"
20 #include "wx/gdicmn.h"
23 #include "wx/cursor.h"
25 #include "wx/palette.h"
27 #include "wx/dialog.h"
28 #include "wx/msgdlg.h"
30 #include "wx/module.h"
31 #include "wx/memory.h"
36 #include "wx/thread.h"
39 #if wxUSE_WX_RESOURCES
40 #include "wx/resource.h"
45 #include <X11/Xutil.h>
46 #include <X11/Xresource.h>
47 #include <X11/Xatom.h>
49 #include "wx/motif/private.h"
53 extern char *wxBuffer
;
54 extern wxList wxPendingDelete
;
57 extern wxList
*wxPendingEvents
;
58 extern wxCriticalSection
*wxPendingEventsLocker
;
59 #endif // wxUSE_THREADS
61 wxApp
*wxTheApp
= NULL
;
63 wxHashTable
*wxWidgetHashTable
= NULL
;
65 #if !USE_SHARED_LIBRARY
66 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
68 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
69 EVT_IDLE(wxApp::OnIdle
)
73 long wxApp::sm_lastMessageTime
= 0;
75 bool wxApp::Initialize()
77 wxBuffer
= new char[BUFSIZ
+ 512];
79 wxClassInfo::InitializeClasses();
81 // GL: I'm annoyed ... I don't know where to put this and I don't want to
82 // create a module for that as it's part of the core.
84 wxPendingEvents
= new wxList();
85 wxPendingEventsLocker
= new wxCriticalSection();
88 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
89 wxTheColourDatabase
->Initialize();
91 wxInitializeStockLists();
92 wxInitializeStockObjects();
94 #if wxUSE_WX_RESOURCES
95 wxInitializeResourceSystem();
98 // For PostScript printing
100 /* Done using wxModule now
101 wxInitializePrintSetupData();
102 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
103 wxThePrintPaperDatabase->CreateDatabase();
107 wxBitmap::InitStandardHandlers();
109 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
111 wxModule::RegisterModules();
112 if (!wxModule::InitializeModules()) return FALSE
;
117 void wxApp::CleanUp()
119 delete wxWidgetHashTable
;
120 wxWidgetHashTable
= NULL
;
122 wxModule::CleanUpModules();
124 #if wxUSE_WX_RESOURCES
125 wxCleanUpResourceSystem();
128 wxDeleteStockObjects() ;
130 // Destroy all GDI lists, etc.
132 delete wxTheBrushList
;
133 wxTheBrushList
= NULL
;
138 delete wxTheFontList
;
139 wxTheFontList
= NULL
;
141 delete wxTheBitmapList
;
142 wxTheBitmapList
= NULL
;
144 delete wxTheColourDatabase
;
145 wxTheColourDatabase
= NULL
;
148 /* Done using wxModule now
149 wxInitializePrintSetupData(FALSE);
150 delete wxThePrintPaperDatabase;
151 wxThePrintPaperDatabase = NULL;
155 wxBitmap::CleanUpHandlers();
160 wxClassInfo::CleanUpClasses();
165 // GL: I'm annoyed ... I don't know where to put this and I don't want to
166 // create a module for that as it's part of the core.
168 delete wxPendingEvents
;
169 delete wxPendingEventsLocker
;
172 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
173 // At this point we want to check if there are any memory
174 // blocks that aren't part of the wxDebugContext itself,
175 // as a special case. Then when dumping we need to ignore
176 // wxDebugContext, too.
177 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
179 wxLogDebug("There were memory leaks.\n");
180 wxDebugContext::Dump();
181 wxDebugContext::PrintStatistics();
185 // do it as the very last thing because everything else can log messages
186 wxLog::DontCreateOnDemand();
187 // do it as the very last thing because everything else can log messages
188 delete wxLog::SetActiveTarget(NULL
);
191 int wxEntry( int argc
, char *argv
[] )
193 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
194 // This seems to be necessary since there are 'rogue'
195 // objects present at this point (perhaps global objects?)
196 // Setting a checkpoint will ignore them as far as the
197 // memory checking facility is concerned.
198 // Of course you may argue that memory allocated in globals should be
199 // checked, but this is a reasonable compromise.
200 wxDebugContext::SetCheckpoint();
203 if (!wxApp::Initialize())
208 if (!wxApp::GetInitializerFunction())
210 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
214 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
219 printf( "wxWindows error: wxTheApp == NULL\n" );
223 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
224 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
226 wxTheApp
->argc
= argc
;
227 wxTheApp
->argv
= argv
;
229 // GUI-specific initialization, such as creating an app context.
230 wxTheApp
->OnInitGui();
232 // Here frames insert themselves automatically into wxTopLevelWindows by
233 // getting created in OnInit().
236 if (wxTheApp
->OnInit())
238 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
241 // flush the logged messages if any
242 wxLog
*pLog
= wxLog::GetActiveTarget();
243 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
246 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
247 // for further messages
249 if (wxTheApp
->GetTopWindow())
251 delete wxTheApp
->GetTopWindow();
252 wxTheApp
->SetTopWindow(NULL
);
255 wxTheApp
->DeletePendingObjects();
264 // Static member initialization
265 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
272 m_wantDebugOutput
= TRUE
;
276 m_exitOnFrameDelete
= TRUE
;
278 m_mainColormap
= (WXColormap
) NULL
;
279 m_appContext
= (WXAppContext
) NULL
;
280 m_topLevelWidget
= (WXWidget
) NULL
;
281 m_maxRequestSize
= 0;
282 m_initialDisplay
= (WXDisplay
*) 0;
285 bool wxApp::Initialized()
293 int wxApp::MainLoop()
298 * Sit around forever waiting to process X-events. Property Change
299 * event are handled special, because they have to refer to
300 * the root window rather than to a widget. therefore we can't
301 * use an Xt-eventhandler.
304 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
305 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
310 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
313 XtAppNextEvent( (XtAppContext
) wxTheApp
->GetAppContext(), &event
);
315 ProcessXEvent((WXEvent
*) & event
);
317 if (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) == 0)
322 // leave the main loop to give other threads a chance to
323 // perform their GUI work
336 // Processes an X event.
337 void wxApp::ProcessXEvent(WXEvent
* _event
)
339 XEvent
* event
= (XEvent
*) _event
;
341 if (event
->type
== KeyPress
)
344 Widget widget
= XtWindowToWidget(event
->xany
.display
, event
->xany
.window
);
345 wxLogDebug("Got key press event for 0x%08x (parent = 0x%08x)",
346 widget
, XtParent(widget
));
349 if (CheckForAccelerator(_event
))
351 // Do nothing! We intercepted and processed the event as an
356 // It seemed before that this hack was redundant and
357 // key down events were being generated by wxCanvasInputEvent.
358 // But no longer - why ???
360 else if (CheckForKeyDown(_event
))
362 // We intercepted and processed the key down event
368 XtDispatchEvent(event
);
372 else if (event
->type
== KeyRelease
)
374 // TODO: work out why we still need this ! -michael
376 if (CheckForKeyUp(_event
))
378 // We intercepted and processed the key up event
383 XtDispatchEvent(event
);
387 else if (event
->type
== PropertyNotify
)
389 HandlePropertyChange(_event
);
392 else if (event
->type
== ResizeRequest
)
394 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
395 * If resize event, don't resize until the last resize event for this
396 * window is recieved. Prevents flicker as windows are resized.
399 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
400 Window win
= event
->xany
.window
;
405 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
407 // TODO: when implementing refresh optimization, we can use
408 // XtAddExposureToRegion to expand the window's paint region.
410 XtDispatchEvent(event
);
414 XtDispatchEvent(event
);
418 // Returns TRUE if more time is needed.
419 bool wxApp::ProcessIdle()
422 event
.SetEventObject(this);
425 return event
.MoreRequested();
428 void wxApp::ExitMainLoop()
433 // Is a message/event pending?
434 bool wxApp::Pending()
436 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
438 // Fix by Doug from STI, to prevent a stall if non-X event
440 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
443 // Dispatch a message.
444 void wxApp::Dispatch()
446 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
449 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
450 ProcessXEvent((WXEvent
*) & event
);
453 // This should be redefined in a derived class for
454 // handling property change events for XAtom IPC.
455 void wxApp::HandlePropertyChange(WXEvent
*event
)
457 // by default do nothing special
458 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
461 void wxApp::OnIdle(wxIdleEvent
& event
)
463 static bool inOnIdle
= FALSE
;
465 // Avoid recursion (via ProcessEvent default case)
471 // 'Garbage' collection of windows deleted with Close().
472 DeletePendingObjects();
475 // Flush pending events.
476 ProcessPendingEvents();
479 // flush the logged messages if any
480 wxLog
*pLog
= wxLog::GetActiveTarget();
481 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
484 // Send OnIdle events to all windows
485 bool needMore
= SendIdleEvents();
488 event
.RequestMore(TRUE
);
493 // Send idle event to all top-level windows
494 bool wxApp::SendIdleEvents()
496 bool needMore
= FALSE
;
498 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
501 wxWindow
* win
= node
->GetData();
502 if (SendIdleEvents(win
))
504 node
= node
->GetNext();
510 // Send idle event to window and all subwindows
511 bool wxApp::SendIdleEvents(wxWindow
* win
)
513 bool needMore
= FALSE
;
516 event
.SetEventObject(win
);
517 win
->ProcessEvent(event
);
519 if (event
.MoreRequested())
522 wxNode
* node
= win
->GetChildren().First();
525 wxWindow
* win
= (wxWindow
*) node
->Data();
526 if (SendIdleEvents(win
))
534 void wxApp::DeletePendingObjects()
536 wxNode
*node
= wxPendingDelete
.First();
539 wxObject
*obj
= (wxObject
*)node
->Data();
543 if (wxPendingDelete
.Member(obj
))
546 // Deleting one object may have deleted other pending
547 // objects, so start from beginning of list again.
548 node
= wxPendingDelete
.First();
553 void wxApp::ProcessPendingEvents()
555 wxNode
*node
= wxPendingEvents
->First();
556 wxCriticalSectionLocker
locker(*wxPendingEventsLocker
);
560 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
562 handler
->ProcessPendingEvents();
565 node
= wxPendingEvents
->First();
568 #endif // wxUSE_THREADS
570 // Create an application context
571 bool wxApp::OnInitGui()
573 XtToolkitInitialize() ;
574 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
575 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
576 (const char*) wxTheApp
->GetClassName(), NULL
, 0,
577 # if XtSpecificationRelease < 5
585 wxString
className(wxTheApp
->GetClassName());
586 wxLogError(_("wxWindows could not open display for '%s': exiting."),
587 (const char*) className
);
590 m_initialDisplay
= (WXDisplay
*) dpy
;
592 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
593 applicationShellWidgetClass
,dpy
,
596 // Add general resize proc
598 rec
.string
= "resize";
599 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
600 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
602 GetMainColormap(dpy
);
603 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
608 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
610 if (!display
) /* Must be called first with non-NULL display */
611 return m_mainColormap
;
613 int defaultScreen
= DefaultScreen((Display
*) display
);
614 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
616 Colormap c
= DefaultColormapOfScreen(screen
);
619 m_mainColormap
= (WXColormap
) c
;
621 return (WXColormap
) c
;
624 // Returns TRUE if an accelerator has been processed
625 bool wxApp::CheckForAccelerator(WXEvent
* event
)
627 XEvent
* xEvent
= (XEvent
*) event
;
628 if (xEvent
->xany
.type
== KeyPress
)
630 // Find a wxWindow for this window
631 // TODO: should get display for the window, not the current display
632 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
633 wxWindow
* win
= NULL
;
635 // Find the first wxWindow that corresponds to this event window
636 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
637 widget
= XtParent(widget
);
642 wxKeyEvent
keyEvent(wxEVT_CHAR
);
643 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
645 // Now we have a wxKeyEvent and we have a wxWindow.
646 // Go up the hierarchy until we find a matching accelerator,
647 // or we get to the top.
650 if (win
->ProcessAccelerator(keyEvent
))
652 win
= win
->GetParent();
659 bool wxApp::CheckForKeyDown(WXEvent
* event
)
661 XEvent
* xEvent
= (XEvent
*) event
;
662 if (xEvent
->xany
.type
== KeyPress
)
664 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
665 xEvent
->xany
.window
);
666 wxWindow
* win
= NULL
;
668 // Find the first wxWindow that corresponds to this event window
669 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
670 widget
= XtParent(widget
);
675 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
676 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
678 return win
->ProcessEvent( keyEvent
);
684 bool wxApp::CheckForKeyUp(WXEvent
* event
)
686 XEvent
* xEvent
= (XEvent
*) event
;
687 if (xEvent
->xany
.type
== KeyRelease
)
689 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
690 xEvent
->xany
.window
);
691 wxWindow
* win
= NULL
;
693 // Find the first wxWindow that corresponds to this event window
694 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
695 widget
= XtParent(widget
);
700 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
701 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
703 return win
->ProcessEvent( keyEvent
);
713 retValue
= wxTheApp
->OnExit();
717 * Exit in some platform-specific way. Not recommended that the app calls this:
718 * only for emergencies.
723 // Yield to other processes
726 while (wxTheApp
&& wxTheApp
->Pending())
727 wxTheApp
->Dispatch();
729 // VZ: is it the same as this (taken from old wxExecute)?
731 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
737 // TODO use XmGetPixmap (?) to get the really standard icons!
739 #include "wx/generic/info.xpm"
740 #include "wx/generic/error.xpm"
741 #include "wx/generic/question.xpm"
742 #include "wx/generic/warning.xpm"
745 wxApp::GetStdIcon(int which
) const
749 case wxICON_INFORMATION
:
750 return wxIcon(info_xpm
);
752 case wxICON_QUESTION
:
753 return wxIcon(question_xpm
);
755 case wxICON_EXCLAMATION
:
756 return wxIcon(warning_xpm
);
759 wxFAIL_MSG("requested non existent standard icon");
760 // still fall through
763 return wxIcon(error_xpm
);