]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/app.cpp 
66fced32c3d7690f9740b666c4cf2bb9e6cefbf0
   1  /////////////////////////////////////////////////////////////////////////////    2  // Name:        src/gtk1/app.cpp    4  // Author:      Robert Roebling    6  // Copyright:   (c) 1998 Robert Roebling, Julian Smart    7  // Licence:     wxWindows licence    8  /////////////////////////////////////////////////////////////////////////////   10  // For compilers that support precompilation, includes "wx.h".   11  #include  "wx/wxprec.h"   19      #include  "wx/dialog.h"   20      #include  "wx/settings.h"   21      #include  "wx/msgdlg.h"   22      #include  "wx/memory.h"   24      #include  "wx/gdicmn.h"   26      #include  "wx/module.h"   30  #include  "wx/filename.h"   31  #include  "wx/thread.h"   37  #ifdef __WXUNIVERSAL__   38      #include  "wx/univ/theme.h"   39      #include  "wx/univ/renderer.h"   43      #include  "wx/thread.h"   52          // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"   53          // in neither poll.h nor sys/poll.h which results in link errors later   66  // we implement poll() ourselves using select() which is supposed exist in   68      #include <sys/types.h>   71      #ifdef HAVE_SYS_SELECT_H   72          #include <sys/select.h>   74  #endif  // HAVE_POLL/!HAVE_POLL   76  #include  "wx/unix/private.h"   77  #include  "wx/gtk1/win_gtk.h"   81  //-----------------------------------------------------------------------------   83  //-----------------------------------------------------------------------------   85  bool    g_mainThreadLocked 
=  false ;   86  gint   g_pendingTag 
=  0 ;   88  static  GtkWidget 
* gs_RootWindow 
=  NULL
;   90  //-----------------------------------------------------------------------------   92  //-----------------------------------------------------------------------------   96  void  wxapp_install_idle_handler ();   99  static  wxMutex gs_idleTagsMutex
;  103  //-----------------------------------------------------------------------------  105  //-----------------------------------------------------------------------------  107  // RR/KH: The wxMutexGui calls are not needed on GTK2 according to  108  // the GTK faq, http://www.gtk.org/faq/#AEN500  109  // The calls to gdk_threads_enter() and leave() are specifically noted  110  // as not being necessary.  The MutexGui calls are still left in for GTK1.  111  // Eliminating the MutexGui calls fixes the long-standing "random" lockup  112  // when using wxPostEvent (which calls WakeUpIdle) from a thread.  114  void  wxApp :: WakeUpIdle ()  117      if  (! wxThread :: IsMain ())  119  #endif  // wxUSE_THREADS  121      wxapp_install_idle_handler ();  124      if  (! wxThread :: IsMain ())  126  #endif  // wxUSE_THREADS  129  //-----------------------------------------------------------------------------  131  //-----------------------------------------------------------------------------  133  // the callback functions must be extern "C" to comply with GTK+ declarations  137  static  gint 
wxapp_pending_callback (  gpointer 
WXUNUSED ( data
) )  139      if  (! wxTheApp
)  return  TRUE
;  141      // When getting called from GDK's time-out handler  142      // we are no longer within GDK's grab on the GUI  143      // thread so we must lock it here ourselves.  146      // Sent idle event to all who request them.  147      wxTheApp
-> ProcessPendingEvents ();  151          wxMutexLocker 
lock ( gs_idleTagsMutex
);  156      // Flush the logged messages if any.  158      wxLog :: FlushActive ();  161      // Release lock again  164      // Return FALSE to indicate that no more idle events are  165      // to be sent (single shot instead of continuous stream)  169  static  gint 
wxapp_idle_callback (  gpointer 
WXUNUSED ( data
) )  175      // don't generate the idle events while the assert modal dialog is shown,  176      // this completely confuses the apps which don't expect to be reentered  177      // from some safely-looking functions  178      if  (  wxTheApp
-> IsInAssert () )  180          // But repaint the assertion message if necessary  181          if  ( wxTopLevelWindows
. GetCount () >  0 )  183              wxWindow
*  win 
= ( wxWindow
*)  wxTopLevelWindows
. GetLast ()-> GetData ();  184              if  ( win
-> IsKindOf ( CLASSINFO ( wxGenericMessageDialog
)))  185                  win
-> OnInternalIdle ();  189  #endif  // wxDEBUG_LEVEL  191      // When getting called from GDK's time-out handler  192      // we are no longer within GDK's grab on the GUI  193      // thread so we must lock it here ourselves.  196      // Indicate that we are now in idle mode and event handlers  197      // will have to reinstall the idle handler again.  200          wxMutexLocker 
lock ( gs_idleTagsMutex
);  203          wxTheApp
-> m_idleTag 
=  0 ;  206      // Send idle event to all who request them as long as  207      // no events have popped up in the event queue.  208      while  ( wxTheApp
-> ProcessIdle () && ( gtk_events_pending () ==  0 ))  211      // Release lock again  214      // Return FALSE to indicate that no more idle events are  215      // to be sent (single shot instead of continuous stream).  223      #define wxPollFd pollfd  226  typedef  GPollFD wxPollFd
;  228  int  wxPoll ( wxPollFd 
* ufds
,  unsigned int  nfds
,  int  timeout
)  230      // convert timeout from ms to struct timeval (s/us)  232      tv_timeout
. tv_sec 
=  timeout
/ 1000 ;  233      tv_timeout
. tv_usec 
= ( timeout%1000
)* 1000 ;  235      // remember the highest fd used here  238      // and fill the sets for select()  243      wxFD_ZERO (& writefds
);  244      wxFD_ZERO (& exceptfds
);  247      for  (  i 
=  0 ;  i 
<  nfds
;  i
++ )  249          wxASSERT_MSG (  ufds
[ i
]. fd 
<  FD_SETSIZE
,  wxT ( "fd out of range" ) );  251          if  (  ufds
[ i
]. events 
&  G_IO_IN 
)  252              wxFD_SET ( ufds
[ i
]. fd
, & readfds
);  254          if  (  ufds
[ i
]. events 
&  G_IO_PRI 
)  255              wxFD_SET ( ufds
[ i
]. fd
, & exceptfds
);  257          if  (  ufds
[ i
]. events 
&  G_IO_OUT 
)  258              wxFD_SET ( ufds
[ i
]. fd
, & writefds
);  260          if  (  ufds
[ i
]. fd 
>  fdMax 
)  265      int  res 
=  select ( fdMax
, & readfds
, & writefds
, & exceptfds
, & tv_timeout
);  267      // translate the results back  268      for  (  i 
=  0 ;  i 
<  nfds
;  i
++ )  272          if  (  wxFD_ISSET ( ufds
[ i
]. fd
, & readfds 
) )  273              ufds
[ i
]. revents 
|=  G_IO_IN
;  275          if  (  wxFD_ISSET ( ufds
[ i
]. fd
, & exceptfds 
) )  276              ufds
[ i
]. revents 
|=  G_IO_PRI
;  278          if  (  wxFD_ISSET ( ufds
[ i
]. fd
, & writefds 
) )  279              ufds
[ i
]. revents 
|=  G_IO_OUT
;  285  #endif  // HAVE_POLL/!HAVE_POLL  287  static  gint 
wxapp_poll_func (  GPollFD 
* ufds
,  guint nfds
,  gint timeout 
)  292      g_mainThreadLocked 
=  true ;  294      // we rely on the fact that glib GPollFD struct is really just pollfd but  295      // I wonder how wise is this in the long term (VZ)  296      gint res 
=  wxPoll ( ( wxPollFd 
*)  ufds
,  nfds
,  timeout 
);  299      g_mainThreadLocked 
=  false ;  306  #endif  // wxUSE_THREADS  310  void  wxapp_install_idle_handler ()  313      wxMutexLocker 
lock ( gs_idleTagsMutex
);  316      // Don't install the handler if it's already installed. This test *MUST*  317      // be done when gs_idleTagsMutex is locked!  321      // GD: this assert is raised when using the thread sample (which works)  322      //     so the test is probably not so easy. Can widget callbacks be  323      //     triggered from child threads and, if so, for which widgets?  324      // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );  326      wxASSERT_MSG (  wxTheApp
-> m_idleTag 
==  0 ,  wxT ( "attempt to install idle handler twice" ) );  330      if  ( g_pendingTag 
==  0 )  331          g_pendingTag 
=  gtk_idle_add_priority (  900 ,  wxapp_pending_callback
, ( gpointer
)  NULL 
);  333      // This routine gets called by all event handlers  334      // indicating that the idle is over. It may also  335      // get called from other thread for sending events  336      // to the main thread (and processing these in  337      // idle time). Very low priority.  338      wxTheApp
-> m_idleTag 
=  gtk_idle_add_priority (  1000 ,  wxapp_idle_callback
, ( gpointer
)  NULL 
);  341  static bool  wxOKlibc ()  343  #if defined(__UNIX__) && defined(__GLIBC__)  344      // glibc 2.0 uses UTF-8 even when it shouldn't  346      if  (( MB_CUR_MAX 
==  2 ) &&  347          ( wxMB2WC (& res
,  " \xdd\xa5 " ,  1 ) ==  1 ) &&  350          // this is UTF-8 allright, check whether that's what we want  351          char  * cur_locale 
=  setlocale ( LC_CTYPE
,  NULL
);  352          if  (( strlen ( cur_locale
) <  4 ) ||  353          ( strcasecmp ( cur_locale 
+  strlen ( cur_locale
) -  4 ,  "utf8" )) ||  354          ( strcasecmp ( cur_locale 
+  strlen ( cur_locale
) -  5 ,  "utf-8" ))) {  355          // nope, don't use libc conversion  363  //-----------------------------------------------------------------------------  364  // Access to the root window global  365  //-----------------------------------------------------------------------------  367  GtkWidget
*  wxGetRootWindow ()  369      if  ( gs_RootWindow 
==  NULL
)  371          gs_RootWindow 
=  gtk_window_new (  GTK_WINDOW_TOPLEVEL 
);  372          gtk_widget_realize (  gs_RootWindow 
);  374      return  gs_RootWindow
;  377  //-----------------------------------------------------------------------------  379  //-----------------------------------------------------------------------------  381  IMPLEMENT_DYNAMIC_CLASS ( wxApp
, wxEvtHandler
)  385      m_isInAssert 
=  false ;  389      wxapp_install_idle_handler ();  392      g_main_set_poll_func (  wxapp_poll_func 
);  395      m_colorCube 
= ( unsigned char *)  NULL
;  397      // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp  398      m_glVisualInfo 
=  NULL
;  404      if  ( m_idleTag
)  gtk_idle_remove (  m_idleTag 
);  406      if  ( m_colorCube
)  free ( m_colorCube
);  409  bool  wxApp :: OnInitGui ()  411      if  ( ! wxAppBase :: OnInitGui () )  414      GdkVisual 
* visual 
=  gdk_visual_get_system ();  416      // if this is a wxGLApp (derived from wxApp), and we've already  417      // chosen a specific visual, then derive the GdkVisual from that  418      if  ( m_glVisualInfo 
!=  NULL
)  420          GdkVisual
*  vis 
=  gdkx_visual_get (  421              (( XVisualInfo 
*)  m_glVisualInfo
) -> visualid 
);  422          gtk_widget_set_default_visual (  vis 
);  424          GdkColormap 
* colormap 
=  gdk_colormap_new (  vis
,  FALSE 
);  425          gtk_widget_set_default_colormap (  colormap 
);  430      // On some machines, the default visual is just 256 colours, so  431      // we make sure we get the best. This can sometimes be wasteful.  434      if  (( gdk_visual_get_best () !=  gdk_visual_get_system ()) && ( m_useBestVisual
))  436          GdkVisual
*  vis 
=  gdk_visual_get_best ();  437          gtk_widget_set_default_visual (  vis 
);  439          GdkColormap 
* colormap 
=  gdk_colormap_new (  vis
,  FALSE 
);  440          gtk_widget_set_default_colormap (  colormap 
);  445      // Nothing to do for 15, 16, 24, 32 bit displays  446      if  ( visual
-> depth 
>  8 )  return  TRUE
;  448      // initialize color cube for 8-bit color reduction dithering  450      GdkColormap 
* cmap 
=  gtk_widget_get_default_colormap ();  452      m_colorCube 
= ( unsigned char *) malloc ( 32  *  32  *  32 );  454      for  ( int  r 
=  0 ;  r 
<  32 ;  r
++)  456          for  ( int  g 
=  0 ;  g 
<  32 ;  g
++)  458              for  ( int  b 
=  0 ;  b 
<  32 ;  b
++)  460                  int  rr 
= ( r 
<<  3 ) | ( r 
>>  2 );  461                  int  gg 
= ( g 
<<  3 ) | ( g 
>>  2 );  462                  int  bb 
= ( b 
<<  3 ) | ( b 
>>  2 );  466                  GdkColor 
* colors 
=  cmap
-> colors
;  471                      for  ( int  i 
=  0 ;  i 
<  cmap
-> size
;  i
++)  473                          int  rdiff 
= (( rr 
<<  8 ) -  colors
[ i
]. red
);  474                          int  gdiff 
= (( gg 
<<  8 ) -  colors
[ i
]. green
);  475                          int  bdiff 
= (( bb 
<<  8 ) -  colors
[ i
]. blue
);  476                          int  sum 
=  ABS  ( rdiff
) +  ABS  ( gdiff
) +  ABS  ( bdiff
);  479                              index 
=  i
;  max 
=  sum
;  485                      // assume 8-bit true or static colors. this really exists  486                      GdkVisual
*  vis 
=  gdk_colormap_get_visual (  cmap 
);  487                      index 
= ( r 
>> ( 5  -  vis
-> red_prec
)) <<  vis
-> red_shift
;  488                      index 
|= ( g 
>> ( 5  -  vis
-> green_prec
)) <<  vis
-> green_shift
;  489                      index 
|= ( b 
>> ( 5  -  vis
-> blue_prec
)) <<  vis
-> blue_shift
;  491                  m_colorCube
[ ( r
* 1024 ) + ( g
* 32 ) +  b 
] =  index
;  499  GdkVisual 
* wxApp :: GetGdkVisual ()  501      GdkVisual 
* visual 
=  NULL
;  504          visual 
=  gdkx_visual_get ( (( XVisualInfo 
*)  m_glVisualInfo
)-> visualid 
);  506          visual 
=  gdk_window_get_visual (  wxGetRootWindow ()-> window 
);  513  bool  wxApp :: Initialize ( int &  argc
,  wxChar 
** argv
)  518      // GTK 1.2 up to version 1.2.3 has broken threads  519      if  (( gtk_major_version 
==  1 ) &&  520          ( gtk_minor_version 
==  2 ) &&  521          ( gtk_micro_version 
<  4 ))  523          printf (  "wxWidgets warning: GUI threading disabled due to outdated GTK version \n "  );  527          if  (! g_thread_supported ())  530  #endif  // wxUSE_THREADS  534      // We should have the wxUSE_WCHAR_T test on the _outside_  536          wxConvCurrent 
= & wxConvLocal
;  539      // gtk_init() wants UTF-8, not wchar_t, so convert  541      char  ** argvGTK 
=  new  char  *[ argc 
+  1 ];  542      for  (  i 
=  0 ;  i 
<  argc
;  i
++ )  544          argvGTK
[ i
] =  wxStrdupA ( wxConvUTF8
. cWX2MB ( argv
[ i
]));  547      argvGTK
[ argc
] =  NULL
;  552      init_result 
=  true ;   // is there a _check() version of this?  553      gpe_application_init ( & argcGTK
, & argvGTK 
);  555      init_result 
=  gtk_init_check ( & argcGTK
, & argvGTK 
);  558      if  (  argcGTK 
!=  argc 
)  560          // we have to drop the parameters which were consumed by GTK+  561          for  (  i 
=  0 ;  i 
<  argcGTK
;  i
++ )  563              while  (  strcmp ( wxConvUTF8
. cWX2MB ( argv
[ i
]),  argvGTK
[ i
]) !=  0  )  565                  memmove ( argv 
+  i
,  argv 
+  i 
+  1 ,  argc 
-  i
);  571      //else: gtk_init() didn't modify our parameters  574      for  (  i 
=  0 ;  i 
<  argcGTK
;  i
++ )  580  #else  // !wxUSE_UNICODE  581  // gtk_init() shouldn't actually change argv itself (just its contents) so  582      // it's ok to pass pointer to it  583      init_result 
=  gtk_init_check ( & argc
, & argv 
);  584  #endif  // wxUSE_UNICODE/!wxUSE_UNICODE  587          wxLogError ( wxT ( "Unable to initialize gtk, is DISPLAY set properly?" ));  591      // we can not enter threads before gtk_init is done  594      if  ( ! wxAppBase :: Initialize ( argc
,  argv
) )  601      wxSetDetectableAutoRepeat (  TRUE 
);  604      wxFont :: SetDefaultEncoding ( wxLocale :: GetSystemEncoding ());  610  void  wxApp :: CleanUp ()  614      wxAppBase :: CleanUp ();  617  void  wxApp :: OnAssertFailure ( const  wxChar 
* file
,  623      // there is no need to do anything if asserts are disabled in this build  626      // block wx idle events while assert dialog is showing  629      wxAppBase :: OnAssertFailure ( file
,  line
,  func
,  cond
,  msg
);  631      m_isInAssert 
=  false ;  632  #else  // !wxDEBUG_LEVEL  638  #endif  // wxDEBUG_LEVEL/!wxDEBUG_LEVEL  641  void  wxApp :: RemoveIdleTag ()  644      wxMutexLocker 
lock ( gs_idleTagsMutex
);  648          gtk_idle_remove (  wxTheApp
-> m_idleTag 
);  649          wxTheApp
-> m_idleTag 
=  0 ;