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 void usleep(unsigned long usec
);
43 #include "wx/gtk/win_gtk.h"
45 #include <unistd.h> // usleep() on solaris
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 wxApp
*wxTheApp
= (wxApp
*) NULL
;
52 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
54 extern wxList wxPendingDelete
;
55 extern wxResourceCache
*wxTheResourceCache
;
57 unsigned char g_palette
[64*3] =
125 //-----------------------------------------------------------------------------
127 //-----------------------------------------------------------------------------
129 extern void wxFlushResources(void);
131 //-----------------------------------------------------------------------------
133 //-----------------------------------------------------------------------------
142 while (gtk_events_pending() > 0) gtk_main_iteration();
146 //-----------------------------------------------------------------------------
148 //-----------------------------------------------------------------------------
150 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
152 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
153 EVT_IDLE(wxApp::OnIdle
)
156 gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
158 if (wxTheApp
) while (wxTheApp
->ProcessIdle()) {}
173 m_topWindow
= (wxWindow
*) NULL
;
174 m_exitOnFrameDelete
= TRUE
;
176 m_idleTag
= gtk_idle_add( wxapp_idle_callback
, (gpointer
) NULL
);
178 m_colorCube
= (unsigned char*) NULL
;
183 gtk_idle_remove( m_idleTag
);
185 if (m_colorCube
) free(m_colorCube
);
188 bool wxApp::OnInitGui()
190 /* Nothing to do for 15, 16, 24, 32 bit displays */
192 GdkVisual
*visual
= gdk_visual_get_system();
193 if (visual
->depth
> 8) return TRUE
;
195 /* this initiates the standard palette as defined by GdkImlib
196 in the GNOME libraries. it ensures that all GNOME applications
197 use the same 64 colormap entries on 8-bit displays so you
198 can use several rather graphics-heavy applications at the
200 NOTE: this doesn't really seem to work this way... */
203 GdkColormap *cmap = gdk_colormap_new( gdk_visual_get_system(), TRUE );
205 for (int i = 0; i < 64; i++)
208 col.red = g_palette[i*3 + 0] << 8;
209 col.green = g_palette[i*3 + 1] << 8;
210 col.blue = g_palette[i*3 + 2] << 8;
213 gdk_color_alloc( cmap, &col );
216 gtk_widget_set_default_colormap( cmap );
219 /* initialize color cube for 8-bit color reduction dithering */
221 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
223 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
225 for (int r
= 0; r
< 32; r
++)
227 for (int g
= 0; g
< 32; g
++)
229 for (int b
= 0; b
< 32; b
++)
231 int rr
= (r
<< 3) | (r
>> 2);
232 int gg
= (g
<< 3) | (g
>> 2);
233 int bb
= (b
<< 3) | (b
>> 2);
235 GdkColor
*colors
= cmap
->colors
;
236 int max
= 3 * (65536);
239 for (int i
= 0; i
< cmap
->size
; i
++)
241 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
242 int gdiff
= ((gg
<< 8)- colors
[i
].green
);
243 int bdiff
= ((bb
<< 8)- colors
[i
].blue
);
244 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
245 if (sum
< max
) { index
= i
; max
= sum
; }
248 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
257 bool wxApp::ProcessIdle(void)
260 event
.SetEventObject( this );
261 ProcessEvent( event
);
263 return event
.MoreRequested();
266 void wxApp::OnIdle( wxIdleEvent
&event
)
268 static bool inOnIdle
= FALSE
;
270 /* Avoid recursion (via ProcessEvent default case) */
276 /* 'Garbage' collection of windows deleted with Close(). */
277 DeletePendingObjects();
279 /* flush the logged messages if any */
280 wxLog
*log
= wxLog::GetActiveTarget();
281 if (log
!= NULL
&& log
->HasPendingMessages())
284 /* Send OnIdle events to all windows */
285 bool needMore
= SendIdleEvents();
288 event
.RequestMore(TRUE
);
293 bool wxApp::SendIdleEvents(void)
295 bool needMore
= FALSE
;
297 wxNode
* node
= wxTopLevelWindows
.First();
300 wxWindow
* win
= (wxWindow
*) node
->Data();
301 if (SendIdleEvents(win
))
308 bool wxApp::SendIdleEvents( wxWindow
* win
)
310 bool needMore
= FALSE
;
313 event
.SetEventObject(win
);
315 win
->OnInternalIdle();
317 win
->ProcessEvent(event
);
319 if (event
.MoreRequested())
322 wxNode
* node
= win
->GetChildren().First();
325 wxWindow
* win
= (wxWindow
*) node
->Data();
326 if (SendIdleEvents(win
))
334 int wxApp::MainLoop(void)
340 void wxApp::ExitMainLoop(void)
345 bool wxApp::Initialized(void)
347 return m_initialized
;
350 bool wxApp::Pending(void)
355 void wxApp::Dispatch(void)
359 void wxApp::DeletePendingObjects(void)
361 wxNode
*node
= wxPendingDelete
.First();
364 wxObject
*obj
= (wxObject
*)node
->Data();
368 if (wxPendingDelete
.Member(obj
))
371 node
= wxPendingDelete
.First();
375 wxWindow
*wxApp::GetTopWindow(void)
377 if (m_topWindow
) return m_topWindow
;
378 wxNode
*node
= wxTopLevelWindows
.First();
379 if (!node
) return (wxWindow
*) NULL
;
380 return (wxWindow
*)node
->Data();
383 void wxApp::SetTopWindow( wxWindow
*win
)
388 bool wxApp::Initialize(void)
390 wxBuffer
= new char[BUFSIZ
+ 512];
392 wxClassInfo::InitializeClasses();
394 wxSystemSettings::Init();
397 wxTheFontNameDirectory = new wxFontNameDirectory;
398 wxTheFontNameDirectory->Initialize();
401 wxTheColourDatabase
= new wxColourDatabase( wxKEY_STRING
);
402 wxTheColourDatabase
->Initialize();
404 wxInitializeStockLists();
405 wxInitializeStockObjects();
407 #if wxUSE_WX_RESOURCES
408 wxTheResourceCache
= new wxResourceCache( wxKEY_STRING
);
410 wxInitializeResourceSystem();
413 wxImage::InitStandardHandlers();
415 /* no global cursor under X
416 g_globalCursor = new wxCursor; */
418 wxModule::RegisterModules();
419 if (!wxModule::InitializeModules()) return FALSE
;
424 void wxApp::CleanUp(void)
426 wxModule::CleanUpModules();
428 #if wxUSE_WX_RESOURCES
431 if (wxTheResourceCache
) delete wxTheResourceCache
;
432 wxTheResourceCache
= (wxResourceCache
*) NULL
;
434 wxCleanUpResourceSystem();
437 if (wxTheColourDatabase
) delete wxTheColourDatabase
;
438 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
441 if (wxTheFontNameDirectory) delete wxTheFontNameDirectory;
442 wxTheFontNameDirectory = (wxFontNameDirectory*) NULL;
445 wxDeleteStockObjects();
447 wxDeleteStockLists();
449 wxImage::CleanUpHandlers();
452 wxTheApp
= (wxApp
*) NULL
;
454 wxSystemSettings::Done();
458 wxClassInfo::CleanUpClasses();
460 /* check for memory leaks */
461 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
462 if (wxDebugContext::CountObjectsLeft() > 0)
464 wxLogDebug("There were memory leaks.\n");
465 wxDebugContext::Dump();
466 wxDebugContext::PrintStatistics();
470 /* do this as the very last thing because everything else can log messages */
471 wxLog::DontCreateOnDemand();
473 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
474 if (oldLog
) delete oldLog
;
477 wxLog
*wxApp::CreateLogTarget()
482 //-----------------------------------------------------------------------------
484 //-----------------------------------------------------------------------------
486 int wxEntry( int argc
, char *argv
[] )
490 gtk_init( &argc
, &argv
);
492 if (!wxApp::Initialize()) return 0;
496 if (!wxApp::GetInitializerFunction())
498 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
502 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
504 wxObject
*test_app
= app_ini();
506 wxTheApp
= (wxApp
*) test_app
;
511 printf( "wxWindows error: wxTheApp == NULL\n" );
515 wxTheApp
->argc
= argc
;
516 wxTheApp
->argv
= argv
;
519 strcpy( name
, argv
[0] );
520 strcpy( name
, wxFileNameFromPath(name
) );
521 wxStripExtension( name
);
522 wxTheApp
->SetAppName( name
);
524 if (!wxTheApp
->OnInitGui()) return 0;
526 /* Here frames insert themselves automatically
527 * into wxTopLevelWindows by getting created
530 if (!wxTheApp
->OnInit()) return 0;
532 wxTheApp
->m_initialized
= (wxTopLevelWindows
.Number() > 0);
536 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
538 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
541 /* Forcibly delete the window. */
542 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
543 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
545 topWindow
->Close( TRUE
);
546 wxTheApp
->DeletePendingObjects();
551 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
557 /* flush the logged messages if any */
558 wxLog
*log
= wxLog::GetActiveTarget();
559 if (log
!= NULL
&& log
->HasPendingMessages())