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 wxImage::InitStandardHandlers();
328 // g_globalCursor = new wxCursor;
331 void wxApp::CommonCleanUp(void)
333 wxDELETE(wxTheColourDatabase
);
334 wxDELETE(wxTheFontNameDirectory
);
335 wxDeleteStockObjects();
339 wxDELETE(wxTheResourceCache
);
341 wxDeleteStockLists();
343 wxCleanUpResourceSystem();
345 wxImage::CleanUpHandlers();
347 wxSystemSettings::Done();
350 wxLog
*wxApp::CreateLogTarget()
355 //-----------------------------------------------------------------------------
357 //-----------------------------------------------------------------------------
359 int wxEntry( int argc
, char *argv
[] )
361 wxBuffer
= new char[BUFSIZ
+ 512];
363 wxClassInfo::InitializeClasses();
365 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
367 streambuf
* sBuf
= new wxDebugStreamBuf
;
368 ostream
* oStr
= new ostream(sBuf
) ;
369 wxDebugContext::SetStream(oStr
, sBuf
);
375 if (!wxApp::GetInitializerFunction())
377 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
381 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
383 wxObject
*test_app
= app_ini();
385 wxTheApp
= (wxApp
*) test_app
;
390 printf( "wxWindows error: wxTheApp == NULL\n" );
394 wxTheApp
->argc
= argc
;
395 wxTheApp
->argv
= argv
;
398 strcpy( name
, argv
[0] );
399 strcpy( name
, wxFileNameFromPath(name
) );
400 wxStripExtension( name
);
401 wxTheApp
->SetAppName( name
);
405 gtk_init( &argc
, &argv
);
407 GdkColormap
*cmap
= gdk_colormap_new( gdk_visual_get_system(), TRUE
);
409 for (int i
= 0; i
< 64; i
++)
412 col
.red
= g_palette
[i
*3 + 0] << 8;
413 col
.green
= g_palette
[i
*3 + 1] << 8;
414 col
.blue
= g_palette
[i
*3 + 2] << 8;
417 gdk_color_alloc( cmap
, &col
);
420 gtk_widget_push_colormap( cmap
);
422 gtk_widget_set_default_colormap( cmap
);
426 wxTheApp
->OnInitGui();
428 // Here frames insert themselves automatically
429 // into wxTopLevelWindows by getting created
432 if (!wxTheApp
->OnInit()) return 0;
434 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
438 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
440 wxTheApp
->DeletePendingObjects();
444 wxApp::CommonCleanUp();
448 wxLog
*oldLog
= wxLog::SetActiveTarget( NULL
);
449 if (oldLog
) delete oldLog
;
451 wxClassInfo::CleanUpClasses();
455 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
457 if (wxDebugContext::CountObjectsLeft() > 0)
459 wxTrace("There were memory leaks.\n");
460 wxDebugContext::Dump();
461 wxDebugContext::PrintStatistics();
463 wxDebugContext::SetStream(NULL
, NULL
);
470 //-----------------------------------------------------------------------------
472 //-----------------------------------------------------------------------------
474 #if defined(AIX) || defined(AIX4) || defined(____HPUX__) || defined(NOMAIN)
476 // main in IMPLEMENT_WX_MAIN in IMPLEMENT_APP in app.h
480 int main(int argc
, char *argv
[]) { return wxEntry(argc
, argv
); }