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"
33 #include "wx/thread.h"
36 #if wxUSE_WX_RESOURCES
37 #include "wx/resource.h"
42 #include <X11/Xutil.h>
43 #include <X11/Xresource.h>
44 #include <X11/Xatom.h>
46 #include "wx/motif/private.h"
50 extern char *wxBuffer
;
51 extern wxList wxPendingDelete
;
53 wxApp
*wxTheApp
= NULL
;
55 wxHashTable
*wxWidgetHashTable
= NULL
;
57 #if !USE_SHARED_LIBRARY
58 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
60 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
61 EVT_IDLE(wxApp::OnIdle
)
65 long wxApp::sm_lastMessageTime
= 0;
67 bool wxApp::Initialize()
69 wxBuffer
= new char[BUFSIZ
+ 512];
71 wxClassInfo::InitializeClasses();
73 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
74 wxTheColourDatabase
->Initialize();
76 wxInitializeStockLists();
77 wxInitializeStockObjects();
79 #if wxUSE_WX_RESOURCES
80 wxInitializeResourceSystem();
83 // For PostScript printing
85 /* Done using wxModule now
86 wxInitializePrintSetupData();
87 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
88 wxThePrintPaperDatabase->CreateDatabase();
92 wxBitmap::InitStandardHandlers();
94 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
96 wxModule::RegisterModules();
97 if (!wxModule::InitializeModules()) return FALSE
;
102 void wxApp::CleanUp()
104 delete wxWidgetHashTable
;
105 wxWidgetHashTable
= NULL
;
107 wxModule::CleanUpModules();
109 #if wxUSE_WX_RESOURCES
110 wxCleanUpResourceSystem();
113 wxDeleteStockObjects() ;
115 // Destroy all GDI lists, etc.
117 delete wxTheBrushList
;
118 wxTheBrushList
= NULL
;
123 delete wxTheFontList
;
124 wxTheFontList
= NULL
;
126 delete wxTheBitmapList
;
127 wxTheBitmapList
= NULL
;
129 delete wxTheColourDatabase
;
130 wxTheColourDatabase
= NULL
;
133 /* Done using wxModule now
134 wxInitializePrintSetupData(FALSE);
135 delete wxThePrintPaperDatabase;
136 wxThePrintPaperDatabase = NULL;
140 wxBitmap::CleanUpHandlers();
145 wxClassInfo::CleanUpClasses();
150 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
151 // At this point we want to check if there are any memory
152 // blocks that aren't part of the wxDebugContext itself,
153 // as a special case. Then when dumping we need to ignore
154 // wxDebugContext, too.
155 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
157 wxLogDebug("There were memory leaks.\n");
158 wxDebugContext::Dump();
159 wxDebugContext::PrintStatistics();
163 // do it as the very last thing because everything else can log messages
164 wxLog::DontCreateOnDemand();
165 // do it as the very last thing because everything else can log messages
166 delete wxLog::SetActiveTarget(NULL
);
169 int wxEntry( int argc
, char *argv
[] )
171 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
172 // This seems to be necessary since there are 'rogue'
173 // objects present at this point (perhaps global objects?)
174 // Setting a checkpoint will ignore them as far as the
175 // memory checking facility is concerned.
176 // Of course you may argue that memory allocated in globals should be
177 // checked, but this is a reasonable compromise.
178 wxDebugContext::SetCheckpoint();
181 if (!wxApp::Initialize())
186 if (!wxApp::GetInitializerFunction())
188 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
192 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
197 printf( "wxWindows error: wxTheApp == NULL\n" );
201 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
202 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
204 wxTheApp
->argc
= argc
;
205 wxTheApp
->argv
= argv
;
207 // GUI-specific initialization, such as creating an app context.
208 wxTheApp
->OnInitGui();
210 // Here frames insert themselves automatically
211 // into wxTopLevelWindows by getting created
215 if (wxTheApp
->OnInit())
217 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
220 // flush the logged messages if any
221 wxLog
*pLog
= wxLog::GetActiveTarget();
222 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
225 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
226 // for further messages
228 if (wxTheApp
->GetTopWindow())
230 delete wxTheApp
->GetTopWindow();
231 wxTheApp
->SetTopWindow(NULL
);
234 wxTheApp
->DeletePendingObjects();
243 // Static member initialization
244 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
251 m_wantDebugOutput
= TRUE
;
255 m_printMode
= wxPRINT_POSTSCRIPT
;
256 m_exitOnFrameDelete
= TRUE
;
259 m_mainColormap
= (WXColormap
) NULL
;
260 m_appContext
= (WXAppContext
) NULL
;
261 m_topLevelWidget
= (WXWidget
) NULL
;
262 m_maxRequestSize
= 0;
263 m_initialDisplay
= (WXDisplay
*) 0;
266 bool wxApp::Initialized()
274 int wxApp::MainLoop()
279 * Sit around forever waiting to process X-events. Property Change
280 * event are handled special, because they have to refer to
281 * the root window rather than to a widget. therefore we can't
282 * use an Xt-eventhandler.
285 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
286 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
291 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
294 XtAppNextEvent( (XtAppContext
) wxTheApp
->GetAppContext(), &event
);
296 ProcessXEvent((WXEvent
*) & event
);
298 if (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) == 0)
303 // leave the main loop to give other threads a chance to
304 // perform their GUI work
317 // Processes an X event.
318 void wxApp::ProcessXEvent(WXEvent
* _event
)
320 XEvent
* event
= (XEvent
*) _event
;
322 if ((event
->type
== KeyPress
) && CheckForAccelerator(_event
))
324 // Do nothing! We intercepted and processed the event as an accelerator.
327 else if (event
->type
== PropertyNotify
)
329 HandlePropertyChange(_event
);
332 else if (event
->type
== ResizeRequest
)
334 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
335 * If resize event, don't resize until the last resize event for this
336 * window is recieved. Prevents flicker as windows are resized.
339 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
340 Window win
= event
->xany
.window
;
345 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
347 // TODO: when implementing refresh optimization, we can use
348 // XtAddExposureToRegion to expand the window's paint region.
350 XtDispatchEvent(event
);
354 XtDispatchEvent(event
);
358 // Returns TRUE if more time is needed.
359 bool wxApp::ProcessIdle()
362 event
.SetEventObject(this);
365 return event
.MoreRequested();
368 void wxApp::ExitMainLoop()
373 // Is a message/event pending?
374 bool wxApp::Pending()
376 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
378 // Fix by Doug from STI, to prevent a stall if non-X event
380 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
383 // Dispatch a message.
384 void wxApp::Dispatch()
386 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
389 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
390 ProcessXEvent((WXEvent
*) & event
);
393 // This should be redefined in a derived class for
394 // handling property change events for XAtom IPC.
395 void wxApp::HandlePropertyChange(WXEvent
*event
)
397 // by default do nothing special
398 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
401 void wxApp::OnIdle(wxIdleEvent
& event
)
403 static bool inOnIdle
= FALSE
;
405 // Avoid recursion (via ProcessEvent default case)
411 // 'Garbage' collection of windows deleted with Close().
412 DeletePendingObjects();
414 // flush the logged messages if any
415 wxLog
*pLog
= wxLog::GetActiveTarget();
416 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
419 // Send OnIdle events to all windows
420 bool needMore
= SendIdleEvents();
423 event
.RequestMore(TRUE
);
428 // Send idle event to all top-level windows
429 bool wxApp::SendIdleEvents()
431 bool needMore
= FALSE
;
432 wxNode
* node
= wxTopLevelWindows
.First();
435 wxWindow
* win
= (wxWindow
*) node
->Data();
436 if (SendIdleEvents(win
))
444 // Send idle event to window and all subwindows
445 bool wxApp::SendIdleEvents(wxWindow
* win
)
447 bool needMore
= FALSE
;
450 event
.SetEventObject(win
);
451 win
->ProcessEvent(event
);
453 if (event
.MoreRequested())
456 wxNode
* node
= win
->GetChildren().First();
459 wxWindow
* win
= (wxWindow
*) node
->Data();
460 if (SendIdleEvents(win
))
468 void wxApp::DeletePendingObjects()
470 wxNode
*node
= wxPendingDelete
.First();
473 wxObject
*obj
= (wxObject
*)node
->Data();
477 if (wxPendingDelete
.Member(obj
))
480 // Deleting one object may have deleted other pending
481 // objects, so start from beginning of list again.
482 node
= wxPendingDelete
.First();
486 wxLog
* wxApp::CreateLogTarget()
491 wxWindow
* wxApp::GetTopWindow() const
495 else if (wxTopLevelWindows
.Number() > 0)
496 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
501 // Create an application context
502 bool wxApp::OnInitGui()
504 XtToolkitInitialize() ;
505 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
506 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
507 (const char*) wxTheApp
->GetClassName(), NULL
, 0,
508 # if XtSpecificationRelease < 5
516 wxString
className(wxTheApp
->GetClassName());
517 wxLogError(_("wxWindows could not open display for '%s': exiting."),
518 (const char*) className
);
521 m_initialDisplay
= (WXDisplay
*) dpy
;
523 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
524 applicationShellWidgetClass
,dpy
,
527 // Add general resize proc
529 rec
.string
= "resize";
530 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
531 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
533 GetMainColormap(dpy
);
534 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
539 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
541 if (!display
) /* Must be called first with non-NULL display */
542 return m_mainColormap
;
544 int defaultScreen
= DefaultScreen((Display
*) display
);
545 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
547 Colormap c
= DefaultColormapOfScreen(screen
);
550 m_mainColormap
= (WXColormap
) c
;
552 return (WXColormap
) c
;
555 // Returns TRUE if an accelerator has been processed
556 bool wxApp::CheckForAccelerator(WXEvent
* event
)
558 XEvent
* xEvent
= (XEvent
*) event
;
559 if (xEvent
->xany
.type
== KeyPress
)
561 // Find a wxWindow for this window
562 // TODO: should get display for the window, not the current display
563 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
564 wxWindow
* win
= NULL
;
566 // Find the first wxWindow that corresponds to this event window
567 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
568 widget
= XtParent(widget
);
573 wxKeyEvent
keyEvent(wxEVT_CHAR
);
574 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
576 // Now we have a wxKeyEvent and we have a wxWindow.
577 // Go up the hierarchy until we find a matching accelerator,
578 // or we get to the top.
581 if (win
->ProcessAccelerator(keyEvent
))
583 win
= win
->GetParent();
594 retValue
= wxTheApp
->OnExit();
598 * Exit in some platform-specific way. Not recommended that the app calls this:
599 * only for emergencies.
604 // Yield to other processes
607 while (wxTheApp
&& wxTheApp
->Pending())
608 wxTheApp
->Dispatch();
610 // VZ: is it the same as this (taken from old wxExecute)?
612 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);