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 extern wxList wxPendingEvents
;
54 extern wxList wxPendingEventsLocker
;
57 wxApp
*wxTheApp
= NULL
;
59 wxHashTable
*wxWidgetHashTable
= NULL
;
61 #if !USE_SHARED_LIBRARY
62 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
64 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
65 EVT_IDLE(wxApp::OnIdle
)
69 long wxApp::sm_lastMessageTime
= 0;
71 bool wxApp::Initialize()
73 wxBuffer
= new char[BUFSIZ
+ 512];
75 wxClassInfo::InitializeClasses();
77 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
78 wxTheColourDatabase
->Initialize();
80 wxInitializeStockLists();
81 wxInitializeStockObjects();
83 #if wxUSE_WX_RESOURCES
84 wxInitializeResourceSystem();
87 // For PostScript printing
89 /* Done using wxModule now
90 wxInitializePrintSetupData();
91 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
92 wxThePrintPaperDatabase->CreateDatabase();
96 wxBitmap::InitStandardHandlers();
98 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
100 wxModule::RegisterModules();
101 if (!wxModule::InitializeModules()) return FALSE
;
106 void wxApp::CleanUp()
108 delete wxWidgetHashTable
;
109 wxWidgetHashTable
= NULL
;
111 wxModule::CleanUpModules();
113 #if wxUSE_WX_RESOURCES
114 wxCleanUpResourceSystem();
117 wxDeleteStockObjects() ;
119 // Destroy all GDI lists, etc.
121 delete wxTheBrushList
;
122 wxTheBrushList
= NULL
;
127 delete wxTheFontList
;
128 wxTheFontList
= NULL
;
130 delete wxTheBitmapList
;
131 wxTheBitmapList
= NULL
;
133 delete wxTheColourDatabase
;
134 wxTheColourDatabase
= NULL
;
137 /* Done using wxModule now
138 wxInitializePrintSetupData(FALSE);
139 delete wxThePrintPaperDatabase;
140 wxThePrintPaperDatabase = NULL;
144 wxBitmap::CleanUpHandlers();
149 wxClassInfo::CleanUpClasses();
154 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
155 // At this point we want to check if there are any memory
156 // blocks that aren't part of the wxDebugContext itself,
157 // as a special case. Then when dumping we need to ignore
158 // wxDebugContext, too.
159 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
161 wxLogDebug("There were memory leaks.\n");
162 wxDebugContext::Dump();
163 wxDebugContext::PrintStatistics();
167 // do it as the very last thing because everything else can log messages
168 wxLog::DontCreateOnDemand();
169 // do it as the very last thing because everything else can log messages
170 delete wxLog::SetActiveTarget(NULL
);
173 int wxEntry( int argc
, char *argv
[] )
175 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
176 // This seems to be necessary since there are 'rogue'
177 // objects present at this point (perhaps global objects?)
178 // Setting a checkpoint will ignore them as far as the
179 // memory checking facility is concerned.
180 // Of course you may argue that memory allocated in globals should be
181 // checked, but this is a reasonable compromise.
182 wxDebugContext::SetCheckpoint();
185 if (!wxApp::Initialize())
190 if (!wxApp::GetInitializerFunction())
192 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
196 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
201 printf( "wxWindows error: wxTheApp == NULL\n" );
205 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
206 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
208 wxTheApp
->argc
= argc
;
209 wxTheApp
->argv
= argv
;
211 // GUI-specific initialization, such as creating an app context.
212 wxTheApp
->OnInitGui();
214 // Here frames insert themselves automatically into wxTopLevelWindows by
215 // getting created in OnInit().
218 if (wxTheApp
->OnInit())
220 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
223 // flush the logged messages if any
224 wxLog
*pLog
= wxLog::GetActiveTarget();
225 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
228 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
229 // for further messages
231 if (wxTheApp
->GetTopWindow())
233 delete wxTheApp
->GetTopWindow();
234 wxTheApp
->SetTopWindow(NULL
);
237 wxTheApp
->DeletePendingObjects();
246 // Static member initialization
247 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
254 m_wantDebugOutput
= TRUE
;
258 m_printMode
= wxPRINT_POSTSCRIPT
;
259 m_exitOnFrameDelete
= TRUE
;
262 m_mainColormap
= (WXColormap
) NULL
;
263 m_appContext
= (WXAppContext
) NULL
;
264 m_topLevelWidget
= (WXWidget
) NULL
;
265 m_maxRequestSize
= 0;
266 m_initialDisplay
= (WXDisplay
*) 0;
269 bool wxApp::Initialized()
277 int wxApp::MainLoop()
282 * Sit around forever waiting to process X-events. Property Change
283 * event are handled special, because they have to refer to
284 * the root window rather than to a widget. therefore we can't
285 * use an Xt-eventhandler.
288 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
289 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
294 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
297 XtAppNextEvent( (XtAppContext
) wxTheApp
->GetAppContext(), &event
);
299 ProcessXEvent((WXEvent
*) & event
);
301 if (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) == 0)
306 // leave the main loop to give other threads a chance to
307 // perform their GUI work
320 // Processes an X event.
321 void wxApp::ProcessXEvent(WXEvent
* _event
)
323 XEvent
* event
= (XEvent
*) _event
;
325 if ((event
->type
== KeyPress
) && CheckForAccelerator(_event
))
327 // Do nothing! We intercepted and processed the event as an accelerator.
330 else if (event
->type
== PropertyNotify
)
332 HandlePropertyChange(_event
);
335 else if (event
->type
== ResizeRequest
)
337 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
338 * If resize event, don't resize until the last resize event for this
339 * window is recieved. Prevents flicker as windows are resized.
342 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
343 Window win
= event
->xany
.window
;
348 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
350 // TODO: when implementing refresh optimization, we can use
351 // XtAddExposureToRegion to expand the window's paint region.
353 XtDispatchEvent(event
);
357 XtDispatchEvent(event
);
361 // Returns TRUE if more time is needed.
362 bool wxApp::ProcessIdle()
365 event
.SetEventObject(this);
368 return event
.MoreRequested();
371 void wxApp::ExitMainLoop()
376 // Is a message/event pending?
377 bool wxApp::Pending()
379 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
381 // Fix by Doug from STI, to prevent a stall if non-X event
383 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
386 // Dispatch a message.
387 void wxApp::Dispatch()
389 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
392 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
393 ProcessXEvent((WXEvent
*) & event
);
396 // This should be redefined in a derived class for
397 // handling property change events for XAtom IPC.
398 void wxApp::HandlePropertyChange(WXEvent
*event
)
400 // by default do nothing special
401 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
404 void wxApp::OnIdle(wxIdleEvent
& event
)
406 static bool inOnIdle
= FALSE
;
408 // Avoid recursion (via ProcessEvent default case)
414 // 'Garbage' collection of windows deleted with Close().
415 DeletePendingObjects();
418 // Flush pending events.
419 ProcessPendingEvents();
422 // flush the logged messages if any
423 wxLog
*pLog
= wxLog::GetActiveTarget();
424 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
427 // Send OnIdle events to all windows
428 bool needMore
= SendIdleEvents();
431 event
.RequestMore(TRUE
);
436 // Send idle event to all top-level windows
437 bool wxApp::SendIdleEvents()
439 bool needMore
= FALSE
;
441 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
444 wxWindow
* win
= node
->GetData();
445 if (SendIdleEvents(win
))
447 node
= node
->GetNext();
453 // Send idle event to window and all subwindows
454 bool wxApp::SendIdleEvents(wxWindow
* win
)
456 bool needMore
= FALSE
;
459 event
.SetEventObject(win
);
460 win
->ProcessEvent(event
);
462 if (event
.MoreRequested())
465 wxNode
* node
= win
->GetChildren().First();
468 wxWindow
* win
= (wxWindow
*) node
->Data();
469 if (SendIdleEvents(win
))
477 void wxApp::DeletePendingObjects()
479 wxNode
*node
= wxPendingDelete
.First();
482 wxObject
*obj
= (wxObject
*)node
->Data();
486 if (wxPendingDelete
.Member(obj
))
489 // Deleting one object may have deleted other pending
490 // objects, so start from beginning of list again.
491 node
= wxPendingDelete
.First();
496 void wxApp::ProcessPendingEvents()
498 wxNode
*node
= wxPendingEvents
.First();
499 wxCriticalSectionLocker
locker(wxPendingEventsLocker
);
503 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
505 handler
->ProcessPendingEvents();
508 node
= wxPendingEvents
.First();
513 wxLog
* wxApp::CreateLogTarget()
518 wxWindow
* wxApp::GetTopWindow() const
522 else if (wxTopLevelWindows
.GetCount() > 0)
523 return wxTopLevelWindows
.GetFirst()->GetData();
528 // Create an application context
529 bool wxApp::OnInitGui()
531 XtToolkitInitialize() ;
532 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
533 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
534 (const char*) wxTheApp
->GetClassName(), NULL
, 0,
535 # if XtSpecificationRelease < 5
543 wxString
className(wxTheApp
->GetClassName());
544 wxLogError(_("wxWindows could not open display for '%s': exiting."),
545 (const char*) className
);
548 m_initialDisplay
= (WXDisplay
*) dpy
;
550 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
551 applicationShellWidgetClass
,dpy
,
554 // Add general resize proc
556 rec
.string
= "resize";
557 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
558 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
560 GetMainColormap(dpy
);
561 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
566 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
568 if (!display
) /* Must be called first with non-NULL display */
569 return m_mainColormap
;
571 int defaultScreen
= DefaultScreen((Display
*) display
);
572 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
574 Colormap c
= DefaultColormapOfScreen(screen
);
577 m_mainColormap
= (WXColormap
) c
;
579 return (WXColormap
) c
;
582 // Returns TRUE if an accelerator has been processed
583 bool wxApp::CheckForAccelerator(WXEvent
* event
)
585 XEvent
* xEvent
= (XEvent
*) event
;
586 if (xEvent
->xany
.type
== KeyPress
)
588 // Find a wxWindow for this window
589 // TODO: should get display for the window, not the current display
590 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
591 wxWindow
* win
= NULL
;
593 // Find the first wxWindow that corresponds to this event window
594 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
595 widget
= XtParent(widget
);
600 wxKeyEvent
keyEvent(wxEVT_CHAR
);
601 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
603 // Now we have a wxKeyEvent and we have a wxWindow.
604 // Go up the hierarchy until we find a matching accelerator,
605 // or we get to the top.
608 if (win
->ProcessAccelerator(keyEvent
))
610 win
= win
->GetParent();
621 retValue
= wxTheApp
->OnExit();
625 * Exit in some platform-specific way. Not recommended that the app calls this:
626 * only for emergencies.
631 // Yield to other processes
634 while (wxTheApp
&& wxTheApp
->Pending())
635 wxTheApp
->Dispatch();
637 // VZ: is it the same as this (taken from old wxExecute)?
639 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);