1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.h"
17 #define XtParent XTPARENT
18 #define XtDisplay XTDISPLAY
24 #include "wx/gdicmn.h"
27 #include "wx/cursor.h"
29 #include "wx/palette.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.h"
34 #include "wx/module.h"
35 #include "wx/memory.h"
40 #include "wx/thread.h"
43 #if wxUSE_WX_RESOURCES
44 #include "wx/resource.h"
48 #pragma message disable nosimpint
52 #include <X11/Xutil.h>
53 #include <X11/Xresource.h>
54 #include <X11/Xatom.h>
56 #pragma message enable nosimpint
59 #include "wx/motif/private.h"
63 extern char *wxBuffer
;
64 extern wxList wxPendingDelete
;
66 wxApp
*wxTheApp
= NULL
;
68 wxHashTable
*wxWidgetHashTable
= NULL
;
70 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
72 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
73 EVT_IDLE(wxApp::OnIdle
)
77 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
79 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
81 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
83 // just forward to the default handler for now
84 return gs_pfnXErrorHandler(dpy
, xevent
);
88 long wxApp::sm_lastMessageTime
= 0;
90 bool wxApp::Initialize()
92 wxBuffer
= new char[BUFSIZ
+ 512];
94 wxClassInfo::InitializeClasses();
96 // GL: I'm annoyed ... I don't know where to put this and I don't want to
97 // create a module for that as it's part of the core.
99 wxPendingEventsLocker
= new wxCriticalSection();
102 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
103 wxTheColourDatabase
->Initialize();
105 wxInitializeStockLists();
106 wxInitializeStockObjects();
108 #if wxUSE_WX_RESOURCES
109 wxInitializeResourceSystem();
112 // For PostScript printing
114 /* Done using wxModule now
115 wxInitializePrintSetupData();
116 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
117 wxThePrintPaperDatabase->CreateDatabase();
121 wxBitmap::InitStandardHandlers();
123 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
125 wxModule::RegisterModules();
126 if (!wxModule::InitializeModules()) return FALSE
;
131 void wxApp::CleanUp()
133 delete wxWidgetHashTable
;
134 wxWidgetHashTable
= NULL
;
136 wxModule::CleanUpModules();
138 #if wxUSE_WX_RESOURCES
139 wxCleanUpResourceSystem();
142 wxDeleteStockObjects() ;
144 // Destroy all GDI lists, etc.
146 delete wxTheBrushList
;
147 wxTheBrushList
= NULL
;
152 delete wxTheFontList
;
153 wxTheFontList
= NULL
;
155 delete wxTheBitmapList
;
156 wxTheBitmapList
= NULL
;
158 delete wxTheColourDatabase
;
159 wxTheColourDatabase
= NULL
;
162 /* Done using wxModule now
163 wxInitializePrintSetupData(FALSE);
164 delete wxThePrintPaperDatabase;
165 wxThePrintPaperDatabase = NULL;
169 wxBitmap::CleanUpHandlers();
174 wxClassInfo::CleanUpClasses();
179 // GL: I'm annoyed ... I don't know where to put this and I don't want to
180 // create a module for that as it's part of the core.
182 delete wxPendingEvents
;
183 delete wxPendingEventsLocker
;
186 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
187 // At this point we want to check if there are any memory
188 // blocks that aren't part of the wxDebugContext itself,
189 // as a special case. Then when dumping we need to ignore
190 // wxDebugContext, too.
191 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
193 wxLogDebug("There were memory leaks.\n");
194 wxDebugContext::Dump();
195 wxDebugContext::PrintStatistics();
199 // do it as the very last thing because everything else can log messages
200 wxLog::DontCreateOnDemand();
201 // do it as the very last thing because everything else can log messages
202 delete wxLog::SetActiveTarget(NULL
);
205 int wxEntry( int argc
, char *argv
[] )
207 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
208 // This seems to be necessary since there are 'rogue'
209 // objects present at this point (perhaps global objects?)
210 // Setting a checkpoint will ignore them as far as the
211 // memory checking facility is concerned.
212 // Of course you may argue that memory allocated in globals should be
213 // checked, but this is a reasonable compromise.
214 wxDebugContext::SetCheckpoint();
217 if (!wxApp::Initialize())
222 if (!wxApp::GetInitializerFunction())
224 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
228 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
233 printf( "wxWindows error: wxTheApp == NULL\n" );
237 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
238 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
240 wxTheApp
->argc
= argc
;
241 wxTheApp
->argv
= argv
;
243 // GUI-specific initialization, such as creating an app context.
244 wxTheApp
->OnInitGui();
246 // Here frames insert themselves automatically into wxTopLevelWindows by
247 // getting created in OnInit().
250 if (wxTheApp
->OnInit())
252 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
255 // flush the logged messages if any
256 wxLog
*pLog
= wxLog::GetActiveTarget();
257 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
260 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
261 // for further messages
263 if (wxTheApp
->GetTopWindow())
265 delete wxTheApp
->GetTopWindow();
266 wxTheApp
->SetTopWindow(NULL
);
269 wxTheApp
->DeletePendingObjects();
278 // Static member initialization
279 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
286 m_wantDebugOutput
= TRUE
;
290 m_exitOnFrameDelete
= TRUE
;
292 m_mainColormap
= (WXColormap
) NULL
;
293 m_appContext
= (WXAppContext
) NULL
;
294 m_topLevelWidget
= (WXWidget
) NULL
;
295 m_maxRequestSize
= 0;
296 m_initialDisplay
= (WXDisplay
*) 0;
299 bool wxApp::Initialized()
307 int wxApp::MainLoop()
312 * Sit around forever waiting to process X-events. Property Change
313 * event are handled special, because they have to refer to
314 * the root window rather than to a widget. therefore we can't
315 * use an Xt-eventhandler.
318 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
319 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
324 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
327 XtAppNextEvent( (XtAppContext
) wxTheApp
->GetAppContext(), &event
);
329 ProcessXEvent((WXEvent
*) & event
);
331 if (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) == 0)
336 // leave the main loop to give other threads a chance to
337 // perform their GUI work
350 // Processes an X event.
351 void wxApp::ProcessXEvent(WXEvent
* _event
)
353 XEvent
* event
= (XEvent
*) _event
;
355 if (event
->type
== KeyPress
)
357 #if 0 // def __WXDEBUG__
358 Widget widget
= XtWindowToWidget(event
->xany
.display
, event
->xany
.window
);
359 wxLogDebug("Got key press event for 0x%08x (parent = 0x%08x)",
360 widget
, XtParent(widget
));
363 if (CheckForAccelerator(_event
))
365 // Do nothing! We intercepted and processed the event as an
370 // It seemed before that this hack was redundant and
371 // key down events were being generated by wxCanvasInputEvent.
372 // But no longer - why ???
374 else if (CheckForKeyDown(_event
))
376 // We intercepted and processed the key down event
382 XtDispatchEvent(event
);
386 else if (event
->type
== KeyRelease
)
388 // TODO: work out why we still need this ! -michael
390 if (CheckForKeyUp(_event
))
392 // We intercepted and processed the key up event
397 XtDispatchEvent(event
);
401 else if (event
->type
== PropertyNotify
)
403 HandlePropertyChange(_event
);
406 else if (event
->type
== ResizeRequest
)
408 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
409 * If resize event, don't resize until the last resize event for this
410 * window is recieved. Prevents flicker as windows are resized.
413 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
414 Window win
= event
->xany
.window
;
419 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
421 // TODO: when implementing refresh optimization, we can use
422 // XtAddExposureToRegion to expand the window's paint region.
424 XtDispatchEvent(event
);
428 XtDispatchEvent(event
);
432 // Returns TRUE if more time is needed.
433 bool wxApp::ProcessIdle()
436 event
.SetEventObject(this);
439 return event
.MoreRequested();
442 void wxApp::ExitMainLoop()
447 // Is a message/event pending?
448 bool wxApp::Pending()
450 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
452 // Fix by Doug from STI, to prevent a stall if non-X event
454 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
457 // Dispatch a message.
458 void wxApp::Dispatch()
460 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
463 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
464 ProcessXEvent((WXEvent
*) & event
);
467 // This should be redefined in a derived class for
468 // handling property change events for XAtom IPC.
469 void wxApp::HandlePropertyChange(WXEvent
*event
)
471 // by default do nothing special
472 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
475 void wxApp::OnIdle(wxIdleEvent
& event
)
477 static bool inOnIdle
= FALSE
;
479 // Avoid recursion (via ProcessEvent default case)
485 // If there are pending events, we must process them: pending events
486 // are either events to the threads other than main or events posted
487 // with wxPostEvent() functions
488 // GRG: I have moved this here so that all pending events are processed
489 // before starting to delete any objects. This behaves better (in
490 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
491 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
492 // Changed Mar/2000 before 2.1.14
494 // Flush pending events.
495 ProcessPendingEvents();
497 // 'Garbage' collection of windows deleted with Close().
498 DeletePendingObjects();
500 // flush the logged messages if any
501 wxLog
*pLog
= wxLog::GetActiveTarget();
502 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
505 // Send OnIdle events to all windows
506 bool needMore
= SendIdleEvents();
509 event
.RequestMore(TRUE
);
516 // **** please implement me! ****
517 // Wake up the idle handler processor, even if it is in another thread...
521 // Send idle event to all top-level windows
522 bool wxApp::SendIdleEvents()
524 bool needMore
= FALSE
;
526 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
529 wxWindow
* win
= node
->GetData();
530 if (SendIdleEvents(win
))
532 node
= node
->GetNext();
538 // Send idle event to window and all subwindows
539 bool wxApp::SendIdleEvents(wxWindow
* win
)
541 bool needMore
= FALSE
;
544 event
.SetEventObject(win
);
545 win
->ProcessEvent(event
);
547 if (event
.MoreRequested())
550 wxNode
* node
= win
->GetChildren().First();
553 wxWindow
* win
= (wxWindow
*) node
->Data();
554 if (SendIdleEvents(win
))
562 void wxApp::DeletePendingObjects()
564 wxNode
*node
= wxPendingDelete
.First();
567 wxObject
*obj
= (wxObject
*)node
->Data();
571 if (wxPendingDelete
.Member(obj
))
574 // Deleting one object may have deleted other pending
575 // objects, so start from beginning of list again.
576 node
= wxPendingDelete
.First();
580 // Create an application context
581 bool wxApp::OnInitGui()
583 XtToolkitInitialize() ;
584 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
585 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
586 (const char*) wxTheApp
->GetClassName(), NULL
, 0,
587 # if XtSpecificationRelease < 5
595 wxString
className(wxTheApp
->GetClassName());
596 wxLogError(_("wxWindows could not open display for '%s': exiting."),
597 (const char*) className
);
600 m_initialDisplay
= (WXDisplay
*) dpy
;
603 // install the X error handler
604 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
605 #endif // __WXDEBUG__
607 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
608 applicationShellWidgetClass
,dpy
,
611 // Add general resize proc
613 rec
.string
= "resize";
614 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
615 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
617 GetMainColormap(dpy
);
618 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
623 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
625 if (!display
) /* Must be called first with non-NULL display */
626 return m_mainColormap
;
628 int defaultScreen
= DefaultScreen((Display
*) display
);
629 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
631 Colormap c
= DefaultColormapOfScreen(screen
);
634 m_mainColormap
= (WXColormap
) c
;
636 return (WXColormap
) c
;
639 // Returns TRUE if an accelerator has been processed
640 bool wxApp::CheckForAccelerator(WXEvent
* event
)
642 XEvent
* xEvent
= (XEvent
*) event
;
643 if (xEvent
->xany
.type
== KeyPress
)
645 // Find a wxWindow for this window
646 // TODO: should get display for the window, not the current display
647 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), 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_CHAR
);
658 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
660 // Now we have a wxKeyEvent and we have a wxWindow.
661 // Go up the hierarchy until we find a matching accelerator,
662 // or we get to the top.
665 if (win
->ProcessAccelerator(keyEvent
))
667 win
= win
->GetParent();
674 bool wxApp::CheckForKeyDown(WXEvent
* event
)
676 XEvent
* xEvent
= (XEvent
*) event
;
677 if (xEvent
->xany
.type
== KeyPress
)
679 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
680 xEvent
->xany
.window
);
681 wxWindow
* win
= NULL
;
683 // Find the first wxWindow that corresponds to this event window
684 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
685 widget
= XtParent(widget
);
690 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
691 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
693 return win
->ProcessEvent( keyEvent
);
699 bool wxApp::CheckForKeyUp(WXEvent
* event
)
701 XEvent
* xEvent
= (XEvent
*) event
;
702 if (xEvent
->xany
.type
== KeyRelease
)
704 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
705 xEvent
->xany
.window
);
706 wxWindow
* win
= NULL
;
708 // Find the first wxWindow that corresponds to this event window
709 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
710 widget
= XtParent(widget
);
715 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
716 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
718 return win
->ProcessEvent( keyEvent
);
728 retValue
= wxTheApp
->OnExit();
732 * Exit in some platform-specific way. Not recommended that the app calls this:
733 * only for emergencies.
738 // Yield to other processes
740 bool wxApp::Yield(bool onlyIfNeeded
)
742 bool s_inYield
= FALSE
;
748 wxFAIL_MSG( wxT("wxYield called recursively" ) );
756 while (wxTheApp
&& wxTheApp
->Pending())
757 wxTheApp
->Dispatch();
764 // TODO use XmGetPixmap (?) to get the really standard icons!
766 #include "wx/generic/info.xpm"
767 #include "wx/generic/error.xpm"
768 #include "wx/generic/question.xpm"
769 #include "wx/generic/warning.xpm"
772 wxApp::GetStdIcon(int which
) const
776 case wxICON_INFORMATION
:
777 return wxIcon(info_xpm
);
779 case wxICON_QUESTION
:
780 return wxIcon(question_xpm
);
782 case wxICON_EXCLAMATION
:
783 return wxIcon(warning_xpm
);
786 wxFAIL_MSG("requested non existent standard icon");
787 // still fall through
790 return wxIcon(error_xpm
);
794 // ----------------------------------------------------------------------------
795 // accessors for C modules
796 // ----------------------------------------------------------------------------
798 extern "C" XtAppContext
wxGetAppContext()
800 return (XtAppContext
)wxTheApp
->GetAppContext();