Added zillions of #if wxUSE_XXX
[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
82 BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
83 EVT_SIZE(wxSpinButton::OnSize)
84 END_EVENT_TABLE()
85
86 wxSpinButton::wxSpinButton()
87 {
88 }
89
90 bool wxSpinButton::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
91 long style, const wxString& name)
92 {
93 m_needParent = TRUE;
94
95 wxSize new_size = size;
96 new_size.x = 15;
97 if (new_size.y == -1)
98 new_size.y = 30;
99
100 PreCreation( parent, id, pos, new_size, style, name );
101
102 // SetValidator( validator );
103
104 m_oldPos = 0.0;
105
106 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
107
108 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
109
110 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), (m_windowStyle & wxSP_WRAP) );
111
112 gtk_signal_connect( GTK_OBJECT (m_adjust),
113 "value_changed",
114 (GtkSignalFunc) gtk_spinbutt_callback,
115 (gpointer) this );
116
117 m_parent->DoAddChild( this );
118
119 PostCreation();
120
121 SetBackgroundColour( parent->GetBackgroundColour() );
122
123 Show( TRUE );
124
125 return TRUE;
126 }
127
128 wxSpinButton::~wxSpinButton()
129 {
130 }
131
132 int wxSpinButton::GetMin() const
133 {
134 wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") );
135
136 return (int)ceil(m_adjust->lower);
137 }
138
139 int wxSpinButton::GetMax() const
140 {
141 wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") );
142
143 return (int)ceil(m_adjust->upper);
144 }
145
146 int wxSpinButton::GetValue() const
147 {
148 wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") );
149
150 return (int)ceil(m_adjust->value);
151 }
152
153 void wxSpinButton::SetValue( int value )
154 {
155 wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") );
156
157 float fpos = (float)value;
158 m_oldPos = fpos;
159 if (fabs(fpos-m_adjust->value) < sensitivity) return;
160
161 m_adjust->value = fpos;
162
163 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
164 }
165
166 void wxSpinButton::SetRange(int minVal, int maxVal)
167 {
168 wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") );
169
170 float fmin = (float)minVal;
171 float fmax = (float)maxVal;
172
173 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
174 (fabs(fmax-m_adjust->upper) < sensitivity))
175 {
176 return;
177 }
178
179 m_adjust->lower = fmin;
180 m_adjust->upper = fmax;
181
182 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
183 }
184
185 void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
186 {
187 wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") );
188
189 m_width = 15;
190 gtk_widget_set_usize( m_widget, m_width, m_height );
191 }
192
193 bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
194 {
195 return GTK_SPIN_BUTTON(m_widget)->panel == window;
196 }
197
198 void wxSpinButton::ApplyWidgetStyle()
199 {
200 SetWidgetStyle();
201 gtk_widget_set_style( m_widget, m_widgetStyle );
202 }
203
204 //-----------------------------------------------------------------------------
205 // wxSpinEvent
206 //-----------------------------------------------------------------------------
207
208 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxScrollEvent)
209
210 wxSpinEvent::wxSpinEvent(wxEventType commandType, int id):
211 wxScrollEvent(commandType, id)
212 {
213 }
214
215 #endif