Small distrib updates,
[wxWidgets.git] / src / gtk / spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: spinbutt.cpp
3 // Purpose: wxSpinButton
4 // Author: Robert
5 // Modified by:
6 // RCS-ID: $Id$
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifdef __GNUG__
12 #pragma implementation "spinbutt.h"
13 #endif
14
15 #include "wx/spinbutt.h"
16
17 #ifdef wxUSE_SPINBTN
18
19 #include "wx/utils.h"
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.2;
39
40 //-----------------------------------------------------------------------------
41 // "value_changed"
42 //-----------------------------------------------------------------------------
43
44 static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *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 = win->m_adjust->value - win->m_oldPos;
52 if (fabs(diff) < sensitivity) return;
53 win->m_oldPos = win->m_adjust->value;
54
55 wxEventType command = wxEVT_NULL;
56
57 float line_step = win->m_adjust->step_increment;
58 float page_step = win->m_adjust->page_increment;
59
60 if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN;
61 else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP;
62 else if (fabs(diff-page_step) < sensitivity) command = wxEVT_SCROLL_PAGEDOWN;
63 else if (fabs(diff+page_step) < sensitivity) command = wxEVT_SCROLL_PAGEUP;
64 else command = wxEVT_SCROLL_THUMBTRACK;
65
66 int value = (int)ceil(win->m_adjust->value);
67
68 wxSpinEvent event( command, win->GetId());
69 event.SetPosition( value );
70 event.SetOrientation( wxVERTICAL );
71 event.SetEventObject( win );
72
73 win->GetEventHandler()->ProcessEvent( event );
74 }
75
76 //-----------------------------------------------------------------------------
77 // wxSpinButton
78 //-----------------------------------------------------------------------------
79
80 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
81 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxScrollEvent);
82
83 BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
84 EVT_SIZE(wxSpinButton::OnSize)
85 END_EVENT_TABLE()
86
87 bool wxSpinButton::Create(wxWindow *parent,
88 wxWindowID id,
89 const wxPoint& pos,
90 const wxSize& size,
91 long style,
92 const wxString& name)
93 {
94 m_needParent = TRUE;
95
96 wxSize new_size = size;
97 new_size.x = 15;
98 if (new_size.y == -1)
99 new_size.y = 30;
100
101 if (!PreCreation( parent, pos, size ) ||
102 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
103 {
104 wxFAIL_MSG( _T("wxXX creation failed") );
105 return FALSE;
106 }
107
108 m_oldPos = 0.0;
109
110 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
111
112 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
113
114 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), (m_windowStyle & wxSP_WRAP) );
115
116 gtk_signal_connect( GTK_OBJECT (m_adjust),
117 "value_changed",
118 (GtkSignalFunc) gtk_spinbutt_callback,
119 (gpointer) this );
120
121 m_parent->DoAddChild( this );
122
123 PostCreation();
124
125 SetBackgroundColour( parent->GetBackgroundColour() );
126
127 Show( TRUE );
128
129 return TRUE;
130 }
131
132 wxSpinButton::~wxSpinButton()
133 {
134 }
135
136 int wxSpinButton::GetMin() const
137 {
138 wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") );
139
140 return (int)ceil(m_adjust->lower);
141 }
142
143 int wxSpinButton::GetMax() const
144 {
145 wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") );
146
147 return (int)ceil(m_adjust->upper);
148 }
149
150 int wxSpinButton::GetValue() const
151 {
152 wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") );
153
154 return (int)ceil(m_adjust->value);
155 }
156
157 void wxSpinButton::SetValue( int value )
158 {
159 wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") );
160
161 float fpos = (float)value;
162 m_oldPos = fpos;
163 if (fabs(fpos-m_adjust->value) < sensitivity) return;
164
165 m_adjust->value = fpos;
166
167 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
168 }
169
170 void wxSpinButton::SetRange(int minVal, int maxVal)
171 {
172 wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") );
173
174 float fmin = (float)minVal;
175 float fmax = (float)maxVal;
176
177 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
178 (fabs(fmax-m_adjust->upper) < sensitivity))
179 {
180 return;
181 }
182
183 m_adjust->lower = fmin;
184 m_adjust->upper = fmax;
185
186 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
187 }
188
189 void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
190 {
191 wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") );
192
193 m_width = 15;
194 gtk_widget_set_usize( m_widget, m_width, m_height );
195 }
196
197 bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
198 {
199 return GTK_SPIN_BUTTON(m_widget)->panel == window;
200 }
201
202 void wxSpinButton::ApplyWidgetStyle()
203 {
204 SetWidgetStyle();
205 gtk_widget_set_style( m_widget, m_widgetStyle );
206 }
207
208 #endif