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"
44 #include <X11/Xutil.h>
45 #include <X11/Xresource.h>
46 #include <X11/Xatom.h>
48 #include "wx/motif/private.h"
52 extern char *wxBuffer
;
53 extern wxList wxPendingDelete
;
55 wxApp
*wxTheApp
= NULL
;
57 wxHashTable
*wxWidgetHashTable
= NULL
;
59 #if !USE_SHARED_LIBRARY
60 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
62 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
63 EVT_IDLE(wxApp::OnIdle
)
67 long wxApp::sm_lastMessageTime
= 0;
69 bool wxApp::Initialize()
71 wxBuffer
= new char[BUFSIZ
+ 512];
73 wxClassInfo::InitializeClasses();
75 // GL: I'm annoyed ... I don't know where to put this and I don't want to
76 // create a module for that as it's part of the core.
78 wxPendingEventsLocker
= new wxCriticalSection();
81 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
82 wxTheColourDatabase
->Initialize();
84 wxInitializeStockLists();
85 wxInitializeStockObjects();
87 #if wxUSE_WX_RESOURCES
88 wxInitializeResourceSystem();
91 // For PostScript printing
93 /* Done using wxModule now
94 wxInitializePrintSetupData();
95 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
96 wxThePrintPaperDatabase->CreateDatabase();
100 wxBitmap::InitStandardHandlers();
102 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
104 wxModule::RegisterModules();
105 if (!wxModule::InitializeModules()) return FALSE
;
110 void wxApp::CleanUp()
112 delete wxWidgetHashTable
;
113 wxWidgetHashTable
= NULL
;
115 wxModule::CleanUpModules();
117 #if wxUSE_WX_RESOURCES
118 wxCleanUpResourceSystem();
121 wxDeleteStockObjects() ;
123 // Destroy all GDI lists, etc.
125 delete wxTheBrushList
;
126 wxTheBrushList
= NULL
;
131 delete wxTheFontList
;
132 wxTheFontList
= NULL
;
134 delete wxTheBitmapList
;
135 wxTheBitmapList
= NULL
;
137 delete wxTheColourDatabase
;
138 wxTheColourDatabase
= NULL
;
141 /* Done using wxModule now
142 wxInitializePrintSetupData(FALSE);
143 delete wxThePrintPaperDatabase;
144 wxThePrintPaperDatabase = NULL;
148 wxBitmap::CleanUpHandlers();
153 wxClassInfo::CleanUpClasses();
158 // GL: I'm annoyed ... I don't know where to put this and I don't want to
159 // create a module for that as it's part of the core.
161 delete wxPendingEvents
;
162 delete wxPendingEventsLocker
;
165 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
166 // At this point we want to check if there are any memory
167 // blocks that aren't part of the wxDebugContext itself,
168 // as a special case. Then when dumping we need to ignore
169 // wxDebugContext, too.
170 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
172 wxLogDebug("There were memory leaks.\n");
173 wxDebugContext::Dump();
174 wxDebugContext::PrintStatistics();
178 // do it as the very last thing because everything else can log messages
179 wxLog::DontCreateOnDemand();
180 // do it as the very last thing because everything else can log messages
181 delete wxLog::SetActiveTarget(NULL
);
184 int wxEntry( int argc
, char *argv
[] )
186 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
187 // This seems to be necessary since there are 'rogue'
188 // objects present at this point (perhaps global objects?)
189 // Setting a checkpoint will ignore them as far as the
190 // memory checking facility is concerned.
191 // Of course you may argue that memory allocated in globals should be
192 // checked, but this is a reasonable compromise.
193 wxDebugContext::SetCheckpoint();
196 if (!wxApp::Initialize())
201 if (!wxApp::GetInitializerFunction())
203 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
207 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
212 printf( "wxWindows error: wxTheApp == NULL\n" );
216 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
217 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
219 wxTheApp
->argc
= argc
;
220 wxTheApp
->argv
= argv
;
222 // GUI-specific initialization, such as creating an app context.
223 wxTheApp
->OnInitGui();
225 // Here frames insert themselves automatically into wxTopLevelWindows by
226 // getting created in OnInit().
229 if (wxTheApp
->OnInit())
231 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
234 // flush the logged messages if any
235 wxLog
*pLog
= wxLog::GetActiveTarget();
236 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
239 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
240 // for further messages
242 if (wxTheApp
->GetTopWindow())
244 delete wxTheApp
->GetTopWindow();
245 wxTheApp
->SetTopWindow(NULL
);
248 wxTheApp
->DeletePendingObjects();
257 // Static member initialization
258 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
265 m_wantDebugOutput
= TRUE
;
269 m_exitOnFrameDelete
= TRUE
;
271 m_mainColormap
= (WXColormap
) NULL
;
272 m_appContext
= (WXAppContext
) NULL
;
273 m_topLevelWidget
= (WXWidget
) NULL
;
274 m_maxRequestSize
= 0;
275 m_initialDisplay
= (WXDisplay
*) 0;
278 bool wxApp::Initialized()
286 int wxApp::MainLoop()
291 * Sit around forever waiting to process X-events. Property Change
292 * event are handled special, because they have to refer to
293 * the root window rather than to a widget. therefore we can't
294 * use an Xt-eventhandler.
297 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
298 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
303 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
306 XtAppNextEvent( (XtAppContext
) wxTheApp
->GetAppContext(), &event
);
308 ProcessXEvent((WXEvent
*) & event
);
310 if (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) == 0)
315 // leave the main loop to give other threads a chance to
316 // perform their GUI work
329 // Processes an X event.
330 void wxApp::ProcessXEvent(WXEvent
* _event
)
332 XEvent
* event
= (XEvent
*) _event
;
334 if (event
->type
== KeyPress
)
337 Widget widget
= XtWindowToWidget(event
->xany
.display
, event
->xany
.window
);
338 wxLogDebug("Got key press event for 0x%08x (parent = 0x%08x)",
339 widget
, XtParent(widget
));
342 if (CheckForAccelerator(_event
))
344 // Do nothing! We intercepted and processed the event as an
349 // It seemed before that this hack was redundant and
350 // key down events were being generated by wxCanvasInputEvent.
351 // But no longer - why ???
353 else if (CheckForKeyDown(_event
))
355 // We intercepted and processed the key down event
361 XtDispatchEvent(event
);
365 else if (event
->type
== KeyRelease
)
367 // TODO: work out why we still need this ! -michael
369 if (CheckForKeyUp(_event
))
371 // We intercepted and processed the key up event
376 XtDispatchEvent(event
);
380 else if (event
->type
== PropertyNotify
)
382 HandlePropertyChange(_event
);
385 else if (event
->type
== ResizeRequest
)
387 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
388 * If resize event, don't resize until the last resize event for this
389 * window is recieved. Prevents flicker as windows are resized.
392 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
393 Window win
= event
->xany
.window
;
398 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
400 // TODO: when implementing refresh optimization, we can use
401 // XtAddExposureToRegion to expand the window's paint region.
403 XtDispatchEvent(event
);
407 XtDispatchEvent(event
);
411 // Returns TRUE if more time is needed.
412 bool wxApp::ProcessIdle()
415 event
.SetEventObject(this);
418 return event
.MoreRequested();
421 void wxApp::ExitMainLoop()
426 // Is a message/event pending?
427 bool wxApp::Pending()
429 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
431 // Fix by Doug from STI, to prevent a stall if non-X event
433 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
436 // Dispatch a message.
437 void wxApp::Dispatch()
439 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
442 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
443 ProcessXEvent((WXEvent
*) & event
);
446 // This should be redefined in a derived class for
447 // handling property change events for XAtom IPC.
448 void wxApp::HandlePropertyChange(WXEvent
*event
)
450 // by default do nothing special
451 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
454 void wxApp::OnIdle(wxIdleEvent
& event
)
456 static bool inOnIdle
= FALSE
;
458 // Avoid recursion (via ProcessEvent default case)
464 // 'Garbage' collection of windows deleted with Close().
465 DeletePendingObjects();
468 // Flush pending events.
469 ProcessPendingEvents();
472 // flush the logged messages if any
473 wxLog
*pLog
= wxLog::GetActiveTarget();
474 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
477 // Send OnIdle events to all windows
478 bool needMore
= SendIdleEvents();
481 event
.RequestMore(TRUE
);
486 // Send idle event to all top-level windows
487 bool wxApp::SendIdleEvents()
489 bool needMore
= FALSE
;
491 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
494 wxWindow
* win
= node
->GetData();
495 if (SendIdleEvents(win
))
497 node
= node
->GetNext();
503 // Send idle event to window and all subwindows
504 bool wxApp::SendIdleEvents(wxWindow
* win
)
506 bool needMore
= FALSE
;
509 event
.SetEventObject(win
);
510 win
->ProcessEvent(event
);
512 if (event
.MoreRequested())
515 wxNode
* node
= win
->GetChildren().First();
518 wxWindow
* win
= (wxWindow
*) node
->Data();
519 if (SendIdleEvents(win
))
527 void wxApp::DeletePendingObjects()
529 wxNode
*node
= wxPendingDelete
.First();
532 wxObject
*obj
= (wxObject
*)node
->Data();
536 if (wxPendingDelete
.Member(obj
))
539 // Deleting one object may have deleted other pending
540 // objects, so start from beginning of list again.
541 node
= wxPendingDelete
.First();
545 // Create an application context
546 bool wxApp::OnInitGui()
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
560 wxString
className(wxTheApp
->GetClassName());
561 wxLogError(_("wxWindows could not open display for '%s': exiting."),
562 (const char*) className
);
565 m_initialDisplay
= (WXDisplay
*) dpy
;
567 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
568 applicationShellWidgetClass
,dpy
,
571 // Add general resize proc
573 rec
.string
= "resize";
574 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
575 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
577 GetMainColormap(dpy
);
578 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
583 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
585 if (!display
) /* Must be called first with non-NULL display */
586 return m_mainColormap
;
588 int defaultScreen
= DefaultScreen((Display
*) display
);
589 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
591 Colormap c
= DefaultColormapOfScreen(screen
);
594 m_mainColormap
= (WXColormap
) c
;
596 return (WXColormap
) c
;
599 // Returns TRUE if an accelerator has been processed
600 bool wxApp::CheckForAccelerator(WXEvent
* event
)
602 XEvent
* xEvent
= (XEvent
*) event
;
603 if (xEvent
->xany
.type
== KeyPress
)
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
;
610 // Find the first wxWindow that corresponds to this event window
611 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
612 widget
= XtParent(widget
);
617 wxKeyEvent
keyEvent(wxEVT_CHAR
);
618 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
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.
625 if (win
->ProcessAccelerator(keyEvent
))
627 win
= win
->GetParent();
634 bool wxApp::CheckForKeyDown(WXEvent
* event
)
636 XEvent
* xEvent
= (XEvent
*) event
;
637 if (xEvent
->xany
.type
== KeyPress
)
639 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
640 xEvent
->xany
.window
);
641 wxWindow
* win
= NULL
;
643 // Find the first wxWindow that corresponds to this event window
644 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
645 widget
= XtParent(widget
);
650 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
651 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
653 return win
->ProcessEvent( keyEvent
);
659 bool wxApp::CheckForKeyUp(WXEvent
* event
)
661 XEvent
* xEvent
= (XEvent
*) event
;
662 if (xEvent
->xany
.type
== KeyRelease
)
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_UP
);
676 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
678 return win
->ProcessEvent( keyEvent
);
688 retValue
= wxTheApp
->OnExit();
692 * Exit in some platform-specific way. Not recommended that the app calls this:
693 * only for emergencies.
698 // Yield to other processes
701 while (wxTheApp
&& wxTheApp
->Pending())
702 wxTheApp
->Dispatch();
704 // VZ: is it the same as this (taken from old wxExecute)?
706 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
712 // TODO use XmGetPixmap (?) to get the really standard icons!
714 #include "wx/generic/info.xpm"
715 #include "wx/generic/error.xpm"
716 #include "wx/generic/question.xpm"
717 #include "wx/generic/warning.xpm"
720 wxApp::GetStdIcon(int which
) const
724 case wxICON_INFORMATION
:
725 return wxIcon(info_xpm
);
727 case wxICON_QUESTION
:
728 return wxIcon(question_xpm
);
730 case wxICON_EXCLAMATION
:
731 return wxIcon(warning_xpm
);
734 wxFAIL_MSG("requested non existent standard icon");
735 // still fall through
738 return wxIcon(error_xpm
);
742 // ----------------------------------------------------------------------------
743 // accessors for C modules
744 // ----------------------------------------------------------------------------
746 extern "C" XtAppContext
wxGetAppContext()
748 return (XtAppContext
)wxTheApp
->GetAppContext();