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) */
290 /* Resend in the main thread events which have been prepared in other
292 ProcessPendingEvents();
294 /* 'Garbage' collection of windows deleted with Close(). */
295 DeletePendingObjects();
297 /* flush the logged messages if any */
298 wxLog
*log
= wxLog::GetActiveTarget();
299 if (log
!= NULL
&& log
->HasPendingMessages())
302 /* Send OnIdle events to all windows */
303 bool needMore
= SendIdleEvents();
306 event
.RequestMore(TRUE
);
311 bool wxApp::SendIdleEvents()
313 bool needMore
= FALSE
;
315 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
318 wxWindow
* win
= node
->GetData();
319 if (SendIdleEvents(win
))
321 node
= node
->GetNext();
327 bool wxApp::SendIdleEvents( wxWindow
* win
)
329 bool needMore
= FALSE
;
332 event
.SetEventObject(win
);
334 win
->OnInternalIdle();
336 win
->ProcessEvent(event
);
338 if (event
.MoreRequested())
341 wxNode
* node
= win
->GetChildren().First();
344 wxWindow
* win
= (wxWindow
*) node
->Data();
345 if (SendIdleEvents(win
))
353 int wxApp::MainLoop()
359 void wxApp::ExitMainLoop()
364 bool wxApp::Initialized()
366 return m_initialized
;
369 bool wxApp::Pending()
374 void wxApp::Dispatch()
379 void wxApp::ProcessPendingEvents()
381 wxNode
*node
= wxPendingEvents
.First();
382 wxCriticalSectionLocker
locker(wxPendingEventsLocker
);
386 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
388 handler
->ProcessPendingEvents();
392 node
= wxPendingEvents
.First();
397 void wxApp::DeletePendingObjects()
399 wxNode
*node
= wxPendingDelete
.First();
402 wxObject
*obj
= (wxObject
*)node
->Data();
406 if (wxPendingDelete
.Member(obj
))
409 node
= wxPendingDelete
.First();
413 wxWindow
*wxApp::GetTopWindow()
417 else if (wxTopLevelWindows
.GetCount() > 0)
418 return wxTopLevelWindows
.GetFirst()->GetData();
423 void wxApp::SetTopWindow( wxWindow
*win
)
428 bool wxApp::Initialize()
430 wxBuffer
= new char[BUFSIZ
+ 512];
432 wxClassInfo::InitializeClasses();
434 wxSystemSettings::Init();
437 wxTheFontNameDirectory = new wxFontNameDirectory;
438 wxTheFontNameDirectory->Initialize();
441 wxTheColourDatabase
= new wxColourDatabase( wxKEY_STRING
);
442 wxTheColourDatabase
->Initialize();
444 wxInitializeStockLists();
445 wxInitializeStockObjects();
447 #if wxUSE_WX_RESOURCES
448 wxTheResourceCache
= new wxResourceCache( wxKEY_STRING
);
450 wxInitializeResourceSystem();
453 wxImage::InitStandardHandlers();
455 /* no global cursor under X
456 g_globalCursor = new wxCursor; */
458 wxModule::RegisterModules();
459 if (!wxModule::InitializeModules()) return FALSE
;
464 void wxApp::CleanUp()
466 wxModule::CleanUpModules();
468 #if wxUSE_WX_RESOURCES
471 if (wxTheResourceCache
)
472 delete wxTheResourceCache
;
473 wxTheResourceCache
= (wxResourceCache
*) NULL
;
475 wxCleanUpResourceSystem();
478 if (wxTheColourDatabase
)
479 delete wxTheColourDatabase
;
480 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
483 if (wxTheFontNameDirectory) delete wxTheFontNameDirectory;
484 wxTheFontNameDirectory = (wxFontNameDirectory*) NULL;
487 wxDeleteStockObjects();
489 wxDeleteStockLists();
491 wxImage::CleanUpHandlers();
494 wxTheApp
= (wxApp
*) NULL
;
496 wxSystemSettings::Done();
500 wxClassInfo::CleanUpClasses();
502 // check for memory leaks
503 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
504 if (wxDebugContext::CountObjectsLeft() > 0)
506 wxLogDebug("There were memory leaks.\n");
507 wxDebugContext::Dump();
508 wxDebugContext::PrintStatistics();
512 // do this as the very last thing because everything else can log messages
513 wxLog::DontCreateOnDemand();
515 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
520 wxLog
*wxApp::CreateLogTarget()
525 //-----------------------------------------------------------------------------
527 //-----------------------------------------------------------------------------
529 int wxEntry( int argc
, char *argv
[] )
533 gtk_init( &argc
, &argv
);
535 if (!wxApp::Initialize())
540 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
541 "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
543 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
545 wxObject
*test_app
= app_ini();
547 wxTheApp
= (wxApp
*) test_app
;
550 wxCHECK_MSG( wxTheApp
, -1, "wxWindows error: no application object" );
552 wxTheApp
->argc
= argc
;
553 wxTheApp
->argv
= argv
;
556 strcpy( name
, argv
[0] );
557 strcpy( name
, wxFileNameFromPath(name
) );
558 wxStripExtension( name
);
559 wxTheApp
->SetAppName( name
);
561 if (!wxTheApp
->OnInitGui())
564 /* Here frames insert themselves automatically
565 * into wxTopLevelWindows by getting created
568 if (!wxTheApp
->OnInit())
571 wxTheApp
->m_initialized
= wxTopLevelWindows
.GetCount() != 0;
575 if (wxTheApp
->Initialized())
576 retValue
= wxTheApp
->OnRun();
578 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
581 // Forcibly delete the window.
582 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
583 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
585 topWindow
->Close( TRUE
);
586 wxTheApp
->DeletePendingObjects();
591 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
597 // flush the logged messages if any
598 wxLog
*log
= wxLog::GetActiveTarget();
599 if (log
!= NULL
&& log
->HasPendingMessages())
602 // continuing to use user defined log target is unsafe from now on because
603 // some resources may be already unavailable, so replace it by something
605 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);