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>
16 #undef ConnectionNumber
20 #include "wx/gdicmn.h"
24 #include "wx/memory.h"
26 #include "wx/settings.h"
27 #include "wx/dialog.h"
28 #include "wx/msgdlg.h"
30 #include "wx/filename.h"
31 #include "wx/module.h"
34 #ifdef __WXUNIVERSAL__
35 #include "wx/univ/theme.h"
36 #include "wx/univ/renderer.h"
40 #include "wx/thread.h"
49 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
50 // in neither poll.h nor sys/poll.h which results in link errors later
63 // we implement poll() ourselves using select() which is supposed exist in
65 #include <sys/types.h>
68 #endif // HAVE_POLL/!HAVE_POLL
70 #include "wx/gtk/win_gtk.h"
75 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
79 wxApp
*wxTheApp
= (wxApp
*) NULL
;
80 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
82 bool g_mainThreadLocked
= FALSE
;
83 gint g_pendingTag
= 0;
85 static GtkWidget
*gs_RootWindow
= (GtkWidget
*) NULL
;
87 //-----------------------------------------------------------------------------
89 //-----------------------------------------------------------------------------
93 void wxapp_install_idle_handler();
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
104 //-----------------------------------------------------------------------------
106 //-----------------------------------------------------------------------------
108 // not static because used by textctrl.cpp
111 bool wxIsInsideYield
= FALSE
;
113 bool wxApp::Yield(bool onlyIfNeeded
)
115 if ( wxIsInsideYield
)
119 wxFAIL_MSG( wxT("wxYield called recursively" ) );
126 if ( !wxThread::IsMain() )
128 // can't call gtk_main_iteration() from other threads like this
131 #endif // wxUSE_THREADS
133 wxIsInsideYield
= TRUE
;
137 // We need to remove idle callbacks or the loop will
139 gtk_idle_remove( m_idleTag
);
144 // disable log flushing from here because a call to wxYield() shouldn't
145 // normally result in message boxes popping up &c
148 while (gtk_events_pending())
149 gtk_main_iteration();
151 // It's necessary to call ProcessIdle() to update the frames sizes which
152 // might have been changed (it also will update other things set from
153 // OnUpdateUI() which is a nice (and desired) side effect). But we
154 // call ProcessIdle() only once since this is not meant for longish
155 // background jobs (controlled by wxIdleEvent::RequestMore() and the
156 // return value of Processidle().
159 // let the logs be flashed again
162 wxIsInsideYield
= FALSE
;
167 //-----------------------------------------------------------------------------
169 //-----------------------------------------------------------------------------
174 if (!wxThread::IsMain())
179 wxapp_install_idle_handler();
182 if (!wxThread::IsMain())
187 //-----------------------------------------------------------------------------
189 //-----------------------------------------------------------------------------
191 // the callback functions must be extern "C" to comply with GTK+ declarations
195 static gint
wxapp_pending_callback( gpointer
WXUNUSED(data
) )
197 if (!wxTheApp
) return TRUE
;
199 // When getting called from GDK's time-out handler
200 // we are no longer within GDK's grab on the GUI
201 // thread so we must lock it here ourselves.
204 // Sent idle event to all who request them.
205 wxTheApp
->ProcessPendingEvents();
209 // Flush the logged messages if any.
211 wxLog::FlushActive();
214 // Release lock again
217 // Return FALSE to indicate that no more idle events are
218 // to be sent (single shot instead of continuous stream)
222 static gint
wxapp_idle_callback( gpointer
WXUNUSED(data
) )
228 // don't generate the idle events while the assert modal dialog is shown,
229 // this completely confuses the apps which don't expect to be reentered
230 // from some safely-looking functions
231 if ( wxTheApp
->IsInAssert() )
233 // But repaint the assertion message if necessary
234 if (wxTopLevelWindows
.GetCount() > 0)
236 wxWindow
* win
= (wxWindow
*) wxTopLevelWindows
.GetLast()->GetData();
238 if (win
->IsKindOf(CLASSINFO(wxMessageDialog
)))
240 if (win
->IsKindOf(CLASSINFO(wxGenericMessageDialog
)))
242 win
->OnInternalIdle();
246 #endif // __WXDEBUG__
248 // When getting called from GDK's time-out handler
249 // we are no longer within GDK's grab on the GUI
250 // thread so we must lock it here ourselves.
253 // Indicate that we are now in idle mode and event handlers
254 // will have to reinstall the idle handler again.
256 wxTheApp
->m_idleTag
= 0;
258 // Send idle event to all who request them as long as
259 // no events have popped up in the event queue.
260 while (wxTheApp
->ProcessIdle() && (gtk_events_pending() == 0))
263 // Release lock again
266 // Return FALSE to indicate that no more idle events are
267 // to be sent (single shot instead of continuous stream).
275 #define wxPollFd pollfd
278 typedef GPollFD wxPollFd
;
280 int wxPoll(wxPollFd
*ufds
, unsigned int nfds
, int timeout
)
282 // convert timeout from ms to struct timeval (s/us)
284 tv_timeout
.tv_sec
= timeout
/1000;
285 tv_timeout
.tv_usec
= (timeout%1000
)*1000;
287 // remember the highest fd used here
290 // and fill the sets for select()
299 for ( i
= 0; i
< nfds
; i
++ )
301 wxASSERT_MSG( ufds
[i
].fd
< FD_SETSIZE
, _T("fd out of range") );
303 if ( ufds
[i
].events
& G_IO_IN
)
304 FD_SET(ufds
[i
].fd
, &readfds
);
306 if ( ufds
[i
].events
& G_IO_PRI
)
307 FD_SET(ufds
[i
].fd
, &exceptfds
);
309 if ( ufds
[i
].events
& G_IO_OUT
)
310 FD_SET(ufds
[i
].fd
, &writefds
);
312 if ( ufds
[i
].fd
> fdMax
)
317 int res
= select(fdMax
, &readfds
, &writefds
, &exceptfds
, &tv_timeout
);
319 // translate the results back
320 for ( i
= 0; i
< nfds
; i
++ )
324 if ( FD_ISSET(ufds
[i
].fd
, &readfds
) )
325 ufds
[i
].revents
|= G_IO_IN
;
327 if ( FD_ISSET(ufds
[i
].fd
, &exceptfds
) )
328 ufds
[i
].revents
|= G_IO_PRI
;
330 if ( FD_ISSET(ufds
[i
].fd
, &writefds
) )
331 ufds
[i
].revents
|= G_IO_OUT
;
337 #endif // HAVE_POLL/!HAVE_POLL
339 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
344 g_mainThreadLocked
= TRUE
;
346 // we rely on the fact that glib GPollFD struct is really just pollfd but
347 // I wonder how wise is this in the long term (VZ)
348 gint res
= wxPoll( (wxPollFd
*) ufds
, nfds
, timeout
);
351 g_mainThreadLocked
= FALSE
;
358 #endif // wxUSE_THREADS
362 void wxapp_install_idle_handler()
364 wxASSERT_MSG( wxTheApp
->m_idleTag
== 0, wxT("attempt to install idle handler twice") );
368 if (g_pendingTag
== 0)
369 g_pendingTag
= gtk_idle_add_priority( 900, wxapp_pending_callback
, (gpointer
) NULL
);
371 // This routine gets called by all event handlers
372 // indicating that the idle is over. It may also
373 // get called from other thread for sending events
374 // to the main thread (and processing these in
375 // idle time). Very low priority.
376 wxTheApp
->m_idleTag
= gtk_idle_add_priority( 1000, wxapp_idle_callback
, (gpointer
) NULL
);
379 //-----------------------------------------------------------------------------
380 // Access to the root window global
381 //-----------------------------------------------------------------------------
383 GtkWidget
* wxGetRootWindow()
385 if (gs_RootWindow
== NULL
)
387 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
388 gtk_widget_realize( gs_RootWindow
);
390 return gs_RootWindow
;
393 //-----------------------------------------------------------------------------
395 //-----------------------------------------------------------------------------
397 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
399 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
400 EVT_IDLE(wxApp::OnIdle
)
405 m_initialized
= FALSE
;
407 m_isInAssert
= FALSE
;
408 #endif // __WXDEBUG__
411 wxapp_install_idle_handler();
414 g_main_set_poll_func( wxapp_poll_func
);
417 m_colorCube
= (unsigned char*) NULL
;
419 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
420 m_glVisualInfo
= (void *) NULL
;
425 if (m_idleTag
) gtk_idle_remove( m_idleTag
);
427 if (m_colorCube
) free(m_colorCube
);
430 bool wxApp::OnInitGui()
432 if ( !wxAppBase::OnInitGui() )
435 GdkVisual
*visual
= gdk_visual_get_system();
437 // if this is a wxGLApp (derived from wxApp), and we've already
438 // chosen a specific visual, then derive the GdkVisual from that
439 if (m_glVisualInfo
!= NULL
)
442 // seems gtk_widget_set_default_visual no longer exists?
443 GdkVisual
* vis
= gtk_widget_get_default_visual();
445 GdkVisual
* vis
= gdkx_visual_get(
446 ((XVisualInfo
*) m_glVisualInfo
) ->visualid
);
447 gtk_widget_set_default_visual( vis
);
450 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
451 gtk_widget_set_default_colormap( colormap
);
456 // On some machines, the default visual is just 256 colours, so
457 // we make sure we get the best. This can sometimes be wasteful.
460 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual
))
463 /* seems gtk_widget_set_default_visual no longer exists? */
464 GdkVisual
* vis
= gtk_widget_get_default_visual();
466 GdkVisual
* vis
= gdk_visual_get_best();
467 gtk_widget_set_default_visual( vis
);
470 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
471 gtk_widget_set_default_colormap( colormap
);
476 // Nothing to do for 15, 16, 24, 32 bit displays
477 if (visual
->depth
> 8) return TRUE
;
479 // initialize color cube for 8-bit color reduction dithering
481 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
483 m_colorCube
= (unsigned char*)malloc(32 * 32 * 32);
485 for (int r
= 0; r
< 32; r
++)
487 for (int g
= 0; g
< 32; g
++)
489 for (int b
= 0; b
< 32; b
++)
491 int rr
= (r
<< 3) | (r
>> 2);
492 int gg
= (g
<< 3) | (g
>> 2);
493 int bb
= (b
<< 3) | (b
>> 2);
497 GdkColor
*colors
= cmap
->colors
;
502 for (int i
= 0; i
< cmap
->size
; i
++)
504 int rdiff
= ((rr
<< 8) - colors
[i
].red
);
505 int gdiff
= ((gg
<< 8) - colors
[i
].green
);
506 int bdiff
= ((bb
<< 8) - colors
[i
].blue
);
507 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
510 index
= i
; max
= sum
;
516 // assume 8-bit true or static colors. this really exists
517 GdkVisual
* vis
= gdk_colormap_get_visual( cmap
);
518 index
= (r
>> (5 - vis
->red_prec
)) << vis
->red_shift
;
519 index
|= (g
>> (5 - vis
->green_prec
)) << vis
->green_shift
;
520 index
|= (b
>> (5 - vis
->blue_prec
)) << vis
->blue_shift
;
522 m_colorCube
[ (r
*1024) + (g
*32) + b
] = index
;
530 GdkVisual
*wxApp::GetGdkVisual()
532 GdkVisual
*visual
= NULL
;
535 visual
= gdkx_visual_get( ((XVisualInfo
*) m_glVisualInfo
)->visualid
);
537 visual
= gdk_window_get_visual( wxGetRootWindow()->window
);
544 bool wxApp::ProcessIdle()
546 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
547 node
= wxTopLevelWindows
.GetFirst();
550 wxWindow
* win
= node
->GetData();
551 CallInternalIdle( win
);
553 node
= node
->GetNext();
557 event
.SetEventObject( this );
558 ProcessEvent( event
);
560 return event
.MoreRequested();
563 void wxApp::OnIdle( wxIdleEvent
&event
)
565 static bool s_inOnIdle
= FALSE
;
567 // Avoid recursion (via ProcessEvent default case)
573 // Resend in the main thread events which have been prepared in other
575 ProcessPendingEvents();
577 // 'Garbage' collection of windows deleted with Close()
578 DeletePendingObjects();
580 // Send OnIdle events to all windows
581 bool needMore
= SendIdleEvents();
584 event
.RequestMore(TRUE
);
589 bool wxApp::SendIdleEvents()
591 bool needMore
= FALSE
;
593 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
596 wxWindow
* win
= node
->GetData();
597 if (SendIdleEvents(win
))
600 node
= node
->GetNext();
606 bool wxApp::CallInternalIdle( wxWindow
* win
)
608 win
->OnInternalIdle();
610 wxWindowList::Node
*node
= win
->GetChildren().GetFirst();
613 wxWindow
*win
= node
->GetData();
615 CallInternalIdle( win
);
616 node
= node
->GetNext();
622 bool wxApp::SendIdleEvents( wxWindow
* win
)
624 bool needMore
= FALSE
;
627 event
.SetEventObject(win
);
629 win
->GetEventHandler()->ProcessEvent(event
);
631 if (event
.MoreRequested())
634 wxWindowList::Node
*node
= win
->GetChildren().GetFirst();
637 wxWindow
*win
= node
->GetData();
639 if (SendIdleEvents(win
))
641 node
= node
->GetNext();
647 int wxApp::MainLoop()
653 void wxApp::ExitMainLoop()
655 if (gtk_main_level() > 0)
659 bool wxApp::Initialized()
661 return m_initialized
;
664 bool wxApp::Pending()
666 return (gtk_events_pending() > 0);
669 void wxApp::Dispatch()
671 gtk_main_iteration();
674 void wxApp::DeletePendingObjects()
676 wxNode
*node
= wxPendingDelete
.GetFirst();
679 wxObject
*obj
= (wxObject
*)node
->GetData();
683 if (wxPendingDelete
.Find(obj
))
686 node
= wxPendingDelete
.GetFirst();
690 bool wxApp::Initialize()
692 wxClassInfo::InitializeClasses();
694 // GL: I'm annoyed ... I don't know where to put this and I don't want to
695 // create a module for that as it's part of the core.
697 wxPendingEvents
= new wxList();
698 wxPendingEventsLocker
= new wxCriticalSection();
701 wxTheColourDatabase
= new wxColourDatabase( wxKEY_STRING
);
702 wxTheColourDatabase
->Initialize();
704 wxInitializeStockLists();
705 wxInitializeStockObjects();
707 wxModule::RegisterModules();
708 if (!wxModule::InitializeModules())
712 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
718 void wxApp::CleanUp()
720 wxModule::CleanUpModules();
722 delete wxTheColourDatabase
;
723 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
725 wxDeleteStockObjects();
727 wxDeleteStockLists();
730 wxTheApp
= (wxApp
*) NULL
;
732 wxClassInfo::CleanUpClasses();
735 delete wxPendingEvents
;
736 delete wxPendingEventsLocker
;
739 // check for memory leaks
740 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
741 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
743 wxLogDebug(wxT("There were memory leaks.\n"));
744 wxDebugContext::Dump();
745 wxDebugContext::PrintStatistics();
750 // do this as the very last thing because everything else can log messages
751 wxLog::DontCreateOnDemand();
753 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
759 //-----------------------------------------------------------------------------
761 //-----------------------------------------------------------------------------
763 // NB: argc and argv may be changed here, pass by reference!
764 int wxEntryStart( int& argc
, char *argv
[] )
767 // GTK 1.2 up to version 1.2.3 has broken threads
768 if ((gtk_major_version
== 1) &&
769 (gtk_minor_version
== 2) &&
770 (gtk_micro_version
< 4))
772 printf( "wxWindows warning: GUI threading disabled due to outdated GTK version\n" );
782 // We should have the wxUSE_WCHAR_T test on the _outside_
784 #if defined(__WXGTK20__)
785 // gtk+ 2.0 supports Unicode through UTF-8 strings
786 wxConvCurrent
= &wxConvUTF8
;
788 if (!wxOKlibc()) wxConvCurrent
= &wxConvLocal
;
791 if (!wxOKlibc()) wxConvCurrent
= (wxMBConv
*) NULL
;
796 gtk_init( &argc
, &argv
);
798 wxSetDetectableAutoRepeat( TRUE
);
800 if (!wxApp::Initialize())
814 if ( !wxTheApp
->OnInitGui() )
823 void wxEntryCleanup()
826 // flush the logged messages if any
827 wxLog
*log
= wxLog::GetActiveTarget();
828 if (log
!= NULL
&& log
->HasPendingMessages())
831 // continuing to use user defined log target is unsafe from now on because
832 // some resources may be already unavailable, so replace it by something
834 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
845 int wxEntry( int argc
, char *argv
[] )
847 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
848 // This seems to be necessary since there are 'rogue'
849 // objects present at this point (perhaps global objects?)
850 // Setting a checkpoint will ignore them as far as the
851 // memory checking facility is concerned.
852 // Of course you may argue that memory allocated in globals should be
853 // checked, but this is a reasonable compromise.
854 wxDebugContext::SetCheckpoint();
856 int err
= wxEntryStart(argc
, argv
);
862 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
863 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
865 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
867 wxObject
*test_app
= app_ini();
869 wxTheApp
= (wxApp
*) test_app
;
872 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
874 wxTheApp
->argc
= argc
;
876 wxTheApp
->argv
= new wxChar
*[argc
+1];
878 while (mb_argc
< argc
)
880 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
883 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
885 wxTheApp
->argv
= argv
;
888 if (wxTheApp
->argc
> 0)
890 wxFileName
fname( wxTheApp
->argv
[0] );
891 wxTheApp
->SetAppName( fname
.GetName() );
895 retValue
= wxEntryInitGui();
897 // Here frames insert themselves automatically into wxTopLevelWindows by
898 // getting created in OnInit().
901 if ( !wxTheApp
->OnInit() )
907 // Delete pending toplevel windows
908 wxTheApp
->DeletePendingObjects();
910 // When is the app not initialized ?
911 wxTheApp
->m_initialized
= TRUE
;
913 if (wxTheApp
->Initialized())
917 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
919 // Delete all pending windows if any
920 wxTheApp
->DeletePendingObjects();
924 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
926 retValue
= wxTheApp
->OnExit();
937 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
* cond
, const wxChar
*msg
)
941 wxAppBase::OnAssert(file
, line
, cond
, msg
);
943 m_isInAssert
= FALSE
;
946 #endif // __WXDEBUG__