1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        wx/gtk/tglbtn.cpp 
   3 // Purpose:     Definition of the wxToggleButton class, which implements a 
   4 //              toggle button under wxGTK. 
   5 // Author:      John Norris, minor changes by Axel Schlueter 
   9 // Copyright:   (c) 2000 Johnny C. Norris II 
  10 // License:     Rocketeer license 
  11 ///////////////////////////////////////////////////////////////////////////// 
  13 #include "wx/tglbtn.h" 
  14 #include "wx/button.h" 
  21 extern void wxapp_install_idle_handler(); 
  23 extern bool      g_blockEventsOnDrag
; 
  24 extern wxCursor   g_globalCursor
; 
  26 // void gtk_togglebutton_clicked_callback(GtkWidget *widget, wxToggleButton *cb) 
  27 // Callback function given to gtk. 
  28 static void gtk_togglebutton_clicked_callback(GtkWidget 
*WXUNUSED(widget
), wxToggleButton 
*cb
) 
  31       wxapp_install_idle_handler(); 
  33    if (!cb
->m_hasVMT 
|| g_blockEventsOnDrag
) 
  36    if (cb
->m_blockEvent
) return; 
  38    // Generate a wx event. 
  39    wxCommandEvent 
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, cb
->GetId()); 
  40    event
.SetInt(cb
->GetValue()); 
  41    event
.SetEventObject(cb
); 
  42    cb
->GetEventHandler()->ProcessEvent(event
); 
  45 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
) 
  46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
) 
  48 bool wxToggleButton::Create(wxWindow 
*parent
, wxWindowID id
, 
  49                             const wxString 
&label
, const wxPoint 
&pos
, 
  50                             const wxSize 
&size
, long style
, 
  51                             const wxValidator
& validator
, 
  55    m_acceptsFocus 
= TRUE
; 
  59    if (!PreCreation(parent
, pos
, size
) || 
  60        !CreateBase(parent
, id
, pos
, size
, style
, validator
, name 
)) { 
  61       wxFAIL_MSG(wxT("wxToggleButton creation failed")); 
  65    wxControl::SetLabel(label
); 
  67    // Create the gtk widget. 
  68    m_widget 
= gtk_toggle_button_new_with_label(m_label
.mbc_str()); 
  70    gtk_signal_connect(GTK_OBJECT(m_widget
), "clicked", 
  71                       GTK_SIGNAL_FUNC(gtk_togglebutton_clicked_callback
), 
  74    m_parent
->DoAddChild(this); 
  78    SetFont(parent
->GetFont()); 
  80    wxSize 
size_best(DoGetBestSize()); 
  81    wxSize 
new_size(size
); 
  83       new_size
.x 
= size_best
.x
; 
  85       new_size
.y 
= size_best
.y
; 
  86    if ((new_size
.x 
!= size
.x
) || (new_size
.y 
!= size
.y
)) 
  87       SetSize(new_size
.x
, new_size
.y
); 
  89    SetBackgroundColour(parent
->GetBackgroundColour()); 
  90    SetForegroundColour(parent
->GetForegroundColour()); 
  97 // void SetValue(bool state) 
  98 // Set the value of the toggle button. 
  99 void wxToggleButton::SetValue(bool state
) 
 101    wxCHECK_RET(m_widget 
!= NULL
, wxT("invalid toggle button")); 
 103    if (state 
== GetValue()) 
 108    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
); 
 110    m_blockEvent 
= FALSE
; 
 113 // bool GetValue() const 
 114 // Get the value of the toggle button. 
 115 bool wxToggleButton::GetValue() const 
 117    wxCHECK_MSG(m_widget 
!= NULL
, FALSE
, wxT("invalid toggle button")); 
 119    return GTK_TOGGLE_BUTTON(m_widget
)->active
; 
 122 void wxToggleButton::SetLabel(const wxString
& label
) 
 124     wxCHECK_RET(m_widget 
!= NULL
, wxT("invalid toggle button")); 
 126     wxControl::SetLabel(label
); 
 128     gtk_label_set(GTK_LABEL(GTK_BUTTON(m_widget
)->child
), 
 129                  GetLabel().mbc_str()); 
 132 bool wxToggleButton::Enable(bool enable 
/*=TRUE*/) 
 134     if (!wxControl::Enable(enable
)) 
 137     gtk_widget_set_sensitive(GTK_BUTTON(m_widget
)->child
, enable
); 
 142 void wxToggleButton::ApplyWidgetStyle() 
 145     gtk_widget_set_style(m_widget
, m_widgetStyle
); 
 146     gtk_widget_set_style(GTK_BUTTON(m_widget
)->child
, m_widgetStyle
); 
 149 bool wxToggleButton::IsOwnGtkWindow(GdkWindow 
*window
) 
 151     return (window 
== GTK_TOGGLE_BUTTON(m_widget
)->event_window
); 
 154 void wxToggleButton::OnInternalIdle() 
 156     wxCursor cursor 
= m_cursor
; 
 158     if (g_globalCursor
.Ok()) 
 159         cursor 
= g_globalCursor
; 
 161     if (GTK_TOGGLE_BUTTON(m_widget
)->event_window 
&& cursor
.Ok()) { 
 162       /* I now set the cursor the anew in every OnInternalIdle call 
 163          as setting the cursor in a parent window also effects the 
 164          windows above so that checking for the current cursor is 
 167         gdk_window_set_cursor(GTK_TOGGLE_BUTTON(m_widget
)->event_window
, 
 174 // wxSize DoGetBestSize() const 
 175 // Get the "best" size for this control. 
 176 wxSize 
wxToggleButton::DoGetBestSize() const 
 178     wxSize 
ret(wxControl::DoGetBestSize()); 
 180     if (!HasFlag(wxBU_EXACTFIT
)) 
 182         if (ret
.x 
< 80) ret
.x 
= 80; 
 189 #endif // wxUSE_TOGGLEBTN