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"
31 #include "wx/thread.h"
33 #if wxUSE_WX_RESOURCES
34 #include "wx/resource.h"
39 #include <X11/Xutil.h>
40 #include <X11/Xresource.h>
41 #include <X11/Xatom.h>
43 #include "wx/motif/private.h"
47 extern char *wxBuffer
;
48 extern wxList wxPendingDelete
;
50 wxApp
*wxTheApp
= NULL
;
52 wxHashTable
*wxWidgetHashTable
= NULL
;
54 #if !USE_SHARED_LIBRARY
55 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
56 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
57 EVT_IDLE(wxApp::OnIdle
)
61 long wxApp::sm_lastMessageTime
= 0;
63 bool wxApp::Initialize()
66 wxBuffer
= new char[1500];
68 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)
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
,
504 # if XtSpecificationRelease < 5
505 0,(Cardinal
*) &argc
,argv
) ;
510 cerr
<< "wxWindows could not open display for " << wxTheApp
->GetClassName() << ": exiting.\n";
513 m_initialDisplay
= (WXDisplay
*) dpy
;
515 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
516 applicationShellWidgetClass
,dpy
,
519 // Add general resize proc
521 rec
.string
= "resize";
522 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
523 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
525 GetMainColormap(dpy
);
526 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
531 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
533 if (!display
) /* Must be called first with non-NULL display */
534 return m_mainColormap
;
537 DefaultColormapOfScreen(XScreenOfDisplay((Display
*) display
,
538 DefaultScreen((Display
*) display
)));
541 m_mainColormap
= (WXColormap
) c
;
543 return (WXColormap
) c
;
546 // Returns TRUE if an accelerator has been processed
547 bool wxApp::CheckForAccelerator(WXEvent
* event
)
549 XEvent
* xEvent
= (XEvent
*) event
;
550 if (xEvent
->xany
.type
== KeyPress
)
552 // Find a wxWindow for this window
553 // TODO: should get display for the window, not the current display
554 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
555 wxWindow
* win
= NULL
;
557 // Find the first wxWindow that corresponds to this event window
558 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
559 widget
= XtParent(widget
);
564 wxKeyEvent
keyEvent(wxEVT_CHAR
);
565 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
567 // Now we have a wxKeyEvent and we have a wxWindow.
568 // Go up the hierarchy until we find a matching accelerator,
569 // or we get to the top.
572 if (win
->ProcessAccelerator(keyEvent
))
574 win
= win
->GetParent();
585 retValue
= wxTheApp
->OnExit();
589 * Exit in some platform-specific way. Not recommended that the app calls this:
590 * only for emergencies.
595 // Yield to other processes
598 while (wxTheApp
&& wxTheApp
->Pending())
599 wxTheApp
->Dispatch();