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 // If there are pending events, we must process them: pending events
481 // are either events to the threads other than main or events posted
482 // with wxPostEvent() functions
483 // GRG: I have moved this here so that all pending events are processed
484 // before starting to delete any objects. This behaves better (in
485 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
486 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
487 // Changed Mar/2000 before 2.1.14
489 // Flush pending events.
490 ProcessPendingEvents();
492 // 'Garbage' collection of windows deleted with Close().
493 DeletePendingObjects();
495 // flush the logged messages if any
496 wxLog
*pLog
= wxLog::GetActiveTarget();
497 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
500 // Send OnIdle events to all windows
501 bool needMore
= SendIdleEvents();
504 event
.RequestMore(TRUE
);
511 // **** please implement me! ****
512 // Wake up the idle handler processor, even if it is in another thread...
516 // Send idle event to all top-level windows
517 bool wxApp::SendIdleEvents()
519 bool needMore
= FALSE
;
521 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
524 wxWindow
* win
= node
->GetData();
525 if (SendIdleEvents(win
))
527 node
= node
->GetNext();
533 // Send idle event to window and all subwindows
534 bool wxApp::SendIdleEvents(wxWindow
* win
)
536 bool needMore
= FALSE
;
539 event
.SetEventObject(win
);
540 win
->ProcessEvent(event
);
542 if (event
.MoreRequested())
545 wxNode
* node
= win
->GetChildren().First();
548 wxWindow
* win
= (wxWindow
*) node
->Data();
549 if (SendIdleEvents(win
))
557 void wxApp::DeletePendingObjects()
559 wxNode
*node
= wxPendingDelete
.First();
562 wxObject
*obj
= (wxObject
*)node
->Data();
566 if (wxPendingDelete
.Member(obj
))
569 // Deleting one object may have deleted other pending
570 // objects, so start from beginning of list again.
571 node
= wxPendingDelete
.First();
575 // Create an application context
576 bool wxApp::OnInitGui()
578 XtToolkitInitialize() ;
579 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
580 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
581 (const char*) wxTheApp
->GetClassName(), NULL
, 0,
582 # if XtSpecificationRelease < 5
590 wxString
className(wxTheApp
->GetClassName());
591 wxLogError(_("wxWindows could not open display for '%s': exiting."),
592 (const char*) className
);
595 m_initialDisplay
= (WXDisplay
*) dpy
;
598 // install the X error handler
599 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
600 #endif // __WXDEBUG__
602 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
603 applicationShellWidgetClass
,dpy
,
606 // Add general resize proc
608 rec
.string
= "resize";
609 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
610 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
612 GetMainColormap(dpy
);
613 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
618 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
620 if (!display
) /* Must be called first with non-NULL display */
621 return m_mainColormap
;
623 int defaultScreen
= DefaultScreen((Display
*) display
);
624 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
626 Colormap c
= DefaultColormapOfScreen(screen
);
629 m_mainColormap
= (WXColormap
) c
;
631 return (WXColormap
) c
;
634 // Returns TRUE if an accelerator has been processed
635 bool wxApp::CheckForAccelerator(WXEvent
* event
)
637 XEvent
* xEvent
= (XEvent
*) event
;
638 if (xEvent
->xany
.type
== KeyPress
)
640 // Find a wxWindow for this window
641 // TODO: should get display for the window, not the current display
642 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
643 wxWindow
* win
= NULL
;
645 // Find the first wxWindow that corresponds to this event window
646 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
647 widget
= XtParent(widget
);
652 wxKeyEvent
keyEvent(wxEVT_CHAR
);
653 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
655 // Now we have a wxKeyEvent and we have a wxWindow.
656 // Go up the hierarchy until we find a matching accelerator,
657 // or we get to the top.
660 if (win
->ProcessAccelerator(keyEvent
))
662 win
= win
->GetParent();
669 bool wxApp::CheckForKeyDown(WXEvent
* event
)
671 XEvent
* xEvent
= (XEvent
*) event
;
672 if (xEvent
->xany
.type
== KeyPress
)
674 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
675 xEvent
->xany
.window
);
676 wxWindow
* win
= NULL
;
678 // Find the first wxWindow that corresponds to this event window
679 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
680 widget
= XtParent(widget
);
685 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
686 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
688 return win
->ProcessEvent( keyEvent
);
694 bool wxApp::CheckForKeyUp(WXEvent
* event
)
696 XEvent
* xEvent
= (XEvent
*) event
;
697 if (xEvent
->xany
.type
== KeyRelease
)
699 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
700 xEvent
->xany
.window
);
701 wxWindow
* win
= NULL
;
703 // Find the first wxWindow that corresponds to this event window
704 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
705 widget
= XtParent(widget
);
710 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
711 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
713 return win
->ProcessEvent( keyEvent
);
723 retValue
= wxTheApp
->OnExit();
727 * Exit in some platform-specific way. Not recommended that the app calls this:
728 * only for emergencies.
733 // Yield to other processes
736 while (wxTheApp
&& wxTheApp
->Pending())
737 wxTheApp
->Dispatch();
739 // VZ: is it the same as this (taken from old wxExecute)?
741 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
747 // TODO use XmGetPixmap (?) to get the really standard icons!
749 #include "wx/generic/info.xpm"
750 #include "wx/generic/error.xpm"
751 #include "wx/generic/question.xpm"
752 #include "wx/generic/warning.xpm"
755 wxApp::GetStdIcon(int which
) const
759 case wxICON_INFORMATION
:
760 return wxIcon(info_xpm
);
762 case wxICON_QUESTION
:
763 return wxIcon(question_xpm
);
765 case wxICON_EXCLAMATION
:
766 return wxIcon(warning_xpm
);
769 wxFAIL_MSG("requested non existent standard icon");
770 // still fall through
773 return wxIcon(error_xpm
);
777 // ----------------------------------------------------------------------------
778 // accessors for C modules
779 // ----------------------------------------------------------------------------
781 extern "C" XtAppContext
wxGetAppContext()
783 return (XtAppContext
)wxTheApp
->GetAppContext();