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"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 wxApp
*wxTheApp
= (wxApp
*) NULL
;
31 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
33 extern wxList wxPendingDelete
;
34 extern wxResourceCache
*wxTheResourceCache
;
36 unsigned char g_palette
[64*3] =
104 //-----------------------------------------------------------------------------
106 //-----------------------------------------------------------------------------
108 extern void wxFlushResources(void);
110 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
121 while (gtk_events_pending() > 0) gtk_main_iteration();
125 //-----------------------------------------------------------------------------
127 //-----------------------------------------------------------------------------
129 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
131 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
132 EVT_IDLE(wxApp::OnIdle
)
135 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
137 if (wxTheApp
) while (wxTheApp
->ProcessIdle()) {}
145 m_topWindow
= (wxWindow
*) NULL
;
146 m_exitOnFrameDelete
= TRUE
;
152 gtk_idle_remove( m_idleTag
);
155 bool wxApp::OnInit(void)
160 bool wxApp::OnInitGui(void)
162 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, NULL
);
166 int wxApp::OnRun(void)
171 bool wxApp::ProcessIdle(void)
174 event
.SetEventObject( this );
175 ProcessEvent( event
);
177 return event
.MoreRequested();
180 void wxApp::OnIdle( wxIdleEvent
&event
)
182 static bool inOnIdle
= FALSE
;
184 // Avoid recursion (via ProcessEvent default case)
190 // 'Garbage' collection of windows deleted with Close().
191 DeletePendingObjects();
193 // flush the logged messages if any
194 wxLog
*pLog
= wxLog::GetActiveTarget();
195 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
198 // Send OnIdle events to all windows
199 bool needMore
= SendIdleEvents();
202 event
.RequestMore(TRUE
);
207 bool wxApp::SendIdleEvents(void)
209 bool needMore
= FALSE
;
210 wxNode
* node
= wxTopLevelWindows
.First();
213 wxWindow
* win
= (wxWindow
*) node
->Data();
214 if (SendIdleEvents(win
))
222 bool wxApp::SendIdleEvents( wxWindow
* win
)
224 bool needMore
= FALSE
;
227 event
.SetEventObject(win
);
228 win
->ProcessEvent(event
);
230 if (event
.MoreRequested())
233 wxNode
* node
= win
->GetChildren()->First();
236 wxWindow
* win
= (wxWindow
*) node
->Data();
237 if (SendIdleEvents(win
))
245 int wxApp::OnExit(void)
250 int wxApp::MainLoop(void)
256 void wxApp::ExitMainLoop(void)
261 bool wxApp::Initialized(void)
263 return m_initialized
;
266 bool wxApp::Pending(void)
271 void wxApp::Dispatch(void)
275 void wxApp::DeletePendingObjects(void)
277 wxNode
*node
= wxPendingDelete
.First();
280 wxObject
*obj
= (wxObject
*)node
->Data();
284 if (wxPendingDelete
.Member(obj
))
287 node
= wxPendingDelete
.First();
291 wxWindow
*wxApp::GetTopWindow(void)
293 if (m_topWindow
) return m_topWindow
;
294 wxNode
*node
= wxTopLevelWindows
.First();
295 if (!node
) return (wxWindow
*) NULL
;
296 return (wxWindow
*)node
->Data();
299 void wxApp::SetTopWindow( wxWindow
*win
)
304 void wxApp::CommonInit(void)
309 (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
312 wxSystemSettings::Init();
313 wxTheResourceCache
= new wxResourceCache(wxKEY_STRING
);
315 wxTheFontNameDirectory
= new wxFontNameDirectory
;
316 wxTheFontNameDirectory
->Initialize();
318 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
319 wxTheColourDatabase
->Initialize();
321 wxInitializeStockLists();
322 wxInitializeStockObjects();
324 wxInitializeResourceSystem();
326 // For PostScript printing
328 /* Now done in wxPostScriptModule
329 wxInitializePrintSetupData();
330 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
331 wxThePrintPaperDatabase->CreateDatabase();
337 wxBitmap::InitStandardHandlers();
339 g_globalCursor = new wxCursor;
343 void wxApp::CommonCleanUp(void)
345 wxDELETE(wxTheColourDatabase
);
346 /* Now done in wxPostScriptModule
347 wxDELETE(wxThePrintPaperDatabase);
348 wxDELETE(wxThePrintSetupData);
350 wxDELETE(wxTheFontNameDirectory
);
351 wxDeleteStockObjects();
355 wxDELETE(wxTheResourceCache
);
357 wxDeleteStockLists();
359 wxCleanUpResourceSystem();
361 wxSystemSettings::Done();
364 wxLog
*wxApp::CreateLogTarget()
369 //-----------------------------------------------------------------------------
371 //-----------------------------------------------------------------------------
373 int wxEntry( int argc
, char *argv
[] )
375 wxBuffer
= new char[BUFSIZ
+ 512];
377 wxClassInfo::InitializeClasses();
379 #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
381 streambuf
* sBuf
= new wxDebugStreamBuf
;
382 ostream
* oStr
= new ostream(sBuf
) ;
383 wxDebugContext::SetStream(oStr
, sBuf
);
389 if (!wxApp::GetInitializerFunction())
391 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
395 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
397 wxObject
*test_app
= app_ini();
399 wxTheApp
= (wxApp
*) test_app
;
404 printf( "wxWindows error: wxTheApp == NULL\n" );
408 wxTheApp
->argc
= argc
;
409 wxTheApp
->argv
= argv
;
412 strcpy( name
, argv
[0] );
413 strcpy( name
, wxFileNameFromPath(name
) );
414 wxStripExtension( name
);
415 wxTheApp
->SetAppName( name
);
419 gtk_init( &argc
, &argv
);
421 GdkColormap
*cmap
= gdk_colormap_new( gdk_visual_get_system(), TRUE
);
423 for (int i
= 0; i
< 64; i
++)
426 col
.red
= g_palette
[i
*3 + 0] << 8;
427 col
.green
= g_palette
[i
*3 + 1] << 8;
428 col
.blue
= g_palette
[i
*3 + 2] << 8;
431 gdk_color_alloc( cmap
, &col
);
434 gtk_widget_push_colormap( cmap
);
436 gtk_widget_set_default_colormap( cmap
);
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 wxApp::CommonCleanUp();
462 wxLog
*oldLog
= wxLog::SetActiveTarget( NULL
);
463 if (oldLog
) delete oldLog
;
465 wxClassInfo::CleanUpClasses();
469 #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
471 if (wxDebugContext::CountObjectsLeft() > 0)
473 wxTrace("There were memory leaks.\n");
474 wxDebugContext::Dump();
475 wxDebugContext::PrintStatistics();
477 wxDebugContext::SetStream(NULL
, NULL
);
484 //-----------------------------------------------------------------------------
486 //-----------------------------------------------------------------------------
488 #if defined(AIX) || defined(AIX4) || defined(____HPUX__) || defined(NOMAIN)
490 // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h
494 int main(int argc
, char *argv
[]) { return wxEntry(argc
, argv
); }