]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/spinctrl.cpp
Fixes to get library to compile
[wxWidgets.git] / src / gtk1 / 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 wxSize new_size = size,
92 sizeBest = DoGetBestSize();
93 if (new_size.x == -1)
94 new_size.x = sizeBest.x;
95 if (new_size.y == -1)
96 new_size.y = sizeBest.y;
97
98 if ((new_size.x != size.x) || (new_size.y != size.y))
99 SetSize( new_size.x, new_size.y );
100
101 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
102 (int)(m_windowStyle & wxSP_WRAP) );
103
104 GtkEnableEvents();
105 m_parent->DoAddChild( this );
106
107 PostCreation();
108
109 SetBackgroundColour( parent->GetBackgroundColour() );
110
111 SetValue( value );
112
113 Show( TRUE );
114
115 return TRUE;
116 }
117
118 void wxSpinCtrl::GtkDisableEvents()
119 {
120 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
121 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
122 (gpointer) this );
123
124 }
125
126 void wxSpinCtrl::GtkEnableEvents()
127 {
128 gtk_signal_connect( GTK_OBJECT (m_adjust),
129 "value_changed",
130 GTK_SIGNAL_FUNC(gtk_spinctrl_callback),
131 (gpointer) this );
132 }
133
134 int wxSpinCtrl::GetMin() const
135 {
136 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
137
138 return (int)ceil(m_adjust->lower);
139 }
140
141 int wxSpinCtrl::GetMax() const
142 {
143 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
144
145 return (int)ceil(m_adjust->upper);
146 }
147
148 int wxSpinCtrl::GetValue() const
149 {
150 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
151
152 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
153
154 return (int)ceil(m_adjust->value);
155 }
156
157 void wxSpinCtrl::SetValue( const wxString& value )
158 {
159 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
160
161 int n;
162 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
163 {
164 // a number - set it
165 SetValue(n);
166 }
167 else
168 {
169 // invalid number - set text as is (wxMSW compatible)
170 GtkDisableEvents();
171 gtk_entry_set_text( GTK_ENTRY(m_widget), value.mbc_str() );
172 GtkEnableEvents();
173 }
174 }
175
176 void wxSpinCtrl::SetValue( int value )
177 {
178 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
179
180 float fpos = (float)value;
181 m_oldPos = fpos;
182 if (fabs(fpos-m_adjust->value) < sensitivity) return;
183
184 m_adjust->value = fpos;
185
186 GtkDisableEvents();
187 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
188 GtkEnableEvents();
189 }
190
191 void wxSpinCtrl::SetRange(int minVal, int maxVal)
192 {
193 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
194
195 float fmin = (float)minVal;
196 float fmax = (float)maxVal;
197
198 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
199 (fabs(fmax-m_adjust->upper) < sensitivity))
200 {
201 return;
202 }
203
204 m_adjust->lower = fmin;
205 m_adjust->upper = fmax;
206
207 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
208
209 // these two calls are required due to some bug in GTK
210 Refresh();
211 SetFocus();
212 }
213
214 void wxSpinCtrl::OnChar( wxKeyEvent &event )
215 {
216 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
217
218 if (event.KeyCode() == WXK_RETURN)
219 {
220 wxWindow *top_frame = m_parent;
221 while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame))
222 top_frame = top_frame->GetParent();
223 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
224
225 if (window->default_widget)
226 {
227 gtk_widget_activate (window->default_widget);
228 return;
229 }
230 }
231
232 event.Skip();
233 }
234
235 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
236 {
237 return GTK_SPIN_BUTTON(m_widget)->panel == window;
238 }
239
240 void wxSpinCtrl::ApplyWidgetStyle()
241 {
242 SetWidgetStyle();
243 gtk_widget_set_style( m_widget, m_widgetStyle );
244 }
245
246 wxSize wxSpinCtrl::DoGetBestSize() const
247 {
248 wxSize ret( wxControl::DoGetBestSize() );
249 return wxSize(95, ret.y);
250 }
251
252 #endif
253 // wxUSE_SPINCTRL