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"
20 #include "wx/memory.h"
22 #include "wx/settings.h"
23 #include "wx/resource.h"
27 #ifdef wxUSE_GDK_IMLIB
28 #include "../gdk_imlib/gdk_imlib.h"
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 wxApp
*wxTheApp
= (wxApp
*) NULL
;
36 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
38 extern wxList wxPendingDelete
;
39 extern wxResourceCache
*wxTheResourceCache
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 extern void wxFlushResources(void);
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
58 while (gtk_events_pending() > 0) gtk_main_iteration();
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
66 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
68 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
69 EVT_IDLE(wxApp::OnIdle
)
72 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
74 if (wxTheApp
) while (wxTheApp
->ProcessIdle()) {}
82 m_topWindow
= (wxWindow
*) NULL
;
83 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 (wxWindow
*) 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 /* Now done in wxPostScriptModule
266 wxInitializePrintSetupData();
267 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
268 wxThePrintPaperDatabase->CreateDatabase();
274 wxBitmap::InitStandardHandlers();
276 g_globalCursor = new wxCursor;
280 void wxApp::CommonCleanUp(void)
282 wxDELETE(wxTheColourDatabase
);
283 /* Now done in wxPostScriptModule
284 wxDELETE(wxThePrintPaperDatabase);
285 wxDELETE(wxThePrintSetupData);
287 wxDELETE(wxTheFontNameDirectory
);
288 wxDeleteStockObjects();
292 wxDELETE(wxTheResourceCache
);
294 wxDeleteStockLists();
296 wxCleanUpResourceSystem();
298 wxSystemSettings::Done();
301 wxLog
*wxApp::CreateLogTarget()
306 //-----------------------------------------------------------------------------
308 //-----------------------------------------------------------------------------
310 int wxEntry( int argc
, char *argv
[] )
312 wxBuffer
= new char[BUFSIZ
+ 512];
314 wxClassInfo::InitializeClasses();
316 #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
318 streambuf
* sBuf
= new wxDebugStreamBuf
;
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
;
349 strcpy( name
, argv
[0] );
350 strcpy( name
, wxFileNameFromPath(name
) );
351 wxStripExtension( name
);
352 wxTheApp
->SetAppName( name
);
356 gtk_init( &argc
, &argv
);
358 #ifdef wxUSE_GDK_IMLIB
362 gtk_widget_push_visual(gdk_imlib_get_visual());
364 gtk_widget_push_colormap(gdk_imlib_get_colormap());
370 wxTheApp
->OnInitGui();
372 // Here frames insert themselves automatically
373 // into wxTopLevelWindows by getting created
376 if (!wxTheApp
->OnInit()) return 0;
378 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
382 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
384 wxTheApp
->DeletePendingObjects();
388 wxApp::CommonCleanUp();
392 wxLog
*oldLog
= wxLog::SetActiveTarget( NULL
);
393 if (oldLog
) delete oldLog
;
395 wxClassInfo::CleanUpClasses();
399 #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
401 if (wxDebugContext::CountObjectsLeft() > 0)
403 wxTrace("There were memory leaks.\n");
404 wxDebugContext::Dump();
405 wxDebugContext::PrintStatistics();
407 wxDebugContext::SetStream(NULL
, NULL
);
414 //-----------------------------------------------------------------------------
416 //-----------------------------------------------------------------------------
418 #if defined(AIX) || defined(AIX4) || defined(____HPUX__) || defined(NOMAIN)
420 // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h
424 int main(int argc
, char *argv
[]) { return wxEntry(argc
, argv
); }