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"
35 #include "wx/thread.h"
38 #if wxUSE_WX_RESOURCES
39 #include "wx/resource.h"
43 #pragma message disable nosimpint
46 #include <X11/Xutil.h>
47 #include <X11/Xresource.h>
48 #include <X11/Xatom.h>
50 #pragma message enable nosimpint
53 #include "wx/x11/private.h"
57 extern char *wxBuffer
;
58 extern wxList wxPendingDelete
;
60 wxApp
*wxTheApp
= NULL
;
62 wxHashTable
*wxWidgetHashTable
= NULL
;
64 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
66 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
67 EVT_IDLE(wxApp::OnIdle
)
71 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
73 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
75 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
77 // just forward to the default handler for now
78 return gs_pfnXErrorHandler(dpy
, xevent
);
82 long wxApp::sm_lastMessageTime
= 0;
84 bool wxApp::Initialize()
86 wxBuffer
= new char[BUFSIZ
+ 512];
88 wxClassInfo::InitializeClasses();
90 // GL: I'm annoyed ... I don't know where to put this and I don't want to
91 // create a module for that as it's part of the core.
93 wxPendingEventsLocker
= new wxCriticalSection();
96 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
97 wxTheColourDatabase
->Initialize();
99 wxInitializeStockLists();
100 wxInitializeStockObjects();
102 #if wxUSE_WX_RESOURCES
103 wxInitializeResourceSystem();
106 wxBitmap::InitStandardHandlers();
108 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
110 wxModule::RegisterModules();
111 if (!wxModule::InitializeModules()) return FALSE
;
116 void wxApp::CleanUp()
118 delete wxWidgetHashTable
;
119 wxWidgetHashTable
= NULL
;
121 wxModule::CleanUpModules();
123 #if wxUSE_WX_RESOURCES
124 wxCleanUpResourceSystem();
127 wxDeleteStockObjects() ;
129 // Destroy all GDI lists, etc.
131 wxDeleteStockLists();
133 delete wxTheColourDatabase
;
134 wxTheColourDatabase
= NULL
;
136 wxBitmap::CleanUpHandlers();
141 wxClassInfo::CleanUpClasses();
146 // GL: I'm annoyed ... I don't know where to put this and I don't want to
147 // create a module for that as it's part of the core.
149 delete wxPendingEvents
;
150 delete wxPendingEventsLocker
;
153 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
154 // At this point we want to check if there are any memory
155 // blocks that aren't part of the wxDebugContext itself,
156 // as a special case. Then when dumping we need to ignore
157 // wxDebugContext, too.
158 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
160 wxLogDebug("There were memory leaks.\n");
161 wxDebugContext::Dump();
162 wxDebugContext::PrintStatistics();
166 // do it as the very last thing because everything else can log messages
167 wxLog::DontCreateOnDemand();
168 // do it as the very last thing because everything else can log messages
169 delete wxLog::SetActiveTarget(NULL
);
172 int wxEntry( int argc
, char *argv
[] )
174 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
175 // This seems to be necessary since there are 'rogue'
176 // objects present at this point (perhaps global objects?)
177 // Setting a checkpoint will ignore them as far as the
178 // memory checking facility is concerned.
179 // Of course you may argue that memory allocated in globals should be
180 // checked, but this is a reasonable compromise.
181 wxDebugContext::SetCheckpoint();
184 if (!wxApp::Initialize())
189 if (!wxApp::GetInitializerFunction())
191 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
195 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
200 printf( "wxWindows error: wxTheApp == NULL\n" );
204 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
205 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
207 wxTheApp
->argc
= argc
;
208 wxTheApp
->argv
= argv
;
210 // GUI-specific initialization, such as creating an app context.
211 wxTheApp
->OnInitGui();
213 // Here frames insert themselves automatically into wxTopLevelWindows by
214 // getting created in OnInit().
217 if (wxTheApp
->OnInit())
219 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
222 // flush the logged messages if any
223 wxLog
*pLog
= wxLog::GetActiveTarget();
224 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
227 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
228 // for further messages
230 if (wxTheApp
->GetTopWindow())
232 delete wxTheApp
->GetTopWindow();
233 wxTheApp
->SetTopWindow(NULL
);
236 wxTheApp
->DeletePendingObjects();
245 // Static member initialization
246 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
253 m_wantDebugOutput
= TRUE
;
257 m_exitOnFrameDelete
= TRUE
;
258 m_mainColormap
= (WXColormap
) NULL
;
259 m_topLevelWidget
= (WXWindow
) NULL
;
260 m_maxRequestSize
= 0;
261 m_initialDisplay
= (WXDisplay
*) 0;
264 bool wxApp::Initialized()
272 int wxApp::MainLoop()
277 * Sit around forever waiting to process X-events. Property Change
278 * event are handled special, because they have to refer to
279 * the root window rather than to a widget. therefore we can't
280 * use an Xt-eventhandler.
283 XSelectInput(wxGetDisplay(),
284 XDefaultRootWindow(wxGetDisplay()),
289 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
292 XNextEvent(wxGetDisplay(), & event
);
294 ProcessXEvent((WXEvent
*) & event
);
296 if (XtPending(wxGetDisplay()) == 0)
301 // leave the main loop to give other threads a chance to
302 // perform their GUI work
315 // Processes an X event.
316 void wxApp::ProcessXEvent(WXEvent
* _event
)
318 XEvent
* event
= (XEvent
*) _event
;
320 if (event
->type
== KeyPress
)
322 if (CheckForAccelerator(_event
))
324 // Do nothing! We intercepted and processed the event as an
329 // It seemed before that this hack was redundant and
330 // key down events were being generated by wxCanvasInputEvent.
331 // But no longer - why ???
333 else if (CheckForKeyDown(_event
))
335 // We intercepted and processed the key down event
341 // TODO for X11 implementation -- the equivalent of XtDispatchEvent.
342 // Presumably, we need to form the wxEvents and
343 // and send them to the appropriate windows.
344 // XtDispatchEvent(event);
348 else if (event
->type
== KeyRelease
)
350 // TODO: work out why we still need this ! -michael
352 if (CheckForKeyUp(_event
))
354 // We intercepted and processed the key up event
359 // TODO: The X equivalent of XtDispatchEvent
361 // XtDispatchEvent(event);
365 else if (event
->type
== PropertyNotify
)
367 HandlePropertyChange(_event
);
370 else if (event
->type
== ResizeRequest
)
372 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
373 * If resize event, don't resize until the last resize event for this
374 * window is recieved. Prevents flicker as windows are resized.
377 Display
*disp
= wxGetDisplay();
378 Window win
= event
->xany
.window
;
383 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
385 // TODO: when implementing refresh optimization, we can use
386 // XtAddExposureToRegion to expand the window's paint region.
388 // TODO: generate resize event
389 // XtDispatchEvent(event);
393 // TODO: generate all other events
394 // XtDispatchEvent(event);
398 // Returns TRUE if more time is needed.
399 bool wxApp::ProcessIdle()
402 event
.SetEventObject(this);
405 return event
.MoreRequested();
408 void wxApp::ExitMainLoop()
413 // Is a message/event pending?
414 bool wxApp::Pending()
416 XFlush(wxGetDisplay());
418 return (XPending(wxGetDisplay()) > 0);
421 // Dispatch a message.
422 void wxApp::Dispatch()
425 XNextEvent(wxGetDisplay(), & event
);
426 ProcessXEvent((WXEvent
*) & event
);
429 // This should be redefined in a derived class for
430 // handling property change events for XAtom IPC.
431 void wxApp::HandlePropertyChange(WXEvent
*event
)
433 // by default do nothing special
434 // TODO: what to do for X11
435 // XtDispatchEvent((XEvent*) event); /* let Motif do the work */
438 void wxApp::OnIdle(wxIdleEvent
& event
)
440 static bool inOnIdle
= FALSE
;
442 // Avoid recursion (via ProcessEvent default case)
448 // If there are pending events, we must process them: pending events
449 // are either events to the threads other than main or events posted
450 // with wxPostEvent() functions
451 // GRG: I have moved this here so that all pending events are processed
452 // before starting to delete any objects. This behaves better (in
453 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
454 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
455 // Changed Mar/2000 before 2.1.14
457 // Flush pending events.
458 ProcessPendingEvents();
460 // 'Garbage' collection of windows deleted with Close().
461 DeletePendingObjects();
463 // flush the logged messages if any
464 wxLog
*pLog
= wxLog::GetActiveTarget();
465 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
468 // Send OnIdle events to all windows
469 bool needMore
= SendIdleEvents();
472 event
.RequestMore(TRUE
);
479 // **** please implement me! ****
480 // Wake up the idle handler processor, even if it is in another thread...
484 // Send idle event to all top-level windows
485 bool wxApp::SendIdleEvents()
487 bool needMore
= FALSE
;
489 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
492 wxWindow
* win
= node
->GetData();
493 if (SendIdleEvents(win
))
495 node
= node
->GetNext();
501 // Send idle event to window and all subwindows
502 bool wxApp::SendIdleEvents(wxWindow
* win
)
504 bool needMore
= FALSE
;
507 event
.SetEventObject(win
);
508 win
->ProcessEvent(event
);
510 if (event
.MoreRequested())
513 wxNode
* node
= win
->GetChildren().First();
516 wxWindow
* win
= (wxWindow
*) node
->Data();
517 if (SendIdleEvents(win
))
525 void wxApp::DeletePendingObjects()
527 wxNode
*node
= wxPendingDelete
.First();
530 wxObject
*obj
= (wxObject
*)node
->Data();
534 if (wxPendingDelete
.Member(obj
))
537 // Deleting one object may have deleted other pending
538 // objects, so start from beginning of list again.
539 node
= wxPendingDelete
.First();
543 // Create an application context
544 bool wxApp::OnInitGui()
546 // TODO: parse argv and get display to pass to XOpenDisplay
547 Display
* dpy
= XOpenDisplay(NULL
);
548 m_initialDisplay
= (WXDisplay
*) dpy
;
551 wxString
className(wxTheApp
->GetClassName());
552 wxLogError(_("wxWindows could not open display for '%s': exiting."),
553 (const char*) className
);
558 // install the X error handler
559 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
560 #endif // __WXDEBUG__
562 // Do we need to create the top-level window initially?
564 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
565 applicationShellWidgetClass
,dpy
,
569 GetMainColormap(dpy
);
570 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
575 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
577 if (!display
) /* Must be called first with non-NULL display */
578 return m_mainColormap
;
580 int defaultScreen
= DefaultScreen((Display
*) display
);
581 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
583 Colormap c
= DefaultColormapOfScreen(screen
);
586 m_mainColormap
= (WXColormap
) c
;
588 return (WXColormap
) c
;
591 static Window
XGetParent(Window window
)
593 Window parent
, root
= 0;
594 unsigned int noChildren
= 0;
595 if (XQueryTree(wxGetDisplay(), window
, & root
, & parent
,
602 // Returns TRUE if an accelerator has been processed
603 bool wxApp::CheckForAccelerator(WXEvent
* event
)
605 XEvent
* xEvent
= (XEvent
*) event
;
606 if (xEvent
->xany
.type
== KeyPress
)
608 // Find a wxWindow for this window
609 // TODO: should get display for the window, not the current display
610 Window window
= xEvent
->xany
.window
;
611 wxWindow
* win
= NULL
;
613 // Find the first wxWindow that corresponds to this event window
614 while (window
&& !(win
= wxGetWindowFromTable(window
)))
615 window
= XGetParent(window
);
620 wxKeyEvent
keyEvent(wxEVT_CHAR
);
621 wxTranslateKeyEvent(keyEvent
, win
, (Window
) 0, xEvent
);
623 // Now we have a wxKeyEvent and we have a wxWindow.
624 // Go up the hierarchy until we find a matching accelerator,
625 // or we get to the top.
628 if (win
->ProcessAccelerator(keyEvent
))
630 win
= win
->GetParent();
637 bool wxApp::CheckForKeyDown(WXEvent
* event
)
639 XEvent
* xEvent
= (XEvent
*) event
;
640 if (xEvent
->xany
.type
== KeyPress
)
642 Window window
= xEvent
->xany
.window
;
643 wxWindow
* win
= NULL
;
645 // Find the first wxWindow that corresponds to this event window
646 while (window
&& !(win
= wxGetWindowFromTable(window
)))
647 window
= XGetParent(window
);
652 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
653 wxTranslateKeyEvent(keyEvent
, win
, (Window
) 0, xEvent
);
655 return win
->ProcessEvent( keyEvent
);
661 bool wxApp::CheckForKeyUp(WXEvent
* event
)
663 XEvent
* xEvent
= (XEvent
*) event
;
664 if (xEvent
->xany
.type
== KeyRelease
)
666 Window window
= xEvent
->xany
.window
;
667 wxWindow
* win
= NULL
;
669 // Find the first wxWindow that corresponds to this event window
670 while (window
&& !(win
= wxGetWindowFromTable(window
)))
671 window
= XGetParent(window
);
676 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
677 wxTranslateKeyEvent(keyEvent
, win
, (Window
) 0, xEvent
);
679 return win
->ProcessEvent( keyEvent
);
689 retValue
= wxTheApp
->OnExit();
693 * Exit in some platform-specific way. Not recommended that the app calls this:
694 * only for emergencies.
699 // Yield to other processes
701 bool wxApp::Yield(bool onlyIfNeeded
)
703 bool s_inYield
= FALSE
;
709 wxFAIL_MSG( wxT("wxYield called recursively" ) );
717 while (wxTheApp
&& wxTheApp
->Pending())
718 wxTheApp
->Dispatch();
725 // TODO use XmGetPixmap (?) to get the really standard icons!
727 // XPM hack: make the arrays const
728 #define static static const
730 #include "wx/generic/info.xpm"
731 #include "wx/generic/error.xpm"
732 #include "wx/generic/question.xpm"
733 #include "wx/generic/warning.xpm"
738 wxApp::GetStdIcon(int which
) const
742 case wxICON_INFORMATION
:
743 return wxIcon(info_xpm
);
745 case wxICON_QUESTION
:
746 return wxIcon(question_xpm
);
748 case wxICON_EXCLAMATION
:
749 return wxIcon(warning_xpm
);
752 wxFAIL_MSG("requested non existent standard icon");
753 // still fall through
756 return wxIcon(error_xpm
);
760 // ----------------------------------------------------------------------------
761 // accessors for C modules
762 // ----------------------------------------------------------------------------
765 extern "C" XtAppContext
wxGetAppContext()
767 return (XtAppContext
)wxTheApp
->GetAppContext();