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"
34 int usleep(unsigned int useconds
);
40 #include "wx/gtk/win_gtk.h"
42 #include <unistd.h> // usleep() on solaris
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 wxApp
*wxTheApp
= (wxApp
*) NULL
;
49 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
51 extern wxList wxPendingDelete
;
52 extern wxResourceCache
*wxTheResourceCache
;
54 unsigned char g_palette
[64*3] =
122 //-----------------------------------------------------------------------------
124 //-----------------------------------------------------------------------------
126 extern void wxFlushResources(void);
128 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
139 while (gtk_events_pending() > 0) gtk_main_iteration();
143 //-----------------------------------------------------------------------------
145 //-----------------------------------------------------------------------------
147 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
149 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
150 EVT_IDLE(wxApp::OnIdle
)
153 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
155 if (wxTheApp
) while (wxTheApp
->ProcessIdle()) {}
170 m_topWindow
= (wxWindow
*) NULL
;
171 m_exitOnFrameDelete
= TRUE
;
173 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, (gpointer
) NULL
);
175 m_colorCube
= (unsigned char*) NULL
;
180 gtk_idle_remove( m_idleTag
);
182 if (m_colorCube
) free(m_colorCube
);
185 bool wxApp::OnInitGui()
187 /* Nothing to do for 15, 16, 24, 32 bit displays */
189 GdkVisual
*visual
= gdk_visual_get_system();
190 if (visual
->depth
> 8) return TRUE
;
192 /* this initiates the standard palette as defined by GdkImlib
193 in the GNOME libraries. it ensures that all GNOME applications
194 use the same 64 colormap entries on 8-bit displays so you
195 can use several rather graphics-heavy applications at the
197 NOTE: this doesn't really seem to work this way... */
200 GdkColormap *cmap = gdk_colormap_new( gdk_visual_get_system(), TRUE );
202 for (int i = 0; i < 64; i++)
205 col.red = g_palette[i*3 + 0] << 8;
206 col.green = g_palette[i*3 + 1] << 8;
207 col.blue = g_palette[i*3 + 2] << 8;
210 gdk_color_alloc( cmap, &col );
213 gtk_widget_set_default_colormap( cmap );
216 /* initialize color cube for 8-bit color reduction dithering */
218 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
220 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
222 for (int r
= 0; r
< 32; r
++)
224 for (int g
= 0; g
< 32; g
++)
226 for (int b
= 0; b
< 32; b
++)
228 int rr
= (r
<< 3) | (r
>> 2);
229 int gg
= (g
<< 3) | (g
>> 2);
230 int bb
= (b
<< 3) | (b
>> 2);
232 GdkColor
*colors
= cmap
->colors
;
233 int max
= 3 * (65536);
236 for (int i
= 0; i
< cmap
->size
; i
++)
238 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
239 int gdiff
= ((gg
<< 8)- colors
[i
].green
);
240 int bdiff
= ((bb
<< 8)- colors
[i
].blue
);
241 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
242 if (sum
< max
) { index
= i
; max
= sum
; }
245 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
254 bool wxApp::ProcessIdle(void)
257 event
.SetEventObject( this );
258 ProcessEvent( event
);
260 return event
.MoreRequested();
263 void wxApp::OnIdle( wxIdleEvent
&event
)
265 static bool inOnIdle
= FALSE
;
267 /* Avoid recursion (via ProcessEvent default case) */
273 /* 'Garbage' collection of windows deleted with Close(). */
274 DeletePendingObjects();
276 /* flush the logged messages if any */
277 wxLog
*log
= wxLog::GetActiveTarget();
278 if (log
!= NULL
&& log
->HasPendingMessages())
281 /* Send OnIdle events to all windows */
282 bool needMore
= SendIdleEvents();
285 event
.RequestMore(TRUE
);
290 bool wxApp::SendIdleEvents(void)
292 bool needMore
= FALSE
;
294 wxNode
* node
= wxTopLevelWindows
.First();
297 wxWindow
* win
= (wxWindow
*) node
->Data();
298 if (SendIdleEvents(win
))
305 bool wxApp::SendIdleEvents( wxWindow
* win
)
307 bool needMore
= FALSE
;
310 event
.SetEventObject(win
);
312 win
->OnInternalIdle();
314 win
->ProcessEvent(event
);
316 if (event
.MoreRequested())
319 wxNode
* node
= win
->GetChildren().First();
322 wxWindow
* win
= (wxWindow
*) node
->Data();
323 if (SendIdleEvents(win
))
331 int wxApp::MainLoop(void)
337 void wxApp::ExitMainLoop(void)
342 bool wxApp::Initialized(void)
344 return m_initialized
;
347 bool wxApp::Pending(void)
352 void wxApp::Dispatch(void)
356 void wxApp::DeletePendingObjects(void)
358 wxNode
*node
= wxPendingDelete
.First();
361 wxObject
*obj
= (wxObject
*)node
->Data();
365 if (wxPendingDelete
.Member(obj
))
368 node
= wxPendingDelete
.First();
372 wxWindow
*wxApp::GetTopWindow(void)
374 if (m_topWindow
) return m_topWindow
;
375 wxNode
*node
= wxTopLevelWindows
.First();
376 if (!node
) return (wxWindow
*) NULL
;
377 return (wxWindow
*)node
->Data();
380 void wxApp::SetTopWindow( wxWindow
*win
)
385 bool wxApp::Initialize(void)
387 wxBuffer
= new char[BUFSIZ
+ 512];
389 wxClassInfo::InitializeClasses();
391 wxSystemSettings::Init();
394 wxTheFontNameDirectory = new wxFontNameDirectory;
395 wxTheFontNameDirectory->Initialize();
398 wxTheColourDatabase
= new wxColourDatabase( wxKEY_STRING
);
399 wxTheColourDatabase
->Initialize();
401 wxInitializeStockLists();
402 wxInitializeStockObjects();
404 #if wxUSE_WX_RESOURCES
405 wxTheResourceCache
= new wxResourceCache( wxKEY_STRING
);
407 wxInitializeResourceSystem();
410 wxImage::InitStandardHandlers();
412 /* no global cursor under X
413 g_globalCursor = new wxCursor; */
415 wxModule::RegisterModules();
416 if (!wxModule::InitializeModules()) return FALSE
;
421 void wxApp::CleanUp(void)
423 wxModule::CleanUpModules();
425 #if wxUSE_WX_RESOURCES
428 if (wxTheResourceCache
) delete wxTheResourceCache
;
429 wxTheResourceCache
= (wxResourceCache
*) NULL
;
431 wxCleanUpResourceSystem();
434 if (wxTheColourDatabase
) delete wxTheColourDatabase
;
435 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
438 if (wxTheFontNameDirectory) delete wxTheFontNameDirectory;
439 wxTheFontNameDirectory = (wxFontNameDirectory*) NULL;
442 wxDeleteStockObjects();
444 wxDeleteStockLists();
446 wxImage::CleanUpHandlers();
449 wxTheApp
= (wxApp
*) NULL
;
451 /* check for memory leaks */
452 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
453 if (wxDebugContext::CountObjectsLeft() > 0)
455 wxLogDebug("There were memory leaks.\n");
456 wxDebugContext::Dump();
457 wxDebugContext::PrintStatistics();
461 /* do this as the very last thing because everything else can log messages */
462 wxLog::DontCreateOnDemand();
464 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
465 if (oldLog
) delete oldLog
;
467 wxSystemSettings::Done();
469 wxClassInfo::CleanUpClasses();
474 wxLog
*wxApp::CreateLogTarget()
479 //-----------------------------------------------------------------------------
481 //-----------------------------------------------------------------------------
483 int wxEntry( int argc
, char *argv
[] )
487 gtk_init( &argc
, &argv
);
489 if (!wxApp::Initialize()) return 0;
493 if (!wxApp::GetInitializerFunction())
495 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
499 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
501 wxObject
*test_app
= app_ini();
503 wxTheApp
= (wxApp
*) test_app
;
508 printf( "wxWindows error: wxTheApp == NULL\n" );
512 wxTheApp
->argc
= argc
;
513 wxTheApp
->argv
= argv
;
516 strcpy( name
, argv
[0] );
517 strcpy( name
, wxFileNameFromPath(name
) );
518 wxStripExtension( name
);
519 wxTheApp
->SetAppName( name
);
521 if (!wxTheApp
->OnInitGui()) return 0;
523 /* Here frames insert themselves automatically
524 * into wxTopLevelWindows by getting created
527 if (!wxTheApp
->OnInit()) return 0;
529 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
533 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
535 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
538 /* Forcibly delete the window. */
539 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
540 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
542 topWindow
->Close( TRUE
);
543 wxTheApp
->DeletePendingObjects();
548 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
554 /* flush the logged messages if any */
555 wxLog
*log
= wxLog::GetActiveTarget();
556 if (log
!= NULL
&& log
->HasPendingMessages())