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 <vms_jackets.h>
19 #include "wx/gdicmn.h"
23 #include "wx/memory.h"
25 #include "wx/settings.h"
26 #include "wx/dialog.h"
28 #if wxUSE_WX_RESOURCES
29 #include "wx/resource.h"
32 #include "wx/module.h"
35 #ifdef __WXUNIVERSAL__
36 #include "wx/univ/theme.h"
37 #include "wx/univ/renderer.h"
41 #include "wx/thread.h"
45 #if defined(__DARWIN__)
46 // FIXME: select must be used instead of poll (GD)
50 # include <sys/poll.h>
52 #include "wx/gtk/win_gtk.h"
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 wxApp
*wxTheApp
= (wxApp
*) NULL
;
62 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
64 bool g_mainThreadLocked
= FALSE
;
65 gint g_pendingTag
= 0;
67 static GtkWidget
*gs_RootWindow
= (GtkWidget
*) NULL
;
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
75 void wxapp_install_idle_handler();
77 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
90 bool wxApp::Yield(bool onlyIfNeeded
)
93 static bool s_inYield
= FALSE
;
99 wxFAIL_MSG( wxT("wxYield called recursively" ) );
106 if ( !wxThread::IsMain() )
108 // can't call gtk_main_iteration() from other threads like this
111 #endif // wxUSE_THREADS
117 // We need to remove idle callbacks or the loop will
119 gtk_idle_remove( m_idleTag
);
124 // disable log flushing from here because a call to wxYield() shouldn't
125 // normally result in message boxes popping up &c
128 while (gtk_events_pending())
129 gtk_main_iteration();
131 // It's necessary to call ProcessIdle() to update the frames sizes which
132 // might have been changed (it also will update other things set from
133 // OnUpdateUI() which is a nice (and desired) side effect). But we
134 // call ProcessIdle() only once since this is not meant for longish
135 // background jobs (controlled by wxIdleEvent::RequestMore() and the
136 // return value of Processidle().
139 // let the logs be flashed again
147 //-----------------------------------------------------------------------------
149 //-----------------------------------------------------------------------------
154 if (!wxThread::IsMain())
159 wxapp_install_idle_handler();
162 if (!wxThread::IsMain())
167 //-----------------------------------------------------------------------------
169 //-----------------------------------------------------------------------------
171 // the callback functions must be extern "C" to comply with GTK+ declarations
175 static gint
wxapp_pending_callback( gpointer
WXUNUSED(data
) )
177 if (!wxTheApp
) return TRUE
;
179 // When getting called from GDK's time-out handler
180 // we are no longer within GDK's grab on the GUI
181 // thread so we must lock it here ourselves.
184 // Sent idle event to all who request them.
185 wxTheApp
->ProcessPendingEvents();
189 // Flush the logged messages if any.
191 wxLog::FlushActive();
194 // Release lock again
197 // Return FALSE to indicate that no more idle events are
198 // to be sent (single shot instead of continuous stream)
202 static gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
208 // don't generate the idle events while the assert modal dialog is shown,
209 // this completely confuses the apps which don't expect to be reentered
210 // from some safely-looking functions
211 if ( wxTheApp
->IsInAssert() )
215 #endif // __WXDEBUG__
217 // When getting called from GDK's time-out handler
218 // we are no longer within GDK's grab on the GUI
219 // thread so we must lock it here ourselves.
222 // Indicate that we are now in idle mode and event handlers
223 // will have to reinstall the idle handler again.
225 wxTheApp
->m_idleTag
= 0;
227 // Send idle event to all who request them as long as
228 // no events have popped up in the event queue.
229 while (wxTheApp
->ProcessIdle() && (gtk_events_pending() == 0))
232 // Release lock again
235 // Return FALSE to indicate that no more idle events are
236 // to be sent (single shot instead of continuous stream).
242 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
248 g_mainThreadLocked
= TRUE
;
251 // FIXME: poll is not available under Darwin/Mac OS X and this needs
252 // to be implemented using select instead (GD)
253 // what about other BSD derived systems?
256 res
= poll( (struct pollfd
*) ufds
, nfds
, timeout
);
260 g_mainThreadLocked
= FALSE
;
267 #endif // wxUSE_THREADS
271 void wxapp_install_idle_handler()
273 wxASSERT_MSG( wxTheApp
->m_idleTag
== 0, wxT("attempt to install idle handler twice") );
277 if (g_pendingTag
== 0)
278 g_pendingTag
= gtk_idle_add_priority( 900, wxapp_pending_callback
, (gpointer
) NULL
);
280 // This routine gets called by all event handlers
281 // indicating that the idle is over. It may also
282 // get called from other thread for sending events
283 // to the main thread (and processing these in
284 // idle time). Very low priority.
285 wxTheApp
->m_idleTag
= gtk_idle_add_priority( 1000, wxapp_idle_callback
, (gpointer
) NULL
);
288 //-----------------------------------------------------------------------------
289 // Access to the root window global
290 //-----------------------------------------------------------------------------
292 GtkWidget
* wxGetRootWindow()
294 if (gs_RootWindow
== NULL
)
296 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
297 gtk_widget_realize( gs_RootWindow
);
299 return gs_RootWindow
;
302 //-----------------------------------------------------------------------------
304 //-----------------------------------------------------------------------------
306 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
308 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
309 EVT_IDLE(wxApp::OnIdle
)
314 m_initialized
= FALSE
;
316 m_isInAssert
= FALSE
;
317 #endif // __WXDEBUG__
320 wxapp_install_idle_handler();
323 g_main_set_poll_func( wxapp_poll_func
);
326 m_colorCube
= (unsigned char*) NULL
;
328 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
329 m_glVisualInfo
= (void *) NULL
;
334 if (m_idleTag
) gtk_idle_remove( m_idleTag
);
336 if (m_colorCube
) free(m_colorCube
);
339 bool wxApp::OnInitGui()
341 if ( !wxAppBase::OnInitGui() )
344 GdkVisual
*visual
= gdk_visual_get_system();
346 // if this is a wxGLApp (derived from wxApp), and we've already
347 // chosen a specific visual, then derive the GdkVisual from that
348 if (m_glVisualInfo
!= NULL
)
351 // seems gtk_widget_set_default_visual no longer exists?
352 GdkVisual
* vis
= gtk_widget_get_default_visual();
354 GdkVisual
* vis
= gdkx_visual_get(
355 ((XVisualInfo
*) m_glVisualInfo
) ->visualid
);
356 gtk_widget_set_default_visual( vis
);
359 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
360 gtk_widget_set_default_colormap( colormap
);
365 // On some machines, the default visual is just 256 colours, so
366 // we make sure we get the best. This can sometimes be wasteful.
369 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual
))
372 /* seems gtk_widget_set_default_visual no longer exists? */
373 GdkVisual
* vis
= gtk_widget_get_default_visual();
375 GdkVisual
* vis
= gdk_visual_get_best();
376 gtk_widget_set_default_visual( vis
);
379 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
380 gtk_widget_set_default_colormap( colormap
);
385 // Nothing to do for 15, 16, 24, 32 bit displays
386 if (visual
->depth
> 8) return TRUE
;
388 // initialize color cube for 8-bit color reduction dithering
390 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
392 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
394 for (int r
= 0; r
< 32; r
++)
396 for (int g
= 0; g
< 32; g
++)
398 for (int b
= 0; b
< 32; b
++)
400 int rr
= (r
<< 3) | (r
>> 2);
401 int gg
= (g
<< 3) | (g
>> 2);
402 int bb
= (b
<< 3) | (b
>> 2);
406 GdkColor
*colors
= cmap
->colors
;
411 for (int i
= 0; i
< cmap
->size
; i
++)
413 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
414 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
415 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
416 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
419 index
= i
; max
= sum
;
425 // assume 8-bit true or static colors. this really exists
426 GdkVisual
* vis
= gdk_colormap_get_visual( cmap
);
427 index
= (r
>> (5 - vis
->red_prec
)) << vis
->red_shift
;
428 index
|= (g
>> (5 - vis
->green_prec
)) << vis
->green_shift
;
429 index
|= (b
>> (5 - vis
->blue_prec
)) << vis
->blue_shift
;
431 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
439 GdkVisual
*wxApp::GetGdkVisual()
441 GdkVisual
*visual
= NULL
;
444 visual
= gdkx_visual_get( ((XVisualInfo
*) m_glVisualInfo
)->visualid
);
446 visual
= gdk_window_get_visual( wxGetRootWindow()->window
);
453 bool wxApp::ProcessIdle()
456 event
.SetEventObject( this );
457 ProcessEvent( event
);
459 return event
.MoreRequested();
462 void wxApp::OnIdle( wxIdleEvent
&event
)
464 static bool s_inOnIdle
= FALSE
;
466 // Avoid recursion (via ProcessEvent default case)
472 // Resend in the main thread events which have been prepared in other
474 ProcessPendingEvents();
476 // 'Garbage' collection of windows deleted with Close()
477 DeletePendingObjects();
479 // Send OnIdle events to all windows
480 bool needMore
= SendIdleEvents();
483 event
.RequestMore(TRUE
);
488 bool wxApp::SendIdleEvents()
490 bool needMore
= FALSE
;
492 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
495 wxWindow
* win
= node
->GetData();
496 if (SendIdleEvents(win
))
499 node
= node
->GetNext();
502 node
= wxTopLevelWindows
.GetFirst();
505 wxWindow
* win
= node
->GetData();
506 CallInternalIdle( win
);
508 node
= node
->GetNext();
513 bool wxApp::CallInternalIdle( wxWindow
* win
)
515 win
->OnInternalIdle();
517 wxNode
* node
= win
->GetChildren().First();
520 wxWindow
* win
= (wxWindow
*) node
->Data();
521 CallInternalIdle( win
);
529 bool wxApp::SendIdleEvents( wxWindow
* win
)
531 bool needMore
= FALSE
;
534 event
.SetEventObject(win
);
536 win
->GetEventHandler()->ProcessEvent(event
);
538 if (event
.MoreRequested())
541 wxNode
* node
= win
->GetChildren().First();
544 wxWindow
* win
= (wxWindow
*) node
->Data();
545 if (SendIdleEvents(win
))
554 int wxApp::MainLoop()
560 void wxApp::ExitMainLoop()
562 if (gtk_main_level() > 0)
566 bool wxApp::Initialized()
568 return m_initialized
;
571 bool wxApp::Pending()
573 return (gtk_events_pending() > 0);
576 void wxApp::Dispatch()
578 gtk_main_iteration();
581 void wxApp::DeletePendingObjects()
583 wxNode
*node
= wxPendingDelete
.First();
586 wxObject
*obj
= (wxObject
*)node
->Data();
590 if (wxPendingDelete
.Find(obj
))
593 node
= wxPendingDelete
.First();
597 bool wxApp::Initialize()
599 wxClassInfo::InitializeClasses();
602 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
605 // GL: I'm annoyed ... I don't know where to put this and I don't want to
606 // create a module for that as it's part of the core.
608 wxPendingEvents
= new wxList();
609 wxPendingEventsLocker
= new wxCriticalSection();
612 wxTheColourDatabase
= new wxColourDatabase( wxKEY_STRING
);
613 wxTheColourDatabase
->Initialize();
615 wxInitializeStockLists();
616 wxInitializeStockObjects();
618 #if wxUSE_WX_RESOURCES
619 wxInitializeResourceSystem();
622 wxModule::RegisterModules();
623 if (!wxModule::InitializeModules()) return FALSE
;
628 void wxApp::CleanUp()
630 wxModule::CleanUpModules();
632 #if wxUSE_WX_RESOURCES
633 wxCleanUpResourceSystem();
636 delete wxTheColourDatabase
;
637 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
639 wxDeleteStockObjects();
641 wxDeleteStockLists();
644 wxTheApp
= (wxApp
*) NULL
;
646 wxClassInfo::CleanUpClasses();
649 delete wxPendingEvents
;
650 delete wxPendingEventsLocker
;
653 // check for memory leaks
654 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
655 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
657 wxLogDebug(wxT("There were memory leaks.\n"));
658 wxDebugContext::Dump();
659 wxDebugContext::PrintStatistics();
664 // do this as the very last thing because everything else can log messages
665 wxLog::DontCreateOnDemand();
667 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
673 //-----------------------------------------------------------------------------
675 //-----------------------------------------------------------------------------
677 // NB: argc and argv may be changed here, pass by reference!
678 int wxEntryStart( int& argc
, char *argv
[] )
681 // GTK 1.2 up to version 1.2.3 has broken threads
682 if ((gtk_major_version
== 1) &&
683 (gtk_minor_version
== 2) &&
684 (gtk_micro_version
< 4))
686 printf( "wxWindows warning: GUI threading disabled due to outdated GTK version\n" );
696 // We should have the wxUSE_WCHAR_T test on the _outside_
698 #if defined(__WXGTK20__)
699 // gtk+ 2.0 supports Unicode through UTF-8 strings
700 wxConvCurrent
= &wxConvUTF8
;
702 if (!wxOKlibc()) wxConvCurrent
= &wxConvLocal
;
705 if (!wxOKlibc()) wxConvCurrent
= (wxMBConv
*) NULL
;
710 gtk_init( &argc
, &argv
);
712 wxSetDetectableAutoRepeat( TRUE
);
714 if (!wxApp::Initialize())
728 if ( !wxTheApp
->OnInitGui() )
737 void wxEntryCleanup()
740 // flush the logged messages if any
741 wxLog
*log
= wxLog::GetActiveTarget();
742 if (log
!= NULL
&& log
->HasPendingMessages())
745 // continuing to use user defined log target is unsafe from now on because
746 // some resources may be already unavailable, so replace it by something
748 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
759 int wxEntry( int argc
, char *argv
[] )
761 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
762 // This seems to be necessary since there are 'rogue'
763 // objects present at this point (perhaps global objects?)
764 // Setting a checkpoint will ignore them as far as the
765 // memory checking facility is concerned.
766 // Of course you may argue that memory allocated in globals should be
767 // checked, but this is a reasonable compromise.
768 wxDebugContext::SetCheckpoint();
770 int err
= wxEntryStart(argc
, argv
);
776 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
777 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
779 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
781 wxObject
*test_app
= app_ini();
783 wxTheApp
= (wxApp
*) test_app
;
786 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
788 wxTheApp
->argc
= argc
;
790 wxTheApp
->argv
= new wxChar
*[argc
+1];
792 while (mb_argc
< argc
)
794 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
797 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
799 wxTheApp
->argv
= argv
;
802 wxString
name(wxFileNameFromPath(argv
[0]));
803 wxStripExtension( name
);
804 wxTheApp
->SetAppName( name
);
807 retValue
= wxEntryInitGui();
809 // Here frames insert themselves automatically into wxTopLevelWindows by
810 // getting created in OnInit().
813 if ( !wxTheApp
->OnInit() )
819 // Delete pending toplevel windows
820 wxTheApp
->DeletePendingObjects();
822 // When is the app not initialized ?
823 wxTheApp
->m_initialized
= TRUE
;
825 if (wxTheApp
->Initialized())
829 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
831 // Delete all pending windows if any
832 wxTheApp
->DeletePendingObjects();
836 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
838 retValue
= wxTheApp
->OnExit();
849 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
)
853 wxAppBase::OnAssert(file
, line
, msg
);
855 m_isInAssert
= FALSE
;
858 #endif // __WXDEBUG__