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"
28 #include "../gdk_imlib/gdk_imlib.h"
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 wxApp
*wxTheApp
= 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()) {};
83 m_exitOnFrameDelete
= TRUE
;
88 gtk_idle_remove( m_idleTag
);
91 bool wxApp::OnInit(void)
96 bool wxApp::OnInitGui(void)
98 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, NULL
);
102 int wxApp::OnRun(void)
107 bool wxApp::ProcessIdle(void)
110 event
.SetEventObject( this );
111 ProcessEvent( event
);
113 return event
.MoreRequested();
116 void wxApp::OnIdle( wxIdleEvent
&event
)
118 static bool inOnIdle
= FALSE
;
120 // Avoid recursion (via ProcessEvent default case)
126 // 'Garbage' collection of windows deleted with Close().
127 DeletePendingObjects();
129 // flush the logged messages if any
130 wxLog
*pLog
= wxLog::GetActiveTarget();
131 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
134 // Send OnIdle events to all windows
135 bool needMore
= SendIdleEvents();
138 event
.RequestMore(TRUE
);
143 bool wxApp::SendIdleEvents(void)
145 bool needMore
= FALSE
;
146 wxNode
* node
= wxTopLevelWindows
.First();
149 wxWindow
* win
= (wxWindow
*) node
->Data();
150 if (SendIdleEvents(win
))
158 bool wxApp::SendIdleEvents( wxWindow
* win
)
160 bool needMore
= FALSE
;
163 event
.SetEventObject(win
);
164 win
->ProcessEvent(event
);
166 if (event
.MoreRequested())
169 wxNode
* node
= win
->GetChildren()->First();
172 wxWindow
* win
= (wxWindow
*) node
->Data();
173 if (SendIdleEvents(win
))
181 int wxApp::OnExit(void)
186 int wxApp::MainLoop(void)
192 void wxApp::ExitMainLoop(void)
197 bool wxApp::Initialized(void)
199 return m_initialized
;
202 bool wxApp::Pending(void)
207 void wxApp::Dispatch(void)
211 void wxApp::DeletePendingObjects(void)
213 wxNode
*node
= wxPendingDelete
.First();
216 wxObject
*obj
= (wxObject
*)node
->Data();
220 if (wxPendingDelete
.Member(obj
))
223 node
= wxPendingDelete
.First();
227 wxWindow
*wxApp::GetTopWindow(void)
229 if (m_topWindow
) return m_topWindow
;
230 wxNode
*node
= wxTopLevelWindows
.First();
231 if (!node
) return NULL
;
232 return (wxWindow
*)node
->Data();
235 void wxApp::SetTopWindow( wxWindow
*win
)
240 void wxApp::CommonInit(void)
245 (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
248 wxSystemSettings::Init();
249 wxTheResourceCache
= new wxResourceCache(wxKEY_STRING
);
251 wxTheFontNameDirectory
= new wxFontNameDirectory
;
252 wxTheFontNameDirectory
->Initialize();
254 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
255 wxTheColourDatabase
->Initialize();
257 wxInitializeStockLists();
258 wxInitializeStockObjects();
260 // For PostScript printing
262 wxInitializePrintSetupData();
263 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
264 wxThePrintPaperDatabase
->CreateDatabase();
269 wxBitmap::InitStandardHandlers();
271 g_globalCursor = new wxCursor;
274 // wxInitializeStockObjects();
277 void wxApp::CommonCleanUp(void)
279 wxDELETE(wxTheColourDatabase
);
280 wxDELETE(wxThePrintPaperDatabase
);
281 wxDELETE(wxThePrintSetupData
);
282 wxDELETE(wxTheFontNameDirectory
);
283 wxDeleteStockObjects();
287 wxDELETE(wxTheResourceCache
);
289 wxDeleteStockLists();
291 wxSystemSettings::Done();
294 wxLog
*wxApp::CreateLogTarget()
299 //-----------------------------------------------------------------------------
301 //-----------------------------------------------------------------------------
303 int wxEntry( int argc
, char *argv
[] )
305 wxBuffer
= new char[BUFSIZ
+ 512];
307 wxClassInfo::InitializeClasses();
309 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
311 #if !defined(_WINDLL)
312 streambuf
* sBuf
= new wxDebugStreamBuf
;
314 streambuf
* sBuf
= NULL
;
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
;
345 gtk_init( &argc
, &argv
);
351 gtk_widget_push_visual(gdk_imlib_get_visual());
353 gtk_widget_push_colormap(gdk_imlib_get_colormap());
359 wxTheApp
->OnInitGui();
361 // Here frames insert themselves automatically
362 // into wxTopLevelWindows by getting created
365 if (!wxTheApp
->OnInit()) return 0;
367 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
371 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
373 wxTheApp
->DeletePendingObjects();
377 wxApp::CommonCleanUp();
381 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
382 // At this point we want to check if there are any memory
383 // blocks that aren't part of the wxDebugContext itself,
384 // as a special case. Then when dumping we need to ignore
385 // wxDebugContext, too.
386 if (wxDebugContext::CountObjectsLeft() > 0)
388 wxTrace("There were memory leaks.\n");
389 wxDebugContext::Dump();
390 wxDebugContext::PrintStatistics();
392 wxDebugContext::SetStream(NULL
, NULL
);
398 //-----------------------------------------------------------------------------
400 //-----------------------------------------------------------------------------
402 #if defined(AIX) || defined(AIX4) || defined(____HPUX__)
404 // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h
408 int main(int argc
, char *argv
[]) { return wxEntry(argc
, argv
); }