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 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
65 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
66 EVT_IDLE(wxApp::OnIdle
)
70 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
72 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
74 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
76 // just forward to the default handler for now
77 return gs_pfnXErrorHandler(dpy
, xevent
);
81 long wxApp::sm_lastMessageTime
= 0;
83 bool wxApp::Initialize()
85 wxBuffer
= new char[BUFSIZ
+ 512];
87 wxClassInfo::InitializeClasses();
89 // GL: I'm annoyed ... I don't know where to put this and I don't want to
90 // create a module for that as it's part of the core.
92 wxPendingEventsLocker
= new wxCriticalSection();
95 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
96 wxTheColourDatabase
->Initialize();
98 wxInitializeStockLists();
99 wxInitializeStockObjects();
101 #if wxUSE_WX_RESOURCES
102 wxInitializeResourceSystem();
105 wxBitmap::InitStandardHandlers();
107 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
109 wxModule::RegisterModules();
110 if (!wxModule::InitializeModules()) return FALSE
;
115 void wxApp::CleanUp()
117 delete wxWidgetHashTable
;
118 wxWidgetHashTable
= NULL
;
120 wxModule::CleanUpModules();
122 #if wxUSE_WX_RESOURCES
123 wxCleanUpResourceSystem();
126 wxDeleteStockObjects() ;
128 // Destroy all GDI lists, etc.
130 wxDeleteStockLists();
132 delete wxTheColourDatabase
;
133 wxTheColourDatabase
= NULL
;
135 wxBitmap::CleanUpHandlers();
140 wxClassInfo::CleanUpClasses();
145 // GL: I'm annoyed ... I don't know where to put this and I don't want to
146 // create a module for that as it's part of the core.
148 delete wxPendingEvents
;
149 delete wxPendingEventsLocker
;
152 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
153 // At this point we want to check if there are any memory
154 // blocks that aren't part of the wxDebugContext itself,
155 // as a special case. Then when dumping we need to ignore
156 // wxDebugContext, too.
157 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
159 wxLogDebug("There were memory leaks.\n");
160 wxDebugContext::Dump();
161 wxDebugContext::PrintStatistics();
165 // do it as the very last thing because everything else can log messages
166 wxLog::DontCreateOnDemand();
167 // do it as the very last thing because everything else can log messages
168 delete wxLog::SetActiveTarget(NULL
);
171 int wxEntry( int argc
, char *argv
[] )
173 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
174 // This seems to be necessary since there are 'rogue'
175 // objects present at this point (perhaps global objects?)
176 // Setting a checkpoint will ignore them as far as the
177 // memory checking facility is concerned.
178 // Of course you may argue that memory allocated in globals should be
179 // checked, but this is a reasonable compromise.
180 wxDebugContext::SetCheckpoint();
183 if (!wxApp::Initialize())
188 if (!wxApp::GetInitializerFunction())
190 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
194 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
199 printf( "wxWindows error: wxTheApp == NULL\n" );
203 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
204 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
206 wxTheApp
->argc
= argc
;
207 wxTheApp
->argv
= argv
;
209 // GUI-specific initialization, such as creating an app context.
210 wxTheApp
->OnInitGui();
212 // Here frames insert themselves automatically into wxTopLevelWindows by
213 // getting created in OnInit().
216 if (wxTheApp
->OnInit())
218 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
221 // flush the logged messages if any
222 wxLog
*pLog
= wxLog::GetActiveTarget();
223 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
226 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
227 // for further messages
229 if (wxTheApp
->GetTopWindow())
231 delete wxTheApp
->GetTopWindow();
232 wxTheApp
->SetTopWindow(NULL
);
235 wxTheApp
->DeletePendingObjects();
244 // Static member initialization
245 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
252 m_wantDebugOutput
= TRUE
;
256 m_exitOnFrameDelete
= TRUE
;
257 m_mainColormap
= (WXColormap
) NULL
;
258 m_topLevelWidget
= (WXWindow
) NULL
;
259 m_maxRequestSize
= 0;
260 m_initialDisplay
= (WXDisplay
*) 0;
264 bool wxApp::Initialized()
272 int wxApp::MainLoop()
275 m_mainLoop
= new wxEventLoop
;
277 rt
= m_mainLoop
->Run();
284 // Processes an X event.
285 void wxApp::ProcessXEvent(WXEvent
* _event
)
287 XEvent
* event
= (XEvent
*) _event
;
289 wxWindow
* win
= NULL
;
290 Window window
= event
->xany
.window
;
291 Window actualWindow
= window
;
293 // Find the first wxWindow that corresponds to this event window
294 // TODO: may need to translate coordinates from actualWindow
295 // to window, if the receiving window != wxWindow window
296 // while (window && !(win = wxGetWindowFromTable(window)))
297 // window = wxGetWindowParent(window);
299 // Because we're receiving events after a window
300 // has been destroyed, assume a 1:1 match between
301 // Window and wxWindow, so if it's not in the table,
302 // it must have been destroyed.
304 win
= wxGetWindowFromTable(window
);
312 if (win
&& !win
->IsEnabled())
318 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
319 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
321 // We didn't process wxEVT_KEY_DOWN, so send
323 if (!win
->GetEventHandler()->ProcessEvent( keyEvent
))
325 keyEvent
.SetEventType(wxEVT_CHAR
);
326 win
->GetEventHandler()->ProcessEvent( keyEvent
);
329 // We intercepted and processed the key down event
337 if (win
&& !win
->IsEnabled())
342 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
343 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
345 win
->GetEventHandler()->ProcessEvent( keyEvent
);
351 HandlePropertyChange(_event
);
356 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);;
357 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);;
359 if (event
->xclient
.message_type
== wm_protocols
)
361 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
371 case ConfigureNotify
:
375 wxSizeEvent
sizeEvent( wxSize(event
->xconfigure
.width
,event
->xconfigure
.height
), win
->GetId() );
376 sizeEvent
.SetEventObject( win
);
378 win
->GetEventHandler()->ProcessEvent( sizeEvent
);
383 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
384 * If resize event, don't resize until the last resize event for this
385 * window is recieved. Prevents flicker as windows are resized.
388 Display
*disp
= (Display
*) wxGetDisplay();
393 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
395 // TODO: when implementing refresh optimization, we can use
396 // XtAddExposureToRegion to expand the window's paint region.
400 wxSize sz
= win
->GetSize();
401 wxSizeEvent
sizeEvent(sz
, win
->GetId());
402 sizeEvent
.SetEventObject(win
);
404 win
->GetEventHandler()->ProcessEvent( sizeEvent
);
413 win
->GetUpdateRegion().Union( event
->xexpose
.x
, event
->xexpose
.y
,
414 event
->xexpose
.width
, event
->xexpose
.height
);
415 if (event
->xexpose
.count
== 0)
417 win
->X11SendPaintEvents(); // TODO let an idle handler do that
429 if (win
&& !win
->IsEnabled())
434 wxMouseEvent wxevent
;
435 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
436 win
->GetEventHandler()->ProcessEvent( wxevent
);
442 if (win
&& event
->xfocus
.detail
!= NotifyPointer
)
444 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
445 focusEvent
.SetEventObject(win
);
446 win
->GetEventHandler()->ProcessEvent(focusEvent
);
452 if (win
&& event
->xfocus
.detail
!= NotifyPointer
)
454 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
455 focusEvent
.SetEventObject(win
);
456 win
->GetEventHandler()->ProcessEvent(focusEvent
);
462 // Do we want to process this (for top-level windows)?
463 // But we want to be able to veto closes, anyway
473 // Returns TRUE if more time is needed.
474 // Note that this duplicates wxEventLoopImpl::SendIdleEvent
475 // but ProcessIdle may be needed by apps, so is kept.
476 bool wxApp::ProcessIdle()
479 event
.SetEventObject(this);
482 return event
.MoreRequested();
485 void wxApp::ExitMainLoop()
491 // Is a message/event pending?
492 bool wxApp::Pending()
494 return wxEventLoop::GetActive()->Pending();
497 // Dispatch a message.
498 void wxApp::Dispatch()
500 wxEventLoop::GetActive()->Dispatch();
503 // This should be redefined in a derived class for
504 // handling property change events for XAtom IPC.
505 void wxApp::HandlePropertyChange(WXEvent
*event
)
507 // by default do nothing special
508 // TODO: what to do for X11
509 // XtDispatchEvent((XEvent*) event); /* let Motif do the work */
512 void wxApp::OnIdle(wxIdleEvent
& event
)
514 static bool inOnIdle
= FALSE
;
516 // Avoid recursion (via ProcessEvent default case)
522 // If there are pending events, we must process them: pending events
523 // are either events to the threads other than main or events posted
524 // with wxPostEvent() functions
525 // GRG: I have moved this here so that all pending events are processed
526 // before starting to delete any objects. This behaves better (in
527 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
528 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
529 // Changed Mar/2000 before 2.1.14
531 // Flush pending events.
532 ProcessPendingEvents();
534 // 'Garbage' collection of windows deleted with Close().
535 DeletePendingObjects();
537 // flush the logged messages if any
538 wxLog
*pLog
= wxLog::GetActiveTarget();
539 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
542 // Send OnIdle events to all windows
543 bool needMore
= SendIdleEvents();
546 event
.RequestMore(TRUE
);
553 // **** please implement me! ****
554 // Wake up the idle handler processor, even if it is in another thread...
558 // Send idle event to all top-level windows
559 bool wxApp::SendIdleEvents()
561 bool needMore
= FALSE
;
563 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
566 wxWindow
* win
= node
->GetData();
567 if (SendIdleEvents(win
))
569 node
= node
->GetNext();
575 // Send idle event to window and all subwindows
576 bool wxApp::SendIdleEvents(wxWindow
* win
)
578 bool needMore
= FALSE
;
581 event
.SetEventObject(win
);
582 win
->ProcessEvent(event
);
584 if (event
.MoreRequested())
587 wxNode
* node
= win
->GetChildren().First();
590 wxWindow
* win
= (wxWindow
*) node
->Data();
591 if (SendIdleEvents(win
))
599 void wxApp::DeletePendingObjects()
601 wxNode
*node
= wxPendingDelete
.First();
604 wxObject
*obj
= (wxObject
*)node
->Data();
608 if (wxPendingDelete
.Member(obj
))
611 // Deleting one object may have deleted other pending
612 // objects, so start from beginning of list again.
613 node
= wxPendingDelete
.First();
617 // Create an application context
618 bool wxApp::OnInitGui()
620 // Eventually this line will be removed, but for
621 // now we don't want to try popping up a dialog
622 // for error messages.
623 delete wxLog::SetActiveTarget(new wxLogStderr
);
624 if (!wxAppBase::OnInitGui())
627 // TODO: parse argv and get display to pass to XOpenDisplay
628 Display
* dpy
= XOpenDisplay(NULL
);
629 m_initialDisplay
= (WXDisplay
*) dpy
;
632 wxString
className(wxTheApp
->GetClassName());
633 wxLogError(_("wxWindows could not open display for '%s': exiting."),
634 (const char*) className
);
637 XSelectInput((Display
*) m_initialDisplay
,
638 XDefaultRootWindow((Display
*) m_initialDisplay
),
642 // install the X error handler
643 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
644 #endif // __WXDEBUG__
646 GetMainColormap(dpy
);
647 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
652 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
654 if (!display
) /* Must be called first with non-NULL display */
655 return m_mainColormap
;
657 int defaultScreen
= DefaultScreen((Display
*) display
);
658 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
660 Colormap c
= DefaultColormapOfScreen(screen
);
663 m_mainColormap
= (WXColormap
) c
;
665 return (WXColormap
) c
;
668 Window
wxGetWindowParent(Window window
)
670 Window parent
, root
= 0;
671 unsigned int noChildren
= 0;
672 Window
* children
= NULL
;
673 int res
= XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
674 & children
, & noChildren
);
687 retValue
= wxTheApp
->OnExit();
691 * Exit in some platform-specific way. Not recommended that the app calls this:
692 * only for emergencies.
697 // Yield to other processes
699 bool wxApp::Yield(bool onlyIfNeeded
)
701 bool s_inYield
= FALSE
;
707 wxFAIL_MSG( wxT("wxYield called recursively" ) );
715 while (wxTheApp
&& wxTheApp
->Pending())
716 wxTheApp
->Dispatch();
723 // TODO use XmGetPixmap (?) to get the really standard icons!
725 // XPM hack: make the arrays const
726 #define static static const
728 #include "wx/generic/info.xpm"
729 #include "wx/generic/error.xpm"
730 #include "wx/generic/question.xpm"
731 #include "wx/generic/warning.xpm"
736 wxApp::GetStdIcon(int which
) const
740 case wxICON_INFORMATION
:
741 return wxIcon(info_xpm
);
743 case wxICON_QUESTION
:
744 return wxIcon(question_xpm
);
746 case wxICON_EXCLAMATION
:
747 return wxIcon(warning_xpm
);
750 wxFAIL_MSG("requested non existent standard icon");
751 // still fall through
754 return wxIcon(error_xpm
);
758 // ----------------------------------------------------------------------------
759 // accessors for C modules
760 // ----------------------------------------------------------------------------
763 extern "C" XtAppContext
wxGetAppContext()
765 return (XtAppContext
)wxTheApp
->GetAppContext();