| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: slider.cpp |
| 3 | // Purpose: |
| 4 | // Author: Robert Roebling |
| 5 | // Id: $Id$ |
| 6 | // Copyright: (c) 1998 Robert Roebling |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | #ifdef __GNUG__ |
| 11 | #pragma implementation "slider.h" |
| 12 | #endif |
| 13 | |
| 14 | #include "wx/slider.h" |
| 15 | |
| 16 | #if wxUSE_SLIDER |
| 17 | |
| 18 | #include "wx/utils.h" |
| 19 | |
| 20 | #include <math.h> |
| 21 | |
| 22 | #include <gdk/gdk.h> |
| 23 | #include <gtk/gtk.h> |
| 24 | |
| 25 | //----------------------------------------------------------------------------- |
| 26 | // idle system |
| 27 | //----------------------------------------------------------------------------- |
| 28 | |
| 29 | extern void wxapp_install_idle_handler(); |
| 30 | extern bool g_isIdle; |
| 31 | |
| 32 | //----------------------------------------------------------------------------- |
| 33 | // data |
| 34 | //----------------------------------------------------------------------------- |
| 35 | |
| 36 | extern bool g_blockEventsOnDrag; |
| 37 | |
| 38 | static const float sensitivity = 0.02; |
| 39 | |
| 40 | //----------------------------------------------------------------------------- |
| 41 | // "value_changed" |
| 42 | //----------------------------------------------------------------------------- |
| 43 | |
| 44 | static void gtk_slider_callback( GtkAdjustment *adjust, wxSlider *win ) |
| 45 | { |
| 46 | if (g_isIdle) wxapp_install_idle_handler(); |
| 47 | |
| 48 | if (!win->m_hasVMT) return; |
| 49 | if (g_blockEventsOnDrag) return; |
| 50 | |
| 51 | float diff = adjust->value - win->m_oldPos; |
| 52 | if (fabs(diff) < sensitivity) return; |
| 53 | |
| 54 | win->m_oldPos = adjust->value; |
| 55 | |
| 56 | GtkRange *range = GTK_RANGE( win->m_widget ); |
| 57 | |
| 58 | wxEventType command = wxEVT_SCROLL_THUMBTRACK; |
| 59 | if (range->scroll_type == GTK_SCROLL_STEP_BACKWARD) command = wxEVT_SCROLL_LINEUP; |
| 60 | else if (range->scroll_type == GTK_SCROLL_STEP_FORWARD) command = wxEVT_SCROLL_LINEDOWN; |
| 61 | else if (range->scroll_type == GTK_SCROLL_PAGE_BACKWARD) command = wxEVT_SCROLL_PAGEUP; |
| 62 | else if (range->scroll_type == GTK_SCROLL_PAGE_FORWARD) command = wxEVT_SCROLL_PAGEDOWN; |
| 63 | |
| 64 | double dvalue = adjust->value; |
| 65 | int value = (int)(dvalue >= 0 ? dvalue - 0.5 : dvalue + 0.5); |
| 66 | |
| 67 | int orient = wxHORIZONTAL; |
| 68 | if ( (win->GetWindowStyleFlag() & wxSB_VERTICAL) == wxSB_VERTICAL) |
| 69 | orient = wxVERTICAL; |
| 70 | |
| 71 | wxScrollEvent event( command, win->GetId(), value, orient ); |
| 72 | event.SetEventObject( win ); |
| 73 | win->GetEventHandler()->ProcessEvent( event ); |
| 74 | |
| 75 | wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, win->GetId() ); |
| 76 | cevent.SetEventObject( win ); |
| 77 | cevent.SetInt( value ); |
| 78 | win->GetEventHandler()->ProcessEvent( cevent ); |
| 79 | } |
| 80 | |
| 81 | //----------------------------------------------------------------------------- |
| 82 | // wxSlider |
| 83 | //----------------------------------------------------------------------------- |
| 84 | |
| 85 | IMPLEMENT_DYNAMIC_CLASS(wxSlider,wxControl) |
| 86 | |
| 87 | bool wxSlider::Create(wxWindow *parent, wxWindowID id, |
| 88 | int value, int minValue, int maxValue, |
| 89 | const wxPoint& pos, const wxSize& size, |
| 90 | long style, const wxValidator& validator, const wxString& name ) |
| 91 | { |
| 92 | m_acceptsFocus = TRUE; |
| 93 | m_needParent = TRUE; |
| 94 | |
| 95 | if (!PreCreation( parent, pos, size ) || |
| 96 | !CreateBase( parent, id, pos, size, style, validator, name )) |
| 97 | { |
| 98 | wxFAIL_MSG( wxT("wxSlider creation failed") ); |
| 99 | return FALSE; |
| 100 | } |
| 101 | |
| 102 | m_oldPos = 0.0; |
| 103 | |
| 104 | if (style & wxSL_VERTICAL) |
| 105 | m_widget = gtk_vscale_new( (GtkAdjustment *) NULL ); |
| 106 | else |
| 107 | m_widget = gtk_hscale_new( (GtkAdjustment *) NULL ); |
| 108 | |
| 109 | if (style & wxSL_LABELS) |
| 110 | { |
| 111 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), TRUE ); |
| 112 | gtk_scale_set_digits( GTK_SCALE( m_widget ), 0 ); |
| 113 | |
| 114 | /* labels need more space and too small window will |
| 115 | cause junk to appear on the dialog */ |
| 116 | if (style & wxSL_VERTICAL) |
| 117 | { |
| 118 | wxSize sz( size ); |
| 119 | if (sz.x < 35) |
| 120 | { |
| 121 | sz.x = 35; |
| 122 | SetSize( sz ); |
| 123 | } |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | wxSize sz( size ); |
| 128 | if (sz.y < 35) |
| 129 | { |
| 130 | sz.y = 35; |
| 131 | SetSize( sz ); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | else |
| 136 | gtk_scale_set_draw_value( GTK_SCALE( m_widget ), FALSE ); |
| 137 | |
| 138 | m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) ); |
| 139 | |
| 140 | gtk_signal_connect( GTK_OBJECT(m_adjust), |
| 141 | "value_changed", |
| 142 | (GtkSignalFunc) gtk_slider_callback, |
| 143 | (gpointer) this ); |
| 144 | |
| 145 | SetRange( minValue, maxValue ); |
| 146 | SetValue( value ); |
| 147 | |
| 148 | m_parent->DoAddChild( this ); |
| 149 | |
| 150 | PostCreation(); |
| 151 | |
| 152 | SetBackgroundColour( parent->GetBackgroundColour() ); |
| 153 | |
| 154 | Show( TRUE ); |
| 155 | |
| 156 | return TRUE; |
| 157 | } |
| 158 | |
| 159 | int wxSlider::GetValue() const |
| 160 | { |
| 161 | // we want to round to the nearest integer, i.e. 0.9 is rounded to 1 and |
| 162 | // -0.9 is rounded to -1 |
| 163 | double val = m_adjust->value; |
| 164 | return (int)(val >= 0 ? val - 0.5 : val + 0.5); |
| 165 | } |
| 166 | |
| 167 | void wxSlider::SetValue( int value ) |
| 168 | { |
| 169 | float fpos = (float)value; |
| 170 | m_oldPos = fpos; |
| 171 | if (fabs(fpos-m_adjust->value) < 0.2) return; |
| 172 | |
| 173 | m_adjust->value = fpos; |
| 174 | |
| 175 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); |
| 176 | } |
| 177 | |
| 178 | void wxSlider::SetRange( int minValue, int maxValue ) |
| 179 | { |
| 180 | float fmin = (float)minValue; |
| 181 | float fmax = (float)maxValue; |
| 182 | |
| 183 | if ((fabs(fmin-m_adjust->lower) < 0.2) && |
| 184 | (fabs(fmax-m_adjust->upper) < 0.2)) |
| 185 | { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | m_adjust->lower = fmin; |
| 190 | m_adjust->upper = fmax; |
| 191 | m_adjust->step_increment = 1.0; |
| 192 | m_adjust->page_increment = ceil((fmax-fmin) / 10.0); |
| 193 | |
| 194 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
| 195 | } |
| 196 | |
| 197 | int wxSlider::GetMin() const |
| 198 | { |
| 199 | return (int)ceil(m_adjust->lower); |
| 200 | } |
| 201 | |
| 202 | int wxSlider::GetMax() const |
| 203 | { |
| 204 | return (int)ceil(m_adjust->upper); |
| 205 | } |
| 206 | |
| 207 | void wxSlider::SetPageSize( int pageSize ) |
| 208 | { |
| 209 | float fpage = (float)pageSize; |
| 210 | |
| 211 | if (fabs(fpage-m_adjust->page_increment) < 0.2) return; |
| 212 | |
| 213 | m_adjust->page_increment = fpage; |
| 214 | |
| 215 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
| 216 | } |
| 217 | |
| 218 | int wxSlider::GetPageSize() const |
| 219 | { |
| 220 | return (int)ceil(m_adjust->page_increment); |
| 221 | } |
| 222 | |
| 223 | void wxSlider::SetThumbLength( int len ) |
| 224 | { |
| 225 | float flen = (float)len; |
| 226 | |
| 227 | if (fabs(flen-m_adjust->page_size) < 0.2) return; |
| 228 | |
| 229 | m_adjust->page_size = flen; |
| 230 | |
| 231 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
| 232 | } |
| 233 | |
| 234 | int wxSlider::GetThumbLength() const |
| 235 | { |
| 236 | return (int)ceil(m_adjust->page_size); |
| 237 | } |
| 238 | |
| 239 | void wxSlider::SetLineSize( int WXUNUSED(lineSize) ) |
| 240 | { |
| 241 | } |
| 242 | |
| 243 | int wxSlider::GetLineSize() const |
| 244 | { |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | void wxSlider::SetTick( int WXUNUSED(tickPos) ) |
| 249 | { |
| 250 | } |
| 251 | |
| 252 | void wxSlider::SetTickFreq( int WXUNUSED(n), int WXUNUSED(pos) ) |
| 253 | { |
| 254 | } |
| 255 | |
| 256 | int wxSlider::GetTickFreq() const |
| 257 | { |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | void wxSlider::ClearTicks() |
| 262 | { |
| 263 | } |
| 264 | |
| 265 | void wxSlider::SetSelection( int WXUNUSED(minPos), int WXUNUSED(maxPos) ) |
| 266 | { |
| 267 | } |
| 268 | |
| 269 | int wxSlider::GetSelEnd() const |
| 270 | { |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | int wxSlider::GetSelStart() const |
| 275 | { |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | void wxSlider::ClearSel() |
| 280 | { |
| 281 | } |
| 282 | |
| 283 | bool wxSlider::IsOwnGtkWindow( GdkWindow *window ) |
| 284 | { |
| 285 | GtkRange *range = GTK_RANGE(m_widget); |
| 286 | return ( (window == GTK_WIDGET(range)->window) || |
| 287 | (window == range->trough) || |
| 288 | (window == range->slider) || |
| 289 | (window == range->step_forw) || |
| 290 | (window == range->step_back) ); |
| 291 | } |
| 292 | |
| 293 | void wxSlider::ApplyWidgetStyle() |
| 294 | { |
| 295 | SetWidgetStyle(); |
| 296 | gtk_widget_set_style( m_widget, m_widgetStyle ); |
| 297 | } |
| 298 | |
| 299 | #endif |