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/dialog.h"
24 #if wxUSE_WX_RESOURCES
25 #include "wx/resource.h"
28 #include "wx/module.h"
31 #include "wx/thread.h"
39 #include "wx/gtk/win_gtk.h"
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 wxApp
*wxTheApp
= (wxApp
*) NULL
;
46 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
48 extern wxList wxPendingDelete
;
50 extern wxList
*wxPendingEvents
;
51 extern wxCriticalSection
*wxPendingEventsLocker
;
53 extern wxResourceCache
*wxTheResourceCache
;
55 unsigned char g_palette
[64*3] =
123 //-----------------------------------------------------------------------------
125 //-----------------------------------------------------------------------------
127 extern void wxFlushResources(void);
129 //-----------------------------------------------------------------------------
131 //-----------------------------------------------------------------------------
139 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) );
143 // it's necessary to call ProcessIdle() to update the frames sizes which
144 // might have been changed (it also will update other things set from
145 // OnUpdateUI() which is a nice (and desired) side effect)
146 for ( wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
148 node
= node
->GetNext() )
150 wxWindow
*win
= node
->GetData();
151 win
->OnInternalIdle();
154 // We need to temporarily remove idle callbacks or the loop will
156 gtk_idle_remove( wxTheApp
->m_idleTag
);
158 while (gtk_events_pending())
159 gtk_main_iteration();
161 wxTheApp
->m_idleTag
= gtk_idle_add( wxapp_idle_callback
, (gpointer
) NULL
);
165 //-----------------------------------------------------------------------------
167 //-----------------------------------------------------------------------------
169 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
171 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
172 EVT_IDLE(wxApp::OnIdle
)
175 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
179 while (wxTheApp
->ProcessIdle())
195 m_topWindow
= (wxWindow
*) NULL
;
196 m_exitOnFrameDelete
= TRUE
;
198 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, (gpointer
) NULL
);
200 m_colorCube
= (unsigned char*) NULL
;
205 gtk_idle_remove( m_idleTag
);
207 if (m_colorCube
) free(m_colorCube
);
210 bool wxApp::OnInitGui()
212 /* Nothing to do for 15, 16, 24, 32 bit displays */
214 GdkVisual
*visual
= gdk_visual_get_system();
215 if (visual
->depth
> 8) return TRUE
;
217 /* this initiates the standard palette as defined by GdkImlib
218 in the GNOME libraries. it ensures that all GNOME applications
219 use the same 64 colormap entries on 8-bit displays so you
220 can use several rather graphics-heavy applications at the
222 NOTE: this doesn't really seem to work this way... */
225 GdkColormap *cmap = gdk_colormap_new( gdk_visual_get_system(), TRUE );
227 for (int i = 0; i < 64; i++)
230 col.red = g_palette[i*3 + 0] << 8;
231 col.green = g_palette[i*3 + 1] << 8;
232 col.blue = g_palette[i*3 + 2] << 8;
235 gdk_color_alloc( cmap, &col );
238 gtk_widget_set_default_colormap( cmap );
241 /* initialize color cube for 8-bit color reduction dithering */
243 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
245 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
247 for (int r
= 0; r
< 32; r
++)
249 for (int g
= 0; g
< 32; g
++)
251 for (int b
= 0; b
< 32; b
++)
253 int rr
= (r
<< 3) | (r
>> 2);
254 int gg
= (g
<< 3) | (g
>> 2);
255 int bb
= (b
<< 3) | (b
>> 2);
257 GdkColor
*colors
= cmap
->colors
;
258 int max
= 3 * (65536);
261 for (int i
= 0; i
< cmap
->size
; i
++)
263 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
264 int gdiff
= ((gg
<< 8)- colors
[i
].green
);
265 int bdiff
= ((bb
<< 8)- colors
[i
].blue
);
266 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
267 if (sum
< max
) { index
= i
; max
= sum
; }
270 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
279 bool wxApp::ProcessIdle()
282 event
.SetEventObject( this );
283 ProcessEvent( event
);
285 return event
.MoreRequested();
288 void wxApp::OnIdle( wxIdleEvent
&event
)
290 static bool inOnIdle
= FALSE
;
292 /* Avoid recursion (via ProcessEvent default case) */
299 /* Resend in the main thread events which have been prepared in other
301 ProcessPendingEvents();
304 /* 'Garbage' collection of windows deleted with Close(). */
305 DeletePendingObjects();
307 /* flush the logged messages if any */
308 wxLog
*log
= wxLog::GetActiveTarget();
309 if (log
!= NULL
&& log
->HasPendingMessages())
312 /* Send OnIdle events to all windows */
313 bool needMore
= SendIdleEvents();
316 event
.RequestMore(TRUE
);
321 bool wxApp::SendIdleEvents()
323 bool needMore
= FALSE
;
325 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
328 wxWindow
* win
= node
->GetData();
329 if (SendIdleEvents(win
))
331 node
= node
->GetNext();
337 bool wxApp::SendIdleEvents( wxWindow
* win
)
339 bool needMore
= FALSE
;
342 event
.SetEventObject(win
);
344 win
->OnInternalIdle();
346 win
->ProcessEvent(event
);
348 if (event
.MoreRequested())
351 wxNode
* node
= win
->GetChildren().First();
354 wxWindow
* win
= (wxWindow
*) node
->Data();
355 if (SendIdleEvents(win
))
363 int wxApp::MainLoop()
369 void wxApp::ExitMainLoop()
374 bool wxApp::Initialized()
376 return m_initialized
;
379 bool wxApp::Pending()
384 void wxApp::Dispatch()
389 void wxApp::ProcessPendingEvents()
391 wxNode
*node
= wxPendingEvents
->First();
392 wxCriticalSectionLocker
locker(*wxPendingEventsLocker
);
396 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
398 handler
->ProcessPendingEvents();
402 node
= wxPendingEvents
->First();
407 void wxApp::DeletePendingObjects()
409 wxNode
*node
= wxPendingDelete
.First();
412 wxObject
*obj
= (wxObject
*)node
->Data();
416 if (wxPendingDelete
.Member(obj
))
419 node
= wxPendingDelete
.First();
423 wxWindow
*wxApp::GetTopWindow()
427 else if (wxTopLevelWindows
.GetCount() > 0)
428 return wxTopLevelWindows
.GetFirst()->GetData();
433 void wxApp::SetTopWindow( wxWindow
*win
)
438 bool wxApp::Initialize()
440 wxBuffer
= new char[BUFSIZ
+ 512];
442 wxClassInfo::InitializeClasses();
444 wxSystemSettings::Init();
446 // GL: I'm annoyed ... I don't know where to put this and I don't want to
447 // create a module for that as it's part of the core.
449 wxPendingEvents
= new wxList();
450 wxPendingEventsLocker
= new wxCriticalSection();
454 wxTheFontNameDirectory = new wxFontNameDirectory;
455 wxTheFontNameDirectory->Initialize();
458 wxTheColourDatabase
= new wxColourDatabase( wxKEY_STRING
);
459 wxTheColourDatabase
->Initialize();
461 wxInitializeStockLists();
462 wxInitializeStockObjects();
464 #if wxUSE_WX_RESOURCES
465 wxTheResourceCache
= new wxResourceCache( wxKEY_STRING
);
467 wxInitializeResourceSystem();
470 wxImage::InitStandardHandlers();
472 /* no global cursor under X
473 g_globalCursor = new wxCursor; */
475 wxModule::RegisterModules();
476 if (!wxModule::InitializeModules()) return FALSE
;
481 void wxApp::CleanUp()
483 wxModule::CleanUpModules();
485 #if wxUSE_WX_RESOURCES
488 if (wxTheResourceCache
)
489 delete wxTheResourceCache
;
490 wxTheResourceCache
= (wxResourceCache
*) NULL
;
492 wxCleanUpResourceSystem();
495 if (wxTheColourDatabase
)
496 delete wxTheColourDatabase
;
497 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
500 if (wxTheFontNameDirectory) delete wxTheFontNameDirectory;
501 wxTheFontNameDirectory = (wxFontNameDirectory*) NULL;
504 wxDeleteStockObjects();
506 wxDeleteStockLists();
508 wxImage::CleanUpHandlers();
511 wxTheApp
= (wxApp
*) NULL
;
513 // GL: I'm annoyed ... I don't know where to put this and I don't want to
514 // create a module for that as it's part of the core.
516 delete wxPendingEvents
;
517 delete wxPendingEventsLocker
;
520 wxSystemSettings::Done();
524 wxClassInfo::CleanUpClasses();
526 // check for memory leaks
527 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
528 if (wxDebugContext::CountObjectsLeft() > 0)
530 wxLogDebug("There were memory leaks.\n");
531 wxDebugContext::Dump();
532 wxDebugContext::PrintStatistics();
536 // do this as the very last thing because everything else can log messages
537 wxLog::DontCreateOnDemand();
539 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
544 wxLog
*wxApp::CreateLogTarget()
549 //-----------------------------------------------------------------------------
551 //-----------------------------------------------------------------------------
553 int wxEntry( int argc
, char *argv
[] )
557 gtk_init( &argc
, &argv
);
559 if (!wxApp::Initialize())
564 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
565 "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
567 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
569 wxObject
*test_app
= app_ini();
571 wxTheApp
= (wxApp
*) test_app
;
574 wxCHECK_MSG( wxTheApp
, -1, "wxWindows error: no application object" );
576 wxTheApp
->argc
= argc
;
577 wxTheApp
->argv
= argv
;
580 strcpy( name
, argv
[0] );
581 strcpy( name
, wxFileNameFromPath(name
) );
582 wxStripExtension( name
);
583 wxTheApp
->SetAppName( name
);
585 if (!wxTheApp
->OnInitGui())
588 /* Here frames insert themselves automatically
589 * into wxTopLevelWindows by getting created
592 if (!wxTheApp
->OnInit())
595 wxTheApp
->m_initialized
= wxTopLevelWindows
.GetCount() != 0;
599 if (wxTheApp
->Initialized())
600 retValue
= wxTheApp
->OnRun();
602 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
605 // Forcibly delete the window.
606 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
607 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
609 topWindow
->Close( TRUE
);
610 wxTheApp
->DeletePendingObjects();
615 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
621 // flush the logged messages if any
622 wxLog
*log
= wxLog::GetActiveTarget();
623 if (log
!= NULL
&& log
->HasPendingMessages())
626 // continuing to use user defined log target is unsafe from now on because
627 // some resources may be already unavailable, so replace it by something
629 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);