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
*log
= wxLog::GetActiveTarget();
205 if (log
!= NULL
&& log
->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();
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();
472 wxLog
*oldLog
= wxLog::SetActiveTarget( NULL
);
473 if (oldLog
) delete oldLog
;
478 //-----------------------------------------------------------------------------