1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "app.h"
15 #include "wx/gdicmn.h"
19 #include "wx/memory.h"
21 #include "wx/settings.h"
22 #include "wx/resource.h"
23 #include "wx/module.h"
25 #include "wx/thread.h"
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 wxApp
*wxTheApp
= (wxApp
*) NULL
;
34 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
36 extern wxList wxPendingDelete
;
37 extern wxResourceCache
*wxTheResourceCache
;
39 unsigned char g_palette
[64*3] =
107 //-----------------------------------------------------------------------------
109 //-----------------------------------------------------------------------------
111 extern void wxFlushResources(void);
113 //-----------------------------------------------------------------------------
115 //-----------------------------------------------------------------------------
124 while (gtk_events_pending() > 0) gtk_main_iteration();
128 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
132 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
134 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
135 EVT_IDLE(wxApp::OnIdle
)
138 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
140 if (wxTheApp
) while (wxTheApp
->ProcessIdle()) {}
150 m_topWindow
= (wxWindow
*) NULL
;
151 m_exitOnFrameDelete
= TRUE
;
157 gtk_idle_remove( m_idleTag
);
160 bool wxApp::OnInit(void)
165 bool wxApp::OnInitGui(void)
167 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, NULL
);
171 int wxApp::OnRun(void)
176 bool wxApp::ProcessIdle(void)
179 event
.SetEventObject( this );
180 ProcessEvent( event
);
182 return event
.MoreRequested();
185 void wxApp::OnIdle( wxIdleEvent
&event
)
187 static bool inOnIdle
= FALSE
;
189 // Avoid recursion (via ProcessEvent default case)
195 // 'Garbage' collection of windows deleted with Close().
196 DeletePendingObjects();
198 // flush the logged messages if any
199 wxLog
*pLog
= wxLog::GetActiveTarget();
200 if (pLog
!= NULL
&& pLog
->HasPendingMessages())
203 // Send OnIdle events to all windows
204 bool needMore
= SendIdleEvents();
207 event
.RequestMore(TRUE
);
212 bool wxApp::SendIdleEvents(void)
214 bool needMore
= FALSE
;
216 wxNode
* node
= wxTopLevelWindows
.First();
219 wxWindow
* win
= (wxWindow
*) node
->Data();
220 if (SendIdleEvents(win
))
227 bool wxApp::SendIdleEvents( wxWindow
* win
)
229 bool needMore
= FALSE
;
232 event
.SetEventObject(win
);
233 win
->ProcessEvent(event
);
235 if (event
.MoreRequested())
238 wxNode
* node
= win
->GetChildren().First();
241 wxWindow
* win
= (wxWindow
*) node
->Data();
242 if (SendIdleEvents(win
))
250 int wxApp::OnExit(void)
255 int wxApp::MainLoop(void)
261 void wxApp::ExitMainLoop(void)
266 bool wxApp::Initialized(void)
268 return m_initialized
;
271 bool wxApp::Pending(void)
276 void wxApp::Dispatch(void)
280 void wxApp::DeletePendingObjects(void)
282 wxNode
*node
= wxPendingDelete
.First();
285 wxObject
*obj
= (wxObject
*)node
->Data();
289 if (wxPendingDelete
.Member(obj
))
292 node
= wxPendingDelete
.First();
296 wxWindow
*wxApp::GetTopWindow(void)
298 if (m_topWindow
) return m_topWindow
;
299 wxNode
*node
= wxTopLevelWindows
.First();
300 if (!node
) return (wxWindow
*) NULL
;
301 return (wxWindow
*)node
->Data();
304 void wxApp::SetTopWindow( wxWindow
*win
)
309 void wxApp::CommonInit(void)
314 (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
317 wxSystemSettings::Init();
318 wxTheResourceCache
= new wxResourceCache(wxKEY_STRING
);
320 wxTheFontNameDirectory
= new wxFontNameDirectory
;
321 wxTheFontNameDirectory
->Initialize();
323 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
324 wxTheColourDatabase
->Initialize();
326 wxInitializeStockLists();
327 wxInitializeStockObjects();
329 wxInitializeResourceSystem();
331 wxImage::InitStandardHandlers();
333 // g_globalCursor = new wxCursor;
336 void wxApp::CommonCleanUp(void)
338 wxDELETE(wxTheColourDatabase
);
339 wxDELETE(wxTheFontNameDirectory
);
340 wxDeleteStockObjects();
344 wxDELETE(wxTheResourceCache
);
346 wxDeleteStockLists();
348 wxCleanUpResourceSystem();
350 wxImage::CleanUpHandlers();
352 wxSystemSettings::Done();
355 wxLog
*wxApp::CreateLogTarget()
360 //-----------------------------------------------------------------------------
362 //-----------------------------------------------------------------------------
364 int wxEntry( int argc
, char *argv
[] )
366 wxBuffer
= new char[BUFSIZ
+ 512];
368 wxClassInfo::InitializeClasses();
370 /* Debug stream no longer used
371 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
373 streambuf* sBuf = new wxDebugStreamBuf;
374 ostream* oStr = new ostream(sBuf) ;
375 wxDebugContext::SetStream(oStr, sBuf);
381 if (!wxApp::GetInitializerFunction())
383 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
387 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
389 wxObject
*test_app
= app_ini();
391 wxTheApp
= (wxApp
*) test_app
;
396 printf( "wxWindows error: wxTheApp == NULL\n" );
400 wxTheApp
->argc
= argc
;
401 wxTheApp
->argv
= argv
;
404 strcpy( name
, argv
[0] );
405 strcpy( name
, wxFileNameFromPath(name
) );
406 wxStripExtension( name
);
407 wxTheApp
->SetAppName( name
);
411 gtk_init( &argc
, &argv
);
413 GdkColormap
*cmap
= gdk_colormap_new( gdk_visual_get_system(), TRUE
);
415 for (int i
= 0; i
< 64; i
++)
418 col
.red
= g_palette
[i
*3 + 0] << 8;
419 col
.green
= g_palette
[i
*3 + 1] << 8;
420 col
.blue
= g_palette
[i
*3 + 2] << 8;
423 gdk_color_alloc( cmap
, &col
);
426 gtk_widget_push_colormap( cmap
);
428 gtk_widget_set_default_colormap( cmap
);
432 wxModule::RegisterModules();
433 if (!wxModule::InitializeModules()) return FALSE
;
435 wxTheApp
->OnInitGui();
437 // Here frames insert themselves automatically
438 // into wxTopLevelWindows by getting created
441 if (!wxTheApp
->OnInit()) return 0;
443 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
447 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
449 wxTheApp
->DeletePendingObjects();
453 wxModule::CleanUpModules();
455 wxApp::CommonCleanUp();
458 wxTheApp
= (wxApp
*) NULL
;
460 wxClassInfo::CleanUpClasses();
464 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
466 if (wxDebugContext::CountObjectsLeft() > 0)
468 wxLogDebug("There were memory leaks.\n");
469 wxDebugContext::Dump();
470 wxDebugContext::PrintStatistics();
472 // wxDebugContext::SetStream(NULL, NULL);
476 wxLog
*oldLog
= wxLog::SetActiveTarget( NULL
);
477 if (oldLog
) delete oldLog
;
482 //-----------------------------------------------------------------------------