]>
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 | ||
33 | //----------------------------------------------------------------------------- | |
34 | // data | |
35 | //----------------------------------------------------------------------------- | |
36 | ||
37 | extern bool g_blockEventsOnDrag; | |
38 | ||
738f9e5a RR |
39 | //----------------------------------------------------------------------------- |
40 | // "value_changed" | |
41 | //----------------------------------------------------------------------------- | |
42 | ||
43 | static void gtk_spinctrl_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win ) | |
44 | { | |
45 | if (g_isIdle) wxapp_install_idle_handler(); | |
46 | ||
47 | if (!win->m_hasVMT) return; | |
48 | if (g_blockEventsOnDrag) return; | |
49 | ||
58c837a4 | 50 | wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId()); |
738f9e5a | 51 | event.SetEventObject( win ); |
58c837a4 | 52 | event.SetInt( win->GetValue() ); |
738f9e5a | 53 | win->GetEventHandler()->ProcessEvent( event ); |
738f9e5a RR |
54 | } |
55 | ||
56 | //----------------------------------------------------------------------------- | |
57 | // wxSpinCtrl | |
58 | //----------------------------------------------------------------------------- | |
59 | ||
60 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl) | |
61 | ||
da048e3d RR |
62 | BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl) |
63 | EVT_CHAR(wxSpinCtrl::OnChar) | |
64 | END_EVENT_TABLE() | |
65 | ||
738f9e5a | 66 | bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id, |
ce89fdd2 VZ |
67 | const wxString& value, |
68 | const wxPoint& pos, const wxSize& size, | |
69 | long style, | |
70 | int min, int max, int initial, | |
71 | const wxString& name) | |
738f9e5a RR |
72 | { |
73 | m_needParent = TRUE; | |
354aa1e3 | 74 | m_acceptsFocus = TRUE; |
738f9e5a RR |
75 | |
76 | wxSize new_size = size; | |
77 | if (new_size.y == -1) | |
78 | new_size.y = 26; | |
79 | ||
80 | if (!PreCreation( parent, pos, new_size ) || | |
81 | !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name )) | |
82 | { | |
83 | wxFAIL_MSG( wxT("wxSpinCtrl creation failed") ); | |
84 | return FALSE; | |
85 | } | |
86 | ||
87 | m_oldPos = initial; | |
88 | ||
89 | m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0); | |
90 | ||
91 | m_widget = gtk_spin_button_new( m_adjust, 1, 0 ); | |
92 | ||
b02da6b1 VZ |
93 | gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), |
94 | (int)(m_windowStyle & wxSP_WRAP) ); | |
738f9e5a RR |
95 | |
96 | gtk_signal_connect( GTK_OBJECT (m_adjust), | |
97 | "value_changed", | |
98 | (GtkSignalFunc) gtk_spinctrl_callback, | |
99 | (gpointer) this ); | |
100 | ||
101 | m_parent->DoAddChild( this ); | |
102 | ||
103 | PostCreation(); | |
104 | ||
105 | SetBackgroundColour( parent->GetBackgroundColour() ); | |
106 | ||
ce89fdd2 VZ |
107 | SetValue( value ); |
108 | ||
738f9e5a RR |
109 | Show( TRUE ); |
110 | ||
111 | return TRUE; | |
112 | } | |
113 | ||
114 | int wxSpinCtrl::GetMin() const | |
115 | { | |
116 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
117 | ||
118 | return (int)ceil(m_adjust->lower); | |
119 | } | |
120 | ||
121 | int wxSpinCtrl::GetMax() const | |
122 | { | |
123 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
124 | ||
125 | return (int)ceil(m_adjust->upper); | |
126 | } | |
127 | ||
128 | int wxSpinCtrl::GetValue() const | |
129 | { | |
130 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
131 | ||
132 | return (int)ceil(m_adjust->value); | |
133 | } | |
134 | ||
ce89fdd2 VZ |
135 | void wxSpinCtrl::SetValue( const wxString& value ) |
136 | { | |
137 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
138 | ||
139 | int n; | |
140 | if ( (wxSscanf(value, wxT("%d"), &n) == 1) ) | |
141 | { | |
142 | // a number - set it | |
143 | SetValue(n); | |
144 | } | |
145 | else | |
146 | { | |
147 | // invalid number - set text as is (wxMSW compatible) | |
7dd62924 | 148 | gtk_entry_set_text( GTK_ENTRY(m_widget), value.mbc_str() ); |
ce89fdd2 VZ |
149 | } |
150 | } | |
151 | ||
738f9e5a RR |
152 | void wxSpinCtrl::SetValue( int value ) |
153 | { | |
154 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
155 | ||
156 | float fpos = (float)value; | |
157 | m_oldPos = fpos; | |
158 | if (fabs(fpos-m_adjust->value) < sensitivity) return; | |
159 | ||
160 | m_adjust->value = fpos; | |
161 | ||
162 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); | |
163 | } | |
164 | ||
165 | void wxSpinCtrl::SetRange(int minVal, int maxVal) | |
166 | { | |
167 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
168 | ||
169 | float fmin = (float)minVal; | |
170 | float fmax = (float)maxVal; | |
171 | ||
172 | if ((fabs(fmin-m_adjust->lower) < sensitivity) && | |
173 | (fabs(fmax-m_adjust->upper) < sensitivity)) | |
174 | { | |
175 | return; | |
176 | } | |
177 | ||
178 | m_adjust->lower = fmin; | |
179 | m_adjust->upper = fmax; | |
180 | ||
181 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); | |
0e0d8857 | 182 | |
738f9e5a RR |
183 | // these two calls are required due to some bug in GTK |
184 | Refresh(); | |
185 | SetFocus(); | |
186 | } | |
187 | ||
da048e3d RR |
188 | void wxSpinCtrl::OnChar( wxKeyEvent &event ) |
189 | { | |
190 | wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") ); | |
191 | ||
192 | if (event.KeyCode() == WXK_RETURN) | |
193 | { | |
194 | wxWindow *top_frame = m_parent; | |
195 | while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame)) | |
196 | top_frame = top_frame->GetParent(); | |
197 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); | |
0e0d8857 | 198 | |
da048e3d RR |
199 | if (window->default_widget) |
200 | { | |
201 | gtk_widget_activate (window->default_widget); | |
202 | return; | |
203 | } | |
204 | } | |
205 | ||
206 | event.Skip(); | |
207 | } | |
208 | ||
738f9e5a RR |
209 | bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window ) |
210 | { | |
211 | return GTK_SPIN_BUTTON(m_widget)->panel == window; | |
212 | } | |
213 | ||
214 | void wxSpinCtrl::ApplyWidgetStyle() | |
215 | { | |
216 | SetWidgetStyle(); | |
217 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
218 | } | |
219 | ||
220 | #endif | |
aec0ed2e | 221 | // wxUSE_SPINCTRL |