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/dialog.h"
25 #include "wx/msgdlg.h"
27 #include "wx/module.h"
28 #include "wx/memory.h"
31 #include "wx/evtloop.h"
34 #include "wx/thread.h"
37 #if wxUSE_WX_RESOURCES
38 #include "wx/resource.h"
42 #pragma message disable nosimpint
45 #include <X11/Xutil.h>
46 #include <X11/Xresource.h>
47 #include <X11/Xatom.h>
49 #pragma message enable nosimpint
52 #include "wx/x11/private.h"
56 extern char *wxBuffer
;
57 extern wxList wxPendingDelete
;
59 wxApp
*wxTheApp
= NULL
;
61 wxHashTable
*wxWidgetHashTable
= NULL
;
63 static Window
wxGetParentWindow(Window window
);
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 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 wxDeleteStockLists();
134 delete wxTheColourDatabase
;
135 wxTheColourDatabase
= NULL
;
137 wxBitmap::CleanUpHandlers();
142 wxClassInfo::CleanUpClasses();
147 // GL: I'm annoyed ... I don't know where to put this and I don't want to
148 // create a module for that as it's part of the core.
150 delete wxPendingEvents
;
151 delete wxPendingEventsLocker
;
154 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
155 // At this point we want to check if there are any memory
156 // blocks that aren't part of the wxDebugContext itself,
157 // as a special case. Then when dumping we need to ignore
158 // wxDebugContext, too.
159 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
161 wxLogDebug("There were memory leaks.\n");
162 wxDebugContext::Dump();
163 wxDebugContext::PrintStatistics();
167 // do it as the very last thing because everything else can log messages
168 wxLog::DontCreateOnDemand();
169 // do it as the very last thing because everything else can log messages
170 delete wxLog::SetActiveTarget(NULL
);
173 int wxEntry( int argc
, char *argv
[] )
175 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
176 // This seems to be necessary since there are 'rogue'
177 // objects present at this point (perhaps global objects?)
178 // Setting a checkpoint will ignore them as far as the
179 // memory checking facility is concerned.
180 // Of course you may argue that memory allocated in globals should be
181 // checked, but this is a reasonable compromise.
182 wxDebugContext::SetCheckpoint();
185 if (!wxApp::Initialize())
190 if (!wxApp::GetInitializerFunction())
192 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
196 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
201 printf( "wxWindows error: wxTheApp == NULL\n" );
205 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
206 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
208 wxTheApp
->argc
= argc
;
209 wxTheApp
->argv
= argv
;
211 // GUI-specific initialization, such as creating an app context.
212 wxTheApp
->OnInitGui();
214 // Here frames insert themselves automatically into wxTopLevelWindows by
215 // getting created in OnInit().
218 if (wxTheApp
->OnInit())
220 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
223 // flush the logged messages if any
224 wxLog
*pLog
= wxLog::GetActiveTarget();
225 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
228 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
229 // for further messages
231 if (wxTheApp
->GetTopWindow())
233 delete wxTheApp
->GetTopWindow();
234 wxTheApp
->SetTopWindow(NULL
);
237 wxTheApp
->DeletePendingObjects();
246 // Static member initialization
247 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
254 m_wantDebugOutput
= TRUE
;
258 m_exitOnFrameDelete
= TRUE
;
259 m_mainColormap
= (WXColormap
) NULL
;
260 m_topLevelWidget
= (WXWindow
) NULL
;
261 m_maxRequestSize
= 0;
262 m_initialDisplay
= (WXDisplay
*) 0;
266 bool wxApp::Initialized()
274 int wxApp::MainLoop()
277 m_mainLoop
= new wxEventLoop
;
279 rt
= m_mainLoop
->Run();
286 // Processes an X event.
287 void wxApp::ProcessXEvent(WXEvent
* _event
)
289 XEvent
* event
= (XEvent
*) _event
;
291 wxWindow
* win
= NULL
;
292 Window window
= event
->xany
.window
;
293 Window actualWindow
= window
;
295 // Find the first wxWindow that corresponds to this event window
296 // TODO: may need to translate coordinates from actualWindow
297 // to window, if the receiving window != wxWindow window
298 while (window
&& !(win
= wxGetWindowFromTable(window
)))
299 window
= wxGetParentWindow(window
);
301 // TODO: shouldn't all the ProcessEvents below
302 // be win->GetEventHandler()->ProcessEvent?
307 if (win
&& !win
->IsEnabled())
310 if (CheckForAccelerator(_event
))
312 // Do nothing! We intercepted and processed the event as an
320 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
321 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
323 // We didn't process wxEVT_KEY_DOWN, so send
325 if (!win
->GetEventHandler()->ProcessEvent( keyEvent
))
327 keyEvent
.SetEventType(wxEVT_CHAR
);
328 win
->GetEventHandler()->ProcessEvent( keyEvent
);
331 // We intercepted and processed the key down event
339 if (win
&& !win
->IsEnabled())
344 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
345 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
347 win
->GetEventHandler()->ProcessEvent( keyEvent
);
353 HandlePropertyChange(_event
);
358 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);;
359 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);;
361 if (event
->xclient
.message_type
== wm_protocols
)
363 if (event
->xclient
.data
.l
[0] == wm_delete_window
)
375 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
376 * If resize event, don't resize until the last resize event for this
377 * window is recieved. Prevents flicker as windows are resized.
380 Display
*disp
= (Display
*) wxGetDisplay();
385 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
387 // TODO: when implementing refresh optimization, we can use
388 // XtAddExposureToRegion to expand the window's paint region.
392 wxSize sz
= win
->GetSize();
393 wxSizeEvent
sizeEvent(sz
, win
->GetId());
394 sizeEvent
.SetEventObject(win
);
396 win
->GetEventHandler()->ProcessEvent( sizeEvent
);
405 win
->AddUpdateRect(event
->xexpose
.x
, event
->xexpose
.y
,
406 event
->xexpose
.width
, event
->xexpose
.height
);
408 if (event
-> xexpose
.count
== 0)
411 win
->ClearUpdateRects();
423 if (win
&& !win
->IsEnabled())
428 wxMouseEvent wxevent
;
429 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
430 win
->GetEventHandler()->ProcessEvent( wxevent
);
436 if (win
&& event
->xfocus
.detail
!= NotifyPointer
)
438 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
439 focusEvent
.SetEventObject(win
);
440 win
->GetEventHandler()->ProcessEvent(focusEvent
);
446 if (win
&& event
->xfocus
.detail
!= NotifyPointer
)
448 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
449 focusEvent
.SetEventObject(win
);
450 win
->GetEventHandler()->ProcessEvent(focusEvent
);
456 // Do we want to process this (for top-level windows)?
457 // But we want to be able to veto closes, anyway
467 // Returns TRUE if more time is needed.
468 // Note that this duplicates wxEventLoopImpl::SendIdleEvent
469 // but ProcessIdle may be needed by apps, so is kept.
470 bool wxApp::ProcessIdle()
473 event
.SetEventObject(this);
476 return event
.MoreRequested();
479 void wxApp::ExitMainLoop()
485 // Is a message/event pending?
486 bool wxApp::Pending()
488 return wxEventLoop::GetActive()->Pending();
491 // Dispatch a message.
492 void wxApp::Dispatch()
494 wxEventLoop::GetActive()->Dispatch();
497 // This should be redefined in a derived class for
498 // handling property change events for XAtom IPC.
499 void wxApp::HandlePropertyChange(WXEvent
*event
)
501 // by default do nothing special
502 // TODO: what to do for X11
503 // XtDispatchEvent((XEvent*) event); /* let Motif do the work */
506 void wxApp::OnIdle(wxIdleEvent
& event
)
508 static bool inOnIdle
= FALSE
;
510 // Avoid recursion (via ProcessEvent default case)
516 // If there are pending events, we must process them: pending events
517 // are either events to the threads other than main or events posted
518 // with wxPostEvent() functions
519 // GRG: I have moved this here so that all pending events are processed
520 // before starting to delete any objects. This behaves better (in
521 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
522 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
523 // Changed Mar/2000 before 2.1.14
525 // Flush pending events.
526 ProcessPendingEvents();
528 // 'Garbage' collection of windows deleted with Close().
529 DeletePendingObjects();
531 // flush the logged messages if any
532 wxLog
*pLog
= wxLog::GetActiveTarget();
533 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
536 // Send OnIdle events to all windows
537 bool needMore
= SendIdleEvents();
540 event
.RequestMore(TRUE
);
547 // **** please implement me! ****
548 // Wake up the idle handler processor, even if it is in another thread...
552 // Send idle event to all top-level windows
553 bool wxApp::SendIdleEvents()
555 bool needMore
= FALSE
;
557 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
560 wxWindow
* win
= node
->GetData();
561 if (SendIdleEvents(win
))
563 node
= node
->GetNext();
569 // Send idle event to window and all subwindows
570 bool wxApp::SendIdleEvents(wxWindow
* win
)
572 bool needMore
= FALSE
;
575 event
.SetEventObject(win
);
576 win
->ProcessEvent(event
);
578 if (event
.MoreRequested())
581 wxNode
* node
= win
->GetChildren().First();
584 wxWindow
* win
= (wxWindow
*) node
->Data();
585 if (SendIdleEvents(win
))
593 void wxApp::DeletePendingObjects()
595 wxNode
*node
= wxPendingDelete
.First();
598 wxObject
*obj
= (wxObject
*)node
->Data();
602 if (wxPendingDelete
.Member(obj
))
605 // Deleting one object may have deleted other pending
606 // objects, so start from beginning of list again.
607 node
= wxPendingDelete
.First();
611 // Create an application context
612 bool wxApp::OnInitGui()
614 // TODO: parse argv and get display to pass to XOpenDisplay
615 Display
* dpy
= XOpenDisplay(NULL
);
616 m_initialDisplay
= (WXDisplay
*) dpy
;
619 wxString
className(wxTheApp
->GetClassName());
620 wxLogError(_("wxWindows could not open display for '%s': exiting."),
621 (const char*) className
);
624 XSelectInput((Display
*) m_initialDisplay
,
625 XDefaultRootWindow((Display
*) m_initialDisplay
),
629 // install the X error handler
630 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
631 #endif // __WXDEBUG__
633 GetMainColormap(dpy
);
634 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
639 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
641 if (!display
) /* Must be called first with non-NULL display */
642 return m_mainColormap
;
644 int defaultScreen
= DefaultScreen((Display
*) display
);
645 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
647 Colormap c
= DefaultColormapOfScreen(screen
);
650 m_mainColormap
= (WXColormap
) c
;
652 return (WXColormap
) c
;
655 Window
wxGetParentWindow(Window window
)
657 Window parent
, root
= 0;
658 unsigned int noChildren
= 0;
659 if (XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
666 // Returns TRUE if an accelerator has been processed
667 bool wxApp::CheckForAccelerator(WXEvent
* event
)
669 XEvent
* xEvent
= (XEvent
*) event
;
670 if (xEvent
->xany
.type
== KeyPress
)
672 // Find a wxWindow for this window
673 // TODO: should get display for the window, not the current display
674 Window window
= xEvent
->xany
.window
;
675 wxWindow
* win
= NULL
;
677 // Find the first wxWindow that corresponds to this event window
678 while (window
&& !(win
= wxGetWindowFromTable(window
)))
679 window
= wxGetWindowParent(window
);
684 wxKeyEvent
keyEvent(wxEVT_CHAR
);
685 wxTranslateKeyEvent(keyEvent
, win
, (Window
) 0, xEvent
);
687 // Now we have a wxKeyEvent and we have a wxWindow.
688 // Go up the hierarchy until we find a matching accelerator,
689 // or we get to the top.
692 if (win
->ProcessAccelerator(keyEvent
))
694 win
= win
->GetParent();
705 retValue
= wxTheApp
->OnExit();
709 * Exit in some platform-specific way. Not recommended that the app calls this:
710 * only for emergencies.
715 // Yield to other processes
717 bool wxApp::Yield(bool onlyIfNeeded
)
719 bool s_inYield
= FALSE
;
725 wxFAIL_MSG( wxT("wxYield called recursively" ) );
733 while (wxTheApp
&& wxTheApp
->Pending())
734 wxTheApp
->Dispatch();
741 // TODO use XmGetPixmap (?) to get the really standard icons!
743 // XPM hack: make the arrays const
744 #define static static const
746 #include "wx/generic/info.xpm"
747 #include "wx/generic/error.xpm"
748 #include "wx/generic/question.xpm"
749 #include "wx/generic/warning.xpm"
754 wxApp::GetStdIcon(int which
) const
758 case wxICON_INFORMATION
:
759 return wxIcon(info_xpm
);
761 case wxICON_QUESTION
:
762 return wxIcon(question_xpm
);
764 case wxICON_EXCLAMATION
:
765 return wxIcon(warning_xpm
);
768 wxFAIL_MSG("requested non existent standard icon");
769 // still fall through
772 return wxIcon(error_xpm
);
776 // ----------------------------------------------------------------------------
777 // accessors for C modules
778 // ----------------------------------------------------------------------------
781 extern "C" XtAppContext
wxGetAppContext()
783 return (XtAppContext
)wxTheApp
->GetAppContext();