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" 
  24 #include "wx/resource.h" 
  29 #include "../gdk_imlib/gdk_imlib.h" 
  32 //----------------------------------------------------------------------------- 
  34 //----------------------------------------------------------------------------- 
  36 wxApp 
*wxTheApp 
= (wxApp 
*)  NULL
; 
  37 wxAppInitializerFunction 
wxApp::m_appInitFn 
= (wxAppInitializerFunction
) NULL
; 
  39 extern wxList wxPendingDelete
; 
  40 extern wxResourceCache 
*wxTheResourceCache
; 
  42 //----------------------------------------------------------------------------- 
  44 //----------------------------------------------------------------------------- 
  46 extern void wxFlushResources(void); 
  48 //----------------------------------------------------------------------------- 
  50 //----------------------------------------------------------------------------- 
  59   while (gtk_events_pending() > 0) gtk_main_iteration(); 
  63 //----------------------------------------------------------------------------- 
  65 //----------------------------------------------------------------------------- 
  67 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
) 
  69 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
) 
  70     EVT_IDLE(wxApp::OnIdle
) 
  73 gint 
wxapp_idle_callback( gpointer 
WXUNUSED(data
) ) 
  75   if (wxTheApp
) while (wxTheApp
->ProcessIdle()) {} 
  83   m_topWindow 
= (wxWindow 
*) NULL
; 
  84   m_exitOnFrameDelete 
= TRUE
; 
  90   gtk_idle_remove( m_idleTag 
); 
  93 bool wxApp::OnInit(void) 
  98 bool wxApp::OnInitGui(void) 
 100   m_idleTag 
= gtk_idle_add( wxapp_idle_callback
, NULL 
); 
 104 int wxApp::OnRun(void) 
 109 bool wxApp::ProcessIdle(void) 
 112   event
.SetEventObject( this ); 
 113   ProcessEvent( event 
); 
 115   return event
.MoreRequested(); 
 118 void wxApp::OnIdle( wxIdleEvent 
&event 
) 
 120   static bool inOnIdle 
= FALSE
; 
 122   // Avoid recursion (via ProcessEvent default case) 
 128   // 'Garbage' collection of windows deleted with Close(). 
 129   DeletePendingObjects(); 
 131   // flush the logged messages if any 
 132   wxLog 
*pLog 
= wxLog::GetActiveTarget(); 
 133   if ( pLog 
!= NULL 
&& pLog
->HasPendingMessages() ) 
 136   // Send OnIdle events to all windows 
 137   bool needMore 
= SendIdleEvents(); 
 140     event
.RequestMore(TRUE
); 
 145 bool wxApp::SendIdleEvents(void) 
 147     bool needMore 
= FALSE
; 
 148         wxNode
* node 
= wxTopLevelWindows
.First(); 
 151                 wxWindow
* win 
= (wxWindow
*) node
->Data(); 
 152                 if (SendIdleEvents(win
)) 
 160 bool wxApp::SendIdleEvents( wxWindow
* win 
) 
 162     bool needMore 
= FALSE
; 
 165         event
.SetEventObject(win
); 
 166         win
->ProcessEvent(event
); 
 168     if (event
.MoreRequested()) 
 171         wxNode
* node 
= win
->GetChildren()->First(); 
 174                 wxWindow
* win 
= (wxWindow
*) node
->Data(); 
 175                 if (SendIdleEvents(win
)) 
 183 int wxApp::OnExit(void) 
 188 int wxApp::MainLoop(void) 
 194 void wxApp::ExitMainLoop(void) 
 199 bool wxApp::Initialized(void) 
 201   return m_initialized
; 
 204 bool wxApp::Pending(void) 
 209 void wxApp::Dispatch(void) 
 213 void wxApp::DeletePendingObjects(void) 
 215   wxNode 
*node 
= wxPendingDelete
.First(); 
 218     wxObject 
*obj 
= (wxObject 
*)node
->Data(); 
 222     if (wxPendingDelete
.Member(obj
)) 
 225     node 
= wxPendingDelete
.First(); 
 229 wxWindow 
*wxApp::GetTopWindow(void) 
 231   if (m_topWindow
) return m_topWindow
; 
 232   wxNode 
*node 
= wxTopLevelWindows
.First(); 
 233   if (!node
) return (wxWindow 
*) NULL
; 
 234   return (wxWindow
*)node
->Data(); 
 237 void wxApp::SetTopWindow( wxWindow 
*win 
) 
 242 void wxApp::CommonInit(void) 
 247   (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion); 
 250   wxSystemSettings::Init(); 
 251   wxTheResourceCache 
= new wxResourceCache(wxKEY_STRING
); 
 253   wxTheFontNameDirectory 
=  new wxFontNameDirectory
; 
 254   wxTheFontNameDirectory
->Initialize(); 
 256   wxTheColourDatabase 
= new wxColourDatabase(wxKEY_STRING
); 
 257   wxTheColourDatabase
->Initialize(); 
 259   wxInitializeStockLists(); 
 260   wxInitializeStockObjects(); 
 262   wxInitializeResourceSystem(); 
 264   // For PostScript printing 
 266   wxInitializePrintSetupData(); 
 267   wxThePrintPaperDatabase 
= new wxPrintPaperDatabase
; 
 268   wxThePrintPaperDatabase
->CreateDatabase(); 
 273   wxBitmap::InitStandardHandlers(); 
 275   g_globalCursor = new wxCursor; 
 279 void wxApp::CommonCleanUp(void) 
 281   wxDELETE(wxTheColourDatabase
); 
 282   wxDELETE(wxThePrintPaperDatabase
); 
 283   wxDELETE(wxThePrintSetupData
); 
 284   wxDELETE(wxTheFontNameDirectory
); 
 285   wxDeleteStockObjects(); 
 289   wxDELETE(wxTheResourceCache
); 
 291   wxDeleteStockLists(); 
 293   wxCleanUpResourceSystem(); 
 295   wxSystemSettings::Done(); 
 297   wxClassInfo::CleanUpClasses(); 
 300 wxLog 
*wxApp::CreateLogTarget() 
 305 //----------------------------------------------------------------------------- 
 307 //----------------------------------------------------------------------------- 
 309 int wxEntry( int argc
, char *argv
[] ) 
 311   wxBuffer 
= new char[BUFSIZ 
+ 512]; 
 313   wxClassInfo::InitializeClasses(); 
 315 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT 
 317 #if !defined(_WINDLL) 
 318   streambuf
* sBuf 
= new wxDebugStreamBuf
; 
 320   streambuf
* sBuf 
= NULL
; 
 322   ostream
* oStr 
= new ostream(sBuf
) ; 
 323   wxDebugContext::SetStream(oStr
, sBuf
); 
 329     if (!wxApp::GetInitializerFunction()) 
 331       printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" ); 
 335     wxAppInitializerFunction app_ini 
= wxApp::GetInitializerFunction(); 
 337     wxObject 
*test_app 
= app_ini(); 
 339     wxTheApp 
= (wxApp
*) test_app
; 
 344     printf( "wxWindows error: wxTheApp == NULL\n" ); 
 348   wxTheApp
->argc 
= argc
; 
 349   wxTheApp
->argv 
= argv
; 
 353   gtk_init( &argc
, &argv 
); 
 359   gtk_widget_push_visual(gdk_imlib_get_visual()); 
 361   gtk_widget_push_colormap(gdk_imlib_get_colormap()); 
 367   wxTheApp
->OnInitGui(); 
 369   // Here frames insert themselves automatically 
 370   // into wxTopLevelWindows by getting created 
 373   if (!wxTheApp
->OnInit()) return 0; 
 375   wxTheApp
->m_initialized 
= (wxTopLevelWindows
.Number() > 0); 
 379   if (wxTheApp
->Initialized()) retValue 
= wxTheApp
->OnRun(); 
 381   wxTheApp
->DeletePendingObjects(); 
 385   wxApp::CommonCleanUp(); 
 389 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT 
 390   // At this point we want to check if there are any memory 
 391   // blocks that aren't part of the wxDebugContext itself, 
 392   // as a special case. Then when dumping we need to ignore 
 393   // wxDebugContext, too. 
 394   if (wxDebugContext::CountObjectsLeft() > 0) 
 396     wxTrace("There were memory leaks.\n"); 
 397     wxDebugContext::Dump(); 
 398     wxDebugContext::PrintStatistics(); 
 400   wxDebugContext::SetStream(NULL
, NULL
); 
 406 //----------------------------------------------------------------------------- 
 408 //----------------------------------------------------------------------------- 
 410 #if defined(AIX) || defined(AIX4) || defined(____HPUX__) || defined(NOMAIN) 
 412  // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h 
 416   int main(int argc
, char *argv
[]) { return wxEntry(argc
, argv
); }