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
47 #include <X11/Xutil.h>
48 #include <X11/Xresource.h>
49 #include <X11/Xatom.h>
51 #pragma message enable nosimpint
54 #include "wx/motif/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
)
71 long wxApp::sm_lastMessageTime
= 0;
73 bool wxApp::Initialize()
75 wxBuffer
= new char[BUFSIZ
+ 512];
77 wxClassInfo::InitializeClasses();
79 // GL: I'm annoyed ... I don't know where to put this and I don't want to
80 // create a module for that as it's part of the core.
82 wxPendingEventsLocker
= new wxCriticalSection();
85 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
86 wxTheColourDatabase
->Initialize();
88 wxInitializeStockLists();
89 wxInitializeStockObjects();
91 #if wxUSE_WX_RESOURCES
92 wxInitializeResourceSystem();
95 // For PostScript printing
97 /* Done using wxModule now
98 wxInitializePrintSetupData();
99 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
100 wxThePrintPaperDatabase->CreateDatabase();
104 wxBitmap::InitStandardHandlers();
106 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
108 wxModule::RegisterModules();
109 if (!wxModule::InitializeModules()) return FALSE
;
114 void wxApp::CleanUp()
116 delete wxWidgetHashTable
;
117 wxWidgetHashTable
= NULL
;
119 wxModule::CleanUpModules();
121 #if wxUSE_WX_RESOURCES
122 wxCleanUpResourceSystem();
125 wxDeleteStockObjects() ;
127 // Destroy all GDI lists, etc.
129 delete wxTheBrushList
;
130 wxTheBrushList
= NULL
;
135 delete wxTheFontList
;
136 wxTheFontList
= NULL
;
138 delete wxTheBitmapList
;
139 wxTheBitmapList
= NULL
;
141 delete wxTheColourDatabase
;
142 wxTheColourDatabase
= NULL
;
145 /* Done using wxModule now
146 wxInitializePrintSetupData(FALSE);
147 delete wxThePrintPaperDatabase;
148 wxThePrintPaperDatabase = NULL;
152 wxBitmap::CleanUpHandlers();
157 wxClassInfo::CleanUpClasses();
162 // GL: I'm annoyed ... I don't know where to put this and I don't want to
163 // create a module for that as it's part of the core.
165 delete wxPendingEvents
;
166 delete wxPendingEventsLocker
;
169 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
170 // At this point we want to check if there are any memory
171 // blocks that aren't part of the wxDebugContext itself,
172 // as a special case. Then when dumping we need to ignore
173 // wxDebugContext, too.
174 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
176 wxLogDebug("There were memory leaks.\n");
177 wxDebugContext::Dump();
178 wxDebugContext::PrintStatistics();
182 // do it as the very last thing because everything else can log messages
183 wxLog::DontCreateOnDemand();
184 // do it as the very last thing because everything else can log messages
185 delete wxLog::SetActiveTarget(NULL
);
188 int wxEntry( int argc
, char *argv
[] )
190 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
191 // This seems to be necessary since there are 'rogue'
192 // objects present at this point (perhaps global objects?)
193 // Setting a checkpoint will ignore them as far as the
194 // memory checking facility is concerned.
195 // Of course you may argue that memory allocated in globals should be
196 // checked, but this is a reasonable compromise.
197 wxDebugContext::SetCheckpoint();
200 if (!wxApp::Initialize())
205 if (!wxApp::GetInitializerFunction())
207 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
211 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
216 printf( "wxWindows error: wxTheApp == NULL\n" );
220 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
221 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
223 wxTheApp
->argc
= argc
;
224 wxTheApp
->argv
= argv
;
226 // GUI-specific initialization, such as creating an app context.
227 wxTheApp
->OnInitGui();
229 // Here frames insert themselves automatically into wxTopLevelWindows by
230 // getting created in OnInit().
233 if (wxTheApp
->OnInit())
235 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
238 // flush the logged messages if any
239 wxLog
*pLog
= wxLog::GetActiveTarget();
240 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
243 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
244 // for further messages
246 if (wxTheApp
->GetTopWindow())
248 delete wxTheApp
->GetTopWindow();
249 wxTheApp
->SetTopWindow(NULL
);
252 wxTheApp
->DeletePendingObjects();
261 // Static member initialization
262 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
269 m_wantDebugOutput
= TRUE
;
273 m_exitOnFrameDelete
= TRUE
;
275 m_mainColormap
= (WXColormap
) NULL
;
276 m_appContext
= (WXAppContext
) NULL
;
277 m_topLevelWidget
= (WXWidget
) NULL
;
278 m_maxRequestSize
= 0;
279 m_initialDisplay
= (WXDisplay
*) 0;
282 bool wxApp::Initialized()
290 int wxApp::MainLoop()
295 * Sit around forever waiting to process X-events. Property Change
296 * event are handled special, because they have to refer to
297 * the root window rather than to a widget. therefore we can't
298 * use an Xt-eventhandler.
301 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
302 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
307 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
310 XtAppNextEvent( (XtAppContext
) wxTheApp
->GetAppContext(), &event
);
312 ProcessXEvent((WXEvent
*) & event
);
314 if (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) == 0)
319 // leave the main loop to give other threads a chance to
320 // perform their GUI work
333 // Processes an X event.
334 void wxApp::ProcessXEvent(WXEvent
* _event
)
336 XEvent
* event
= (XEvent
*) _event
;
338 if (event
->type
== KeyPress
)
341 Widget widget
= XtWindowToWidget(event
->xany
.display
, event
->xany
.window
);
342 wxLogDebug("Got key press event for 0x%08x (parent = 0x%08x)",
343 widget
, XtParent(widget
));
346 if (CheckForAccelerator(_event
))
348 // Do nothing! We intercepted and processed the event as an
353 // It seemed before that this hack was redundant and
354 // key down events were being generated by wxCanvasInputEvent.
355 // But no longer - why ???
357 else if (CheckForKeyDown(_event
))
359 // We intercepted and processed the key down event
365 XtDispatchEvent(event
);
369 else if (event
->type
== KeyRelease
)
371 // TODO: work out why we still need this ! -michael
373 if (CheckForKeyUp(_event
))
375 // We intercepted and processed the key up event
380 XtDispatchEvent(event
);
384 else if (event
->type
== PropertyNotify
)
386 HandlePropertyChange(_event
);
389 else if (event
->type
== ResizeRequest
)
391 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
392 * If resize event, don't resize until the last resize event for this
393 * window is recieved. Prevents flicker as windows are resized.
396 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
397 Window win
= event
->xany
.window
;
402 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
404 // TODO: when implementing refresh optimization, we can use
405 // XtAddExposureToRegion to expand the window's paint region.
407 XtDispatchEvent(event
);
411 XtDispatchEvent(event
);
415 // Returns TRUE if more time is needed.
416 bool wxApp::ProcessIdle()
419 event
.SetEventObject(this);
422 return event
.MoreRequested();
425 void wxApp::ExitMainLoop()
430 // Is a message/event pending?
431 bool wxApp::Pending()
433 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
435 // Fix by Doug from STI, to prevent a stall if non-X event
437 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
440 // Dispatch a message.
441 void wxApp::Dispatch()
443 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
446 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
447 ProcessXEvent((WXEvent
*) & event
);
450 // This should be redefined in a derived class for
451 // handling property change events for XAtom IPC.
452 void wxApp::HandlePropertyChange(WXEvent
*event
)
454 // by default do nothing special
455 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
458 void wxApp::OnIdle(wxIdleEvent
& event
)
460 static bool inOnIdle
= FALSE
;
462 // Avoid recursion (via ProcessEvent default case)
468 // 'Garbage' collection of windows deleted with Close().
469 DeletePendingObjects();
472 // Flush pending events.
473 ProcessPendingEvents();
476 // flush the logged messages if any
477 wxLog
*pLog
= wxLog::GetActiveTarget();
478 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
481 // Send OnIdle events to all windows
482 bool needMore
= SendIdleEvents();
485 event
.RequestMore(TRUE
);
492 // **** please implement me! ****
493 // Wake up the idle handler processor, even if it is in another thread...
497 // Send idle event to all top-level windows
498 bool wxApp::SendIdleEvents()
500 bool needMore
= FALSE
;
502 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
505 wxWindow
* win
= node
->GetData();
506 if (SendIdleEvents(win
))
508 node
= node
->GetNext();
514 // Send idle event to window and all subwindows
515 bool wxApp::SendIdleEvents(wxWindow
* win
)
517 bool needMore
= FALSE
;
520 event
.SetEventObject(win
);
521 win
->ProcessEvent(event
);
523 if (event
.MoreRequested())
526 wxNode
* node
= win
->GetChildren().First();
529 wxWindow
* win
= (wxWindow
*) node
->Data();
530 if (SendIdleEvents(win
))
538 void wxApp::DeletePendingObjects()
540 wxNode
*node
= wxPendingDelete
.First();
543 wxObject
*obj
= (wxObject
*)node
->Data();
547 if (wxPendingDelete
.Member(obj
))
550 // Deleting one object may have deleted other pending
551 // objects, so start from beginning of list again.
552 node
= wxPendingDelete
.First();
556 // Create an application context
557 bool wxApp::OnInitGui()
559 XtToolkitInitialize() ;
560 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
561 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
562 (const char*) wxTheApp
->GetClassName(), NULL
, 0,
563 # if XtSpecificationRelease < 5
571 wxString
className(wxTheApp
->GetClassName());
572 wxLogError(_("wxWindows could not open display for '%s': exiting."),
573 (const char*) className
);
576 m_initialDisplay
= (WXDisplay
*) dpy
;
578 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
579 applicationShellWidgetClass
,dpy
,
582 // Add general resize proc
584 rec
.string
= "resize";
585 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
586 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
588 GetMainColormap(dpy
);
589 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
594 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
596 if (!display
) /* Must be called first with non-NULL display */
597 return m_mainColormap
;
599 int defaultScreen
= DefaultScreen((Display
*) display
);
600 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
602 Colormap c
= DefaultColormapOfScreen(screen
);
605 m_mainColormap
= (WXColormap
) c
;
607 return (WXColormap
) c
;
610 // Returns TRUE if an accelerator has been processed
611 bool wxApp::CheckForAccelerator(WXEvent
* event
)
613 XEvent
* xEvent
= (XEvent
*) event
;
614 if (xEvent
->xany
.type
== KeyPress
)
616 // Find a wxWindow for this window
617 // TODO: should get display for the window, not the current display
618 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
619 wxWindow
* win
= NULL
;
621 // Find the first wxWindow that corresponds to this event window
622 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
623 widget
= XtParent(widget
);
628 wxKeyEvent
keyEvent(wxEVT_CHAR
);
629 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
631 // Now we have a wxKeyEvent and we have a wxWindow.
632 // Go up the hierarchy until we find a matching accelerator,
633 // or we get to the top.
636 if (win
->ProcessAccelerator(keyEvent
))
638 win
= win
->GetParent();
645 bool wxApp::CheckForKeyDown(WXEvent
* event
)
647 XEvent
* xEvent
= (XEvent
*) event
;
648 if (xEvent
->xany
.type
== KeyPress
)
650 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
651 xEvent
->xany
.window
);
652 wxWindow
* win
= NULL
;
654 // Find the first wxWindow that corresponds to this event window
655 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
656 widget
= XtParent(widget
);
661 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
662 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
664 return win
->ProcessEvent( keyEvent
);
670 bool wxApp::CheckForKeyUp(WXEvent
* event
)
672 XEvent
* xEvent
= (XEvent
*) event
;
673 if (xEvent
->xany
.type
== KeyRelease
)
675 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
676 xEvent
->xany
.window
);
677 wxWindow
* win
= NULL
;
679 // Find the first wxWindow that corresponds to this event window
680 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
681 widget
= XtParent(widget
);
686 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
687 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
689 return win
->ProcessEvent( keyEvent
);
699 retValue
= wxTheApp
->OnExit();
703 * Exit in some platform-specific way. Not recommended that the app calls this:
704 * only for emergencies.
709 // Yield to other processes
712 while (wxTheApp
&& wxTheApp
->Pending())
713 wxTheApp
->Dispatch();
715 // VZ: is it the same as this (taken from old wxExecute)?
717 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
723 // TODO use XmGetPixmap (?) to get the really standard icons!
725 #include "wx/generic/info.xpm"
726 #include "wx/generic/error.xpm"
727 #include "wx/generic/question.xpm"
728 #include "wx/generic/warning.xpm"
731 wxApp::GetStdIcon(int which
) const
735 case wxICON_INFORMATION
:
736 return wxIcon(info_xpm
);
738 case wxICON_QUESTION
:
739 return wxIcon(question_xpm
);
741 case wxICON_EXCLAMATION
:
742 return wxIcon(warning_xpm
);
745 wxFAIL_MSG("requested non existent standard icon");
746 // still fall through
749 return wxIcon(error_xpm
);
753 // ----------------------------------------------------------------------------
754 // accessors for C modules
755 // ----------------------------------------------------------------------------
757 extern "C" XtAppContext
wxGetAppContext()
759 return (XtAppContext
)wxTheApp
->GetAppContext();