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"
32 #include "wx/thread.h"
34 #if wxUSE_WX_RESOURCES
35 #include "wx/resource.h"
40 #include <X11/Xutil.h>
41 #include <X11/Xresource.h>
42 #include <X11/Xatom.h>
44 #include "wx/motif/private.h"
48 extern char *wxBuffer
;
49 extern wxList wxPendingDelete
;
51 wxApp
*wxTheApp
= NULL
;
53 wxHashTable
*wxWidgetHashTable
= NULL
;
55 #if !USE_SHARED_LIBRARY
56 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
58 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
59 EVT_IDLE(wxApp::OnIdle
)
63 long wxApp::sm_lastMessageTime
= 0;
65 bool wxApp::Initialize()
67 wxBuffer
= new char[BUFSIZ
+ 512];
69 wxClassInfo::InitializeClasses();
71 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
72 wxTheColourDatabase
->Initialize();
74 wxInitializeStockLists();
75 wxInitializeStockObjects();
77 #if wxUSE_WX_RESOURCES
78 wxInitializeResourceSystem();
81 // For PostScript printing
83 /* Done using wxModule now
84 wxInitializePrintSetupData();
85 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
86 wxThePrintPaperDatabase->CreateDatabase();
90 wxBitmap::InitStandardHandlers();
92 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
94 wxModule::RegisterModules();
95 if (!wxModule::InitializeModules()) return FALSE
;
100 void wxApp::CleanUp()
102 delete wxWidgetHashTable
;
103 wxWidgetHashTable
= NULL
;
105 wxModule::CleanUpModules();
107 #if wxUSE_WX_RESOURCES
108 wxCleanUpResourceSystem();
111 wxDeleteStockObjects() ;
113 // Destroy all GDI lists, etc.
115 delete wxTheBrushList
;
116 wxTheBrushList
= NULL
;
121 delete wxTheFontList
;
122 wxTheFontList
= NULL
;
124 delete wxTheBitmapList
;
125 wxTheBitmapList
= NULL
;
127 delete wxTheColourDatabase
;
128 wxTheColourDatabase
= NULL
;
131 /* Done using wxModule now
132 wxInitializePrintSetupData(FALSE);
133 delete wxThePrintPaperDatabase;
134 wxThePrintPaperDatabase = NULL;
138 wxBitmap::CleanUpHandlers();
143 wxClassInfo::CleanUpClasses();
148 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
149 // At this point we want to check if there are any memory
150 // blocks that aren't part of the wxDebugContext itself,
151 // as a special case. Then when dumping we need to ignore
152 // wxDebugContext, too.
153 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
155 wxLogDebug("There were memory leaks.\n");
156 wxDebugContext::Dump();
157 wxDebugContext::PrintStatistics();
161 // do it as the very last thing because everything else can log messages
162 wxLog::DontCreateOnDemand();
163 // do it as the very last thing because everything else can log messages
164 delete wxLog::SetActiveTarget(NULL
);
167 int wxEntry( int argc
, char *argv
[] )
169 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
170 // This seems to be necessary since there are 'rogue'
171 // objects present at this point (perhaps global objects?)
172 // Setting a checkpoint will ignore them as far as the
173 // memory checking facility is concerned.
174 // Of course you may argue that memory allocated in globals should be
175 // checked, but this is a reasonable compromise.
176 wxDebugContext::SetCheckpoint();
179 if (!wxApp::Initialize())
184 if (!wxApp::GetInitializerFunction())
186 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
190 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
195 printf( "wxWindows error: wxTheApp == NULL\n" );
199 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
200 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
202 wxTheApp
->argc
= argc
;
203 wxTheApp
->argv
= argv
;
205 // GUI-specific initialization, such as creating an app context.
206 wxTheApp
->OnInitGui();
208 // Here frames insert themselves automatically
209 // into wxTopLevelWindows by getting created
213 if (wxTheApp
->OnInit())
215 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
218 // flush the logged messages if any
219 wxLog
*pLog
= wxLog::GetActiveTarget();
220 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
223 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
224 // for further messages
226 if (wxTheApp
->GetTopWindow())
228 delete wxTheApp
->GetTopWindow();
229 wxTheApp
->SetTopWindow(NULL
);
232 wxTheApp
->DeletePendingObjects();
241 // Static member initialization
242 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
249 m_wantDebugOutput
= TRUE
;
253 m_printMode
= wxPRINT_POSTSCRIPT
;
254 m_exitOnFrameDelete
= TRUE
;
257 m_mainColormap
= (WXColormap
) NULL
;
258 m_appContext
= (WXAppContext
) NULL
;
259 m_topLevelWidget
= (WXWidget
) 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(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
284 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
289 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
292 XtAppNextEvent( (XtAppContext
) wxTheApp
->GetAppContext(), &event
);
294 ProcessXEvent((WXEvent
*) & event
);
296 if (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) == 0)
300 // leave the main loop to give other threads a chance to
301 // perform their GUI work
313 // Processes an X event.
314 void wxApp::ProcessXEvent(WXEvent
* _event
)
316 XEvent
* event
= (XEvent
*) _event
;
318 if ((event
->type
== KeyPress
) && CheckForAccelerator(_event
))
320 // Do nothing! We intercepted and processed the event as an accelerator.
323 else if (event
->type
== PropertyNotify
)
325 HandlePropertyChange(_event
);
328 else if (event
->type
== ResizeRequest
)
330 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
331 * If resize event, don't resize until the last resize event for this
332 * window is recieved. Prevents flicker as windows are resized.
335 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
336 Window win
= event
->xany
.window
;
341 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
343 // TODO: when implementing refresh optimization, we can use
344 // XtAddExposureToRegion to expand the window's paint region.
346 XtDispatchEvent(event
);
350 XtDispatchEvent(event
);
354 // Returns TRUE if more time is needed.
355 bool wxApp::ProcessIdle()
358 event
.SetEventObject(this);
361 return event
.MoreRequested();
364 void wxApp::ExitMainLoop()
369 // Is a message/event pending?
370 bool wxApp::Pending()
372 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
374 // Fix by Doug from STI, to prevent a stall if non-X event
376 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
379 // Dispatch a message.
380 void wxApp::Dispatch()
382 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
385 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
386 ProcessXEvent((WXEvent
*) & event
);
389 // This should be redefined in a derived class for
390 // handling property change events for XAtom IPC.
391 void wxApp::HandlePropertyChange(WXEvent
*event
)
393 // by default do nothing special
394 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
397 void wxApp::OnIdle(wxIdleEvent
& event
)
399 static bool inOnIdle
= FALSE
;
401 // Avoid recursion (via ProcessEvent default case)
407 // 'Garbage' collection of windows deleted with Close().
408 DeletePendingObjects();
410 // flush the logged messages if any
411 wxLog
*pLog
= wxLog::GetActiveTarget();
412 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
415 // Send OnIdle events to all windows
416 bool needMore
= SendIdleEvents();
419 event
.RequestMore(TRUE
);
424 // Send idle event to all top-level windows
425 bool wxApp::SendIdleEvents()
427 bool needMore
= FALSE
;
428 wxNode
* node
= wxTopLevelWindows
.First();
431 wxWindow
* win
= (wxWindow
*) node
->Data();
432 if (SendIdleEvents(win
))
440 // Send idle event to window and all subwindows
441 bool wxApp::SendIdleEvents(wxWindow
* win
)
443 bool needMore
= FALSE
;
446 event
.SetEventObject(win
);
447 win
->ProcessEvent(event
);
449 if (event
.MoreRequested())
452 wxNode
* node
= win
->GetChildren().First();
455 wxWindow
* win
= (wxWindow
*) node
->Data();
456 if (SendIdleEvents(win
))
464 void wxApp::DeletePendingObjects()
466 wxNode
*node
= wxPendingDelete
.First();
469 wxObject
*obj
= (wxObject
*)node
->Data();
473 if (wxPendingDelete
.Member(obj
))
476 // Deleting one object may have deleted other pending
477 // objects, so start from beginning of list again.
478 node
= wxPendingDelete
.First();
482 wxLog
* wxApp::CreateLogTarget()
487 wxWindow
* wxApp::GetTopWindow() const
491 else if (wxTopLevelWindows
.Number() > 0)
492 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
497 // Create an application context
498 bool wxApp::OnInitGui()
500 XtToolkitInitialize() ;
501 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
502 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
503 (const char*) wxTheApp
->GetClassName(), NULL
, 0,
504 # if XtSpecificationRelease < 5
512 wxLogError(_("wxWindows could not open display for '%s': exiting."),
513 wxTheApp
->GetClassName());
516 m_initialDisplay
= (WXDisplay
*) dpy
;
518 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
519 applicationShellWidgetClass
,dpy
,
522 // Add general resize proc
524 rec
.string
= "resize";
525 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
526 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
528 GetMainColormap(dpy
);
529 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
534 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
536 if (!display
) /* Must be called first with non-NULL display */
537 return m_mainColormap
;
539 int defaultScreen
= DefaultScreen((Display
*) display
);
540 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
542 Colormap c
= DefaultColormapOfScreen(screen
);
545 m_mainColormap
= (WXColormap
) c
;
547 return (WXColormap
) c
;
550 // Returns TRUE if an accelerator has been processed
551 bool wxApp::CheckForAccelerator(WXEvent
* event
)
553 XEvent
* xEvent
= (XEvent
*) event
;
554 if (xEvent
->xany
.type
== KeyPress
)
556 // Find a wxWindow for this window
557 // TODO: should get display for the window, not the current display
558 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
559 wxWindow
* win
= NULL
;
561 // Find the first wxWindow that corresponds to this event window
562 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
563 widget
= XtParent(widget
);
568 wxKeyEvent
keyEvent(wxEVT_CHAR
);
569 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
571 // Now we have a wxKeyEvent and we have a wxWindow.
572 // Go up the hierarchy until we find a matching accelerator,
573 // or we get to the top.
576 if (win
->ProcessAccelerator(keyEvent
))
578 win
= win
->GetParent();
589 retValue
= wxTheApp
->OnExit();
593 * Exit in some platform-specific way. Not recommended that the app calls this:
594 * only for emergencies.
599 // Yield to other processes
602 while (wxTheApp
&& wxTheApp
->Pending())
603 wxTheApp
->Dispatch();
605 // VZ: is it the same as this (taken from old wxExecute)?
607 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);