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 GdkVisual
*wxVisualSetByExternal
= (GdkVisual
*) NULL
;
48 GdkColormap
*wxColormapSetByExternal
= (GdkColormap
*) NULL
;
50 unsigned char g_palette
[64*3] =
118 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
122 extern void wxFlushResources(void);
124 //-----------------------------------------------------------------------------
126 //-----------------------------------------------------------------------------
135 while (gtk_events_pending() > 0) gtk_main_iteration();
139 //-----------------------------------------------------------------------------
141 //-----------------------------------------------------------------------------
143 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
145 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
146 EVT_IDLE(wxApp::OnIdle
)
149 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
151 if (wxTheApp
) while (wxTheApp
->ProcessIdle()) {}
165 m_topWindow
= (wxWindow
*) NULL
;
166 m_exitOnFrameDelete
= TRUE
;
172 gtk_idle_remove( m_idleTag
);
175 bool wxApp::InitVisual()
177 if (wxVisualSetByExternal
)
179 /* this happens in the wxModule code of the OpenGl canvas.
180 it chooses the best display for OpenGl and stores it
181 in wxDisplaySetByExternal. we then have to make it the
182 default for the system */
184 gtk_widget_set_default_visual( wxVisualSetByExternal
);
187 if (wxColormapSetByExternal
)
189 /* OpenGl also gives us a colormap */
191 gtk_widget_set_default_colormap( wxColormapSetByExternal
);
195 /* this initiates the standard palette as defined by GdkImlib
196 in the GNOME libraries. it ensures that all GNOME applications
197 use the same 64 colormap entries on 8-bit displays so you
198 can use several rather graphics-heavy applications at the
201 GdkColormap
*cmap
= gdk_colormap_new( gdk_visual_get_system(), TRUE
);
203 for (int i
= 0; i
< 64; i
++)
206 col
.red
= g_palette
[i
*3 + 0] << 8;
207 col
.green
= g_palette
[i
*3 + 1] << 8;
208 col
.blue
= g_palette
[i
*3 + 2] << 8;
211 gdk_color_alloc( cmap
, &col
);
214 gtk_widget_set_default_colormap( cmap
);
220 bool wxApp::OnInitGui(void)
222 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, NULL
);
227 bool wxApp::OnInit(void)
232 int wxApp::OnRun(void)
237 bool wxApp::ProcessIdle(void)
240 event
.SetEventObject( this );
241 ProcessEvent( event
);
243 return event
.MoreRequested();
246 void wxApp::OnIdle( wxIdleEvent
&event
)
248 static bool inOnIdle
= FALSE
;
250 /* Avoid recursion (via ProcessEvent default case) */
256 /* 'Garbage' collection of windows deleted with Close(). */
257 DeletePendingObjects();
259 /* flush the logged messages if any */
260 wxLog
*log
= wxLog::GetActiveTarget();
261 if (log
!= NULL
&& log
->HasPendingMessages())
264 /* Send OnIdle events to all windows */
265 bool needMore
= SendIdleEvents();
268 event
.RequestMore(TRUE
);
273 bool wxApp::SendIdleEvents(void)
275 bool needMore
= FALSE
;
277 wxNode
* node
= wxTopLevelWindows
.First();
280 wxWindow
* win
= (wxWindow
*) node
->Data();
281 if (SendIdleEvents(win
))
288 bool wxApp::SendIdleEvents( wxWindow
* win
)
290 bool needMore
= FALSE
;
293 event
.SetEventObject(win
);
294 win
->ProcessEvent(event
);
296 if (event
.MoreRequested())
299 wxNode
* node
= win
->GetChildren().First();
302 wxWindow
* win
= (wxWindow
*) node
->Data();
303 if (SendIdleEvents(win
))
311 int wxApp::OnExit(void)
316 int wxApp::MainLoop(void)
322 void wxApp::ExitMainLoop(void)
327 bool wxApp::Initialized(void)
329 return m_initialized
;
332 bool wxApp::Pending(void)
337 void wxApp::Dispatch(void)
341 void wxApp::DeletePendingObjects(void)
343 wxNode
*node
= wxPendingDelete
.First();
346 wxObject
*obj
= (wxObject
*)node
->Data();
350 if (wxPendingDelete
.Member(obj
))
353 node
= wxPendingDelete
.First();
357 wxWindow
*wxApp::GetTopWindow(void)
359 if (m_topWindow
) return m_topWindow
;
360 wxNode
*node
= wxTopLevelWindows
.First();
361 if (!node
) return (wxWindow
*) NULL
;
362 return (wxWindow
*)node
->Data();
365 void wxApp::SetTopWindow( wxWindow
*win
)
370 void wxApp::CommonInit(void)
372 wxSystemSettings::Init();
374 wxTheFontNameDirectory
= new wxFontNameDirectory
;
375 wxTheFontNameDirectory
->Initialize();
377 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
378 wxTheColourDatabase
->Initialize();
380 wxInitializeStockLists();
381 wxInitializeStockObjects();
383 #if wxUSE_WX_RESOURCES
384 wxTheResourceCache
= new wxResourceCache(wxKEY_STRING
);
386 wxInitializeResourceSystem();
389 wxImage::InitStandardHandlers();
391 // g_globalCursor = new wxCursor;
394 void wxApp::CommonCleanUp(void)
396 if (wxTheColourDatabase
) delete wxTheColourDatabase
;
397 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
399 if (wxTheFontNameDirectory
) delete wxTheFontNameDirectory
;
400 wxTheFontNameDirectory
= (wxFontNameDirectory
*) NULL
;
402 wxDeleteStockObjects();
404 #if wxUSE_WX_RESOURCES
407 if (wxTheResourceCache
) delete wxTheResourceCache
;
408 wxTheResourceCache
= (wxResourceCache
*) NULL
;
410 wxCleanUpResourceSystem();
413 wxDeleteStockLists();
415 wxImage::CleanUpHandlers();
417 wxSystemSettings::Done();
420 wxLog
*wxApp::CreateLogTarget()
425 //-----------------------------------------------------------------------------
427 //-----------------------------------------------------------------------------
429 int wxEntry( int argc
, char *argv
[] )
431 wxBuffer
= new char[BUFSIZ
+ 512];
433 wxClassInfo::InitializeClasses();
438 if (!wxApp::GetInitializerFunction())
440 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
444 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
446 wxObject
*test_app
= app_ini();
448 wxTheApp
= (wxApp
*) test_app
;
453 printf( "wxWindows error: wxTheApp == NULL\n" );
457 wxTheApp
->argc
= argc
;
458 wxTheApp
->argv
= argv
;
461 strcpy( name
, argv
[0] );
462 strcpy( name
, wxFileNameFromPath(name
) );
463 wxStripExtension( name
);
464 wxTheApp
->SetAppName( name
);
468 gtk_init( &argc
, &argv
);
470 wxModule::RegisterModules();
471 if (!wxModule::InitializeModules()) return FALSE
;
473 if (!wxTheApp
->InitVisual()) return 0;
477 if (!wxTheApp
->OnInitGui()) return 0;
479 // Here frames insert themselves automatically
480 // into wxTopLevelWindows by getting created
483 if (!wxTheApp
->OnInit()) return 0;
485 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
489 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
491 wxTheApp
->DeletePendingObjects();
495 wxModule::CleanUpModules();
497 wxApp::CommonCleanUp();
500 wxTheApp
= (wxApp
*) NULL
;
502 wxClassInfo::CleanUpClasses();
506 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
508 if (wxDebugContext::CountObjectsLeft() > 0)
510 wxLogDebug("There were memory leaks.\n");
511 wxDebugContext::Dump();
512 wxDebugContext::PrintStatistics();
517 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
518 if (oldLog
) delete oldLog
;