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 (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();
82 wxInitializeStockObjects();
84 #if wxUSE_WX_RESOURCES
85 wxInitializeResourceSystem();
88 // For PostScript printing
90 /* Done using wxModule now
91 wxInitializePrintSetupData();
92 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
93 wxThePrintPaperDatabase->CreateDatabase();
97 wxBitmap::InitStandardHandlers();
99 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
101 wxModule::RegisterModules();
102 wxASSERT( wxModule::InitializeModules() == TRUE
);
107 void wxApp::CleanUp()
109 delete wxWidgetHashTable
;
110 wxWidgetHashTable
= NULL
;
112 wxModule::CleanUpModules();
114 #if wxUSE_WX_RESOURCES
115 wxCleanUpResourceSystem();
118 wxDeleteStockObjects() ;
120 // Destroy all GDI lists, etc.
122 delete wxTheBrushList
;
123 wxTheBrushList
= NULL
;
128 delete wxTheFontList
;
129 wxTheFontList
= NULL
;
131 delete wxTheBitmapList
;
132 wxTheBitmapList
= NULL
;
134 delete wxTheColourDatabase
;
135 wxTheColourDatabase
= NULL
;
138 /* Done using wxModule now
139 wxInitializePrintSetupData(FALSE);
140 delete wxThePrintPaperDatabase;
141 wxThePrintPaperDatabase = NULL;
145 wxBitmap::CleanUpHandlers();
150 wxClassInfo::CleanUpClasses();
152 // do it as the very last thing because everything else can log messages
153 wxLog::DontCreateOnDemand();
154 // do it as the very last thing because everything else can log messages
155 delete wxLog::SetActiveTarget(NULL
);
158 int wxEntry( int argc
, char *argv
[] )
160 if (!wxApp::Initialize())
164 if (!wxApp::GetInitializerFunction())
166 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
170 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
175 printf( "wxWindows error: wxTheApp == NULL\n" );
179 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
180 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
182 wxTheApp
->argc
= argc
;
183 wxTheApp
->argv
= argv
;
185 // GUI-specific initialization, such as creating an app context.
186 wxTheApp
->OnInitGui();
188 // Here frames insert themselves automatically
189 // into wxTopLevelWindows by getting created
192 if (!wxTheApp
->OnInit()) return 0;
196 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
198 if (wxTheApp
->GetTopWindow())
200 delete wxTheApp
->GetTopWindow();
201 wxTheApp
->SetTopWindow(NULL
);
204 wxTheApp
->DeletePendingObjects();
213 #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
214 // At this point we want to check if there are any memory
215 // blocks that aren't part of the wxDebugContext itself,
216 // as a special case. Then when dumping we need to ignore
217 // wxDebugContext, too.
218 if (wxDebugContext::CountObjectsLeft() > 0)
220 wxTrace("There were memory leaks.\n");
221 wxDebugContext::Dump();
222 wxDebugContext::PrintStatistics();
224 wxDebugContext::SetStream(NULL
, NULL
);
230 // Static member initialization
231 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
238 m_wantDebugOutput
= TRUE
;
242 m_printMode
= wxPRINT_POSTSCRIPT
;
243 m_exitOnFrameDelete
= TRUE
;
246 m_mainColormap
= (WXColormap
) NULL
;
247 m_appContext
= (WXAppContext
) NULL
;
248 m_topLevelWidget
= (WXWidget
) NULL
;
249 m_maxRequestSize
= 0;
250 m_initialDisplay
= (WXDisplay
*) 0;
253 bool wxApp::Initialized()
261 int wxApp::MainLoop()
266 * Sit around forever waiting to process X-events. Property Change
267 * event are handled special, because they have to refer to
268 * the root window rather than to a widget. therefore we can't
269 * use an Xt-eventhandler.
272 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
273 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
278 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
281 XtAppNextEvent( (XtAppContext
) wxTheApp
->GetAppContext(), &event
);
282 if(event
.type
== PropertyNotify
)
284 HandlePropertyChange((WXEvent
*) &event
);
287 // Terry Gitnick <terryg@scientech.com> - 1/21/98
288 /* if resize event, don't resize until the last resize event for this
289 window is recieved. Prevents flicker as windows are resized. */
290 if (event
.type
== ResizeRequest
)
292 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
293 Window win
= event
.xany
.window
;
298 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
300 // TODO: when implementing refresh optimization, we can use
301 // XtAddExposureToRegion to expand the window's paint region.
303 XtDispatchEvent(&event
);
305 DeletePendingObjects();
312 // Returns TRUE if more time is needed.
313 bool wxApp::ProcessIdle()
316 event
.SetEventObject(this);
319 return event
.MoreRequested();
322 void wxApp::ExitMainLoop()
327 // Is a message/event pending?
328 bool wxApp::Pending()
330 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
331 return (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) != 0) ;
334 // Dispatch a message.
335 void wxApp::Dispatch()
337 XtAppProcessEvent( (XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
340 // This should be redefined in a derived class for
341 // handling property change events for XAtom IPC.
342 void wxApp::HandlePropertyChange(WXEvent
*event
)
344 // by default do nothing special
345 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
348 void wxApp::OnIdle(wxIdleEvent
& event
)
350 static bool inOnIdle
= FALSE
;
352 // Avoid recursion (via ProcessEvent default case)
358 // 'Garbage' collection of windows deleted with Close().
359 DeletePendingObjects();
361 // flush the logged messages if any
362 wxLog
*pLog
= wxLog::GetActiveTarget();
363 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
366 // Send OnIdle events to all windows
367 bool needMore
= SendIdleEvents();
370 event
.RequestMore(TRUE
);
375 // Send idle event to all top-level windows
376 bool wxApp::SendIdleEvents()
378 bool needMore
= FALSE
;
379 wxNode
* node
= wxTopLevelWindows
.First();
382 wxWindow
* win
= (wxWindow
*) node
->Data();
383 if (SendIdleEvents(win
))
391 // Send idle event to window and all subwindows
392 bool wxApp::SendIdleEvents(wxWindow
* win
)
394 bool needMore
= FALSE
;
397 event
.SetEventObject(win
);
398 win
->ProcessEvent(event
);
400 if (event
.MoreRequested())
403 wxNode
* node
= win
->GetChildren()->First();
406 wxWindow
* win
= (wxWindow
*) node
->Data();
407 if (SendIdleEvents(win
))
415 void wxApp::DeletePendingObjects()
417 wxNode
*node
= wxPendingDelete
.First();
420 wxObject
*obj
= (wxObject
*)node
->Data();
424 if (wxPendingDelete
.Member(obj
))
427 // Deleting one object may have deleted other pending
428 // objects, so start from beginning of list again.
429 node
= wxPendingDelete
.First();
433 wxLog
* wxApp::CreateLogTarget()
438 wxWindow
* wxApp::GetTopWindow() const
442 else if (wxTopLevelWindows
.Number() > 0)
443 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
448 // Create an application context
449 bool wxApp::OnInitGui()
451 XtToolkitInitialize() ;
452 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
453 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
454 (const char*) wxTheApp
->GetClassName(), NULL
,
455 # if XtSpecificationRelease < 5
456 0,(Cardinal
*) &argc
,argv
) ;
461 cerr
<< "wxWindows could not open display for " << wxTheApp
->GetClassName() << ": exiting.\n";
464 m_initialDisplay
= (WXDisplay
*) dpy
;
466 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
467 applicationShellWidgetClass
,dpy
,
470 // Add general resize proc
472 rec
.string
= "resize";
473 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
474 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
476 GetMainColormap(dpy
);
477 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
482 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
484 if (!display
) /* Must be called first with non-NULL display */
485 return m_mainColormap
;
488 DefaultColormapOfScreen(XScreenOfDisplay((Display
*) display
,
489 DefaultScreen((Display
*) display
)));
492 m_mainColormap
= (WXColormap
) c
;
494 return (WXColormap
) c
;
501 retValue
= wxTheApp
->OnExit();
505 * Exit in some platform-specific way. Not recommended that the app calls this:
506 * only for emergencies.
511 // Yield to other processes
514 while (wxTheApp
&& wxTheApp
->Pending())
515 wxTheApp
->Dispatch();