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 #if wxUSE_WX_RESOURCES
23 #include "wx/resource.h"
25 #include "wx/module.h"
28 #include "wx/thread.h"
35 #include "wx/gtk/win_gtk.h"
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 wxApp
*wxTheApp
= (wxApp
*) NULL
;
42 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
44 extern wxList wxPendingDelete
;
45 extern wxResourceCache
*wxTheResourceCache
;
47 unsigned char g_palette
[64*3] =
115 //-----------------------------------------------------------------------------
117 //-----------------------------------------------------------------------------
119 extern void wxFlushResources(void);
121 //-----------------------------------------------------------------------------
123 //-----------------------------------------------------------------------------
132 while (gtk_events_pending() > 0) gtk_main_iteration();
136 //-----------------------------------------------------------------------------
138 //-----------------------------------------------------------------------------
140 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
142 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
143 EVT_IDLE(wxApp::OnIdle
)
146 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
148 if (wxTheApp
) while (wxTheApp
->ProcessIdle()) {}
162 m_topWindow
= (wxWindow
*) NULL
;
163 m_exitOnFrameDelete
= TRUE
;
169 gtk_idle_remove( m_idleTag
);
172 bool wxApp::InitVisual()
176 /* this initiates the standard palette as defined by GdkImlib
177 in the GNOME libraries. it ensures that all GNOME applications
178 use the same 64 colormap entries on 8-bit displays so you
179 can use several rather graphics-heavy applications at the
182 GdkColormap
*cmap
= gdk_colormap_new( gdk_visual_get_system(), TRUE
);
184 for (int i
= 0; i
< 64; i
++)
187 col
.red
= g_palette
[i
*3 + 0] << 8;
188 col
.green
= g_palette
[i
*3 + 1] << 8;
189 col
.blue
= g_palette
[i
*3 + 2] << 8;
192 gdk_color_alloc( cmap
, &col
);
195 gtk_widget_set_default_colormap( cmap
);
200 bool wxApp::OnInitGui(void)
202 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, NULL
);
207 bool wxApp::OnInit(void)
212 int wxApp::OnRun(void)
217 bool wxApp::ProcessIdle(void)
220 event
.SetEventObject( this );
221 ProcessEvent( event
);
223 return event
.MoreRequested();
226 void wxApp::OnIdle( wxIdleEvent
&event
)
228 static bool inOnIdle
= FALSE
;
230 /* Avoid recursion (via ProcessEvent default case) */
236 /* 'Garbage' collection of windows deleted with Close(). */
237 DeletePendingObjects();
239 /* flush the logged messages if any */
240 wxLog
*log
= wxLog::GetActiveTarget();
241 if (log
!= NULL
&& log
->HasPendingMessages())
244 /* Send OnIdle events to all windows */
245 bool needMore
= SendIdleEvents();
248 event
.RequestMore(TRUE
);
253 bool wxApp::SendIdleEvents(void)
255 bool needMore
= FALSE
;
257 wxNode
* node
= wxTopLevelWindows
.First();
260 wxWindow
* win
= (wxWindow
*) node
->Data();
261 if (SendIdleEvents(win
))
268 bool wxApp::SendIdleEvents( wxWindow
* win
)
270 bool needMore
= FALSE
;
273 event
.SetEventObject(win
);
274 win
->ProcessEvent(event
);
276 if (event
.MoreRequested())
279 wxNode
* node
= win
->GetChildren().First();
282 wxWindow
* win
= (wxWindow
*) node
->Data();
283 if (SendIdleEvents(win
))
291 int wxApp::OnExit(void)
296 int wxApp::MainLoop(void)
302 void wxApp::ExitMainLoop(void)
307 bool wxApp::Initialized(void)
309 return m_initialized
;
312 bool wxApp::Pending(void)
317 void wxApp::Dispatch(void)
321 void wxApp::DeletePendingObjects(void)
323 wxNode
*node
= wxPendingDelete
.First();
326 wxObject
*obj
= (wxObject
*)node
->Data();
330 if (wxPendingDelete
.Member(obj
))
333 node
= wxPendingDelete
.First();
337 wxWindow
*wxApp::GetTopWindow(void)
339 if (m_topWindow
) return m_topWindow
;
340 wxNode
*node
= wxTopLevelWindows
.First();
341 if (!node
) return (wxWindow
*) NULL
;
342 return (wxWindow
*)node
->Data();
345 void wxApp::SetTopWindow( wxWindow
*win
)
350 void wxApp::CommonInit(void)
352 wxSystemSettings::Init();
354 wxTheFontNameDirectory
= new wxFontNameDirectory
;
355 wxTheFontNameDirectory
->Initialize();
357 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
358 wxTheColourDatabase
->Initialize();
360 wxInitializeStockLists();
361 wxInitializeStockObjects();
363 #if wxUSE_WX_RESOURCES
364 wxTheResourceCache
= new wxResourceCache(wxKEY_STRING
);
366 wxInitializeResourceSystem();
369 wxImage::InitStandardHandlers();
371 // g_globalCursor = new wxCursor;
374 void wxApp::CommonCleanUp(void)
376 if (wxTheColourDatabase
) delete wxTheColourDatabase
;
377 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
379 if (wxTheFontNameDirectory
) delete wxTheFontNameDirectory
;
380 wxTheFontNameDirectory
= (wxFontNameDirectory
*) NULL
;
382 wxDeleteStockObjects();
384 #if wxUSE_WX_RESOURCES
387 if (wxTheResourceCache
) delete wxTheResourceCache
;
388 wxTheResourceCache
= (wxResourceCache
*) NULL
;
390 wxCleanUpResourceSystem();
393 wxDeleteStockLists();
395 wxImage::CleanUpHandlers();
397 wxSystemSettings::Done();
400 wxLog
*wxApp::CreateLogTarget()
405 //-----------------------------------------------------------------------------
407 //-----------------------------------------------------------------------------
409 int wxEntry( int argc
, char *argv
[] )
411 wxBuffer
= new char[BUFSIZ
+ 512];
413 wxClassInfo::InitializeClasses();
418 if (!wxApp::GetInitializerFunction())
420 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
424 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
426 wxObject
*test_app
= app_ini();
428 wxTheApp
= (wxApp
*) test_app
;
433 printf( "wxWindows error: wxTheApp == NULL\n" );
437 wxTheApp
->argc
= argc
;
438 wxTheApp
->argv
= argv
;
441 strcpy( name
, argv
[0] );
442 strcpy( name
, wxFileNameFromPath(name
) );
443 wxStripExtension( name
);
444 wxTheApp
->SetAppName( name
);
448 gtk_init( &argc
, &argv
);
450 if (!wxTheApp
->InitVisual()) return 0;
454 if (!wxTheApp
->OnInitGui()) return 0;
456 wxModule::RegisterModules();
457 if (!wxModule::InitializeModules()) return FALSE
;
459 // Here frames insert themselves automatically
460 // into wxTopLevelWindows by getting created
463 if (!wxTheApp
->OnInit()) return 0;
465 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
469 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
471 wxTheApp
->DeletePendingObjects();
475 wxModule::CleanUpModules();
477 wxApp::CommonCleanUp();
480 wxTheApp
= (wxApp
*) NULL
;
482 wxClassInfo::CleanUpClasses();
486 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
488 if (wxDebugContext::CountObjectsLeft() > 0)
490 wxLogDebug("There were memory leaks.\n");
491 wxDebugContext::Dump();
492 wxDebugContext::PrintStatistics();
497 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
498 if (oldLog
) delete oldLog
;