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"
27 #include "wx/thread.h"
34 #include "wx/gtk/win_gtk.h"
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 wxApp
*wxTheApp
= (wxApp
*) NULL
;
41 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
43 extern wxList wxPendingDelete
;
44 extern wxResourceCache
*wxTheResourceCache
;
46 unsigned char g_palette
[64*3] =
114 //-----------------------------------------------------------------------------
116 //-----------------------------------------------------------------------------
118 extern void wxFlushResources(void);
120 //-----------------------------------------------------------------------------
122 //-----------------------------------------------------------------------------
131 while (gtk_events_pending() > 0) gtk_main_iteration();
135 //-----------------------------------------------------------------------------
137 //-----------------------------------------------------------------------------
139 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
141 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
142 EVT_IDLE(wxApp::OnIdle
)
145 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
147 if (wxTheApp
) while (wxTheApp
->ProcessIdle()) {}
157 m_topWindow
= (wxWindow
*) NULL
;
158 m_exitOnFrameDelete
= TRUE
;
164 gtk_idle_remove( m_idleTag
);
167 bool wxApp::OnInit(void)
172 bool wxApp::OnInitGui(void)
174 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, NULL
);
178 int wxApp::OnRun(void)
183 bool wxApp::ProcessIdle(void)
186 event
.SetEventObject( this );
187 ProcessEvent( event
);
189 return event
.MoreRequested();
192 void wxApp::OnIdle( wxIdleEvent
&event
)
194 static bool inOnIdle
= FALSE
;
196 /* Avoid recursion (via ProcessEvent default case) */
202 /* 'Garbage' collection of windows deleted with Close(). */
203 DeletePendingObjects();
205 /* flush the logged messages if any */
206 wxLog
*log
= wxLog::GetActiveTarget();
207 if (log
!= NULL
&& log
->HasPendingMessages())
210 /* Send OnIdle events to all windows */
211 bool needMore
= SendIdleEvents();
214 event
.RequestMore(TRUE
);
219 bool wxApp::SendIdleEvents(void)
221 bool needMore
= FALSE
;
223 wxNode
* node
= wxTopLevelWindows
.First();
226 wxWindow
* win
= (wxWindow
*) node
->Data();
227 if (SendIdleEvents(win
))
234 bool wxApp::SendIdleEvents( wxWindow
* win
)
236 bool needMore
= FALSE
;
239 event
.SetEventObject(win
);
240 win
->ProcessEvent(event
);
242 if (event
.MoreRequested())
245 wxNode
* node
= win
->GetChildren().First();
248 wxWindow
* win
= (wxWindow
*) node
->Data();
249 if (SendIdleEvents(win
))
257 int wxApp::OnExit(void)
262 int wxApp::MainLoop(void)
268 void wxApp::ExitMainLoop(void)
273 bool wxApp::Initialized(void)
275 return m_initialized
;
278 bool wxApp::Pending(void)
283 void wxApp::Dispatch(void)
287 void wxApp::DeletePendingObjects(void)
289 wxNode
*node
= wxPendingDelete
.First();
292 wxObject
*obj
= (wxObject
*)node
->Data();
296 if (wxPendingDelete
.Member(obj
))
299 node
= wxPendingDelete
.First();
303 wxWindow
*wxApp::GetTopWindow(void)
305 if (m_topWindow
) return m_topWindow
;
306 wxNode
*node
= wxTopLevelWindows
.First();
307 if (!node
) return (wxWindow
*) NULL
;
308 return (wxWindow
*)node
->Data();
311 void wxApp::SetTopWindow( wxWindow
*win
)
316 void wxApp::CommonInit(void)
321 (void) wxGetResource("wxWindows", "OsVersion", &wxOsVersion);
324 wxSystemSettings::Init();
326 wxTheFontNameDirectory
= new wxFontNameDirectory
;
327 wxTheFontNameDirectory
->Initialize();
329 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
330 wxTheColourDatabase
->Initialize();
332 wxInitializeStockLists();
333 wxInitializeStockObjects();
335 #ifdef wxUSE_WX_RESOURCES
336 wxTheResourceCache
= new wxResourceCache(wxKEY_STRING
);
338 wxInitializeResourceSystem();
341 wxImage::InitStandardHandlers();
343 // g_globalCursor = new wxCursor;
346 void wxApp::CommonCleanUp(void)
348 wxDELETE(wxTheColourDatabase
);
349 wxDELETE(wxTheFontNameDirectory
);
350 wxDeleteStockObjects();
352 #ifdef wxUSE_WX_RESOURCES
355 wxDELETE(wxTheResourceCache
);
357 wxCleanUpResourceSystem();
360 wxDeleteStockLists();
362 wxImage::CleanUpHandlers();
364 wxSystemSettings::Done();
367 wxLog
*wxApp::CreateLogTarget()
372 //-----------------------------------------------------------------------------
374 //-----------------------------------------------------------------------------
376 int wxEntry( int argc
, char *argv
[] )
378 wxBuffer
= new char[BUFSIZ
+ 512];
380 wxClassInfo::InitializeClasses();
385 if (!wxApp::GetInitializerFunction())
387 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
391 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
393 wxObject
*test_app
= app_ini();
395 wxTheApp
= (wxApp
*) test_app
;
400 printf( "wxWindows error: wxTheApp == NULL\n" );
404 wxTheApp
->argc
= argc
;
405 wxTheApp
->argv
= argv
;
408 strcpy( name
, argv
[0] );
409 strcpy( name
, wxFileNameFromPath(name
) );
410 wxStripExtension( name
);
411 wxTheApp
->SetAppName( name
);
415 gtk_init( &argc
, &argv
);
417 GdkColormap
*cmap
= gdk_colormap_new( gdk_visual_get_system(), TRUE
);
419 for (int i
= 0; i
< 64; i
++)
422 col
.red
= g_palette
[i
*3 + 0] << 8;
423 col
.green
= g_palette
[i
*3 + 1] << 8;
424 col
.blue
= g_palette
[i
*3 + 2] << 8;
427 gdk_color_alloc( cmap
, &col
);
430 gtk_widget_push_colormap( cmap
);
432 gtk_widget_set_default_colormap( cmap
);
436 wxModule::RegisterModules();
437 if (!wxModule::InitializeModules()) return FALSE
;
439 wxTheApp
->OnInitGui();
441 // Here frames insert themselves automatically
442 // into wxTopLevelWindows by getting created
445 if (!wxTheApp
->OnInit()) return 0;
447 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
451 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
453 wxTheApp
->DeletePendingObjects();
457 wxModule::CleanUpModules();
459 wxApp::CommonCleanUp();
462 wxTheApp
= (wxApp
*) NULL
;
464 wxClassInfo::CleanUpClasses();
468 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
470 if (wxDebugContext::CountObjectsLeft() > 0)
472 wxLogDebug("There were memory leaks.\n");
473 wxDebugContext::Dump();
474 wxDebugContext::PrintStatistics();
479 wxLog
*oldLog
= wxLog::SetActiveTarget( NULL
);
480 if (oldLog
) delete oldLog
;
485 //-----------------------------------------------------------------------------