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" 
  32 #include "wx/gtk/win_gtk.h" 
  34 //----------------------------------------------------------------------------- 
  36 //----------------------------------------------------------------------------- 
  38 wxApp 
*wxTheApp 
= (wxApp 
*)  NULL
; 
  39 wxAppInitializerFunction 
wxApp::m_appInitFn 
= (wxAppInitializerFunction
) NULL
; 
  41 extern wxList wxPendingDelete
; 
  42 extern wxResourceCache 
*wxTheResourceCache
; 
  44 unsigned char g_palette
[64*3] = 
 112 //----------------------------------------------------------------------------- 
 114 //----------------------------------------------------------------------------- 
 116 extern void wxFlushResources(void); 
 118 //----------------------------------------------------------------------------- 
 120 //----------------------------------------------------------------------------- 
 129     while (gtk_events_pending() > 0) gtk_main_iteration(); 
 133 //----------------------------------------------------------------------------- 
 135 //----------------------------------------------------------------------------- 
 137 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
) 
 139 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
) 
 140     EVT_IDLE(wxApp::OnIdle
) 
 143 gint 
wxapp_idle_callback( gpointer 
WXUNUSED(data
) ) 
 145     if (wxTheApp
) while (wxTheApp
->ProcessIdle()) {} 
 155     m_topWindow 
= (wxWindow 
*) NULL
; 
 156     m_exitOnFrameDelete 
= TRUE
; 
 162     gtk_idle_remove( m_idleTag 
); 
 165 bool wxApp::OnInit(void) 
 170 bool wxApp::OnInitGui(void) 
 172     m_idleTag 
= gtk_idle_add( wxapp_idle_callback
, NULL 
); 
 176 int wxApp::OnRun(void) 
 181 bool wxApp::ProcessIdle(void) 
 184     event
.SetEventObject( this ); 
 185     ProcessEvent( event 
); 
 187     return event
.MoreRequested(); 
 190 void wxApp::OnIdle( wxIdleEvent 
&event 
) 
 192     static bool inOnIdle 
= FALSE
; 
 194     // Avoid recursion (via ProcessEvent default case) 
 200     // 'Garbage' collection of windows deleted with Close(). 
 201     DeletePendingObjects(); 
 203     // flush the logged messages if any 
 204     wxLog 
*pLog 
= wxLog::GetActiveTarget(); 
 205     if (pLog 
!= NULL 
&& pLog
->HasPendingMessages()) 
 208     // Send OnIdle events to all windows 
 209     bool needMore 
= SendIdleEvents(); 
 212         event
.RequestMore(TRUE
); 
 217 bool wxApp::SendIdleEvents(void) 
 219     bool needMore 
= FALSE
; 
 221     wxNode
* node 
= wxTopLevelWindows
.First(); 
 224         wxWindow
* win 
= (wxWindow
*) node
->Data(); 
 225         if (SendIdleEvents(win
)) 
 232 bool wxApp::SendIdleEvents( wxWindow
* win 
) 
 234     bool needMore 
= FALSE
; 
 237     event
.SetEventObject(win
); 
 238     win
->ProcessEvent(event
); 
 240     if (event
.MoreRequested()) 
 243     wxNode
* node 
= win
->GetChildren().First(); 
 246         wxWindow
* win 
= (wxWindow
*) node
->Data(); 
 247         if (SendIdleEvents(win
)) 
 255 int wxApp::OnExit(void) 
 260 int wxApp::MainLoop(void) 
 266 void wxApp::ExitMainLoop(void) 
 271 bool wxApp::Initialized(void) 
 273     return m_initialized
; 
 276 bool wxApp::Pending(void) 
 281 void wxApp::Dispatch(void) 
 285 void wxApp::DeletePendingObjects(void) 
 287     wxNode 
*node 
= wxPendingDelete
.First(); 
 290         wxObject 
*obj 
= (wxObject 
*)node
->Data(); 
 294         if (wxPendingDelete
.Member(obj
)) 
 297         node 
= wxPendingDelete
.First(); 
 301 wxWindow 
*wxApp::GetTopWindow(void) 
 303     if (m_topWindow
) return m_topWindow
; 
 304     wxNode 
*node 
= wxTopLevelWindows
.First(); 
 305     if (!node
) return (wxWindow 
*) NULL
; 
 306     return (wxWindow
*)node
->Data(); 
 309 void wxApp::SetTopWindow( wxWindow 
*win 
) 
 314 void wxApp::CommonInit(void) 
 319   (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion); 
 322   wxSystemSettings::Init(); 
 323   wxTheResourceCache 
= new wxResourceCache(wxKEY_STRING
); 
 325   wxTheFontNameDirectory 
=  new wxFontNameDirectory
; 
 326   wxTheFontNameDirectory
->Initialize(); 
 328   wxTheColourDatabase 
= new wxColourDatabase(wxKEY_STRING
); 
 329   wxTheColourDatabase
->Initialize(); 
 331   wxInitializeStockLists(); 
 332   wxInitializeStockObjects(); 
 334   wxInitializeResourceSystem(); 
 336   wxImage::InitStandardHandlers(); 
 338 //  g_globalCursor = new wxCursor; 
 341 void wxApp::CommonCleanUp(void) 
 343     wxDELETE(wxTheColourDatabase
); 
 344     wxDELETE(wxTheFontNameDirectory
); 
 345     wxDeleteStockObjects(); 
 349     wxDELETE(wxTheResourceCache
); 
 351     wxDeleteStockLists(); 
 353     wxCleanUpResourceSystem(); 
 355     wxImage::CleanUpHandlers(); 
 357     wxSystemSettings::Done(); 
 360 wxLog 
*wxApp::CreateLogTarget() 
 365 //----------------------------------------------------------------------------- 
 367 //----------------------------------------------------------------------------- 
 369 int wxEntry( int argc
, char *argv
[] ) 
 371     wxBuffer 
= new char[BUFSIZ 
+ 512]; 
 373     wxClassInfo::InitializeClasses(); 
 375   /* Debug stream no longer used 
 376 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT 
 378   streambuf* sBuf = new wxDebugStreamBuf; 
 379   ostream* oStr = new ostream(sBuf) ; 
 380   wxDebugContext::SetStream(oStr, sBuf); 
 386         if (!wxApp::GetInitializerFunction()) 
 388             printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" ); 
 392         wxAppInitializerFunction app_ini 
= wxApp::GetInitializerFunction(); 
 394         wxObject 
*test_app 
= app_ini(); 
 396         wxTheApp 
= (wxApp
*) test_app
; 
 401         printf( "wxWindows error: wxTheApp == NULL\n" ); 
 405     wxTheApp
->argc 
= argc
; 
 406     wxTheApp
->argv 
= argv
; 
 409     strcpy( name
, argv
[0] ); 
 410     strcpy( name
, wxFileNameFromPath(name
) ); 
 411     wxStripExtension( name 
); 
 412     wxTheApp
->SetAppName( name 
); 
 416     gtk_init( &argc
, &argv 
); 
 418     GdkColormap 
*cmap 
= gdk_colormap_new( gdk_visual_get_system(), TRUE 
); 
 420     for (int i 
= 0; i 
< 64; i
++) 
 423         col
.red    
= g_palette
[i
*3 + 0] << 8; 
 424         col
.green  
= g_palette
[i
*3 + 1] << 8; 
 425         col
.blue   
= g_palette
[i
*3 + 2] << 8; 
 428         gdk_color_alloc( cmap
, &col 
); 
 431     gtk_widget_push_colormap( cmap 
); 
 433     gtk_widget_set_default_colormap( cmap 
); 
 437     wxModule::RegisterModules(); 
 438     if (!wxModule::InitializeModules()) return FALSE
; 
 440     wxTheApp
->OnInitGui(); 
 442     // Here frames insert themselves automatically 
 443     // into wxTopLevelWindows by getting created 
 446     if (!wxTheApp
->OnInit()) return 0; 
 448     wxTheApp
->m_initialized 
= (wxTopLevelWindows
.Number() > 0); 
 452     if (wxTheApp
->Initialized()) retValue 
= wxTheApp
->OnRun(); 
 454     wxTheApp
->DeletePendingObjects(); 
 458     wxModule::CleanUpModules(); 
 460     wxApp::CommonCleanUp(); 
 463     wxTheApp 
= (wxApp
*) NULL
; 
 465     wxClassInfo::CleanUpClasses(); 
 469 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT 
 471     if (wxDebugContext::CountObjectsLeft() > 0) 
 473         wxLogDebug("There were memory leaks.\n"); 
 474         wxDebugContext::Dump(); 
 475         wxDebugContext::PrintStatistics(); 
 477 //  wxDebugContext::SetStream(NULL, NULL); 
 481     wxLog 
*oldLog 
= wxLog::SetActiveTarget( NULL 
); 
 482     if (oldLog
) delete oldLog
; 
 487 //-----------------------------------------------------------------------------