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
);
488 // **** please implement me! ****
489 // Wake up the idle handler processor, even if it is in another thread...
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();
552 // Create an application context
553 bool wxApp::OnInitGui()
555 XtToolkitInitialize() ;
556 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
557 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
558 (const char*) wxTheApp
->GetClassName(), NULL
, 0,
559 # if XtSpecificationRelease < 5
567 wxString
className(wxTheApp
->GetClassName());
568 wxLogError(_("wxWindows could not open display for '%s': exiting."),
569 (const char*) className
);
572 m_initialDisplay
= (WXDisplay
*) dpy
;
574 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
575 applicationShellWidgetClass
,dpy
,
578 // Add general resize proc
580 rec
.string
= "resize";
581 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
582 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
584 GetMainColormap(dpy
);
585 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
590 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
592 if (!display
) /* Must be called first with non-NULL display */
593 return m_mainColormap
;
595 int defaultScreen
= DefaultScreen((Display
*) display
);
596 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
598 Colormap c
= DefaultColormapOfScreen(screen
);
601 m_mainColormap
= (WXColormap
) c
;
603 return (WXColormap
) c
;
606 // Returns TRUE if an accelerator has been processed
607 bool wxApp::CheckForAccelerator(WXEvent
* event
)
609 XEvent
* xEvent
= (XEvent
*) event
;
610 if (xEvent
->xany
.type
== KeyPress
)
612 // Find a wxWindow for this window
613 // TODO: should get display for the window, not the current display
614 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
615 wxWindow
* win
= NULL
;
617 // Find the first wxWindow that corresponds to this event window
618 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
619 widget
= XtParent(widget
);
624 wxKeyEvent
keyEvent(wxEVT_CHAR
);
625 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
627 // Now we have a wxKeyEvent and we have a wxWindow.
628 // Go up the hierarchy until we find a matching accelerator,
629 // or we get to the top.
632 if (win
->ProcessAccelerator(keyEvent
))
634 win
= win
->GetParent();
641 bool wxApp::CheckForKeyDown(WXEvent
* event
)
643 XEvent
* xEvent
= (XEvent
*) event
;
644 if (xEvent
->xany
.type
== KeyPress
)
646 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
647 xEvent
->xany
.window
);
648 wxWindow
* win
= NULL
;
650 // Find the first wxWindow that corresponds to this event window
651 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
652 widget
= XtParent(widget
);
657 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
658 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
660 return win
->ProcessEvent( keyEvent
);
666 bool wxApp::CheckForKeyUp(WXEvent
* event
)
668 XEvent
* xEvent
= (XEvent
*) event
;
669 if (xEvent
->xany
.type
== KeyRelease
)
671 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
672 xEvent
->xany
.window
);
673 wxWindow
* win
= NULL
;
675 // Find the first wxWindow that corresponds to this event window
676 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
677 widget
= XtParent(widget
);
682 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
683 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
685 return win
->ProcessEvent( keyEvent
);
695 retValue
= wxTheApp
->OnExit();
699 * Exit in some platform-specific way. Not recommended that the app calls this:
700 * only for emergencies.
705 // Yield to other processes
708 while (wxTheApp
&& wxTheApp
->Pending())
709 wxTheApp
->Dispatch();
711 // VZ: is it the same as this (taken from old wxExecute)?
713 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
719 // TODO use XmGetPixmap (?) to get the really standard icons!
721 #include "wx/generic/info.xpm"
722 #include "wx/generic/error.xpm"
723 #include "wx/generic/question.xpm"
724 #include "wx/generic/warning.xpm"
727 wxApp::GetStdIcon(int which
) const
731 case wxICON_INFORMATION
:
732 return wxIcon(info_xpm
);
734 case wxICON_QUESTION
:
735 return wxIcon(question_xpm
);
737 case wxICON_EXCLAMATION
:
738 return wxIcon(warning_xpm
);
741 wxFAIL_MSG("requested non existent standard icon");
742 // still fall through
745 return wxIcon(error_xpm
);
749 // ----------------------------------------------------------------------------
750 // accessors for C modules
751 // ----------------------------------------------------------------------------
753 extern "C" XtAppContext
wxGetAppContext()
755 return (XtAppContext
)wxTheApp
->GetAppContext();