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 //-----------------------------------------------------------------------------
140 // it's necessary to call ProcessIdle() to update the frames sizes which
141 // might have been changed (it also will update other things set from
142 // OnUpdateUI() which is a nice (and desired) side effect)
143 for ( wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst();
145 node
= node
->GetNext() )
147 wxWindow
*win
= node
->GetData();
148 win
->OnInternalIdle();
151 while (gtk_events_pending() > 0)
152 gtk_main_iteration();
157 //-----------------------------------------------------------------------------
159 //-----------------------------------------------------------------------------
161 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
163 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
164 EVT_IDLE(wxApp::OnIdle
)
167 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
171 while (wxTheApp
->ProcessIdle())
187 m_topWindow
= (wxWindow
*) NULL
;
188 m_exitOnFrameDelete
= TRUE
;
190 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, (gpointer
) NULL
);
192 m_colorCube
= (unsigned char*) NULL
;
197 gtk_idle_remove( m_idleTag
);
199 if (m_colorCube
) free(m_colorCube
);
202 bool wxApp::OnInitGui()
204 /* Nothing to do for 15, 16, 24, 32 bit displays */
206 GdkVisual
*visual
= gdk_visual_get_system();
207 if (visual
->depth
> 8) return TRUE
;
209 /* this initiates the standard palette as defined by GdkImlib
210 in the GNOME libraries. it ensures that all GNOME applications
211 use the same 64 colormap entries on 8-bit displays so you
212 can use several rather graphics-heavy applications at the
214 NOTE: this doesn't really seem to work this way... */
217 GdkColormap *cmap = gdk_colormap_new( gdk_visual_get_system(), TRUE );
219 for (int i = 0; i < 64; i++)
222 col.red = g_palette[i*3 + 0] << 8;
223 col.green = g_palette[i*3 + 1] << 8;
224 col.blue = g_palette[i*3 + 2] << 8;
227 gdk_color_alloc( cmap, &col );
230 gtk_widget_set_default_colormap( cmap );
233 /* initialize color cube for 8-bit color reduction dithering */
235 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
237 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
239 for (int r
= 0; r
< 32; r
++)
241 for (int g
= 0; g
< 32; g
++)
243 for (int b
= 0; b
< 32; b
++)
245 int rr
= (r
<< 3) | (r
>> 2);
246 int gg
= (g
<< 3) | (g
>> 2);
247 int bb
= (b
<< 3) | (b
>> 2);
249 GdkColor
*colors
= cmap
->colors
;
250 int max
= 3 * (65536);
253 for (int i
= 0; i
< cmap
->size
; i
++)
255 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
256 int gdiff
= ((gg
<< 8)- colors
[i
].green
);
257 int bdiff
= ((bb
<< 8)- colors
[i
].blue
);
258 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
259 if (sum
< max
) { index
= i
; max
= sum
; }
262 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
271 bool wxApp::ProcessIdle()
274 event
.SetEventObject( this );
275 ProcessEvent( event
);
277 return event
.MoreRequested();
280 void wxApp::OnIdle( wxIdleEvent
&event
)
282 static bool inOnIdle
= FALSE
;
284 /* Avoid recursion (via ProcessEvent default case) */
291 /* Resend in the main thread events which have been prepared in other
293 ProcessPendingEvents();
296 /* 'Garbage' collection of windows deleted with Close(). */
297 DeletePendingObjects();
299 /* flush the logged messages if any */
300 wxLog
*log
= wxLog::GetActiveTarget();
301 if (log
!= NULL
&& log
->HasPendingMessages())
304 /* Send OnIdle events to all windows */
305 bool needMore
= SendIdleEvents();
308 event
.RequestMore(TRUE
);
313 bool wxApp::SendIdleEvents()
315 bool needMore
= FALSE
;
317 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
320 wxWindow
* win
= node
->GetData();
321 if (SendIdleEvents(win
))
323 node
= node
->GetNext();
329 bool wxApp::SendIdleEvents( wxWindow
* win
)
331 bool needMore
= FALSE
;
334 event
.SetEventObject(win
);
336 win
->OnInternalIdle();
338 win
->ProcessEvent(event
);
340 if (event
.MoreRequested())
343 wxNode
* node
= win
->GetChildren().First();
346 wxWindow
* win
= (wxWindow
*) node
->Data();
347 if (SendIdleEvents(win
))
355 int wxApp::MainLoop()
361 void wxApp::ExitMainLoop()
366 bool wxApp::Initialized()
368 return m_initialized
;
371 bool wxApp::Pending()
376 void wxApp::Dispatch()
381 void wxApp::ProcessPendingEvents()
383 wxNode
*node
= wxPendingEvents
->First();
384 wxCriticalSectionLocker
locker(*wxPendingEventsLocker
);
388 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
390 handler
->ProcessPendingEvents();
394 node
= wxPendingEvents
->First();
399 void wxApp::DeletePendingObjects()
401 wxNode
*node
= wxPendingDelete
.First();
404 wxObject
*obj
= (wxObject
*)node
->Data();
408 if (wxPendingDelete
.Member(obj
))
411 node
= wxPendingDelete
.First();
415 wxWindow
*wxApp::GetTopWindow()
419 else if (wxTopLevelWindows
.GetCount() > 0)
420 return wxTopLevelWindows
.GetFirst()->GetData();
425 void wxApp::SetTopWindow( wxWindow
*win
)
430 bool wxApp::Initialize()
432 wxBuffer
= new char[BUFSIZ
+ 512];
434 wxClassInfo::InitializeClasses();
436 wxSystemSettings::Init();
438 // GL: I'm annoyed ... I don't know where to put this and I don't want to
439 // create a module for that as it's part of the core.
441 wxPendingEvents
= new wxList();
442 wxPendingEventsLocker
= new wxCriticalSection();
446 wxTheFontNameDirectory = new wxFontNameDirectory;
447 wxTheFontNameDirectory->Initialize();
450 wxTheColourDatabase
= new wxColourDatabase( wxKEY_STRING
);
451 wxTheColourDatabase
->Initialize();
453 wxInitializeStockLists();
454 wxInitializeStockObjects();
456 #if wxUSE_WX_RESOURCES
457 wxTheResourceCache
= new wxResourceCache( wxKEY_STRING
);
459 wxInitializeResourceSystem();
462 wxImage::InitStandardHandlers();
464 /* no global cursor under X
465 g_globalCursor = new wxCursor; */
467 wxModule::RegisterModules();
468 if (!wxModule::InitializeModules()) return FALSE
;
473 void wxApp::CleanUp()
475 wxModule::CleanUpModules();
477 #if wxUSE_WX_RESOURCES
480 if (wxTheResourceCache
)
481 delete wxTheResourceCache
;
482 wxTheResourceCache
= (wxResourceCache
*) NULL
;
484 wxCleanUpResourceSystem();
487 if (wxTheColourDatabase
)
488 delete wxTheColourDatabase
;
489 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
492 if (wxTheFontNameDirectory) delete wxTheFontNameDirectory;
493 wxTheFontNameDirectory = (wxFontNameDirectory*) NULL;
496 wxDeleteStockObjects();
498 wxDeleteStockLists();
500 wxImage::CleanUpHandlers();
503 wxTheApp
= (wxApp
*) NULL
;
505 // GL: I'm annoyed ... I don't know where to put this and I don't want to
506 // create a module for that as it's part of the core.
508 delete wxPendingEvents
;
509 delete wxPendingEventsLocker
;
512 wxSystemSettings::Done();
516 wxClassInfo::CleanUpClasses();
518 // check for memory leaks
519 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
520 if (wxDebugContext::CountObjectsLeft() > 0)
522 wxLogDebug("There were memory leaks.\n");
523 wxDebugContext::Dump();
524 wxDebugContext::PrintStatistics();
528 // do this as the very last thing because everything else can log messages
529 wxLog::DontCreateOnDemand();
531 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
536 wxLog
*wxApp::CreateLogTarget()
541 //-----------------------------------------------------------------------------
543 //-----------------------------------------------------------------------------
545 int wxEntry( int argc
, char *argv
[] )
549 gtk_init( &argc
, &argv
);
551 if (!wxApp::Initialize())
556 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
557 "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
559 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
561 wxObject
*test_app
= app_ini();
563 wxTheApp
= (wxApp
*) test_app
;
566 wxCHECK_MSG( wxTheApp
, -1, "wxWindows error: no application object" );
568 wxTheApp
->argc
= argc
;
569 wxTheApp
->argv
= argv
;
572 strcpy( name
, argv
[0] );
573 strcpy( name
, wxFileNameFromPath(name
) );
574 wxStripExtension( name
);
575 wxTheApp
->SetAppName( name
);
577 if (!wxTheApp
->OnInitGui())
580 /* Here frames insert themselves automatically
581 * into wxTopLevelWindows by getting created
584 if (!wxTheApp
->OnInit())
587 wxTheApp
->m_initialized
= wxTopLevelWindows
.GetCount() != 0;
591 if (wxTheApp
->Initialized())
592 retValue
= wxTheApp
->OnRun();
594 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
597 // Forcibly delete the window.
598 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
599 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
601 topWindow
->Close( TRUE
);
602 wxTheApp
->DeletePendingObjects();
607 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
613 // flush the logged messages if any
614 wxLog
*log
= wxLog::GetActiveTarget();
615 if (log
!= NULL
&& log
->HasPendingMessages())
618 // continuing to use user defined log target is unsafe from now on because
619 // some resources may be already unavailable, so replace it by something
621 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);