]>
Commit | Line | Data |
---|---|---|
738f9e5a | 1 | ///////////////////////////////////////////////////////////////////////////// |
8e13c1ec | 2 | // Name: src/gtk/spinbutt.cpp |
738f9e5a RR |
3 | // Purpose: wxSpinCtrl |
4 | // Author: Robert | |
5 | // Modified by: | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) Robert Roebling | |
65571936 | 8 | // Licence: wxWindows licence |
738f9e5a RR |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
14f355c2 VS |
11 | // For compilers that support precompilation, includes "wx.h". |
12 | #include "wx/wxprec.h" | |
13 | ||
0e0d8857 | 14 | #if wxUSE_SPINCTRL |
738f9e5a | 15 | |
de6185e2 WS |
16 | #include "wx/spinctrl.h" |
17 | ||
18 | #ifndef WX_PRECOMP | |
7f81dfa1 | 19 | #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED |
de6185e2 | 20 | #include "wx/utils.h" |
0cb7e05c | 21 | #include "wx/wxcrtvararg.h" |
de6185e2 | 22 | #endif |
aec0ed2e | 23 | |
e1910715 | 24 | #include "wx/gtk/private.h" |
738f9e5a | 25 | |
738f9e5a RR |
26 | //----------------------------------------------------------------------------- |
27 | // data | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | extern bool g_blockEventsOnDrag; | |
31 | ||
738f9e5a RR |
32 | //----------------------------------------------------------------------------- |
33 | // "value_changed" | |
34 | //----------------------------------------------------------------------------- | |
35 | ||
865bb325 | 36 | extern "C" { |
7f81dfa1 PC |
37 | static void |
38 | gtk_value_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win) | |
738f9e5a | 39 | { |
7f81dfa1 | 40 | win->m_pos = int(gtk_spin_button_get_value(spinbutton)); |
00dc9772 | 41 | if (!win->m_hasVMT || g_blockEventsOnDrag) |
7f81dfa1 | 42 | return; |
738f9e5a | 43 | |
58c837a4 | 44 | wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId()); |
738f9e5a | 45 | event.SetEventObject( win ); |
4477936a VZ |
46 | |
47 | // note that we don't use wxSpinCtrl::GetValue() here because it would | |
48 | // adjust the value to fit into the control range and this means that we | |
49 | // would never be able to enter an "invalid" value in the control, even | |
50 | // temporarily - and trying to enter 10 into the control which accepts the | |
51 | // values in range 5..50 is then, ummm, quite challenging (hint: you can't | |
52 | // enter 1!) (VZ) | |
7f81dfa1 | 53 | event.SetInt(win->m_pos); |
937013e0 | 54 | win->HandleWindowEvent( event ); |
738f9e5a | 55 | } |
865bb325 | 56 | } |
738f9e5a | 57 | |
0a07a7d8 RR |
58 | //----------------------------------------------------------------------------- |
59 | // "changed" | |
60 | //----------------------------------------------------------------------------- | |
61 | ||
865bb325 | 62 | extern "C" { |
0a07a7d8 | 63 | static void |
7f81dfa1 | 64 | gtk_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win) |
0a07a7d8 | 65 | { |
00dc9772 | 66 | if (!win->m_hasVMT) |
7f81dfa1 PC |
67 | return; |
68 | ||
0a07a7d8 RR |
69 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() ); |
70 | event.SetEventObject( win ); | |
cf5d5057 | 71 | event.SetString( GTK_ENTRY(spinbutton)->text ); |
3d257b8d | 72 | |
ea46eba0 | 73 | // see above |
7f81dfa1 | 74 | event.SetInt(win->m_pos); |
937013e0 | 75 | win->HandleWindowEvent( event ); |
0a07a7d8 | 76 | } |
865bb325 | 77 | } |
0a07a7d8 | 78 | |
738f9e5a RR |
79 | //----------------------------------------------------------------------------- |
80 | // wxSpinCtrl | |
81 | //----------------------------------------------------------------------------- | |
82 | ||
83 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl) | |
84 | ||
da048e3d RR |
85 | BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl) |
86 | EVT_CHAR(wxSpinCtrl::OnChar) | |
87 | END_EVENT_TABLE() | |
88 | ||
7f81dfa1 PC |
89 | wxSpinCtrl::wxSpinCtrl() |
90 | { | |
91 | m_pos = 0; | |
92 | } | |
93 | ||
738f9e5a | 94 | bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id, |
ce89fdd2 VZ |
95 | const wxString& value, |
96 | const wxPoint& pos, const wxSize& size, | |
97 | long style, | |
98 | int min, int max, int initial, | |
99 | const wxString& name) | |
738f9e5a | 100 | { |
0279e844 RR |
101 | if (!PreCreation( parent, pos, size ) || |
102 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
738f9e5a RR |
103 | { |
104 | wxFAIL_MSG( wxT("wxSpinCtrl creation failed") ); | |
8e13c1ec | 105 | return false; |
738f9e5a RR |
106 | } |
107 | ||
7f81dfa1 | 108 | m_widget = gtk_spin_button_new_with_range(min, max, 1); |
4a783bb4 RR |
109 | gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), initial); |
110 | m_pos = (int) gtk_spin_button_get_value( GTK_SPIN_BUTTON(m_widget)); | |
3d257b8d | 111 | |
b02da6b1 VZ |
112 | gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), |
113 | (int)(m_windowStyle & wxSP_WRAP) ); | |
738f9e5a | 114 | |
0bf36922 RR |
115 | g_signal_connect_after(m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this); |
116 | g_signal_connect_after(m_widget, "changed", G_CALLBACK(gtk_changed), this); | |
3d257b8d | 117 | |
738f9e5a RR |
118 | m_parent->DoAddChild( this ); |
119 | ||
abdeb9e7 | 120 | PostCreation(size); |
db434467 | 121 | |
7f81dfa1 PC |
122 | if (!value.empty()) |
123 | { | |
124 | SetValue(value); | |
125 | } | |
ce89fdd2 | 126 | |
8e13c1ec | 127 | return true; |
738f9e5a RR |
128 | } |
129 | ||
130 | int wxSpinCtrl::GetMin() const | |
131 | { | |
132 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
133 | ||
7f81dfa1 | 134 | double min; |
4a783bb4 | 135 | gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), &min, NULL); |
7f81dfa1 | 136 | return int(min); |
738f9e5a RR |
137 | } |
138 | ||
139 | int wxSpinCtrl::GetMax() const | |
140 | { | |
141 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
142 | ||
7f81dfa1 | 143 | double max; |
4a783bb4 | 144 | gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), NULL, &max); |
7f81dfa1 | 145 | return int(max); |
738f9e5a RR |
146 | } |
147 | ||
148 | int wxSpinCtrl::GetValue() const | |
149 | { | |
150 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
151 | ||
0bf36922 | 152 | GtkDisableEvents(); |
33720b2d | 153 | gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) ); |
301f6963 PC |
154 | wx_const_cast(wxSpinCtrl*, this)->m_pos = |
155 | int(gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget))); | |
0bf36922 | 156 | GtkEnableEvents(); |
33720b2d | 157 | |
7f81dfa1 | 158 | return m_pos; |
738f9e5a RR |
159 | } |
160 | ||
ce89fdd2 VZ |
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) | |
0bf36922 | 174 | GtkDisableEvents(); |
fab591c5 | 175 | gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) ); |
0bf36922 | 176 | GtkEnableEvents(); |
ce89fdd2 VZ |
177 | } |
178 | } | |
179 | ||
738f9e5a RR |
180 | void wxSpinCtrl::SetValue( int value ) |
181 | { | |
182 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
183 | ||
0bf36922 | 184 | GtkDisableEvents(); |
4a783bb4 RR |
185 | gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), value); |
186 | m_pos = (int) gtk_spin_button_get_value( GTK_SPIN_BUTTON(m_widget)); | |
0bf36922 | 187 | GtkEnableEvents(); |
738f9e5a RR |
188 | } |
189 | ||
f8f9ec55 VZ |
190 | void wxSpinCtrl::SetSelection(long from, long to) |
191 | { | |
77ffb593 | 192 | // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the |
f8f9ec55 VZ |
193 | // entire range |
194 | if ( from == -1 && to == -1 ) | |
195 | { | |
196 | from = 0; | |
197 | to = INT_MAX; | |
198 | } | |
199 | ||
200 | gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to ); | |
201 | } | |
202 | ||
738f9e5a RR |
203 | void wxSpinCtrl::SetRange(int minVal, int maxVal) |
204 | { | |
205 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
206 | ||
0bf36922 | 207 | GtkDisableEvents(); |
4a783bb4 | 208 | gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget), minVal, maxVal); |
00dc9772 | 209 | m_pos = int(gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget))); |
0bf36922 RR |
210 | GtkEnableEvents(); |
211 | } | |
212 | ||
213 | ||
214 | void wxSpinCtrl::GtkDisableEvents() const | |
215 | { | |
216 | g_signal_handlers_block_by_func( m_widget, | |
217 | (gpointer)gtk_value_changed, (void*) this); | |
00dc9772 | 218 | |
0bf36922 RR |
219 | g_signal_handlers_block_by_func(m_widget, |
220 | (gpointer)gtk_changed, (void*) this); | |
221 | } | |
222 | ||
223 | void wxSpinCtrl::GtkEnableEvents() const | |
224 | { | |
225 | g_signal_handlers_unblock_by_func(m_widget, | |
226 | (gpointer)gtk_value_changed, (void*) this); | |
00dc9772 | 227 | |
0bf36922 RR |
228 | g_signal_handlers_unblock_by_func(m_widget, |
229 | (gpointer)gtk_changed, (void*) this); | |
738f9e5a RR |
230 | } |
231 | ||
da048e3d RR |
232 | void wxSpinCtrl::OnChar( wxKeyEvent &event ) |
233 | { | |
234 | wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") ); | |
235 | ||
12a3f227 | 236 | if (event.GetKeyCode() == WXK_RETURN) |
da048e3d | 237 | { |
7f81dfa1 | 238 | wxWindow *top_frame = wxGetTopLevelParent(m_parent); |
0e0d8857 | 239 | |
fa8a793a | 240 | if ( GTK_IS_WINDOW(top_frame->m_widget) ) |
da048e3d | 241 | { |
fa8a793a VZ |
242 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); |
243 | if ( window ) | |
244 | { | |
245 | GtkWidget *widgetDef = window->default_widget; | |
246 | ||
055e633d | 247 | if ( widgetDef ) |
fa8a793a VZ |
248 | { |
249 | gtk_widget_activate(widgetDef); | |
250 | return; | |
251 | } | |
252 | } | |
9750fc42 | 253 | } |
da048e3d RR |
254 | } |
255 | ||
8e13c1ec | 256 | if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER)) |
4a11cca2 RR |
257 | { |
258 | wxCommandEvent evt( wxEVT_COMMAND_TEXT_ENTER, m_windowId ); | |
259 | evt.SetEventObject(this); | |
260 | GtkSpinButton *gsb = GTK_SPIN_BUTTON(m_widget); | |
261 | wxString val = wxGTK_CONV_BACK( gtk_entry_get_text( &gsb->entry ) ); | |
262 | evt.SetString( val ); | |
937013e0 | 263 | if (HandleWindowEvent(evt)) return; |
4a11cca2 RR |
264 | } |
265 | ||
da048e3d RR |
266 | event.Skip(); |
267 | } | |
268 | ||
ef5c70f9 | 269 | GdkWindow *wxSpinCtrl::GTKGetWindow(wxArrayGdkWindows& windows) const |
738f9e5a | 270 | { |
7f81dfa1 | 271 | GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget); |
ef5c70f9 VZ |
272 | |
273 | windows.push_back(spinbutton->entry.text_area); | |
274 | windows.push_back(spinbutton->panel); | |
275 | ||
276 | return NULL; | |
738f9e5a RR |
277 | } |
278 | ||
9d9b7755 VZ |
279 | wxSize wxSpinCtrl::DoGetBestSize() const |
280 | { | |
0279e844 | 281 | wxSize ret( wxControl::DoGetBestSize() ); |
ef5c70f9 | 282 | wxSize best(95, ret.y); // FIXME: 95? |
9f884528 RD |
283 | CacheBestSize(best); |
284 | return best; | |
9d9b7755 VZ |
285 | } |
286 | ||
9d522606 RD |
287 | // static |
288 | wxVisualAttributes | |
289 | wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
290 | { | |
291 | // TODO: overload to accept functions like gtk_spin_button_new? | |
292 | // Until then use a similar type | |
293 | return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true); | |
294 | } | |
295 | ||
738f9e5a | 296 | #endif |
aec0ed2e | 297 | // wxUSE_SPINCTRL |