]>
Commit | Line | Data |
---|---|---|
738f9e5a RR |
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 | ||
0e0d8857 | 17 | #if wxUSE_SPINCTRL |
738f9e5a RR |
18 | |
19 | #include "wx/utils.h" | |
aec0ed2e | 20 | |
738f9e5a RR |
21 | #include <math.h> |
22 | ||
aec0ed2e RR |
23 | #include <gdk/gdk.h> |
24 | #include <gtk/gtk.h> | |
738f9e5a RR |
25 | |
26 | //----------------------------------------------------------------------------- | |
27 | // idle system | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | extern void wxapp_install_idle_handler(); | |
31 | extern bool g_isIdle; | |
32 | ||
0c623889 RR |
33 | static const float sensitivity = 0.02; |
34 | ||
738f9e5a RR |
35 | //----------------------------------------------------------------------------- |
36 | // data | |
37 | //----------------------------------------------------------------------------- | |
38 | ||
39 | extern bool g_blockEventsOnDrag; | |
40 | ||
738f9e5a RR |
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 | ||
58c837a4 | 52 | wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId()); |
738f9e5a | 53 | event.SetEventObject( win ); |
58c837a4 | 54 | event.SetInt( win->GetValue() ); |
738f9e5a | 55 | win->GetEventHandler()->ProcessEvent( event ); |
738f9e5a RR |
56 | } |
57 | ||
58 | //----------------------------------------------------------------------------- | |
59 | // wxSpinCtrl | |
60 | //----------------------------------------------------------------------------- | |
61 | ||
62 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl) | |
63 | ||
da048e3d RR |
64 | BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl) |
65 | EVT_CHAR(wxSpinCtrl::OnChar) | |
66 | END_EVENT_TABLE() | |
67 | ||
738f9e5a | 68 | bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id, |
ce89fdd2 VZ |
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) | |
738f9e5a RR |
74 | { |
75 | m_needParent = TRUE; | |
354aa1e3 | 76 | m_acceptsFocus = TRUE; |
738f9e5a | 77 | |
0279e844 RR |
78 | if (!PreCreation( parent, pos, size ) || |
79 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
738f9e5a RR |
80 | { |
81 | wxFAIL_MSG( wxT("wxSpinCtrl creation failed") ); | |
9750fc42 | 82 | return FALSE; |
738f9e5a RR |
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 ); | |
0279e844 | 90 | |
b02da6b1 VZ |
91 | gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), |
92 | (int)(m_windowStyle & wxSP_WRAP) ); | |
738f9e5a | 93 | |
07f5b19a | 94 | GtkEnableEvents(); |
db434467 | 95 | |
738f9e5a RR |
96 | m_parent->DoAddChild( this ); |
97 | ||
98 | PostCreation(); | |
99 | ||
db434467 RR |
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.x != size.x) || (new_size.y != size.y)) | |
109 | SetSize( new_size.x, new_size.y ); | |
110 | ||
738f9e5a RR |
111 | SetBackgroundColour( parent->GetBackgroundColour() ); |
112 | ||
ce89fdd2 VZ |
113 | SetValue( value ); |
114 | ||
738f9e5a RR |
115 | Show( TRUE ); |
116 | ||
117 | return TRUE; | |
118 | } | |
119 | ||
07f5b19a RR |
120 | void wxSpinCtrl::GtkDisableEvents() |
121 | { | |
122 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust), | |
123 | GTK_SIGNAL_FUNC(gtk_spinctrl_callback), | |
124 | (gpointer) this ); | |
125 | ||
126 | } | |
127 | ||
128 | void wxSpinCtrl::GtkEnableEvents() | |
129 | { | |
130 | gtk_signal_connect( GTK_OBJECT (m_adjust), | |
131 | "value_changed", | |
132 | GTK_SIGNAL_FUNC(gtk_spinctrl_callback), | |
133 | (gpointer) this ); | |
134 | } | |
135 | ||
738f9e5a RR |
136 | int wxSpinCtrl::GetMin() const |
137 | { | |
138 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
139 | ||
140 | return (int)ceil(m_adjust->lower); | |
141 | } | |
142 | ||
143 | int wxSpinCtrl::GetMax() const | |
144 | { | |
145 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
146 | ||
147 | return (int)ceil(m_adjust->upper); | |
148 | } | |
149 | ||
150 | int wxSpinCtrl::GetValue() const | |
151 | { | |
152 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
153 | ||
33720b2d RR |
154 | gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) ); |
155 | ||
738f9e5a RR |
156 | return (int)ceil(m_adjust->value); |
157 | } | |
158 | ||
ce89fdd2 VZ |
159 | void wxSpinCtrl::SetValue( const wxString& value ) |
160 | { | |
161 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
162 | ||
163 | int n; | |
164 | if ( (wxSscanf(value, wxT("%d"), &n) == 1) ) | |
165 | { | |
166 | // a number - set it | |
167 | SetValue(n); | |
168 | } | |
169 | else | |
170 | { | |
171 | // invalid number - set text as is (wxMSW compatible) | |
07f5b19a | 172 | GtkDisableEvents(); |
7dd62924 | 173 | gtk_entry_set_text( GTK_ENTRY(m_widget), value.mbc_str() ); |
07f5b19a | 174 | GtkEnableEvents(); |
ce89fdd2 VZ |
175 | } |
176 | } | |
177 | ||
738f9e5a RR |
178 | void wxSpinCtrl::SetValue( int value ) |
179 | { | |
180 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
181 | ||
182 | float fpos = (float)value; | |
183 | m_oldPos = fpos; | |
184 | if (fabs(fpos-m_adjust->value) < sensitivity) return; | |
185 | ||
186 | m_adjust->value = fpos; | |
187 | ||
07f5b19a | 188 | GtkDisableEvents(); |
738f9e5a | 189 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); |
07f5b19a | 190 | GtkEnableEvents(); |
738f9e5a RR |
191 | } |
192 | ||
193 | void wxSpinCtrl::SetRange(int minVal, int maxVal) | |
194 | { | |
195 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
196 | ||
197 | float fmin = (float)minVal; | |
198 | float fmax = (float)maxVal; | |
199 | ||
200 | if ((fabs(fmin-m_adjust->lower) < sensitivity) && | |
201 | (fabs(fmax-m_adjust->upper) < sensitivity)) | |
202 | { | |
203 | return; | |
204 | } | |
205 | ||
206 | m_adjust->lower = fmin; | |
207 | m_adjust->upper = fmax; | |
208 | ||
209 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); | |
0e0d8857 | 210 | |
738f9e5a RR |
211 | // these two calls are required due to some bug in GTK |
212 | Refresh(); | |
213 | SetFocus(); | |
214 | } | |
215 | ||
da048e3d RR |
216 | void wxSpinCtrl::OnChar( wxKeyEvent &event ) |
217 | { | |
218 | wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") ); | |
219 | ||
220 | if (event.KeyCode() == WXK_RETURN) | |
221 | { | |
222 | wxWindow *top_frame = m_parent; | |
223 | while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame)) | |
224 | top_frame = top_frame->GetParent(); | |
9750fc42 | 225 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); |
0e0d8857 | 226 | |
9750fc42 | 227 | if (window->default_widget) |
da048e3d RR |
228 | { |
229 | gtk_widget_activate (window->default_widget); | |
9750fc42 VZ |
230 | return; |
231 | } | |
da048e3d RR |
232 | } |
233 | ||
234 | event.Skip(); | |
235 | } | |
236 | ||
738f9e5a RR |
237 | bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window ) |
238 | { | |
239 | return GTK_SPIN_BUTTON(m_widget)->panel == window; | |
240 | } | |
241 | ||
242 | void wxSpinCtrl::ApplyWidgetStyle() | |
243 | { | |
244 | SetWidgetStyle(); | |
245 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
246 | } | |
247 | ||
9d9b7755 VZ |
248 | wxSize wxSpinCtrl::DoGetBestSize() const |
249 | { | |
0279e844 RR |
250 | wxSize ret( wxControl::DoGetBestSize() ); |
251 | return wxSize(95, ret.y); | |
9d9b7755 VZ |
252 | } |
253 | ||
738f9e5a | 254 | #endif |
aec0ed2e | 255 | // wxUSE_SPINCTRL |