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"
23 #if wxUSE_WX_RESOURCES
24 #include "wx/resource.h"
26 #include "wx/module.h"
29 #include "wx/thread.h"
36 #include "wx/gtk/win_gtk.h"
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 wxApp
*wxTheApp
= (wxApp
*) NULL
;
43 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
45 extern wxList wxPendingDelete
;
46 extern wxResourceCache
*wxTheResourceCache
;
48 unsigned char g_palette
[64*3] =
116 //-----------------------------------------------------------------------------
118 //-----------------------------------------------------------------------------
120 extern void wxFlushResources(void);
122 //-----------------------------------------------------------------------------
124 //-----------------------------------------------------------------------------
133 while (gtk_events_pending() > 0) gtk_main_iteration();
137 //-----------------------------------------------------------------------------
139 //-----------------------------------------------------------------------------
141 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
143 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
144 EVT_IDLE(wxApp::OnIdle
)
147 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
149 if (wxTheApp
) while (wxTheApp
->ProcessIdle()) {}
164 m_topWindow
= (wxWindow
*) NULL
;
165 m_exitOnFrameDelete
= TRUE
;
167 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, (gpointer
) NULL
);
169 m_colorCube
= (unsigned char*) NULL
;
174 gtk_idle_remove( m_idleTag
);
176 if (m_colorCube
) free(m_colorCube
);
179 bool wxApp::OnInitGui()
181 /* Nothing to do for 15, 16, 24, 32 bit displays */
183 GdkVisual
*visual
= gdk_visual_get_system();
184 if (visual
->depth
> 8) return TRUE
;
186 /* this initiates the standard palette as defined by GdkImlib
187 in the GNOME libraries. it ensures that all GNOME applications
188 use the same 64 colormap entries on 8-bit displays so you
189 can use several rather graphics-heavy applications at the
191 NOTE: this doesn't really seem to work this way... */
194 GdkColormap *cmap = gdk_colormap_new( gdk_visual_get_system(), TRUE );
196 for (int i = 0; i < 64; i++)
199 col.red = g_palette[i*3 + 0] << 8;
200 col.green = g_palette[i*3 + 1] << 8;
201 col.blue = g_palette[i*3 + 2] << 8;
204 gdk_color_alloc( cmap, &col );
207 gtk_widget_set_default_colormap( cmap );
210 /* initialize color cube for 8-bit color reduction dithering */
212 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
214 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
216 for (int r
= 0; r
< 32; r
++)
218 for (int g
= 0; g
< 32; g
++)
220 for (int b
= 0; b
< 32; b
++)
222 int rr
= (r
<< 3) | (r
>> 2);
223 int gg
= (g
<< 3) | (g
>> 2);
224 int bb
= (b
<< 3) | (b
>> 2);
226 GdkColor
*colors
= cmap
->colors
;
227 int max
= 3 * (65536);
230 for (int i
= 0; i
< cmap
->size
; i
++)
232 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
233 int gdiff
= ((gg
<< 8)- colors
[i
].green
);
234 int bdiff
= ((bb
<< 8)- colors
[i
].blue
);
235 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
236 if (sum
< max
) { index
= i
; max
= sum
; }
239 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
248 bool wxApp::ProcessIdle(void)
251 event
.SetEventObject( this );
252 ProcessEvent( event
);
254 return event
.MoreRequested();
257 void wxApp::OnIdle( wxIdleEvent
&event
)
259 static bool inOnIdle
= FALSE
;
261 /* Avoid recursion (via ProcessEvent default case) */
267 /* 'Garbage' collection of windows deleted with Close(). */
268 DeletePendingObjects();
270 /* flush the logged messages if any */
271 wxLog
*log
= wxLog::GetActiveTarget();
272 if (log
!= NULL
&& log
->HasPendingMessages())
275 /* Send OnIdle events to all windows */
276 bool needMore
= SendIdleEvents();
279 event
.RequestMore(TRUE
);
284 bool wxApp::SendIdleEvents(void)
286 bool needMore
= FALSE
;
288 wxNode
* node
= wxTopLevelWindows
.First();
291 wxWindow
* win
= (wxWindow
*) node
->Data();
292 if (SendIdleEvents(win
))
299 bool wxApp::SendIdleEvents( wxWindow
* win
)
301 bool needMore
= FALSE
;
304 event
.SetEventObject(win
);
306 win
->OnInternalIdle();
308 win
->ProcessEvent(event
);
310 if (event
.MoreRequested())
313 wxNode
* node
= win
->GetChildren().First();
316 wxWindow
* win
= (wxWindow
*) node
->Data();
317 if (SendIdleEvents(win
))
325 int wxApp::MainLoop(void)
331 void wxApp::ExitMainLoop(void)
336 bool wxApp::Initialized(void)
338 return m_initialized
;
341 bool wxApp::Pending(void)
346 void wxApp::Dispatch(void)
350 void wxApp::DeletePendingObjects(void)
352 wxNode
*node
= wxPendingDelete
.First();
355 wxObject
*obj
= (wxObject
*)node
->Data();
359 if (wxPendingDelete
.Member(obj
))
362 node
= wxPendingDelete
.First();
366 wxWindow
*wxApp::GetTopWindow(void)
368 if (m_topWindow
) return m_topWindow
;
369 wxNode
*node
= wxTopLevelWindows
.First();
370 if (!node
) return (wxWindow
*) NULL
;
371 return (wxWindow
*)node
->Data();
374 void wxApp::SetTopWindow( wxWindow
*win
)
379 bool wxApp::Initialize(void)
381 wxBuffer
= new char[BUFSIZ
+ 512];
383 wxClassInfo::InitializeClasses();
385 wxSystemSettings::Init();
387 wxTheFontNameDirectory
= new wxFontNameDirectory
;
388 wxTheFontNameDirectory
->Initialize();
390 wxTheColourDatabase
= new wxColourDatabase( wxKEY_STRING
);
391 wxTheColourDatabase
->Initialize();
393 wxInitializeStockLists();
394 wxInitializeStockObjects();
396 #if wxUSE_WX_RESOURCES
397 wxTheResourceCache
= new wxResourceCache( wxKEY_STRING
);
399 wxInitializeResourceSystem();
402 wxImage::InitStandardHandlers();
404 /* no global cursor under X
405 g_globalCursor = new wxCursor; */
407 wxModule::RegisterModules();
408 if (!wxModule::InitializeModules()) return FALSE
;
413 void wxApp::CleanUp(void)
415 wxModule::CleanUpModules();
417 #if wxUSE_WX_RESOURCES
420 if (wxTheResourceCache
) delete wxTheResourceCache
;
421 wxTheResourceCache
= (wxResourceCache
*) NULL
;
423 wxCleanUpResourceSystem();
426 if (wxTheColourDatabase
) delete wxTheColourDatabase
;
427 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
429 if (wxTheFontNameDirectory
) delete wxTheFontNameDirectory
;
430 wxTheFontNameDirectory
= (wxFontNameDirectory
*) NULL
;
432 wxDeleteStockObjects();
434 wxDeleteStockLists();
436 wxImage::CleanUpHandlers();
439 wxTheApp
= (wxApp
*) NULL
;
441 /* check for memory leaks */
442 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
443 if (wxDebugContext::CountObjectsLeft() > 0)
445 wxLogDebug("There were memory leaks.\n");
446 wxDebugContext::Dump();
447 wxDebugContext::PrintStatistics();
451 /* do this as the very last thing because everything else can log messages */
452 wxLog::DontCreateOnDemand();
454 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
455 if (oldLog
) delete oldLog
;
457 wxSystemSettings::Done();
459 wxClassInfo::CleanUpClasses();
464 wxLog
*wxApp::CreateLogTarget()
469 //-----------------------------------------------------------------------------
471 //-----------------------------------------------------------------------------
473 int wxEntry( int argc
, char *argv
[] )
477 gtk_init( &argc
, &argv
);
479 if (!wxApp::Initialize()) return 0;
483 if (!wxApp::GetInitializerFunction())
485 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
489 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
491 wxObject
*test_app
= app_ini();
493 wxTheApp
= (wxApp
*) test_app
;
498 printf( "wxWindows error: wxTheApp == NULL\n" );
502 wxTheApp
->argc
= argc
;
503 wxTheApp
->argv
= argv
;
506 strcpy( name
, argv
[0] );
507 strcpy( name
, wxFileNameFromPath(name
) );
508 wxStripExtension( name
);
509 wxTheApp
->SetAppName( name
);
511 if (!wxTheApp
->OnInitGui()) return 0;
513 /* Here frames insert themselves automatically
514 * into wxTopLevelWindows by getting created
517 if (!wxTheApp
->OnInit()) return 0;
519 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
523 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
525 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
528 /* Forcibly delete the window. */
529 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
530 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
532 topWindow
->Close( TRUE
);
533 wxTheApp
->DeletePendingObjects();
538 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
544 /* flush the logged messages if any */
545 wxLog
*log
= wxLog::GetActiveTarget();
546 if (log
!= NULL
&& log
->HasPendingMessages())