1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.h"
14 #pragma implementation "appbase.h"
20 #include "wx/gdicmn.h"
23 #include "wx/cursor.h"
25 #include "wx/palette.h"
27 #include "wx/dialog.h"
28 #include "wx/msgdlg.h"
30 #include "wx/module.h"
31 #include "wx/memory.h"
36 #include "wx/thread.h"
39 #if wxUSE_WX_RESOURCES
40 #include "wx/resource.h"
45 #include <X11/Xutil.h>
46 #include <X11/Xresource.h>
47 #include <X11/Xatom.h>
49 #include "wx/motif/private.h"
53 extern char *wxBuffer
;
54 extern wxList wxPendingDelete
;
56 wxApp
*wxTheApp
= NULL
;
58 wxHashTable
*wxWidgetHashTable
= NULL
;
60 #if !USE_SHARED_LIBRARY
61 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
63 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
64 EVT_IDLE(wxApp::OnIdle
)
68 long wxApp::sm_lastMessageTime
= 0;
70 bool wxApp::Initialize()
72 wxBuffer
= new char[BUFSIZ
+ 512];
74 wxClassInfo::InitializeClasses();
76 // GL: I'm annoyed ... I don't know where to put this and I don't want to
77 // create a module for that as it's part of the core.
79 wxPendingEventsLocker
= new wxCriticalSection();
82 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
83 wxTheColourDatabase
->Initialize();
85 wxInitializeStockLists();
86 wxInitializeStockObjects();
88 #if wxUSE_WX_RESOURCES
89 wxInitializeResourceSystem();
92 // For PostScript printing
94 /* Done using wxModule now
95 wxInitializePrintSetupData();
96 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
97 wxThePrintPaperDatabase->CreateDatabase();
101 wxBitmap::InitStandardHandlers();
103 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
105 wxModule::RegisterModules();
106 if (!wxModule::InitializeModules()) return FALSE
;
111 void wxApp::CleanUp()
113 delete wxWidgetHashTable
;
114 wxWidgetHashTable
= NULL
;
116 wxModule::CleanUpModules();
118 #if wxUSE_WX_RESOURCES
119 wxCleanUpResourceSystem();
122 wxDeleteStockObjects() ;
124 // Destroy all GDI lists, etc.
126 delete wxTheBrushList
;
127 wxTheBrushList
= NULL
;
132 delete wxTheFontList
;
133 wxTheFontList
= NULL
;
135 delete wxTheBitmapList
;
136 wxTheBitmapList
= NULL
;
138 delete wxTheColourDatabase
;
139 wxTheColourDatabase
= NULL
;
142 /* Done using wxModule now
143 wxInitializePrintSetupData(FALSE);
144 delete wxThePrintPaperDatabase;
145 wxThePrintPaperDatabase = NULL;
149 wxBitmap::CleanUpHandlers();
154 wxClassInfo::CleanUpClasses();
159 // GL: I'm annoyed ... I don't know where to put this and I don't want to
160 // create a module for that as it's part of the core.
162 delete wxPendingEvents
;
163 delete wxPendingEventsLocker
;
166 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
167 // At this point we want to check if there are any memory
168 // blocks that aren't part of the wxDebugContext itself,
169 // as a special case. Then when dumping we need to ignore
170 // wxDebugContext, too.
171 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
173 wxLogDebug("There were memory leaks.\n");
174 wxDebugContext::Dump();
175 wxDebugContext::PrintStatistics();
179 // do it as the very last thing because everything else can log messages
180 wxLog::DontCreateOnDemand();
181 // do it as the very last thing because everything else can log messages
182 delete wxLog::SetActiveTarget(NULL
);
185 int wxEntry( int argc
, char *argv
[] )
187 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
188 // This seems to be necessary since there are 'rogue'
189 // objects present at this point (perhaps global objects?)
190 // Setting a checkpoint will ignore them as far as the
191 // memory checking facility is concerned.
192 // Of course you may argue that memory allocated in globals should be
193 // checked, but this is a reasonable compromise.
194 wxDebugContext::SetCheckpoint();
197 if (!wxApp::Initialize())
202 if (!wxApp::GetInitializerFunction())
204 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
208 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
213 printf( "wxWindows error: wxTheApp == NULL\n" );
217 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
218 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
220 wxTheApp
->argc
= argc
;
221 wxTheApp
->argv
= argv
;
223 // GUI-specific initialization, such as creating an app context.
224 wxTheApp
->OnInitGui();
226 // Here frames insert themselves automatically into wxTopLevelWindows by
227 // getting created in OnInit().
230 if (wxTheApp
->OnInit())
232 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
235 // flush the logged messages if any
236 wxLog
*pLog
= wxLog::GetActiveTarget();
237 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
240 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
241 // for further messages
243 if (wxTheApp
->GetTopWindow())
245 delete wxTheApp
->GetTopWindow();
246 wxTheApp
->SetTopWindow(NULL
);
249 wxTheApp
->DeletePendingObjects();
258 // Static member initialization
259 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
266 m_wantDebugOutput
= TRUE
;
270 m_exitOnFrameDelete
= TRUE
;
272 m_mainColormap
= (WXColormap
) NULL
;
273 m_appContext
= (WXAppContext
) NULL
;
274 m_topLevelWidget
= (WXWidget
) NULL
;
275 m_maxRequestSize
= 0;
276 m_initialDisplay
= (WXDisplay
*) 0;
279 bool wxApp::Initialized()
287 int wxApp::MainLoop()
292 * Sit around forever waiting to process X-events. Property Change
293 * event are handled special, because they have to refer to
294 * the root window rather than to a widget. therefore we can't
295 * use an Xt-eventhandler.
298 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
299 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
304 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
307 XtAppNextEvent( (XtAppContext
) wxTheApp
->GetAppContext(), &event
);
309 ProcessXEvent((WXEvent
*) & event
);
311 if (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) == 0)
316 // leave the main loop to give other threads a chance to
317 // perform their GUI work
330 // Processes an X event.
331 void wxApp::ProcessXEvent(WXEvent
* _event
)
333 XEvent
* event
= (XEvent
*) _event
;
335 if (event
->type
== KeyPress
)
338 Widget widget
= XtWindowToWidget(event
->xany
.display
, event
->xany
.window
);
339 wxLogDebug("Got key press event for 0x%08x (parent = 0x%08x)",
340 widget
, XtParent(widget
));
343 if (CheckForAccelerator(_event
))
345 // Do nothing! We intercepted and processed the event as an
350 // It seemed before that this hack was redundant and
351 // key down events were being generated by wxCanvasInputEvent.
352 // But no longer - why ???
354 else if (CheckForKeyDown(_event
))
356 // We intercepted and processed the key down event
362 XtDispatchEvent(event
);
366 else if (event
->type
== KeyRelease
)
368 // TODO: work out why we still need this ! -michael
370 if (CheckForKeyUp(_event
))
372 // We intercepted and processed the key up event
377 XtDispatchEvent(event
);
381 else if (event
->type
== PropertyNotify
)
383 HandlePropertyChange(_event
);
386 else if (event
->type
== ResizeRequest
)
388 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
389 * If resize event, don't resize until the last resize event for this
390 * window is recieved. Prevents flicker as windows are resized.
393 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
394 Window win
= event
->xany
.window
;
399 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
401 // TODO: when implementing refresh optimization, we can use
402 // XtAddExposureToRegion to expand the window's paint region.
404 XtDispatchEvent(event
);
408 XtDispatchEvent(event
);
412 // Returns TRUE if more time is needed.
413 bool wxApp::ProcessIdle()
416 event
.SetEventObject(this);
419 return event
.MoreRequested();
422 void wxApp::ExitMainLoop()
427 // Is a message/event pending?
428 bool wxApp::Pending()
430 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
432 // Fix by Doug from STI, to prevent a stall if non-X event
434 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
437 // Dispatch a message.
438 void wxApp::Dispatch()
440 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
443 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
444 ProcessXEvent((WXEvent
*) & event
);
447 // This should be redefined in a derived class for
448 // handling property change events for XAtom IPC.
449 void wxApp::HandlePropertyChange(WXEvent
*event
)
451 // by default do nothing special
452 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
455 void wxApp::OnIdle(wxIdleEvent
& event
)
457 static bool inOnIdle
= FALSE
;
459 // Avoid recursion (via ProcessEvent default case)
465 // 'Garbage' collection of windows deleted with Close().
466 DeletePendingObjects();
469 // Flush pending events.
470 ProcessPendingEvents();
473 // flush the logged messages if any
474 wxLog
*pLog
= wxLog::GetActiveTarget();
475 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
478 // Send OnIdle events to all windows
479 bool needMore
= SendIdleEvents();
482 event
.RequestMore(TRUE
);
487 // Send idle event to all top-level windows
488 bool wxApp::SendIdleEvents()
490 bool needMore
= FALSE
;
492 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
495 wxWindow
* win
= node
->GetData();
496 if (SendIdleEvents(win
))
498 node
= node
->GetNext();
504 // Send idle event to window and all subwindows
505 bool wxApp::SendIdleEvents(wxWindow
* win
)
507 bool needMore
= FALSE
;
510 event
.SetEventObject(win
);
511 win
->ProcessEvent(event
);
513 if (event
.MoreRequested())
516 wxNode
* node
= win
->GetChildren().First();
519 wxWindow
* win
= (wxWindow
*) node
->Data();
520 if (SendIdleEvents(win
))
528 void wxApp::DeletePendingObjects()
530 wxNode
*node
= wxPendingDelete
.First();
533 wxObject
*obj
= (wxObject
*)node
->Data();
537 if (wxPendingDelete
.Member(obj
))
540 // Deleting one object may have deleted other pending
541 // objects, so start from beginning of list again.
542 node
= wxPendingDelete
.First();
546 // Create an application context
547 bool wxApp::OnInitGui()
549 XtToolkitInitialize() ;
550 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
551 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
552 (const char*) wxTheApp
->GetClassName(), NULL
, 0,
553 # if XtSpecificationRelease < 5
561 wxString
className(wxTheApp
->GetClassName());
562 wxLogError(_("wxWindows could not open display for '%s': exiting."),
563 (const char*) className
);
566 m_initialDisplay
= (WXDisplay
*) dpy
;
568 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
569 applicationShellWidgetClass
,dpy
,
572 // Add general resize proc
574 rec
.string
= "resize";
575 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
576 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
578 GetMainColormap(dpy
);
579 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
584 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
586 if (!display
) /* Must be called first with non-NULL display */
587 return m_mainColormap
;
589 int defaultScreen
= DefaultScreen((Display
*) display
);
590 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
592 Colormap c
= DefaultColormapOfScreen(screen
);
595 m_mainColormap
= (WXColormap
) c
;
597 return (WXColormap
) c
;
600 // Returns TRUE if an accelerator has been processed
601 bool wxApp::CheckForAccelerator(WXEvent
* event
)
603 XEvent
* xEvent
= (XEvent
*) event
;
604 if (xEvent
->xany
.type
== KeyPress
)
606 // Find a wxWindow for this window
607 // TODO: should get display for the window, not the current display
608 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
609 wxWindow
* win
= NULL
;
611 // Find the first wxWindow that corresponds to this event window
612 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
613 widget
= XtParent(widget
);
618 wxKeyEvent
keyEvent(wxEVT_CHAR
);
619 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
621 // Now we have a wxKeyEvent and we have a wxWindow.
622 // Go up the hierarchy until we find a matching accelerator,
623 // or we get to the top.
626 if (win
->ProcessAccelerator(keyEvent
))
628 win
= win
->GetParent();
635 bool wxApp::CheckForKeyDown(WXEvent
* event
)
637 XEvent
* xEvent
= (XEvent
*) event
;
638 if (xEvent
->xany
.type
== KeyPress
)
640 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
641 xEvent
->xany
.window
);
642 wxWindow
* win
= NULL
;
644 // Find the first wxWindow that corresponds to this event window
645 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
646 widget
= XtParent(widget
);
651 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
652 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
654 return win
->ProcessEvent( keyEvent
);
660 bool wxApp::CheckForKeyUp(WXEvent
* event
)
662 XEvent
* xEvent
= (XEvent
*) event
;
663 if (xEvent
->xany
.type
== KeyRelease
)
665 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
666 xEvent
->xany
.window
);
667 wxWindow
* win
= NULL
;
669 // Find the first wxWindow that corresponds to this event window
670 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
671 widget
= XtParent(widget
);
676 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
677 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 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
702 while (wxTheApp
&& wxTheApp
->Pending())
703 wxTheApp
->Dispatch();
705 // VZ: is it the same as this (taken from old wxExecute)?
707 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
713 // TODO use XmGetPixmap (?) to get the really standard icons!
715 #include "wx/generic/info.xpm"
716 #include "wx/generic/error.xpm"
717 #include "wx/generic/question.xpm"
718 #include "wx/generic/warning.xpm"
721 wxApp::GetStdIcon(int which
) const
725 case wxICON_INFORMATION
:
726 return wxIcon(info_xpm
);
728 case wxICON_QUESTION
:
729 return wxIcon(question_xpm
);
731 case wxICON_EXCLAMATION
:
732 return wxIcon(warning_xpm
);
735 wxFAIL_MSG("requested non existent standard icon");
736 // still fall through
739 return wxIcon(error_xpm
);
743 // ----------------------------------------------------------------------------
744 // accessors for C modules
745 // ----------------------------------------------------------------------------
747 extern "C" XtAppContext
wxGetAppContext()
749 return (XtAppContext
)wxTheApp
->GetAppContext();