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