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
= (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 && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
315 #if !defined(_WINDLL)
316 streambuf
* sBuf
= new wxDebugStreamBuf
;
318 streambuf
* sBuf
= NULL
;
320 ostream
* oStr
= new ostream(sBuf
) ;
321 wxDebugContext::SetStream(oStr
, sBuf
);
327 if (!wxApp::GetInitializerFunction())
329 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
333 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
335 wxObject
*test_app
= app_ini();
337 wxTheApp
= (wxApp
*) test_app
;
342 printf( "wxWindows error: wxTheApp == NULL\n" );
346 wxTheApp
->argc
= argc
;
347 wxTheApp
->argv
= argv
;
351 gtk_init( &argc
, &argv
);
357 gtk_widget_push_visual(gdk_imlib_get_visual());
359 gtk_widget_push_colormap(gdk_imlib_get_colormap());
365 wxTheApp
->OnInitGui();
367 // Here frames insert themselves automatically
368 // into wxTopLevelWindows by getting created
371 if (!wxTheApp
->OnInit()) return 0;
373 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
377 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
379 wxTheApp
->DeletePendingObjects();
383 wxApp::CommonCleanUp();
387 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
388 // At this point we want to check if there are any memory
389 // blocks that aren't part of the wxDebugContext itself,
390 // as a special case. Then when dumping we need to ignore
391 // wxDebugContext, too.
392 if (wxDebugContext::CountObjectsLeft() > 0)
394 wxTrace("There were memory leaks.\n");
395 wxDebugContext::Dump();
396 wxDebugContext::PrintStatistics();
398 wxDebugContext::SetStream(NULL
, NULL
);
404 //-----------------------------------------------------------------------------
406 //-----------------------------------------------------------------------------
408 #if defined(AIX) || defined(AIX4) || defined(____HPUX__) || defined(NOMAIN)
410 // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h
414 int main(int argc
, char *argv
[]) { return wxEntry(argc
, argv
); }