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"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 wxApp
*wxTheApp
= (wxApp
*) NULL
;
33 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
35 extern wxList wxPendingDelete
;
36 extern wxResourceCache
*wxTheResourceCache
;
38 unsigned char g_palette
[64*3] =
106 //-----------------------------------------------------------------------------
108 //-----------------------------------------------------------------------------
110 extern void wxFlushResources(void);
112 //-----------------------------------------------------------------------------
114 //-----------------------------------------------------------------------------
123 while (gtk_events_pending() > 0) gtk_main_iteration();
127 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
131 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
133 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
134 EVT_IDLE(wxApp::OnIdle
)
137 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
139 if (wxTheApp
) while (wxTheApp
->ProcessIdle()) {}
147 m_topWindow
= (wxWindow
*) NULL
;
148 m_exitOnFrameDelete
= TRUE
;
154 gtk_idle_remove( m_idleTag
);
157 bool wxApp::OnInit(void)
162 bool wxApp::OnInitGui(void)
164 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, NULL
);
168 int wxApp::OnRun(void)
173 bool wxApp::ProcessIdle(void)
176 event
.SetEventObject( this );
177 ProcessEvent( event
);
179 return event
.MoreRequested();
182 void wxApp::OnIdle( wxIdleEvent
&event
)
184 static bool inOnIdle
= FALSE
;
186 // Avoid recursion (via ProcessEvent default case)
192 // 'Garbage' collection of windows deleted with Close().
193 DeletePendingObjects();
195 // flush the logged messages if any
196 wxLog
*pLog
= wxLog::GetActiveTarget();
197 if (pLog
!= NULL
&& pLog
->HasPendingMessages())
200 // Send OnIdle events to all windows
201 bool needMore
= SendIdleEvents();
204 event
.RequestMore(TRUE
);
209 bool wxApp::SendIdleEvents(void)
211 bool needMore
= FALSE
;
213 wxNode
* node
= wxTopLevelWindows
.First();
216 wxWindow
* win
= (wxWindow
*) node
->Data();
217 if (SendIdleEvents(win
))
224 bool wxApp::SendIdleEvents( wxWindow
* win
)
226 bool needMore
= FALSE
;
229 event
.SetEventObject(win
);
230 win
->ProcessEvent(event
);
232 if (event
.MoreRequested())
235 wxNode
* node
= win
->GetChildren()->First();
238 wxWindow
* win
= (wxWindow
*) node
->Data();
239 if (SendIdleEvents(win
))
247 int wxApp::OnExit(void)
252 int wxApp::MainLoop(void)
258 void wxApp::ExitMainLoop(void)
263 bool wxApp::Initialized(void)
265 return m_initialized
;
268 bool wxApp::Pending(void)
273 void wxApp::Dispatch(void)
277 void wxApp::DeletePendingObjects(void)
279 wxNode
*node
= wxPendingDelete
.First();
282 wxObject
*obj
= (wxObject
*)node
->Data();
286 if (wxPendingDelete
.Member(obj
))
289 node
= wxPendingDelete
.First();
293 wxWindow
*wxApp::GetTopWindow(void)
295 if (m_topWindow
) return m_topWindow
;
296 wxNode
*node
= wxTopLevelWindows
.First();
297 if (!node
) return (wxWindow
*) NULL
;
298 return (wxWindow
*)node
->Data();
301 void wxApp::SetTopWindow( wxWindow
*win
)
306 void wxApp::CommonInit(void)
311 (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
314 wxSystemSettings::Init();
315 wxTheResourceCache
= new wxResourceCache(wxKEY_STRING
);
317 wxTheFontNameDirectory
= new wxFontNameDirectory
;
318 wxTheFontNameDirectory
->Initialize();
320 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
321 wxTheColourDatabase
->Initialize();
323 wxInitializeStockLists();
324 wxInitializeStockObjects();
326 wxInitializeResourceSystem();
328 wxImage::InitStandardHandlers();
330 // g_globalCursor = new wxCursor;
333 void wxApp::CommonCleanUp(void)
335 wxDELETE(wxTheColourDatabase
);
336 wxDELETE(wxTheFontNameDirectory
);
337 wxDeleteStockObjects();
341 wxDELETE(wxTheResourceCache
);
343 wxDeleteStockLists();
345 wxCleanUpResourceSystem();
347 wxImage::CleanUpHandlers();
349 wxSystemSettings::Done();
352 wxLog
*wxApp::CreateLogTarget()
357 //-----------------------------------------------------------------------------
359 //-----------------------------------------------------------------------------
361 int wxEntry( int argc
, char *argv
[] )
363 wxBuffer
= new char[BUFSIZ
+ 512];
365 wxClassInfo::InitializeClasses();
367 /* Debug stream no longer used
368 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
370 streambuf* sBuf = new wxDebugStreamBuf;
371 ostream* oStr = new ostream(sBuf) ;
372 wxDebugContext::SetStream(oStr, sBuf);
378 if (!wxApp::GetInitializerFunction())
380 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
384 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
386 wxObject
*test_app
= app_ini();
388 wxTheApp
= (wxApp
*) test_app
;
393 printf( "wxWindows error: wxTheApp == NULL\n" );
397 wxTheApp
->argc
= argc
;
398 wxTheApp
->argv
= argv
;
401 strcpy( name
, argv
[0] );
402 strcpy( name
, wxFileNameFromPath(name
) );
403 wxStripExtension( name
);
404 wxTheApp
->SetAppName( name
);
408 gtk_init( &argc
, &argv
);
410 GdkColormap
*cmap
= gdk_colormap_new( gdk_visual_get_system(), TRUE
);
412 for (int i
= 0; i
< 64; i
++)
415 col
.red
= g_palette
[i
*3 + 0] << 8;
416 col
.green
= g_palette
[i
*3 + 1] << 8;
417 col
.blue
= g_palette
[i
*3 + 2] << 8;
420 gdk_color_alloc( cmap
, &col
);
423 gtk_widget_push_colormap( cmap
);
425 gtk_widget_set_default_colormap( cmap
);
429 wxModule::RegisterModules();
430 if (!wxModule::InitializeModules()) return FALSE
;
432 wxTheApp
->OnInitGui();
434 // Here frames insert themselves automatically
435 // into wxTopLevelWindows by getting created
438 if (!wxTheApp
->OnInit()) return 0;
440 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
444 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
446 wxTheApp
->DeletePendingObjects();
450 wxModule::CleanUpModules();
452 wxApp::CommonCleanUp();
455 wxTheApp
= (wxApp
*) NULL
;
457 wxClassInfo::CleanUpClasses();
461 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
463 if (wxDebugContext::CountObjectsLeft() > 0)
465 wxLogDebug("There were memory leaks.\n");
466 wxDebugContext::Dump();
467 wxDebugContext::PrintStatistics();
469 // wxDebugContext::SetStream(NULL, NULL);
473 wxLog
*oldLog
= wxLog::SetActiveTarget( NULL
);
474 if (oldLog
) delete oldLog
;
479 //-----------------------------------------------------------------------------
481 //-----------------------------------------------------------------------------
483 #if defined(AIX) || defined(AIX4) || defined(____HPUX__) || defined(NOMAIN)
485 // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h
489 int main(int argc
, char *argv
[]) { return wxEntry(argc
, argv
); }