]>
Commit | Line | Data |
---|---|---|
8e189077 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: spinbutt.cpp | |
3 | // Purpose: wxSpinButton | |
4 | // Author: Robert | |
5 | // Modified by: | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) Robert Roebling | |
6380910c | 8 | // Licence: wxWindows licence |
8e189077 RR |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "spinbutt.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/spinbutt.h" | |
dcf924a3 RR |
16 | |
17 | #ifdef wxUSE_SPINBTN | |
18 | ||
8e189077 | 19 | #include "wx/utils.h" |
f5368809 | 20 | #include <math.h> |
8e189077 | 21 | |
83624f79 RR |
22 | #include "gdk/gdk.h" |
23 | #include "gtk/gtk.h" | |
24 | ||
acfd422a RR |
25 | //----------------------------------------------------------------------------- |
26 | // idle system | |
27 | //----------------------------------------------------------------------------- | |
28 | ||
29 | extern void wxapp_install_idle_handler(); | |
30 | extern bool g_isIdle; | |
31 | ||
8e189077 RR |
32 | //----------------------------------------------------------------------------- |
33 | // data | |
34 | //----------------------------------------------------------------------------- | |
35 | ||
36 | extern bool g_blockEventsOnDrag; | |
37 | ||
738f9e5a | 38 | static const float sensitivity = 0.02; |
6380910c | 39 | |
8e189077 RR |
40 | //----------------------------------------------------------------------------- |
41 | // "value_changed" | |
42 | //----------------------------------------------------------------------------- | |
43 | ||
44 | static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win ) | |
6380910c | 45 | { |
acfd422a RR |
46 | if (g_isIdle) wxapp_install_idle_handler(); |
47 | ||
a2053b27 | 48 | if (!win->m_hasVMT) return; |
83624f79 | 49 | if (g_blockEventsOnDrag) return; |
828f655f | 50 | |
83624f79 | 51 | float diff = win->m_adjust->value - win->m_oldPos; |
6380910c | 52 | if (fabs(diff) < sensitivity) return; |
828f655f | 53 | win->m_oldPos = win->m_adjust->value; |
6380910c | 54 | |
83624f79 | 55 | wxEventType command = wxEVT_NULL; |
6380910c | 56 | |
83624f79 | 57 | float line_step = win->m_adjust->step_increment; |
6380910c VZ |
58 | |
59 | if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN; | |
60 | else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP; | |
83624f79 | 61 | else command = wxEVT_SCROLL_THUMBTRACK; |
8e189077 | 62 | |
66d78146 | 63 | int value = (int)ceil(win->m_adjust->value); |
6380910c | 64 | |
83624f79 RR |
65 | wxSpinEvent event( command, win->GetId()); |
66 | event.SetPosition( value ); | |
83624f79 | 67 | event.SetEventObject( win ); |
828f655f | 68 | win->GetEventHandler()->ProcessEvent( event ); |
e65cc56a RR |
69 | |
70 | /* always send a thumbtrack event */ | |
71 | if (command != wxEVT_SCROLL_THUMBTRACK) | |
72 | { | |
73 | command = wxEVT_SCROLL_THUMBTRACK; | |
74 | wxSpinEvent event2( command, win->GetId()); | |
75 | event2.SetPosition( value ); | |
76 | event2.SetEventObject( win ); | |
77 | win->GetEventHandler()->ProcessEvent( event2 ); | |
78 | } | |
8e189077 RR |
79 | } |
80 | ||
81 | //----------------------------------------------------------------------------- | |
82 | // wxSpinButton | |
83 | //----------------------------------------------------------------------------- | |
84 | ||
85 | IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl) | |
31528cd3 | 86 | IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxScrollEvent); |
8e189077 RR |
87 | |
88 | BEGIN_EVENT_TABLE(wxSpinButton, wxControl) | |
89 | EVT_SIZE(wxSpinButton::OnSize) | |
90 | END_EVENT_TABLE() | |
91 | ||
31528cd3 VZ |
92 | bool wxSpinButton::Create(wxWindow *parent, |
93 | wxWindowID id, | |
94 | const wxPoint& pos, | |
95 | const wxSize& size, | |
96 | long style, | |
97 | const wxString& name) | |
8e189077 | 98 | { |
83624f79 | 99 | m_needParent = TRUE; |
6380910c | 100 | |
83624f79 | 101 | wxSize new_size = size; |
19da4326 | 102 | new_size.x = 15; |
6380910c | 103 | if (new_size.y == -1) |
738f9e5a | 104 | new_size.y = 26; |
6380910c | 105 | |
2259e007 | 106 | if (!PreCreation( parent, pos, new_size ) || |
4dcaf11a RR |
107 | !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name )) |
108 | { | |
223d09f6 | 109 | wxFAIL_MSG( wxT("wxXX creation failed") ); |
4dcaf11a RR |
110 | return FALSE; |
111 | } | |
8e189077 | 112 | |
83624f79 | 113 | m_oldPos = 0.0; |
8e189077 | 114 | |
83624f79 | 115 | m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0); |
6380910c | 116 | |
83624f79 | 117 | m_widget = gtk_spin_button_new( m_adjust, 0, 0 ); |
6380910c | 118 | |
b02da6b1 VZ |
119 | gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), |
120 | (int)(m_windowStyle & wxSP_WRAP) ); | |
6380910c VZ |
121 | |
122 | gtk_signal_connect( GTK_OBJECT (m_adjust), | |
83624f79 | 123 | "value_changed", |
6380910c VZ |
124 | (GtkSignalFunc) gtk_spinbutt_callback, |
125 | (gpointer) this ); | |
126 | ||
f03fc89f | 127 | m_parent->DoAddChild( this ); |
6380910c | 128 | |
83624f79 | 129 | PostCreation(); |
6380910c | 130 | |
83624f79 | 131 | SetBackgroundColour( parent->GetBackgroundColour() ); |
8e189077 | 132 | |
83624f79 | 133 | Show( TRUE ); |
6380910c | 134 | |
83624f79 | 135 | return TRUE; |
8e189077 RR |
136 | } |
137 | ||
8e189077 RR |
138 | int wxSpinButton::GetMin() const |
139 | { | |
223d09f6 | 140 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); |
6380910c | 141 | |
66d78146 | 142 | return (int)ceil(m_adjust->lower); |
8e189077 RR |
143 | } |
144 | ||
145 | int wxSpinButton::GetMax() const | |
146 | { | |
223d09f6 | 147 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); |
6380910c | 148 | |
66d78146 | 149 | return (int)ceil(m_adjust->upper); |
8e189077 RR |
150 | } |
151 | ||
152 | int wxSpinButton::GetValue() const | |
153 | { | |
223d09f6 | 154 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); |
6380910c | 155 | |
66d78146 | 156 | return (int)ceil(m_adjust->value); |
8e189077 RR |
157 | } |
158 | ||
159 | void wxSpinButton::SetValue( int value ) | |
160 | { | |
223d09f6 | 161 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); |
6380910c | 162 | |
83624f79 RR |
163 | float fpos = (float)value; |
164 | m_oldPos = fpos; | |
6380910c VZ |
165 | if (fabs(fpos-m_adjust->value) < sensitivity) return; |
166 | ||
83624f79 | 167 | m_adjust->value = fpos; |
6380910c | 168 | |
83624f79 | 169 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); |
8e189077 RR |
170 | } |
171 | ||
172 | void wxSpinButton::SetRange(int minVal, int maxVal) | |
173 | { | |
223d09f6 | 174 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); |
6380910c | 175 | |
83624f79 RR |
176 | float fmin = (float)minVal; |
177 | float fmax = (float)maxVal; | |
6380910c VZ |
178 | |
179 | if ((fabs(fmin-m_adjust->lower) < sensitivity) && | |
180 | (fabs(fmax-m_adjust->upper) < sensitivity)) | |
83624f79 RR |
181 | { |
182 | return; | |
183 | } | |
6380910c | 184 | |
83624f79 RR |
185 | m_adjust->lower = fmin; |
186 | m_adjust->upper = fmax; | |
8e189077 | 187 | |
83624f79 | 188 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
65045edd RR |
189 | |
190 | // these two calls are required due to some bug in GTK | |
191 | Refresh(); | |
192 | SetFocus(); | |
8e189077 RR |
193 | } |
194 | ||
195 | void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) ) | |
196 | { | |
223d09f6 | 197 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); |
6380910c | 198 | |
19da4326 | 199 | m_width = 15; |
83624f79 | 200 | gtk_widget_set_usize( m_widget, m_width, m_height ); |
8e189077 RR |
201 | } |
202 | ||
203 | bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window ) | |
204 | { | |
83624f79 | 205 | return GTK_SPIN_BUTTON(m_widget)->panel == window; |
8e189077 RR |
206 | } |
207 | ||
208 | void wxSpinButton::ApplyWidgetStyle() | |
209 | { | |
83624f79 RR |
210 | SetWidgetStyle(); |
211 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
8e189077 RR |
212 | } |
213 | ||
31528cd3 | 214 | #endif |