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 #ifdef wxUSE_WX_RESOURCES 
  23 #include "wx/resource.h" 
  25 #include "wx/module.h" 
  28 #include "wx/thread.h" 
  35 #include "wx/gtk/win_gtk.h" 
  37 //----------------------------------------------------------------------------- 
  39 //----------------------------------------------------------------------------- 
  41 wxApp 
*wxTheApp 
= (wxApp 
*)  NULL
; 
  42 wxAppInitializerFunction 
wxApp::m_appInitFn 
= (wxAppInitializerFunction
) NULL
; 
  44 extern wxList wxPendingDelete
; 
  45 extern wxResourceCache 
*wxTheResourceCache
; 
  47 unsigned char g_palette
[64*3] = 
 115 //----------------------------------------------------------------------------- 
 117 //----------------------------------------------------------------------------- 
 119 extern void wxFlushResources(void); 
 121 //----------------------------------------------------------------------------- 
 123 //----------------------------------------------------------------------------- 
 132     while (gtk_events_pending() > 0) gtk_main_iteration(); 
 136 //----------------------------------------------------------------------------- 
 138 //----------------------------------------------------------------------------- 
 140 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
) 
 142 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
) 
 143     EVT_IDLE(wxApp::OnIdle
) 
 146 gint 
wxapp_idle_callback( gpointer 
WXUNUSED(data
) ) 
 148     if (wxTheApp
) while (wxTheApp
->ProcessIdle()) {} 
 162     m_topWindow 
= (wxWindow 
*) NULL
; 
 163     m_exitOnFrameDelete 
= TRUE
; 
 169     gtk_idle_remove( m_idleTag 
); 
 172 bool wxApp::OnInit(void) 
 177 bool wxApp::OnInitGui(void) 
 179     m_idleTag 
= gtk_idle_add( wxapp_idle_callback
, NULL 
); 
 183 int wxApp::OnRun(void) 
 188 bool wxApp::ProcessIdle(void) 
 191     event
.SetEventObject( this ); 
 192     ProcessEvent( event 
); 
 194     return event
.MoreRequested(); 
 197 void wxApp::OnIdle( wxIdleEvent 
&event 
) 
 199     static bool inOnIdle 
= FALSE
; 
 201     /* Avoid recursion (via ProcessEvent default case) */ 
 207     /* 'Garbage' collection of windows deleted with Close(). */ 
 208     DeletePendingObjects(); 
 210     /* flush the logged messages if any */ 
 211     wxLog 
*log 
= wxLog::GetActiveTarget(); 
 212     if (log 
!= NULL 
&& log
->HasPendingMessages()) 
 215     /* Send OnIdle events to all windows */ 
 216     bool needMore 
= SendIdleEvents(); 
 219         event
.RequestMore(TRUE
); 
 224 bool wxApp::SendIdleEvents(void) 
 226     bool needMore 
= FALSE
; 
 228     wxNode
* node 
= wxTopLevelWindows
.First(); 
 231         wxWindow
* win 
= (wxWindow
*) node
->Data(); 
 232         if (SendIdleEvents(win
)) 
 239 bool wxApp::SendIdleEvents( wxWindow
* win 
) 
 241     bool needMore 
= FALSE
; 
 244     event
.SetEventObject(win
); 
 245     win
->ProcessEvent(event
); 
 247     if (event
.MoreRequested()) 
 250     wxNode
* node 
= win
->GetChildren().First(); 
 253         wxWindow
* win 
= (wxWindow
*) node
->Data(); 
 254         if (SendIdleEvents(win
)) 
 262 int wxApp::OnExit(void) 
 267 int wxApp::MainLoop(void) 
 273 void wxApp::ExitMainLoop(void) 
 278 bool wxApp::Initialized(void) 
 280     return m_initialized
; 
 283 bool wxApp::Pending(void) 
 288 void wxApp::Dispatch(void) 
 292 void wxApp::DeletePendingObjects(void) 
 294     wxNode 
*node 
= wxPendingDelete
.First(); 
 297         wxObject 
*obj 
= (wxObject 
*)node
->Data(); 
 301         if (wxPendingDelete
.Member(obj
)) 
 304         node 
= wxPendingDelete
.First(); 
 308 wxWindow 
*wxApp::GetTopWindow(void) 
 310     if (m_topWindow
) return m_topWindow
; 
 311     wxNode 
*node 
= wxTopLevelWindows
.First(); 
 312     if (!node
) return (wxWindow 
*) NULL
; 
 313     return (wxWindow
*)node
->Data(); 
 316 void wxApp::SetTopWindow( wxWindow 
*win 
) 
 321 void wxApp::CommonInit(void) 
 326   (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion); 
 329   wxSystemSettings::Init(); 
 331   wxTheFontNameDirectory 
=  new wxFontNameDirectory
; 
 332   wxTheFontNameDirectory
->Initialize(); 
 334   wxTheColourDatabase 
= new wxColourDatabase(wxKEY_STRING
); 
 335   wxTheColourDatabase
->Initialize(); 
 337   wxInitializeStockLists(); 
 338   wxInitializeStockObjects(); 
 340 #ifdef wxUSE_WX_RESOURCES 
 341   wxTheResourceCache 
= new wxResourceCache(wxKEY_STRING
); 
 343   wxInitializeResourceSystem(); 
 346   wxImage::InitStandardHandlers(); 
 348 //  g_globalCursor = new wxCursor; 
 351 void wxApp::CommonCleanUp(void) 
 353     wxDELETE(wxTheColourDatabase
); 
 354     wxDELETE(wxTheFontNameDirectory
); 
 355     wxDeleteStockObjects(); 
 357 #ifdef wxUSE_WX_RESOURCES 
 360     wxDELETE(wxTheResourceCache
); 
 362     wxCleanUpResourceSystem(); 
 365     wxDeleteStockLists(); 
 367     wxImage::CleanUpHandlers(); 
 369     wxSystemSettings::Done(); 
 372 wxLog 
*wxApp::CreateLogTarget() 
 377 //----------------------------------------------------------------------------- 
 379 //----------------------------------------------------------------------------- 
 381 int wxEntry( int argc
, char *argv
[] ) 
 383     wxBuffer 
= new char[BUFSIZ 
+ 512]; 
 385     wxClassInfo::InitializeClasses(); 
 390         if (!wxApp::GetInitializerFunction()) 
 392             printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" ); 
 396         wxAppInitializerFunction app_ini 
= wxApp::GetInitializerFunction(); 
 398         wxObject 
*test_app 
= app_ini(); 
 400         wxTheApp 
= (wxApp
*) test_app
; 
 405         printf( "wxWindows error: wxTheApp == NULL\n" ); 
 409     wxTheApp
->argc 
= argc
; 
 410     wxTheApp
->argv 
= argv
; 
 413     strcpy( name
, argv
[0] ); 
 414     strcpy( name
, wxFileNameFromPath(name
) ); 
 415     wxStripExtension( name 
); 
 416     wxTheApp
->SetAppName( name 
); 
 420     gtk_init( &argc
, &argv 
); 
 422     GdkColormap 
*cmap 
= gdk_colormap_new( gdk_visual_get_system(), TRUE 
); 
 424     for (int i 
= 0; i 
< 64; i
++) 
 427         col
.red    
= g_palette
[i
*3 + 0] << 8; 
 428         col
.green  
= g_palette
[i
*3 + 1] << 8; 
 429         col
.blue   
= g_palette
[i
*3 + 2] << 8; 
 432         gdk_color_alloc( cmap
, &col 
); 
 435     gtk_widget_push_colormap( cmap 
); 
 437     gtk_widget_set_default_colormap( cmap 
); 
 441     wxModule::RegisterModules(); 
 442     if (!wxModule::InitializeModules()) return FALSE
; 
 444     wxTheApp
->OnInitGui(); 
 446     // Here frames insert themselves automatically 
 447     // into wxTopLevelWindows by getting created 
 450     if (!wxTheApp
->OnInit()) return 0; 
 452     wxTheApp
->m_initialized 
= (wxTopLevelWindows
.Number() > 0); 
 456     if (wxTheApp
->Initialized()) retValue 
= wxTheApp
->OnRun(); 
 458     wxTheApp
->DeletePendingObjects(); 
 462     wxModule::CleanUpModules(); 
 464     wxApp::CommonCleanUp(); 
 467     wxTheApp 
= (wxApp
*) NULL
; 
 469     wxClassInfo::CleanUpClasses(); 
 473 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT 
 475     if (wxDebugContext::CountObjectsLeft() > 0) 
 477         wxLogDebug("There were memory leaks.\n"); 
 478         wxDebugContext::Dump(); 
 479         wxDebugContext::PrintStatistics(); 
 484     wxLog 
*oldLog 
= wxLog::SetActiveTarget( NULL 
); 
 485     if (oldLog
) delete oldLog
; 
 490 //-----------------------------------------------------------------------------