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"
33 #include "wx/evtloop.h"
36 #include "wx/thread.h"
39 #if wxUSE_WX_RESOURCES
40 #include "wx/resource.h"
44 #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/x11/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 Window
XGetParent(Window window
);
78 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
80 // just forward to the default handler for now
81 return gs_pfnXErrorHandler(dpy
, xevent
);
85 long wxApp::sm_lastMessageTime
= 0;
87 bool wxApp::Initialize()
89 wxBuffer
= new char[BUFSIZ
+ 512];
91 wxClassInfo::InitializeClasses();
93 // GL: I'm annoyed ... I don't know where to put this and I don't want to
94 // create a module for that as it's part of the core.
96 wxPendingEventsLocker
= new wxCriticalSection();
99 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
100 wxTheColourDatabase
->Initialize();
102 wxInitializeStockLists();
103 wxInitializeStockObjects();
105 #if wxUSE_WX_RESOURCES
106 wxInitializeResourceSystem();
109 wxBitmap::InitStandardHandlers();
111 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
113 wxModule::RegisterModules();
114 if (!wxModule::InitializeModules()) return FALSE
;
119 void wxApp::CleanUp()
121 delete wxWidgetHashTable
;
122 wxWidgetHashTable
= NULL
;
124 wxModule::CleanUpModules();
126 #if wxUSE_WX_RESOURCES
127 wxCleanUpResourceSystem();
130 wxDeleteStockObjects() ;
132 // Destroy all GDI lists, etc.
134 wxDeleteStockLists();
136 delete wxTheColourDatabase
;
137 wxTheColourDatabase
= NULL
;
139 wxBitmap::CleanUpHandlers();
144 wxClassInfo::CleanUpClasses();
149 // GL: I'm annoyed ... I don't know where to put this and I don't want to
150 // create a module for that as it's part of the core.
152 delete wxPendingEvents
;
153 delete wxPendingEventsLocker
;
156 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
157 // At this point we want to check if there are any memory
158 // blocks that aren't part of the wxDebugContext itself,
159 // as a special case. Then when dumping we need to ignore
160 // wxDebugContext, too.
161 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
163 wxLogDebug("There were memory leaks.\n");
164 wxDebugContext::Dump();
165 wxDebugContext::PrintStatistics();
169 // do it as the very last thing because everything else can log messages
170 wxLog::DontCreateOnDemand();
171 // do it as the very last thing because everything else can log messages
172 delete wxLog::SetActiveTarget(NULL
);
175 int wxEntry( int argc
, char *argv
[] )
177 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
178 // This seems to be necessary since there are 'rogue'
179 // objects present at this point (perhaps global objects?)
180 // Setting a checkpoint will ignore them as far as the
181 // memory checking facility is concerned.
182 // Of course you may argue that memory allocated in globals should be
183 // checked, but this is a reasonable compromise.
184 wxDebugContext::SetCheckpoint();
187 if (!wxApp::Initialize())
192 if (!wxApp::GetInitializerFunction())
194 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
198 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
203 printf( "wxWindows error: wxTheApp == NULL\n" );
207 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
208 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
210 wxTheApp
->argc
= argc
;
211 wxTheApp
->argv
= argv
;
213 // GUI-specific initialization, such as creating an app context.
214 wxTheApp
->OnInitGui();
216 // Here frames insert themselves automatically into wxTopLevelWindows by
217 // getting created in OnInit().
220 if (wxTheApp
->OnInit())
222 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
225 // flush the logged messages if any
226 wxLog
*pLog
= wxLog::GetActiveTarget();
227 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
230 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
231 // for further messages
233 if (wxTheApp
->GetTopWindow())
235 delete wxTheApp
->GetTopWindow();
236 wxTheApp
->SetTopWindow(NULL
);
239 wxTheApp
->DeletePendingObjects();
248 // Static member initialization
249 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
256 m_wantDebugOutput
= TRUE
;
260 m_exitOnFrameDelete
= TRUE
;
261 m_mainColormap
= (WXColormap
) NULL
;
262 m_topLevelWidget
= (WXWindow
) NULL
;
263 m_maxRequestSize
= 0;
264 m_initialDisplay
= (WXDisplay
*) 0;
268 bool wxApp::Initialized()
276 int wxApp::MainLoop()
279 m_mainLoop
= new wxEventLoop
;
281 rt
= m_mainLoop
->Run();
288 // Processes an X event.
289 void wxApp::ProcessXEvent(WXEvent
* _event
)
291 XEvent
* event
= (XEvent
*) _event
;
293 wxWindow
* win
= NULL
;
294 Window window
= event
->xany
.window
;
295 Window actualWindow
= window
;
297 // Find the first wxWindow that corresponds to this event window
298 // TODO: may need to translate coordinates from actualWindow
299 // to window, if the receiving window != wxWindow window
300 while (window
&& !(win
= wxGetWindowFromTable(window
)))
301 window
= XGetParent(window
);
303 // TODO: shouldn't all the ProcessEvents below
304 // be win->GetEventHandler()->ProcessEvent?
309 if (CheckForAccelerator(_event
))
311 // Do nothing! We intercepted and processed the event as an
319 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
320 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
322 // We didn't process wxEVT_KEY_DOWN, so send
324 if (!win
->ProcessEvent( keyEvent
))
326 keyEvent
.SetEventType(wxEVT_CHAR
);
327 win
->ProcessEvent( keyEvent
);
330 // We intercepted and processed the key down event
340 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
341 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
343 win
->ProcessEvent( keyEvent
);
349 HandlePropertyChange(_event
);
354 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
355 * If resize event, don't resize until the last resize event for this
356 * window is recieved. Prevents flicker as windows are resized.
359 Display
*disp
= (Display
*) wxGetDisplay();
364 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
366 // TODO: when implementing refresh optimization, we can use
367 // XtAddExposureToRegion to expand the window's paint region.
371 wxSize sz
= win
->GetSize();
372 wxSizeEvent
sizeEvent(sz
, win
->GetId());
373 sizeEvent
.SetEventObject(win
);
375 win
->ProcessEvent( sizeEvent
);
384 win
->AddUpdateRect(event
->xexpose
.x
, event
->xexpose
.y
,
385 event
->xexpose
.width
, event
->xexpose
.height
);
387 if (event
-> xexpose
.count
== 0)
390 win
->ClearUpdateRects();
404 wxMouseEvent wxevent
;
405 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
406 win
->ProcessEvent( wxevent
);
412 if (win
&& event
->xfocus
.detail
!= NotifyPointer
)
414 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
415 focusEvent
.SetEventObject(win
);
416 win
->ProcessEvent(focusEvent
);
422 if (win
&& event
->xfocus
.detail
!= NotifyPointer
)
424 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
425 focusEvent
.SetEventObject(win
);
426 win
->ProcessEvent(focusEvent
);
437 // Returns TRUE if more time is needed.
438 // Note that this duplicates wxEventLoopImpl::SendIdleEvent
439 // but ProcessIdle may be needed by apps, so is kept.
440 bool wxApp::ProcessIdle()
443 event
.SetEventObject(this);
446 return event
.MoreRequested();
449 void wxApp::ExitMainLoop()
455 // Is a message/event pending?
456 bool wxApp::Pending()
458 return wxEventLoop::GetActive()->Pending();
461 // Dispatch a message.
462 void wxApp::Dispatch()
464 wxEventLoop::GetActive()->Dispatch();
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 // TODO: what to do for X11
473 // XtDispatchEvent((XEvent*) event); /* let Motif do the work */
476 void wxApp::OnIdle(wxIdleEvent
& event
)
478 static bool inOnIdle
= FALSE
;
480 // Avoid recursion (via ProcessEvent default case)
486 // If there are pending events, we must process them: pending events
487 // are either events to the threads other than main or events posted
488 // with wxPostEvent() functions
489 // GRG: I have moved this here so that all pending events are processed
490 // before starting to delete any objects. This behaves better (in
491 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
492 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
493 // Changed Mar/2000 before 2.1.14
495 // Flush pending events.
496 ProcessPendingEvents();
498 // 'Garbage' collection of windows deleted with Close().
499 DeletePendingObjects();
501 // flush the logged messages if any
502 wxLog
*pLog
= wxLog::GetActiveTarget();
503 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
506 // Send OnIdle events to all windows
507 bool needMore
= SendIdleEvents();
510 event
.RequestMore(TRUE
);
517 // **** please implement me! ****
518 // Wake up the idle handler processor, even if it is in another thread...
522 // Send idle event to all top-level windows
523 bool wxApp::SendIdleEvents()
525 bool needMore
= FALSE
;
527 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
530 wxWindow
* win
= node
->GetData();
531 if (SendIdleEvents(win
))
533 node
= node
->GetNext();
539 // Send idle event to window and all subwindows
540 bool wxApp::SendIdleEvents(wxWindow
* win
)
542 bool needMore
= FALSE
;
545 event
.SetEventObject(win
);
546 win
->ProcessEvent(event
);
548 if (event
.MoreRequested())
551 wxNode
* node
= win
->GetChildren().First();
554 wxWindow
* win
= (wxWindow
*) node
->Data();
555 if (SendIdleEvents(win
))
563 void wxApp::DeletePendingObjects()
565 wxNode
*node
= wxPendingDelete
.First();
568 wxObject
*obj
= (wxObject
*)node
->Data();
572 if (wxPendingDelete
.Member(obj
))
575 // Deleting one object may have deleted other pending
576 // objects, so start from beginning of list again.
577 node
= wxPendingDelete
.First();
581 // Create an application context
582 bool wxApp::OnInitGui()
584 // TODO: parse argv and get display to pass to XOpenDisplay
585 Display
* dpy
= XOpenDisplay(NULL
);
586 m_initialDisplay
= (WXDisplay
*) dpy
;
589 wxString
className(wxTheApp
->GetClassName());
590 wxLogError(_("wxWindows could not open display for '%s': exiting."),
591 (const char*) className
);
594 XSelectInput((Display
*) m_initialDisplay
,
595 XDefaultRootWindow((Display
*) m_initialDisplay
),
599 // install the X error handler
600 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
601 #endif // __WXDEBUG__
603 GetMainColormap(dpy
);
604 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
609 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
611 if (!display
) /* Must be called first with non-NULL display */
612 return m_mainColormap
;
614 int defaultScreen
= DefaultScreen((Display
*) display
);
615 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
617 Colormap c
= DefaultColormapOfScreen(screen
);
620 m_mainColormap
= (WXColormap
) c
;
622 return (WXColormap
) c
;
625 static Window
XGetParent(Window window
)
627 Window parent
, root
= 0;
628 unsigned int noChildren
= 0;
629 if (XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
636 // Returns TRUE if an accelerator has been processed
637 bool wxApp::CheckForAccelerator(WXEvent
* event
)
639 XEvent
* xEvent
= (XEvent
*) event
;
640 if (xEvent
->xany
.type
== KeyPress
)
642 // Find a wxWindow for this window
643 // TODO: should get display for the window, not the current display
644 Window window
= xEvent
->xany
.window
;
645 wxWindow
* win
= NULL
;
647 // Find the first wxWindow that corresponds to this event window
648 while (window
&& !(win
= wxGetWindowFromTable(window
)))
649 window
= XGetParent(window
);
654 wxKeyEvent
keyEvent(wxEVT_CHAR
);
655 wxTranslateKeyEvent(keyEvent
, win
, (Window
) 0, xEvent
);
657 // Now we have a wxKeyEvent and we have a wxWindow.
658 // Go up the hierarchy until we find a matching accelerator,
659 // or we get to the top.
662 if (win
->ProcessAccelerator(keyEvent
))
664 win
= win
->GetParent();
675 retValue
= wxTheApp
->OnExit();
679 * Exit in some platform-specific way. Not recommended that the app calls this:
680 * only for emergencies.
685 // Yield to other processes
687 bool wxApp::Yield(bool onlyIfNeeded
)
689 bool s_inYield
= FALSE
;
695 wxFAIL_MSG( wxT("wxYield called recursively" ) );
703 while (wxTheApp
&& wxTheApp
->Pending())
704 wxTheApp
->Dispatch();
711 // TODO use XmGetPixmap (?) to get the really standard icons!
713 // XPM hack: make the arrays const
714 #define static static const
716 #include "wx/generic/info.xpm"
717 #include "wx/generic/error.xpm"
718 #include "wx/generic/question.xpm"
719 #include "wx/generic/warning.xpm"
724 wxApp::GetStdIcon(int which
) const
728 case wxICON_INFORMATION
:
729 return wxIcon(info_xpm
);
731 case wxICON_QUESTION
:
732 return wxIcon(question_xpm
);
734 case wxICON_EXCLAMATION
:
735 return wxIcon(warning_xpm
);
738 wxFAIL_MSG("requested non existent standard icon");
739 // still fall through
742 return wxIcon(error_xpm
);
746 // ----------------------------------------------------------------------------
747 // accessors for C modules
748 // ----------------------------------------------------------------------------
751 extern "C" XtAppContext
wxGetAppContext()
753 return (XtAppContext
)wxTheApp
->GetAppContext();