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"
33 // many versions of Unices have this function, but it is not defined in system
34 // headers - please add your system here if it is the case for your OS.
35 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
36 #if (defined(__SUN__) && !defined(__SunOs_5_6) && \
37 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
41 void usleep(unsigned long usec
);
43 #endif // Unices without usleep()
48 #include "wx/gtk/win_gtk.h"
50 #include <unistd.h> // usleep() on solaris
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 wxApp
*wxTheApp
= (wxApp
*) NULL
;
57 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
59 extern wxList wxPendingDelete
;
60 extern wxResourceCache
*wxTheResourceCache
;
62 unsigned char g_palette
[64*3] =
130 //-----------------------------------------------------------------------------
132 //-----------------------------------------------------------------------------
134 extern void wxFlushResources(void);
136 //-----------------------------------------------------------------------------
138 //-----------------------------------------------------------------------------
147 while (gtk_events_pending() > 0) gtk_main_iteration();
151 //-----------------------------------------------------------------------------
153 //-----------------------------------------------------------------------------
155 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
157 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
158 EVT_IDLE(wxApp::OnIdle
)
161 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
163 if (wxTheApp
) while (wxTheApp
->ProcessIdle()) {}
178 m_topWindow
= (wxWindow
*) NULL
;
179 m_exitOnFrameDelete
= TRUE
;
181 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, (gpointer
) NULL
);
183 m_colorCube
= (unsigned char*) NULL
;
188 gtk_idle_remove( m_idleTag
);
190 if (m_colorCube
) free(m_colorCube
);
193 bool wxApp::OnInitGui()
195 /* Nothing to do for 15, 16, 24, 32 bit displays */
197 GdkVisual
*visual
= gdk_visual_get_system();
198 if (visual
->depth
> 8) return TRUE
;
200 /* this initiates the standard palette as defined by GdkImlib
201 in the GNOME libraries. it ensures that all GNOME applications
202 use the same 64 colormap entries on 8-bit displays so you
203 can use several rather graphics-heavy applications at the
205 NOTE: this doesn't really seem to work this way... */
208 GdkColormap *cmap = gdk_colormap_new( gdk_visual_get_system(), TRUE );
210 for (int i = 0; i < 64; i++)
213 col.red = g_palette[i*3 + 0] << 8;
214 col.green = g_palette[i*3 + 1] << 8;
215 col.blue = g_palette[i*3 + 2] << 8;
218 gdk_color_alloc( cmap, &col );
221 gtk_widget_set_default_colormap( cmap );
224 /* initialize color cube for 8-bit color reduction dithering */
226 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
228 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
230 for (int r
= 0; r
< 32; r
++)
232 for (int g
= 0; g
< 32; g
++)
234 for (int b
= 0; b
< 32; b
++)
236 int rr
= (r
<< 3) | (r
>> 2);
237 int gg
= (g
<< 3) | (g
>> 2);
238 int bb
= (b
<< 3) | (b
>> 2);
240 GdkColor
*colors
= cmap
->colors
;
241 int max
= 3 * (65536);
244 for (int i
= 0; i
< cmap
->size
; i
++)
246 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
247 int gdiff
= ((gg
<< 8)- colors
[i
].green
);
248 int bdiff
= ((bb
<< 8)- colors
[i
].blue
);
249 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
250 if (sum
< max
) { index
= i
; max
= sum
; }
253 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
262 bool wxApp::ProcessIdle(void)
265 event
.SetEventObject( this );
266 ProcessEvent( event
);
268 return event
.MoreRequested();
271 void wxApp::OnIdle( wxIdleEvent
&event
)
273 static bool inOnIdle
= FALSE
;
275 /* Avoid recursion (via ProcessEvent default case) */
281 /* 'Garbage' collection of windows deleted with Close(). */
282 DeletePendingObjects();
284 /* flush the logged messages if any */
285 wxLog
*log
= wxLog::GetActiveTarget();
286 if (log
!= NULL
&& log
->HasPendingMessages())
289 /* Send OnIdle events to all windows */
290 bool needMore
= SendIdleEvents();
293 event
.RequestMore(TRUE
);
298 bool wxApp::SendIdleEvents(void)
300 bool needMore
= FALSE
;
302 wxNode
* node
= wxTopLevelWindows
.First();
305 wxWindow
* win
= (wxWindow
*) node
->Data();
306 if (SendIdleEvents(win
))
313 bool wxApp::SendIdleEvents( wxWindow
* win
)
315 bool needMore
= FALSE
;
318 event
.SetEventObject(win
);
320 win
->OnInternalIdle();
322 win
->ProcessEvent(event
);
324 if (event
.MoreRequested())
327 wxNode
* node
= win
->GetChildren().First();
330 wxWindow
* win
= (wxWindow
*) node
->Data();
331 if (SendIdleEvents(win
))
339 int wxApp::MainLoop(void)
345 void wxApp::ExitMainLoop(void)
350 bool wxApp::Initialized(void)
352 return m_initialized
;
355 bool wxApp::Pending(void)
360 void wxApp::Dispatch(void)
364 void wxApp::DeletePendingObjects(void)
366 wxNode
*node
= wxPendingDelete
.First();
369 wxObject
*obj
= (wxObject
*)node
->Data();
373 if (wxPendingDelete
.Member(obj
))
376 node
= wxPendingDelete
.First();
380 wxWindow
*wxApp::GetTopWindow(void)
382 if (m_topWindow
) return m_topWindow
;
383 wxNode
*node
= wxTopLevelWindows
.First();
384 if (!node
) return (wxWindow
*) NULL
;
385 return (wxWindow
*)node
->Data();
388 void wxApp::SetTopWindow( wxWindow
*win
)
393 bool wxApp::Initialize(void)
395 wxBuffer
= new char[BUFSIZ
+ 512];
397 wxClassInfo::InitializeClasses();
399 wxSystemSettings::Init();
402 wxTheFontNameDirectory = new wxFontNameDirectory;
403 wxTheFontNameDirectory->Initialize();
406 wxTheColourDatabase
= new wxColourDatabase( wxKEY_STRING
);
407 wxTheColourDatabase
->Initialize();
409 wxInitializeStockLists();
410 wxInitializeStockObjects();
412 #if wxUSE_WX_RESOURCES
413 wxTheResourceCache
= new wxResourceCache( wxKEY_STRING
);
415 wxInitializeResourceSystem();
418 wxImage::InitStandardHandlers();
420 /* no global cursor under X
421 g_globalCursor = new wxCursor; */
423 wxModule::RegisterModules();
424 if (!wxModule::InitializeModules()) return FALSE
;
429 void wxApp::CleanUp(void)
431 wxModule::CleanUpModules();
433 #if wxUSE_WX_RESOURCES
436 if (wxTheResourceCache
) delete wxTheResourceCache
;
437 wxTheResourceCache
= (wxResourceCache
*) NULL
;
439 wxCleanUpResourceSystem();
442 if (wxTheColourDatabase
) delete wxTheColourDatabase
;
443 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
446 if (wxTheFontNameDirectory) delete wxTheFontNameDirectory;
447 wxTheFontNameDirectory = (wxFontNameDirectory*) NULL;
450 wxDeleteStockObjects();
452 wxDeleteStockLists();
454 wxImage::CleanUpHandlers();
457 wxTheApp
= (wxApp
*) NULL
;
459 wxSystemSettings::Done();
463 wxClassInfo::CleanUpClasses();
465 /* check for memory leaks */
466 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
467 if (wxDebugContext::CountObjectsLeft() > 0)
469 wxLogDebug("There were memory leaks.\n");
470 wxDebugContext::Dump();
471 wxDebugContext::PrintStatistics();
475 /* do this as the very last thing because everything else can log messages */
476 wxLog::DontCreateOnDemand();
478 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
479 if (oldLog
) delete oldLog
;
482 wxLog
*wxApp::CreateLogTarget()
487 //-----------------------------------------------------------------------------
489 //-----------------------------------------------------------------------------
491 int wxEntry( int argc
, char *argv
[] )
495 gtk_init( &argc
, &argv
);
497 if (!wxApp::Initialize()) return 0;
501 if (!wxApp::GetInitializerFunction())
503 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
507 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
509 wxObject
*test_app
= app_ini();
511 wxTheApp
= (wxApp
*) test_app
;
516 printf( "wxWindows error: wxTheApp == NULL\n" );
520 wxTheApp
->argc
= argc
;
521 wxTheApp
->argv
= argv
;
524 strcpy( name
, argv
[0] );
525 strcpy( name
, wxFileNameFromPath(name
) );
526 wxStripExtension( name
);
527 wxTheApp
->SetAppName( name
);
529 if (!wxTheApp
->OnInitGui()) return 0;
531 /* Here frames insert themselves automatically
532 * into wxTopLevelWindows by getting created
535 if (!wxTheApp
->OnInit()) return 0;
537 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
541 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
543 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
546 /* Forcibly delete the window. */
547 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
548 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
550 topWindow
->Close( TRUE
);
551 wxTheApp
->DeletePendingObjects();
556 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
562 /* flush the logged messages if any */
563 wxLog
*log
= wxLog::GetActiveTarget();
564 if (log
!= NULL
&& log
->HasPendingMessages())