]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/spinctrl.cpp
Committing in .
[wxWidgets.git] / src / gtk / spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: spinbutt.cpp
3 // Purpose: wxSpinCtrl
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 "spinctrl.h"
13 #endif
14
15 #include "wx/spinctrl.h"
16
17 #if wxUSE_SPINCTRL
18
19 #include "wx/utils.h"
20
21 #include <math.h>
22
23 #include <gdk/gdk.h>
24 #include <gtk/gtk.h>
25
26 //-----------------------------------------------------------------------------
27 // idle system
28 //-----------------------------------------------------------------------------
29
30 extern void wxapp_install_idle_handler();
31 extern bool g_isIdle;
32
33 static const float sensitivity = 0.02;
34
35 //-----------------------------------------------------------------------------
36 // data
37 //-----------------------------------------------------------------------------
38
39 extern bool g_blockEventsOnDrag;
40
41 //-----------------------------------------------------------------------------
42 // "value_changed"
43 //-----------------------------------------------------------------------------
44
45 static void gtk_spinctrl_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win )
46 {
47 if (g_isIdle) wxapp_install_idle_handler();
48
49 if (!win->m_hasVMT) return;
50 if (g_blockEventsOnDrag) return;
51
52 wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
53 event.SetEventObject( win );
54 event.SetInt( win->GetValue() );
55 win->GetEventHandler()->ProcessEvent( event );
56 }
57
58 //-----------------------------------------------------------------------------
59 // wxSpinCtrl
60 //-----------------------------------------------------------------------------
61
62 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl)
63
64 BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
65 EVT_CHAR(wxSpinCtrl::OnChar)
66 END_EVENT_TABLE()
67
68 bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
69 const wxString& value,
70 const wxPoint& pos, const wxSize& size,
71 long style,
72 int min, int max, int initial,
73 const wxString& name)
74 {
75 m_needParent = TRUE;
76 m_acceptsFocus = TRUE;
77
78 if (!PreCreation( parent, pos, size ) ||
79 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
80 {
81 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
82 return FALSE;
83 }
84
85 m_oldPos = initial;
86
87 m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0);
88
89 m_widget = gtk_spin_button_new( m_adjust, 1, 0 );
90
91 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
92 (int)(m_windowStyle & wxSP_WRAP) );
93
94 GtkEnableEvents();
95
96 m_parent->DoAddChild( this );
97
98 PostCreation();
99
100 SetFont( parent->GetFont() );
101
102 wxSize size_best( DoGetBestSize() );
103 wxSize new_size( size );
104 if (new_size.x == -1)
105 new_size.x = size_best.x;
106 if (new_size.y == -1)
107 new_size.y = size_best.y;
108 if (new_size.y > size_best.y)
109 new_size.y = size_best.y;
110 if ((new_size.x != size.x) || (new_size.y != size.y))
111 SetSize( new_size.x, new_size.y );
112
113 SetBackgroundColour( parent->GetBackgroundColour() );
114
115 SetValue( value );
116
117 Show( TRUE );
118
119 return TRUE;
120 }
121
122 void wxSpinCtrl::GtkDisableEvents()
123 {
124 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
125 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
126 (gpointer) this );
127
128 }
129
130 void wxSpinCtrl::GtkEnableEvents()
131 {
132 gtk_signal_connect( GTK_OBJECT (m_adjust),
133 "value_changed",
134 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
135 (gpointer) this );
136 }
137
138 int wxSpinCtrl::GetMin() const
139 {
140 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
141
142 return (int)ceil(m_adjust->lower);
143 }
144
145 int wxSpinCtrl::GetMax() const
146 {
147 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
148
149 return (int)ceil(m_adjust->upper);
150 }
151
152 int wxSpinCtrl::GetValue() const
153 {
154 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
155
156 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
157
158 return (int)ceil(m_adjust->value);
159 }
160
161 void wxSpinCtrl::SetValue( const wxString& value )
162 {
163 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
164
165 int n;
166 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
167 {
168 // a number - set it
169 SetValue(n);
170 }
171 else
172 {
173 // invalid number - set text as is (wxMSW compatible)
174 GtkDisableEvents();
175 gtk_entry_set_text( GTK_ENTRY(m_widget), value.mbc_str() );
176 GtkEnableEvents();
177 }
178 }
179
180 void wxSpinCtrl::SetValue( int value )
181 {
182 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
183
184 float fpos = (float)value;
185 m_oldPos = fpos;
186 if (fabs(fpos-m_adjust->value) < sensitivity) return;
187
188 m_adjust->value = fpos;
189
190 GtkDisableEvents();
191 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
192 GtkEnableEvents();
193 }
194
195 void wxSpinCtrl::SetRange(int minVal, int maxVal)
196 {
197 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
198
199 float fmin = (float)minVal;
200 float fmax = (float)maxVal;
201
202 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
203 (fabs(fmax-m_adjust->upper) < sensitivity))
204 {
205 return;
206 }
207
208 m_adjust->lower = fmin;
209 m_adjust->upper = fmax;
210
211 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
212
213 // these two calls are required due to some bug in GTK
214 Refresh();
215 SetFocus();
216 }
217
218 void wxSpinCtrl::OnChar( wxKeyEvent &event )
219 {
220 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
221
222 if (event.KeyCode() == WXK_RETURN)
223 {
224 wxWindow *top_frame = m_parent;
225 while (top_frame->GetParent() && !(top_frame->GetParent()->IsTopLevel()))
226 top_frame = top_frame->GetParent();
227 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
228
229 if (window->default_widget)
230 {
231 gtk_widget_activate (window->default_widget);
232 return;
233 }
234 }
235
236 event.Skip();
237 }
238
239 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
240 {
241 return GTK_SPIN_BUTTON(m_widget)->panel == window;
242 }
243
244 void wxSpinCtrl::ApplyWidgetStyle()
245 {
246 SetWidgetStyle();
247 gtk_widget_set_style( m_widget, m_widgetStyle );
248 }
249
250 wxSize wxSpinCtrl::DoGetBestSize() const
251 {
252 wxSize ret( wxControl::DoGetBestSize() );
253 return wxSize(95, ret.y);
254 }
255
256 #endif
257 // wxUSE_SPINCTRL