]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/spinbutt.cpp
   1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     wxSpinButton 
   7 // Copyright:   (c) Robert Roebling 
   8 // Licence:     wxWindows licence 
   9 ///////////////////////////////////////////////////////////////////////////// 
  11 // For compilers that support precompilation, includes "wx.h". 
  12 #include "wx/wxprec.h" 
  14 #include "wx/spinbutt.h" 
  20 #include "wx/gtk/private.h" 
  22 //----------------------------------------------------------------------------- 
  24 //----------------------------------------------------------------------------- 
  26 extern bool   g_blockEventsOnDrag
; 
  28 static const float sensitivity 
= 0.02; 
  30 //----------------------------------------------------------------------------- 
  32 //----------------------------------------------------------------------------- 
  35 static void gtk_spinbutt_callback( GtkWidget 
*WXUNUSED(widget
), wxSpinButton 
*win 
) 
  37     if (g_isIdle
) wxapp_install_idle_handler(); 
  39     if (!win
->m_hasVMT
) return; 
  40     if (g_blockEventsOnDrag
) return; 
  42     float diff 
= win
->m_adjust
->value 
- win
->m_oldPos
; 
  43     if (fabs(diff
) < sensitivity
) return; 
  45     wxEventType command 
= wxEVT_NULL
; 
  47     float line_step 
= win
->m_adjust
->step_increment
; 
  49     if (fabs(diff
-line_step
) < sensitivity
) command 
= wxEVT_SCROLL_LINEUP
; 
  50     else if (fabs(diff
+line_step
) < sensitivity
) command 
= wxEVT_SCROLL_LINEDOWN
; 
  51     else command 
= wxEVT_SCROLL_THUMBTRACK
; 
  53     int value 
= (int)ceil(win
->m_adjust
->value
); 
  55     wxSpinEvent 
event( command
, win
->GetId()); 
  56     event
.SetPosition( value 
); 
  57     event
.SetEventObject( win 
); 
  59     if ((win
->GetEventHandler()->ProcessEvent( event 
)) && 
  62         /* program has vetoed */ 
  63         win
->m_adjust
->value 
= win
->m_oldPos
; 
  65         g_signal_handlers_disconnect_by_func (win
->m_adjust
, 
  66                                               (gpointer
) gtk_spinbutt_callback
, 
  69         g_signal_emit_by_name (win
->m_adjust
, "value_changed"); 
  71         g_signal_connect (win
->m_adjust
, "value_changed", 
  72                           G_CALLBACK (gtk_spinbutt_callback
), win
); 
  76     win
->m_oldPos 
= win
->m_adjust
->value
; 
  78     /* always send a thumbtrack event */ 
  79     if (command 
!= wxEVT_SCROLL_THUMBTRACK
) 
  81         command 
= wxEVT_SCROLL_THUMBTRACK
; 
  82         wxSpinEvent 
event2( command
, win
->GetId()); 
  83         event2
.SetPosition( value 
); 
  84         event2
.SetEventObject( win 
); 
  85         win
->GetEventHandler()->ProcessEvent( event2 
); 
  90 //----------------------------------------------------------------------------- 
  92 //----------------------------------------------------------------------------- 
  94 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
,wxControl
) 
  95 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxNotifyEvent
) 
  97 BEGIN_EVENT_TABLE(wxSpinButton
, wxControl
) 
  98     EVT_SIZE(wxSpinButton::OnSize
) 
 101 bool wxSpinButton::Create(wxWindow 
*parent
, 
 106                           const wxString
& name
) 
 110     wxSize new_size 
= size
, 
 111            sizeBest 
= DoGetBestSize(); 
 112     new_size
.x 
= sizeBest
.x
;            // override width always 
 113     if (new_size
.y 
== -1) 
 114         new_size
.y 
= sizeBest
.y
; 
 116     if (!PreCreation( parent
, pos
, new_size 
) || 
 117         !CreateBase( parent
, id
, pos
, new_size
, style
, wxDefaultValidator
, name 
)) 
 119         wxFAIL_MSG( wxT("wxXX creation failed") ); 
 125     m_adjust 
= (GtkAdjustment
*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0); 
 127     m_widget 
= gtk_spin_button_new( m_adjust
, 0, 0 ); 
 129     gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
), 
 130                               (int)(m_windowStyle 
& wxSP_WRAP
) ); 
 132     g_signal_connect (m_adjust
, "value_changed", 
 133                       G_CALLBACK (gtk_spinbutt_callback
), this); 
 135     m_parent
->DoAddChild( this ); 
 137     PostCreation(new_size
); 
 142 int wxSpinButton::GetMin() const 
 144     wxCHECK_MSG( (m_widget 
!= NULL
), 0, wxT("invalid spin button") ); 
 146     return (int)ceil(m_adjust
->lower
); 
 149 int wxSpinButton::GetMax() const 
 151     wxCHECK_MSG( (m_widget 
!= NULL
), 0, wxT("invalid spin button") ); 
 153     return (int)ceil(m_adjust
->upper
); 
 156 int wxSpinButton::GetValue() const 
 158     wxCHECK_MSG( (m_widget 
!= NULL
), 0, wxT("invalid spin button") ); 
 160     return (int)ceil(m_adjust
->value
); 
 163 void wxSpinButton::SetValue( int value 
) 
 165     wxCHECK_RET( (m_widget 
!= NULL
), wxT("invalid spin button") ); 
 167     float fpos 
= (float)value
; 
 169     if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return; 
 171     m_adjust
->value 
= fpos
; 
 173     g_signal_emit_by_name (m_adjust
, "value_changed"); 
 176 void wxSpinButton::SetRange(int minVal
, int maxVal
) 
 178     wxCHECK_RET( (m_widget 
!= NULL
), wxT("invalid spin button") ); 
 180     float fmin 
= (float)minVal
; 
 181     float fmax 
= (float)maxVal
; 
 183     if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) && 
 184         (fabs(fmax
-m_adjust
->upper
) < sensitivity
)) 
 189     m_adjust
->lower 
= fmin
; 
 190     m_adjust
->upper 
= fmax
; 
 192     g_signal_emit_by_name (m_adjust
, "changed"); 
 194     // these two calls are required due to some bug in GTK 
 199 void wxSpinButton::OnSize( wxSizeEvent 
&WXUNUSED(event
) ) 
 201     wxCHECK_RET( (m_widget 
!= NULL
), wxT("invalid spin button") ); 
 203     m_width 
= DoGetBestSize().x
; 
 204     gtk_widget_set_size_request( m_widget
, m_width
, m_height 
); 
 207 bool wxSpinButton::IsOwnGtkWindow( GdkWindow 
*window 
) 
 209     return GTK_SPIN_BUTTON(m_widget
)->panel 
== window
; 
 212 wxSize 
wxSpinButton::DoGetBestSize() const 
 214     wxSize 
best(15, 26); // FIXME 
 221 wxSpinButton::GetClassDefaultAttributes(wxWindowVariant 
WXUNUSED(variant
)) 
 223     // TODO: overload to accept functions like gtk_spin_button_new? 
 224     // Until then use a similar type 
 225     return GetDefaultAttributesFromGTKWidget(gtk_button_new
);