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"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 wxApp
*wxTheApp
= (wxApp
*) NULL
;
32 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
34 extern wxList wxPendingDelete
;
35 extern wxResourceCache
*wxTheResourceCache
;
37 unsigned char g_palette
[64*3] =
105 //-----------------------------------------------------------------------------
107 //-----------------------------------------------------------------------------
109 extern void wxFlushResources(void);
111 //-----------------------------------------------------------------------------
113 //-----------------------------------------------------------------------------
122 while (gtk_events_pending() > 0) gtk_main_iteration();
126 //-----------------------------------------------------------------------------
128 //-----------------------------------------------------------------------------
130 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
132 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
133 EVT_IDLE(wxApp::OnIdle
)
136 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
138 if (wxTheApp
) while (wxTheApp
->ProcessIdle()) {}
146 m_topWindow
= (wxWindow
*) NULL
;
147 m_exitOnFrameDelete
= TRUE
;
153 gtk_idle_remove( m_idleTag
);
156 bool wxApp::OnInit(void)
161 bool wxApp::OnInitGui(void)
163 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, NULL
);
167 int wxApp::OnRun(void)
172 bool wxApp::ProcessIdle(void)
175 event
.SetEventObject( this );
176 ProcessEvent( event
);
178 return event
.MoreRequested();
181 void wxApp::OnIdle( wxIdleEvent
&event
)
183 static bool inOnIdle
= FALSE
;
185 // Avoid recursion (via ProcessEvent default case)
191 // 'Garbage' collection of windows deleted with Close().
192 DeletePendingObjects();
194 // flush the logged messages if any
195 wxLog
*pLog
= wxLog::GetActiveTarget();
196 if (pLog
!= NULL
&& pLog
->HasPendingMessages())
199 // Send OnIdle events to all windows
200 bool needMore
= SendIdleEvents();
203 event
.RequestMore(TRUE
);
208 bool wxApp::SendIdleEvents(void)
210 bool needMore
= FALSE
;
212 wxNode
* node
= wxTopLevelWindows
.First();
215 wxWindow
* win
= (wxWindow
*) node
->Data();
216 if (SendIdleEvents(win
))
223 bool wxApp::SendIdleEvents( wxWindow
* win
)
225 bool needMore
= FALSE
;
228 event
.SetEventObject(win
);
229 win
->ProcessEvent(event
);
231 if (event
.MoreRequested())
234 wxNode
* node
= win
->GetChildren()->First();
237 wxWindow
* win
= (wxWindow
*) node
->Data();
238 if (SendIdleEvents(win
))
246 int wxApp::OnExit(void)
251 int wxApp::MainLoop(void)
257 void wxApp::ExitMainLoop(void)
262 bool wxApp::Initialized(void)
264 return m_initialized
;
267 bool wxApp::Pending(void)
272 void wxApp::Dispatch(void)
276 void wxApp::DeletePendingObjects(void)
278 wxNode
*node
= wxPendingDelete
.First();
281 wxObject
*obj
= (wxObject
*)node
->Data();
285 if (wxPendingDelete
.Member(obj
))
288 node
= wxPendingDelete
.First();
292 wxWindow
*wxApp::GetTopWindow(void)
294 if (m_topWindow
) return m_topWindow
;
295 wxNode
*node
= wxTopLevelWindows
.First();
296 if (!node
) return (wxWindow
*) NULL
;
297 return (wxWindow
*)node
->Data();
300 void wxApp::SetTopWindow( wxWindow
*win
)
305 void wxApp::CommonInit(void)
310 (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
313 wxSystemSettings::Init();
314 wxTheResourceCache
= new wxResourceCache(wxKEY_STRING
);
316 wxTheFontNameDirectory
= new wxFontNameDirectory
;
317 wxTheFontNameDirectory
->Initialize();
319 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
320 wxTheColourDatabase
->Initialize();
322 wxInitializeStockLists();
323 wxInitializeStockObjects();
325 wxInitializeResourceSystem();
327 wxImage::InitStandardHandlers();
329 // g_globalCursor = new wxCursor;
332 void wxApp::CommonCleanUp(void)
334 wxDELETE(wxTheColourDatabase
);
335 wxDELETE(wxTheFontNameDirectory
);
336 wxDeleteStockObjects();
340 wxDELETE(wxTheResourceCache
);
342 wxDeleteStockLists();
344 wxCleanUpResourceSystem();
346 wxImage::CleanUpHandlers();
348 wxSystemSettings::Done();
351 wxLog
*wxApp::CreateLogTarget()
356 //-----------------------------------------------------------------------------
358 //-----------------------------------------------------------------------------
360 int wxEntry( int argc
, char *argv
[] )
362 wxBuffer
= new char[BUFSIZ
+ 512];
364 wxClassInfo::InitializeClasses();
366 /* Debug stream no longer used
367 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
369 streambuf* sBuf = new wxDebugStreamBuf;
370 ostream* oStr = new ostream(sBuf) ;
371 wxDebugContext::SetStream(oStr, sBuf);
377 if (!wxApp::GetInitializerFunction())
379 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
383 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
385 wxObject
*test_app
= app_ini();
387 wxTheApp
= (wxApp
*) test_app
;
392 printf( "wxWindows error: wxTheApp == NULL\n" );
396 wxTheApp
->argc
= argc
;
397 wxTheApp
->argv
= argv
;
400 strcpy( name
, argv
[0] );
401 strcpy( name
, wxFileNameFromPath(name
) );
402 wxStripExtension( name
);
403 wxTheApp
->SetAppName( name
);
407 gtk_init( &argc
, &argv
);
409 GdkColormap
*cmap
= gdk_colormap_new( gdk_visual_get_system(), TRUE
);
411 for (int i
= 0; i
< 64; i
++)
414 col
.red
= g_palette
[i
*3 + 0] << 8;
415 col
.green
= g_palette
[i
*3 + 1] << 8;
416 col
.blue
= g_palette
[i
*3 + 2] << 8;
419 gdk_color_alloc( cmap
, &col
);
422 gtk_widget_push_colormap( cmap
);
424 gtk_widget_set_default_colormap( cmap
);
428 wxModule::RegisterModules();
429 if (!wxModule::InitializeModules()) return FALSE
;
431 wxTheApp
->OnInitGui();
433 // Here frames insert themselves automatically
434 // into wxTopLevelWindows by getting created
437 if (!wxTheApp
->OnInit()) return 0;
439 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
443 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
445 wxTheApp
->DeletePendingObjects();
449 wxModule::CleanUpModules();
451 wxApp::CommonCleanUp();
454 wxTheApp
= (wxApp
*) NULL
;
456 wxClassInfo::CleanUpClasses();
460 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
462 if (wxDebugContext::CountObjectsLeft() > 0)
464 wxLogDebug("There were memory leaks.\n");
465 wxDebugContext::Dump();
466 wxDebugContext::PrintStatistics();
468 // wxDebugContext::SetStream(NULL, NULL);
472 wxLog
*oldLog
= wxLog::SetActiveTarget( NULL
);
473 if (oldLog
) delete oldLog
;
478 //-----------------------------------------------------------------------------
480 //-----------------------------------------------------------------------------
482 #if defined(AIX) || defined(AIX4) || defined(____HPUX__) || defined(NOMAIN)
484 // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h
488 int main(int argc
, char *argv
[]) { return wxEntry(argc
, argv
); }