1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "app.h"
16 #include "wx/gdicmn.h"
18 #include "wx/postscrp.h"
21 #include "wx/memory.h"
23 #include "wx/settings.h"
24 #include "wx/resource.h"
29 #include "../gdk_imlib/gdk_imlib.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 wxApp
*wxTheApp
= NULL
;
37 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
39 extern wxList wxPendingDelete
;
40 extern wxResourceCache
*wxTheResourceCache
;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 extern void wxFlushResources(void);
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
59 while (gtk_events_pending() > 0) gtk_main_iteration();
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
67 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
69 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
70 EVT_IDLE(wxApp::OnIdle
)
73 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
75 if (wxTheApp
) while (wxTheApp
->ProcessIdle()) {}
84 m_exitOnFrameDelete
= TRUE
;
89 gtk_idle_remove( m_idleTag
);
92 bool wxApp::OnInit(void)
97 bool wxApp::OnInitGui(void)
99 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, NULL
);
103 int wxApp::OnRun(void)
108 bool wxApp::ProcessIdle(void)
111 event
.SetEventObject( this );
112 ProcessEvent( event
);
114 return event
.MoreRequested();
117 void wxApp::OnIdle( wxIdleEvent
&event
)
119 static bool inOnIdle
= FALSE
;
121 // Avoid recursion (via ProcessEvent default case)
127 // 'Garbage' collection of windows deleted with Close().
128 DeletePendingObjects();
130 // flush the logged messages if any
131 wxLog
*pLog
= wxLog::GetActiveTarget();
132 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
135 // Send OnIdle events to all windows
136 bool needMore
= SendIdleEvents();
139 event
.RequestMore(TRUE
);
144 bool wxApp::SendIdleEvents(void)
146 bool needMore
= FALSE
;
147 wxNode
* node
= wxTopLevelWindows
.First();
150 wxWindow
* win
= (wxWindow
*) node
->Data();
151 if (SendIdleEvents(win
))
159 bool wxApp::SendIdleEvents( wxWindow
* win
)
161 bool needMore
= FALSE
;
164 event
.SetEventObject(win
);
165 win
->ProcessEvent(event
);
167 if (event
.MoreRequested())
170 wxNode
* node
= win
->GetChildren()->First();
173 wxWindow
* win
= (wxWindow
*) node
->Data();
174 if (SendIdleEvents(win
))
182 int wxApp::OnExit(void)
187 int wxApp::MainLoop(void)
193 void wxApp::ExitMainLoop(void)
198 bool wxApp::Initialized(void)
200 return m_initialized
;
203 bool wxApp::Pending(void)
208 void wxApp::Dispatch(void)
212 void wxApp::DeletePendingObjects(void)
214 wxNode
*node
= wxPendingDelete
.First();
217 wxObject
*obj
= (wxObject
*)node
->Data();
221 if (wxPendingDelete
.Member(obj
))
224 node
= wxPendingDelete
.First();
228 wxWindow
*wxApp::GetTopWindow(void)
230 if (m_topWindow
) return m_topWindow
;
231 wxNode
*node
= wxTopLevelWindows
.First();
232 if (!node
) return NULL
;
233 return (wxWindow
*)node
->Data();
236 void wxApp::SetTopWindow( wxWindow
*win
)
241 void wxApp::CommonInit(void)
246 (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
249 wxSystemSettings::Init();
250 wxTheResourceCache
= new wxResourceCache(wxKEY_STRING
);
252 wxTheFontNameDirectory
= new wxFontNameDirectory
;
253 wxTheFontNameDirectory
->Initialize();
255 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
256 wxTheColourDatabase
->Initialize();
258 wxInitializeStockLists();
259 wxInitializeStockObjects();
261 wxInitializeResourceSystem();
263 // For PostScript printing
265 wxInitializePrintSetupData();
266 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
267 wxThePrintPaperDatabase
->CreateDatabase();
272 wxBitmap::InitStandardHandlers();
274 g_globalCursor = new wxCursor;
278 void wxApp::CommonCleanUp(void)
280 wxDELETE(wxTheColourDatabase
);
281 wxDELETE(wxThePrintPaperDatabase
);
282 wxDELETE(wxThePrintSetupData
);
283 wxDELETE(wxTheFontNameDirectory
);
284 wxDeleteStockObjects();
288 wxDELETE(wxTheResourceCache
);
290 wxDeleteStockLists();
292 wxCleanUpResourceSystem();
294 wxSystemSettings::Done();
297 wxLog
*wxApp::CreateLogTarget()
302 //-----------------------------------------------------------------------------
304 //-----------------------------------------------------------------------------
306 int wxEntry( int argc
, char *argv
[] )
308 wxBuffer
= new char[BUFSIZ
+ 512];
310 wxClassInfo::InitializeClasses();
312 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
314 #if !defined(_WINDLL)
315 streambuf
* sBuf
= new wxDebugStreamBuf
;
317 streambuf
* sBuf
= NULL
;
319 ostream
* oStr
= new ostream(sBuf
) ;
320 wxDebugContext::SetStream(oStr
, sBuf
);
326 if (!wxApp::GetInitializerFunction())
328 printf( _("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
332 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
334 wxObject
*test_app
= app_ini();
336 wxTheApp
= (wxApp
*) test_app
;
341 printf( _("wxWindows error: wxTheApp == NULL\n") );
345 wxTheApp
->argc
= argc
;
346 wxTheApp
->argv
= argv
;
348 gtk_init( &argc
, &argv
);
354 gtk_widget_push_visual(gdk_imlib_get_visual());
356 gtk_widget_push_colormap(gdk_imlib_get_colormap());
362 wxTheApp
->OnInitGui();
364 // Here frames insert themselves automatically
365 // into wxTopLevelWindows by getting created
368 if (!wxTheApp
->OnInit()) return 0;
370 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
374 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
376 wxTheApp
->DeletePendingObjects();
380 wxApp::CommonCleanUp();
384 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
385 // At this point we want to check if there are any memory
386 // blocks that aren't part of the wxDebugContext itself,
387 // as a special case. Then when dumping we need to ignore
388 // wxDebugContext, too.
389 if (wxDebugContext::CountObjectsLeft() > 0)
391 wxTrace("There were memory leaks.\n");
392 wxDebugContext::Dump();
393 wxDebugContext::PrintStatistics();
395 wxDebugContext::SetStream(NULL
, NULL
);
401 //-----------------------------------------------------------------------------
403 //-----------------------------------------------------------------------------
405 #if defined(AIX) || defined(AIX4) || defined(____HPUX__)
407 // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h
411 int main(int argc
, char *argv
[]) { return wxEntry(argc
, argv
); }