warnings suppressed
[wxWidgets.git] / src / gtk1 / 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 #include "wx/utils.h"
17 #include <math.h>
18
19 #include "gdk/gdk.h"
20 #include "gtk/gtk.h"
21
22 //-----------------------------------------------------------------------------
23 // data
24 //-----------------------------------------------------------------------------
25
26 extern bool g_blockEventsOnDrag;
27
28 static const float sensitivity = 0.2;
29
30 //-----------------------------------------------------------------------------
31 // "value_changed"
32 //-----------------------------------------------------------------------------
33
34 static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win )
35 {
36 if (!win->HasVMT()) return;
37 if (g_blockEventsOnDrag) return;
38
39 float diff = win->m_adjust->value - win->m_oldPos;
40 if (fabs(diff) < sensitivity) return;
41 win->m_oldPos = win->m_adjust->value;
42
43 wxEventType command = wxEVT_NULL;
44
45 float line_step = win->m_adjust->step_increment;
46 float page_step = win->m_adjust->page_increment;
47
48 if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN;
49 else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP;
50 else if (fabs(diff-page_step) < sensitivity) command = wxEVT_SCROLL_PAGEDOWN;
51 else if (fabs(diff+page_step) < sensitivity) command = wxEVT_SCROLL_PAGEUP;
52 else command = wxEVT_SCROLL_THUMBTRACK;
53
54 int value = (int)ceil(win->m_adjust->value);
55
56 wxSpinEvent event( command, win->GetId());
57 event.SetPosition( value );
58 event.SetOrientation( wxVERTICAL );
59 event.SetEventObject( win );
60
61 win->GetEventHandler()->ProcessEvent( event );
62 }
63
64 //-----------------------------------------------------------------------------
65 // wxSpinButton
66 //-----------------------------------------------------------------------------
67
68 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
69
70 BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
71 EVT_SIZE(wxSpinButton::OnSize)
72 END_EVENT_TABLE()
73
74 wxSpinButton::wxSpinButton()
75 {
76 }
77
78 bool wxSpinButton::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
79 long style, const wxString& name)
80 {
81 m_needParent = TRUE;
82
83 wxSize new_size = size;
84 new_size.x = 16;
85 if (new_size.y == -1)
86 new_size.y = 30;
87
88 PreCreation( parent, id, pos, new_size, style, name );
89
90 // SetValidator( validator );
91
92 m_oldPos = 0.0;
93
94 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
95
96 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
97
98 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), (m_windowStyle & wxSP_WRAP) );
99
100 gtk_signal_connect( GTK_OBJECT (m_adjust),
101 "value_changed",
102 (GtkSignalFunc) gtk_spinbutt_callback,
103 (gpointer) this );
104
105 m_parent->AddChild( this );
106
107 (m_parent->m_insertCallback)( m_parent, this );
108
109 PostCreation();
110
111 SetBackgroundColour( parent->GetBackgroundColour() );
112
113 Show( TRUE );
114
115 return TRUE;
116 }
117
118 wxSpinButton::~wxSpinButton()
119 {
120 }
121
122 int wxSpinButton::GetMin() const
123 {
124 wxCHECK_MSG( (m_widget != NULL), 0, "invalid spin button" );
125
126 return (int)ceil(m_adjust->lower);
127 }
128
129 int wxSpinButton::GetMax() const
130 {
131 wxCHECK_MSG( (m_widget != NULL), 0, "invalid spin button" );
132
133 return (int)ceil(m_adjust->upper);
134 }
135
136 int wxSpinButton::GetValue() const
137 {
138 wxCHECK_MSG( (m_widget != NULL), 0, "invalid spin button" );
139
140 return (int)ceil(m_adjust->value);
141 }
142
143 void wxSpinButton::SetValue( int value )
144 {
145 wxCHECK_RET( (m_widget != NULL), "invalid spin button" );
146
147 float fpos = (float)value;
148 m_oldPos = fpos;
149 if (fabs(fpos-m_adjust->value) < sensitivity) return;
150
151 m_adjust->value = fpos;
152
153 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
154 }
155
156 void wxSpinButton::SetRange(int minVal, int maxVal)
157 {
158 wxCHECK_RET( (m_widget != NULL), "invalid spin button" );
159
160 float fmin = (float)minVal;
161 float fmax = (float)maxVal;
162
163 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
164 (fabs(fmax-m_adjust->upper) < sensitivity))
165 {
166 return;
167 }
168
169 m_adjust->lower = fmin;
170 m_adjust->upper = fmax;
171
172 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
173 }
174
175 void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
176 {
177 wxCHECK_RET( (m_widget != NULL), "invalid spin button" );
178
179 m_width = 16;
180 gtk_widget_set_usize( m_widget, m_width, m_height );
181 }
182
183 bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
184 {
185 return GTK_SPIN_BUTTON(m_widget)->panel == window;
186 }
187
188 void wxSpinButton::ApplyWidgetStyle()
189 {
190 SetWidgetStyle();
191 gtk_widget_set_style( m_widget, m_widgetStyle );
192 }
193
194 //-----------------------------------------------------------------------------
195 // wxSpinEvent
196 //-----------------------------------------------------------------------------
197
198 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxScrollEvent)
199
200 wxSpinEvent::wxSpinEvent(wxEventType commandType, int id):
201 wxScrollEvent(commandType, id)
202 {
203 }
204
205