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"
28 #ifdef wxUSE_GDK_IMLIB
29 #include "../gdk_imlib/gdk_imlib.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 wxApp
*wxTheApp
= (wxApp
*) 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()) {}
83 m_topWindow
= (wxWindow
*) NULL
;
84 m_exitOnFrameDelete
= TRUE
;
90 gtk_idle_remove( m_idleTag
);
93 bool wxApp::OnInit(void)
98 bool wxApp::OnInitGui(void)
100 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, NULL
);
104 int wxApp::OnRun(void)
109 bool wxApp::ProcessIdle(void)
112 event
.SetEventObject( this );
113 ProcessEvent( event
);
115 return event
.MoreRequested();
118 void wxApp::OnIdle( wxIdleEvent
&event
)
120 static bool inOnIdle
= FALSE
;
122 // Avoid recursion (via ProcessEvent default case)
128 // 'Garbage' collection of windows deleted with Close().
129 DeletePendingObjects();
131 // flush the logged messages if any
132 wxLog
*pLog
= wxLog::GetActiveTarget();
133 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
136 // Send OnIdle events to all windows
137 bool needMore
= SendIdleEvents();
140 event
.RequestMore(TRUE
);
145 bool wxApp::SendIdleEvents(void)
147 bool needMore
= FALSE
;
148 wxNode
* node
= wxTopLevelWindows
.First();
151 wxWindow
* win
= (wxWindow
*) node
->Data();
152 if (SendIdleEvents(win
))
160 bool wxApp::SendIdleEvents( wxWindow
* win
)
162 bool needMore
= FALSE
;
165 event
.SetEventObject(win
);
166 win
->ProcessEvent(event
);
168 if (event
.MoreRequested())
171 wxNode
* node
= win
->GetChildren()->First();
174 wxWindow
* win
= (wxWindow
*) node
->Data();
175 if (SendIdleEvents(win
))
183 int wxApp::OnExit(void)
188 int wxApp::MainLoop(void)
194 void wxApp::ExitMainLoop(void)
199 bool wxApp::Initialized(void)
201 return m_initialized
;
204 bool wxApp::Pending(void)
209 void wxApp::Dispatch(void)
213 void wxApp::DeletePendingObjects(void)
215 wxNode
*node
= wxPendingDelete
.First();
218 wxObject
*obj
= (wxObject
*)node
->Data();
222 if (wxPendingDelete
.Member(obj
))
225 node
= wxPendingDelete
.First();
229 wxWindow
*wxApp::GetTopWindow(void)
231 if (m_topWindow
) return m_topWindow
;
232 wxNode
*node
= wxTopLevelWindows
.First();
233 if (!node
) return (wxWindow
*) NULL
;
234 return (wxWindow
*)node
->Data();
237 void wxApp::SetTopWindow( wxWindow
*win
)
242 void wxApp::CommonInit(void)
247 (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
250 wxSystemSettings::Init();
251 wxTheResourceCache
= new wxResourceCache(wxKEY_STRING
);
253 wxTheFontNameDirectory
= new wxFontNameDirectory
;
254 wxTheFontNameDirectory
->Initialize();
256 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
257 wxTheColourDatabase
->Initialize();
259 wxInitializeStockLists();
260 wxInitializeStockObjects();
262 wxInitializeResourceSystem();
264 // For PostScript printing
266 wxInitializePrintSetupData();
267 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
268 wxThePrintPaperDatabase
->CreateDatabase();
273 wxBitmap::InitStandardHandlers();
275 g_globalCursor = new wxCursor;
279 void wxApp::CommonCleanUp(void)
281 wxDELETE(wxTheColourDatabase
);
282 wxDELETE(wxThePrintPaperDatabase
);
283 wxDELETE(wxThePrintSetupData
);
284 wxDELETE(wxTheFontNameDirectory
);
285 wxDeleteStockObjects();
289 wxDELETE(wxTheResourceCache
);
291 wxDeleteStockLists();
293 wxCleanUpResourceSystem();
295 wxSystemSettings::Done();
298 wxLog
*wxApp::CreateLogTarget()
303 //-----------------------------------------------------------------------------
305 //-----------------------------------------------------------------------------
307 int wxEntry( int argc
, char *argv
[] )
309 wxBuffer
= new char[BUFSIZ
+ 512];
311 wxClassInfo::InitializeClasses();
313 #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
315 streambuf
* sBuf
= new wxDebugStreamBuf
;
316 ostream
* oStr
= new ostream(sBuf
) ;
317 wxDebugContext::SetStream(oStr
, sBuf
);
323 if (!wxApp::GetInitializerFunction())
325 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
329 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
331 wxObject
*test_app
= app_ini();
333 wxTheApp
= (wxApp
*) test_app
;
338 printf( "wxWindows error: wxTheApp == NULL\n" );
342 wxTheApp
->argc
= argc
;
343 wxTheApp
->argv
= argv
;
346 strcpy( name
, argv
[0] );
347 strcpy( name
, wxFileNameFromPath(name
) );
348 wxStripExtension( name
);
349 wxTheApp
->SetAppName( name
);
353 gtk_init( &argc
, &argv
);
355 #ifdef wxUSE_GDK_IMLIB
359 gtk_widget_push_visual(gdk_imlib_get_visual());
361 gtk_widget_push_colormap(gdk_imlib_get_colormap());
367 wxTheApp
->OnInitGui();
369 // Here frames insert themselves automatically
370 // into wxTopLevelWindows by getting created
373 if (!wxTheApp
->OnInit()) return 0;
375 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
379 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
381 wxTheApp
->DeletePendingObjects();
385 wxApp::CommonCleanUp();
389 wxLog
*oldLog
= wxLog::SetActiveTarget( NULL
);
390 if (oldLog
) delete oldLog
;
392 wxClassInfo::CleanUpClasses();
396 #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
398 if (wxDebugContext::CountObjectsLeft() > 0)
400 wxTrace("There were memory leaks.\n");
401 wxDebugContext::Dump();
402 wxDebugContext::PrintStatistics();
404 wxDebugContext::SetStream(NULL
, NULL
);
411 //-----------------------------------------------------------------------------
413 //-----------------------------------------------------------------------------
415 #if defined(AIX) || defined(AIX4) || defined(____HPUX__) || defined(NOMAIN)
417 // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h
421 int main(int argc
, char *argv
[]) { return wxEntry(argc
, argv
); }