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
;
49 extern wxResourceCache
*wxTheResourceCache
;
51 unsigned char g_palette
[64*3] =
119 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
123 extern void wxFlushResources(void);
125 //-----------------------------------------------------------------------------
127 //-----------------------------------------------------------------------------
136 // it's necessary to call ProcessIdle() to update the frames sizes which
137 // might have been changed (it also will update other things set from
138 // OnUpdateUI() which is a nice (and desired) side effect)
139 for ( wxNode
*node
= wxTopLevelWindows
.GetFirst();
141 node
= node
->GetNext() )
143 wxWindow
*win
= ((wxWindow
*)node
->GetData());
144 win
->OnInternalIdle();
147 while (gtk_events_pending() > 0)
148 gtk_main_iteration();
153 //-----------------------------------------------------------------------------
155 //-----------------------------------------------------------------------------
157 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
159 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
160 EVT_IDLE(wxApp::OnIdle
)
163 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
167 while (wxTheApp
->ProcessIdle())
183 m_topWindow
= (wxWindow
*) NULL
;
184 m_exitOnFrameDelete
= TRUE
;
186 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, (gpointer
) NULL
);
188 m_colorCube
= (unsigned char*) NULL
;
193 gtk_idle_remove( m_idleTag
);
195 if (m_colorCube
) free(m_colorCube
);
198 bool wxApp::OnInitGui()
200 /* Nothing to do for 15, 16, 24, 32 bit displays */
202 GdkVisual
*visual
= gdk_visual_get_system();
203 if (visual
->depth
> 8) return TRUE
;
205 /* this initiates the standard palette as defined by GdkImlib
206 in the GNOME libraries. it ensures that all GNOME applications
207 use the same 64 colormap entries on 8-bit displays so you
208 can use several rather graphics-heavy applications at the
210 NOTE: this doesn't really seem to work this way... */
213 GdkColormap *cmap = gdk_colormap_new( gdk_visual_get_system(), TRUE );
215 for (int i = 0; i < 64; i++)
218 col.red = g_palette[i*3 + 0] << 8;
219 col.green = g_palette[i*3 + 1] << 8;
220 col.blue = g_palette[i*3 + 2] << 8;
223 gdk_color_alloc( cmap, &col );
226 gtk_widget_set_default_colormap( cmap );
229 /* initialize color cube for 8-bit color reduction dithering */
231 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
233 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
235 for (int r
= 0; r
< 32; r
++)
237 for (int g
= 0; g
< 32; g
++)
239 for (int b
= 0; b
< 32; b
++)
241 int rr
= (r
<< 3) | (r
>> 2);
242 int gg
= (g
<< 3) | (g
>> 2);
243 int bb
= (b
<< 3) | (b
>> 2);
245 GdkColor
*colors
= cmap
->colors
;
246 int max
= 3 * (65536);
249 for (int i
= 0; i
< cmap
->size
; i
++)
251 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
252 int gdiff
= ((gg
<< 8)- colors
[i
].green
);
253 int bdiff
= ((bb
<< 8)- colors
[i
].blue
);
254 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
255 if (sum
< max
) { index
= i
; max
= sum
; }
258 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
267 bool wxApp::ProcessIdle()
270 event
.SetEventObject( this );
271 ProcessEvent( event
);
273 return event
.MoreRequested();
276 void wxApp::OnIdle( wxIdleEvent
&event
)
278 static bool inOnIdle
= FALSE
;
280 /* Avoid recursion (via ProcessEvent default case) */
286 /* 'Garbage' collection of windows deleted with Close(). */
287 DeletePendingObjects();
289 /* flush the logged messages if any */
290 wxLog
*log
= wxLog::GetActiveTarget();
291 if (log
!= NULL
&& log
->HasPendingMessages())
294 /* Send OnIdle events to all windows */
295 bool needMore
= SendIdleEvents();
298 event
.RequestMore(TRUE
);
303 bool wxApp::SendIdleEvents()
305 bool needMore
= FALSE
;
307 wxNode
* node
= wxTopLevelWindows
.First();
310 wxWindow
* win
= (wxWindow
*) node
->Data();
311 if (SendIdleEvents(win
))
318 bool wxApp::SendIdleEvents( wxWindow
* win
)
320 bool needMore
= FALSE
;
323 event
.SetEventObject(win
);
325 win
->OnInternalIdle();
327 win
->ProcessEvent(event
);
329 if (event
.MoreRequested())
332 wxNode
* node
= win
->GetChildren().First();
335 wxWindow
* win
= (wxWindow
*) node
->Data();
336 if (SendIdleEvents(win
))
344 int wxApp::MainLoop()
350 void wxApp::ExitMainLoop()
355 bool wxApp::Initialized()
357 return m_initialized
;
360 bool wxApp::Pending()
365 void wxApp::Dispatch()
369 void wxApp::DeletePendingObjects()
371 wxNode
*node
= wxPendingDelete
.First();
374 wxObject
*obj
= (wxObject
*)node
->Data();
378 if (wxPendingDelete
.Member(obj
))
381 node
= wxPendingDelete
.First();
385 wxWindow
*wxApp::GetTopWindow()
387 if (m_topWindow
) return m_topWindow
;
388 wxNode
*node
= wxTopLevelWindows
.First();
389 if (!node
) return (wxWindow
*) NULL
;
390 return (wxWindow
*)node
->Data();
393 void wxApp::SetTopWindow( wxWindow
*win
)
398 bool wxApp::Initialize()
400 wxBuffer
= new char[BUFSIZ
+ 512];
402 wxClassInfo::InitializeClasses();
404 wxSystemSettings::Init();
407 wxTheFontNameDirectory = new wxFontNameDirectory;
408 wxTheFontNameDirectory->Initialize();
411 wxTheColourDatabase
= new wxColourDatabase( wxKEY_STRING
);
412 wxTheColourDatabase
->Initialize();
414 wxInitializeStockLists();
415 wxInitializeStockObjects();
417 #if wxUSE_WX_RESOURCES
418 wxTheResourceCache
= new wxResourceCache( wxKEY_STRING
);
420 wxInitializeResourceSystem();
423 wxImage::InitStandardHandlers();
425 /* no global cursor under X
426 g_globalCursor = new wxCursor; */
428 wxModule::RegisterModules();
429 if (!wxModule::InitializeModules()) return FALSE
;
434 void wxApp::CleanUp()
436 wxModule::CleanUpModules();
438 #if wxUSE_WX_RESOURCES
441 if (wxTheResourceCache
)
442 delete wxTheResourceCache
;
443 wxTheResourceCache
= (wxResourceCache
*) NULL
;
445 wxCleanUpResourceSystem();
448 if (wxTheColourDatabase
)
449 delete wxTheColourDatabase
;
450 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
453 if (wxTheFontNameDirectory) delete wxTheFontNameDirectory;
454 wxTheFontNameDirectory = (wxFontNameDirectory*) NULL;
457 wxDeleteStockObjects();
459 wxDeleteStockLists();
461 wxImage::CleanUpHandlers();
464 wxTheApp
= (wxApp
*) NULL
;
466 wxSystemSettings::Done();
470 wxClassInfo::CleanUpClasses();
472 // check for memory leaks
473 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
474 if (wxDebugContext::CountObjectsLeft() > 0)
476 wxLogDebug("There were memory leaks.\n");
477 wxDebugContext::Dump();
478 wxDebugContext::PrintStatistics();
482 // do this as the very last thing because everything else can log messages
483 wxLog::DontCreateOnDemand();
485 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
490 wxLog
*wxApp::CreateLogTarget()
495 //-----------------------------------------------------------------------------
497 //-----------------------------------------------------------------------------
499 int wxEntry( int argc
, char *argv
[] )
503 gtk_init( &argc
, &argv
);
505 if (!wxApp::Initialize())
510 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
511 "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
513 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
515 wxObject
*test_app
= app_ini();
517 wxTheApp
= (wxApp
*) test_app
;
520 wxCHECK_MSG( wxTheApp
, -1, "wxWindows error: no application object" );
522 wxTheApp
->argc
= argc
;
523 wxTheApp
->argv
= argv
;
526 strcpy( name
, argv
[0] );
527 strcpy( name
, wxFileNameFromPath(name
) );
528 wxStripExtension( name
);
529 wxTheApp
->SetAppName( name
);
531 if (!wxTheApp
->OnInitGui())
534 /* Here frames insert themselves automatically
535 * into wxTopLevelWindows by getting created
538 if (!wxTheApp
->OnInit())
541 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
545 if (wxTheApp
->Initialized())
546 retValue
= wxTheApp
->OnRun();
548 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
551 // Forcibly delete the window.
552 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
553 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
555 topWindow
->Close( TRUE
);
556 wxTheApp
->DeletePendingObjects();
561 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
567 // flush the logged messages if any
568 wxLog
*log
= wxLog::GetActiveTarget();
569 if (log
!= NULL
&& log
->HasPendingMessages())
572 // continuing to use user defined log target is unsafe from now on because
573 // some resources may be already unavailable, so replace it by something
575 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);