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 #if wxUSE_WX_RESOURCES
33 #include "wx/resource.h"
38 #include <X11/Xutil.h>
39 #include <X11/Xresource.h>
40 #include <X11/Xatom.h>
42 #include "wx/motif/private.h"
46 extern char *wxBuffer
;
47 extern wxList wxPendingDelete
;
49 wxApp
*wxTheApp
= NULL
;
51 wxHashTable
*wxWidgetHashTable
= NULL
;
53 #if !USE_SHARED_LIBRARY
54 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
55 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
56 EVT_IDLE(wxApp::OnIdle
)
60 long wxApp::sm_lastMessageTime
= 0;
62 bool wxApp::Initialize()
65 wxBuffer
= new char[1500];
67 wxBuffer
= new char[BUFSIZ
+ 512];
70 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
72 streambuf
* sBuf
= new wxDebugStreamBuf
;
73 ostream
* oStr
= new ostream(sBuf
) ;
74 wxDebugContext::SetStream(oStr
, sBuf
);
78 wxClassInfo::InitializeClasses();
80 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
81 wxTheColourDatabase
->Initialize();
83 wxInitializeStockLists();
84 wxInitializeStockObjects();
86 #if wxUSE_WX_RESOURCES
87 wxInitializeResourceSystem();
90 // For PostScript printing
92 /* Done using wxModule now
93 wxInitializePrintSetupData();
94 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
95 wxThePrintPaperDatabase->CreateDatabase();
99 wxBitmap::InitStandardHandlers();
101 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
103 wxModule::RegisterModules();
104 wxASSERT( wxModule::InitializeModules() == TRUE
);
109 void wxApp::CleanUp()
111 delete wxWidgetHashTable
;
112 wxWidgetHashTable
= NULL
;
114 wxModule::CleanUpModules();
116 #if wxUSE_WX_RESOURCES
117 wxCleanUpResourceSystem();
120 wxDeleteStockObjects() ;
122 // Destroy all GDI lists, etc.
124 delete wxTheBrushList
;
125 wxTheBrushList
= NULL
;
130 delete wxTheFontList
;
131 wxTheFontList
= NULL
;
133 delete wxTheBitmapList
;
134 wxTheBitmapList
= NULL
;
136 delete wxTheColourDatabase
;
137 wxTheColourDatabase
= NULL
;
140 /* Done using wxModule now
141 wxInitializePrintSetupData(FALSE);
142 delete wxThePrintPaperDatabase;
143 wxThePrintPaperDatabase = NULL;
147 wxBitmap::CleanUpHandlers();
152 wxClassInfo::CleanUpClasses();
154 // do it as the very last thing because everything else can log messages
155 wxLog::DontCreateOnDemand();
156 // do it as the very last thing because everything else can log messages
157 delete wxLog::SetActiveTarget(NULL
);
160 int wxEntry( int argc
, char *argv
[] )
162 if (!wxApp::Initialize())
166 if (!wxApp::GetInitializerFunction())
168 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
172 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
177 printf( "wxWindows error: wxTheApp == NULL\n" );
181 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
182 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
184 wxTheApp
->argc
= argc
;
185 wxTheApp
->argv
= argv
;
187 // GUI-specific initialization, such as creating an app context.
188 wxTheApp
->OnInitGui();
190 // Here frames insert themselves automatically
191 // into wxTopLevelWindows by getting created
194 if (!wxTheApp
->OnInit()) return 0;
198 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
200 // flush the logged messages if any
201 wxLog
*pLog
= wxLog::GetActiveTarget();
202 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
205 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
206 // for further messages
208 if (wxTheApp
->GetTopWindow())
210 delete wxTheApp
->GetTopWindow();
211 wxTheApp
->SetTopWindow(NULL
);
214 wxTheApp
->DeletePendingObjects();
224 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
225 // At this point we want to check if there are any memory
226 // blocks that aren't part of the wxDebugContext itself,
227 // as a special case. Then when dumping we need to ignore
228 // wxDebugContext, too.
229 if (wxDebugContext::CountObjectsLeft() > 0)
231 wxTrace("There were memory leaks.\n");
232 wxDebugContext::Dump();
233 wxDebugContext::PrintStatistics();
235 wxDebugContext::SetStream(NULL
, NULL
);
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
);
301 // Processes an X event.
302 void wxApp::ProcessXEvent(WXEvent
* _event
)
304 XEvent
* event
= (XEvent
*) _event
;
306 if ((event
->type
== KeyPress
) && CheckForAccelerator(_event
))
308 // Do nothing! We intercepted and processed the event as an accelerator.
311 else if (event
->type
== PropertyNotify
)
313 HandlePropertyChange(_event
);
316 else if (event
->type
== ResizeRequest
)
318 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
319 * If resize event, don't resize until the last resize event for this
320 * window is recieved. Prevents flicker as windows are resized.
323 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
324 Window win
= event
->xany
.window
;
329 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
331 // TODO: when implementing refresh optimization, we can use
332 // XtAddExposureToRegion to expand the window's paint region.
334 XtDispatchEvent(event
);
338 XtDispatchEvent(event
);
342 // Returns TRUE if more time is needed.
343 bool wxApp::ProcessIdle()
346 event
.SetEventObject(this);
349 return event
.MoreRequested();
352 void wxApp::ExitMainLoop()
357 // Is a message/event pending?
358 bool wxApp::Pending()
360 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
362 // Fix by Doug from STI, to prevent a stall if non-X event
364 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
367 // Dispatch a message.
368 void wxApp::Dispatch()
370 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
373 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
374 ProcessXEvent((WXEvent
*) & event
);
377 // This should be redefined in a derived class for
378 // handling property change events for XAtom IPC.
379 void wxApp::HandlePropertyChange(WXEvent
*event
)
381 // by default do nothing special
382 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
385 void wxApp::OnIdle(wxIdleEvent
& event
)
387 static bool inOnIdle
= FALSE
;
389 // Avoid recursion (via ProcessEvent default case)
395 // 'Garbage' collection of windows deleted with Close().
396 DeletePendingObjects();
398 // flush the logged messages if any
399 wxLog
*pLog
= wxLog::GetActiveTarget();
400 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
403 // Send OnIdle events to all windows
404 bool needMore
= SendIdleEvents();
407 event
.RequestMore(TRUE
);
412 // Send idle event to all top-level windows
413 bool wxApp::SendIdleEvents()
415 bool needMore
= FALSE
;
416 wxNode
* node
= wxTopLevelWindows
.First();
419 wxWindow
* win
= (wxWindow
*) node
->Data();
420 if (SendIdleEvents(win
))
428 // Send idle event to window and all subwindows
429 bool wxApp::SendIdleEvents(wxWindow
* win
)
431 bool needMore
= FALSE
;
434 event
.SetEventObject(win
);
435 win
->ProcessEvent(event
);
437 if (event
.MoreRequested())
440 wxNode
* node
= win
->GetChildren()->First();
443 wxWindow
* win
= (wxWindow
*) node
->Data();
444 if (SendIdleEvents(win
))
452 void wxApp::DeletePendingObjects()
454 wxNode
*node
= wxPendingDelete
.First();
457 wxObject
*obj
= (wxObject
*)node
->Data();
461 if (wxPendingDelete
.Member(obj
))
464 // Deleting one object may have deleted other pending
465 // objects, so start from beginning of list again.
466 node
= wxPendingDelete
.First();
470 wxLog
* wxApp::CreateLogTarget()
475 wxWindow
* wxApp::GetTopWindow() const
479 else if (wxTopLevelWindows
.Number() > 0)
480 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
485 // Create an application context
486 bool wxApp::OnInitGui()
488 XtToolkitInitialize() ;
489 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
490 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
491 (const char*) wxTheApp
->GetClassName(), NULL
,
492 # if XtSpecificationRelease < 5
493 0,(Cardinal
*) &argc
,argv
) ;
498 cerr
<< "wxWindows could not open display for " << wxTheApp
->GetClassName() << ": exiting.\n";
501 m_initialDisplay
= (WXDisplay
*) dpy
;
503 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
504 applicationShellWidgetClass
,dpy
,
507 // Add general resize proc
509 rec
.string
= "resize";
510 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
511 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
513 GetMainColormap(dpy
);
514 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
519 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
521 if (!display
) /* Must be called first with non-NULL display */
522 return m_mainColormap
;
525 DefaultColormapOfScreen(XScreenOfDisplay((Display
*) display
,
526 DefaultScreen((Display
*) display
)));
529 m_mainColormap
= (WXColormap
) c
;
531 return (WXColormap
) c
;
534 // Returns TRUE if an accelerator has been processed
535 bool wxApp::CheckForAccelerator(WXEvent
* event
)
537 XEvent
* xEvent
= (XEvent
*) event
;
538 if (xEvent
->xany
.type
== KeyPress
)
540 // Find a wxWindow for this window
541 // TODO: should get display for the window, not the current display
542 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
543 wxWindow
* win
= NULL
;
545 // Find the first wxWindow that corresponds to this event window
546 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
547 widget
= XtParent(widget
);
552 wxKeyEvent
keyEvent(wxEVT_CHAR
);
553 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
555 // Now we have a wxKeyEvent and we have a wxWindow.
556 // Go up the hierarchy until we find a matching accelerator,
557 // or we get to the top.
560 if (win
->ProcessAccelerator(keyEvent
))
562 win
= win
->GetParent();
573 retValue
= wxTheApp
->OnExit();
577 * Exit in some platform-specific way. Not recommended that the app calls this:
578 * only for emergencies.
583 // Yield to other processes
586 while (wxTheApp
&& wxTheApp
->Pending())
587 wxTheApp
->Dispatch();