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())
313 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
314 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
316 // We didn't process wxEVT_KEY_DOWN, so send
318 if (!win
->GetEventHandler()->ProcessEvent( keyEvent
))
320 keyEvent
.SetEventType(wxEVT_CHAR
);
321 win
->GetEventHandler()->ProcessEvent( keyEvent
);
324 // We intercepted and processed the key down event
332 if (win
&& !win
->IsEnabled())
337 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
338 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
340 win
->GetEventHandler()->ProcessEvent( keyEvent
);
346 HandlePropertyChange(_event
);
351 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);;
352 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);;
354 if (event
->xclient
.message_type
== wm_protocols
)
356 if (event
->xclient
.data
.l
[0] == wm_delete_window
)
368 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
369 * If resize event, don't resize until the last resize event for this
370 * window is recieved. Prevents flicker as windows are resized.
373 Display
*disp
= (Display
*) wxGetDisplay();
378 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
380 // TODO: when implementing refresh optimization, we can use
381 // XtAddExposureToRegion to expand the window's paint region.
385 wxSize sz
= win
->GetSize();
386 wxSizeEvent
sizeEvent(sz
, win
->GetId());
387 sizeEvent
.SetEventObject(win
);
389 win
->GetEventHandler()->ProcessEvent( sizeEvent
);
398 win
->GetUpdateRegion().Union( event
->xexpose
.x
, event
->xexpose
.y
,
399 event
->xexpose
.width
, event
->xexpose
.height
);
400 if (event
->xexpose
.count
== 0)
402 win
->X11SendPaintEvents(); // TODO let an idle handler do that
414 if (win
&& !win
->IsEnabled())
419 wxMouseEvent wxevent
;
420 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
421 win
->GetEventHandler()->ProcessEvent( wxevent
);
427 if (win
&& event
->xfocus
.detail
!= NotifyPointer
)
429 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
430 focusEvent
.SetEventObject(win
);
431 win
->GetEventHandler()->ProcessEvent(focusEvent
);
437 if (win
&& event
->xfocus
.detail
!= NotifyPointer
)
439 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
440 focusEvent
.SetEventObject(win
);
441 win
->GetEventHandler()->ProcessEvent(focusEvent
);
447 // Do we want to process this (for top-level windows)?
448 // But we want to be able to veto closes, anyway
458 // Returns TRUE if more time is needed.
459 // Note that this duplicates wxEventLoopImpl::SendIdleEvent
460 // but ProcessIdle may be needed by apps, so is kept.
461 bool wxApp::ProcessIdle()
464 event
.SetEventObject(this);
467 return event
.MoreRequested();
470 void wxApp::ExitMainLoop()
476 // Is a message/event pending?
477 bool wxApp::Pending()
479 return wxEventLoop::GetActive()->Pending();
482 // Dispatch a message.
483 void wxApp::Dispatch()
485 wxEventLoop::GetActive()->Dispatch();
488 // This should be redefined in a derived class for
489 // handling property change events for XAtom IPC.
490 void wxApp::HandlePropertyChange(WXEvent
*event
)
492 // by default do nothing special
493 // TODO: what to do for X11
494 // XtDispatchEvent((XEvent*) event); /* let Motif do the work */
497 void wxApp::OnIdle(wxIdleEvent
& event
)
499 static bool inOnIdle
= FALSE
;
501 // Avoid recursion (via ProcessEvent default case)
507 // If there are pending events, we must process them: pending events
508 // are either events to the threads other than main or events posted
509 // with wxPostEvent() functions
510 // GRG: I have moved this here so that all pending events are processed
511 // before starting to delete any objects. This behaves better (in
512 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
513 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
514 // Changed Mar/2000 before 2.1.14
516 // Flush pending events.
517 ProcessPendingEvents();
519 // 'Garbage' collection of windows deleted with Close().
520 DeletePendingObjects();
522 // flush the logged messages if any
523 wxLog
*pLog
= wxLog::GetActiveTarget();
524 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
527 // Send OnIdle events to all windows
528 bool needMore
= SendIdleEvents();
531 event
.RequestMore(TRUE
);
538 // **** please implement me! ****
539 // Wake up the idle handler processor, even if it is in another thread...
543 // Send idle event to all top-level windows
544 bool wxApp::SendIdleEvents()
546 bool needMore
= FALSE
;
548 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
551 wxWindow
* win
= node
->GetData();
552 if (SendIdleEvents(win
))
554 node
= node
->GetNext();
560 // Send idle event to window and all subwindows
561 bool wxApp::SendIdleEvents(wxWindow
* win
)
563 bool needMore
= FALSE
;
566 event
.SetEventObject(win
);
567 win
->ProcessEvent(event
);
569 if (event
.MoreRequested())
572 wxNode
* node
= win
->GetChildren().First();
575 wxWindow
* win
= (wxWindow
*) node
->Data();
576 if (SendIdleEvents(win
))
584 void wxApp::DeletePendingObjects()
586 wxNode
*node
= wxPendingDelete
.First();
589 wxObject
*obj
= (wxObject
*)node
->Data();
593 if (wxPendingDelete
.Member(obj
))
596 // Deleting one object may have deleted other pending
597 // objects, so start from beginning of list again.
598 node
= wxPendingDelete
.First();
602 // Create an application context
603 bool wxApp::OnInitGui()
605 // TODO: parse argv and get display to pass to XOpenDisplay
606 Display
* dpy
= XOpenDisplay(NULL
);
607 m_initialDisplay
= (WXDisplay
*) dpy
;
610 wxString
className(wxTheApp
->GetClassName());
611 wxLogError(_("wxWindows could not open display for '%s': exiting."),
612 (const char*) className
);
615 XSelectInput((Display
*) m_initialDisplay
,
616 XDefaultRootWindow((Display
*) m_initialDisplay
),
620 // install the X error handler
621 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
622 #endif // __WXDEBUG__
624 GetMainColormap(dpy
);
625 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
630 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
632 if (!display
) /* Must be called first with non-NULL display */
633 return m_mainColormap
;
635 int defaultScreen
= DefaultScreen((Display
*) display
);
636 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
638 Colormap c
= DefaultColormapOfScreen(screen
);
641 m_mainColormap
= (WXColormap
) c
;
643 return (WXColormap
) c
;
646 Window
wxGetParentWindow(Window window
)
648 Window parent
, root
= 0;
649 unsigned int noChildren
= 0;
650 if (XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
661 retValue
= wxTheApp
->OnExit();
665 * Exit in some platform-specific way. Not recommended that the app calls this:
666 * only for emergencies.
671 // Yield to other processes
673 bool wxApp::Yield(bool onlyIfNeeded
)
675 bool s_inYield
= FALSE
;
681 wxFAIL_MSG( wxT("wxYield called recursively" ) );
689 while (wxTheApp
&& wxTheApp
->Pending())
690 wxTheApp
->Dispatch();
697 // TODO use XmGetPixmap (?) to get the really standard icons!
699 // XPM hack: make the arrays const
700 #define static static const
702 #include "wx/generic/info.xpm"
703 #include "wx/generic/error.xpm"
704 #include "wx/generic/question.xpm"
705 #include "wx/generic/warning.xpm"
710 wxApp::GetStdIcon(int which
) const
714 case wxICON_INFORMATION
:
715 return wxIcon(info_xpm
);
717 case wxICON_QUESTION
:
718 return wxIcon(question_xpm
);
720 case wxICON_EXCLAMATION
:
721 return wxIcon(warning_xpm
);
724 wxFAIL_MSG("requested non existent standard icon");
725 // still fall through
728 return wxIcon(error_xpm
);
732 // ----------------------------------------------------------------------------
733 // accessors for C modules
734 // ----------------------------------------------------------------------------
737 extern "C" XtAppContext
wxGetAppContext()
739 return (XtAppContext
)wxTheApp
->GetAppContext();