]>
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" | |
16 | #include "wx/utils.h" | |
f5368809 | 17 | #include <math.h> |
8e189077 | 18 | |
83624f79 RR |
19 | #include "gdk/gdk.h" |
20 | #include "gtk/gtk.h" | |
21 | ||
acfd422a RR |
22 | //----------------------------------------------------------------------------- |
23 | // idle system | |
24 | //----------------------------------------------------------------------------- | |
25 | ||
26 | extern void wxapp_install_idle_handler(); | |
27 | extern bool g_isIdle; | |
28 | ||
8e189077 RR |
29 | //----------------------------------------------------------------------------- |
30 | // data | |
31 | //----------------------------------------------------------------------------- | |
32 | ||
33 | extern bool g_blockEventsOnDrag; | |
34 | ||
6380910c VZ |
35 | static const float sensitivity = 0.2; |
36 | ||
8e189077 RR |
37 | //----------------------------------------------------------------------------- |
38 | // "value_changed" | |
39 | //----------------------------------------------------------------------------- | |
40 | ||
41 | static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win ) | |
6380910c | 42 | { |
acfd422a RR |
43 | if (g_isIdle) wxapp_install_idle_handler(); |
44 | ||
a2053b27 | 45 | if (!win->m_hasVMT) return; |
83624f79 | 46 | if (g_blockEventsOnDrag) return; |
828f655f | 47 | |
83624f79 | 48 | float diff = win->m_adjust->value - win->m_oldPos; |
6380910c | 49 | if (fabs(diff) < sensitivity) return; |
828f655f | 50 | win->m_oldPos = win->m_adjust->value; |
6380910c | 51 | |
83624f79 | 52 | wxEventType command = wxEVT_NULL; |
6380910c | 53 | |
83624f79 RR |
54 | float line_step = win->m_adjust->step_increment; |
55 | float page_step = win->m_adjust->page_increment; | |
6380910c VZ |
56 | |
57 | if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN; | |
58 | else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP; | |
59 | else if (fabs(diff-page_step) < sensitivity) command = wxEVT_SCROLL_PAGEDOWN; | |
60 | else if (fabs(diff+page_step) < sensitivity) command = wxEVT_SCROLL_PAGEUP; | |
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 ); | |
67 | event.SetOrientation( wxVERTICAL ); | |
68 | event.SetEventObject( win ); | |
6380910c | 69 | |
828f655f | 70 | win->GetEventHandler()->ProcessEvent( event ); |
8e189077 RR |
71 | } |
72 | ||
73 | //----------------------------------------------------------------------------- | |
74 | // wxSpinButton | |
75 | //----------------------------------------------------------------------------- | |
76 | ||
77 | IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl) | |
78 | ||
79 | BEGIN_EVENT_TABLE(wxSpinButton, wxControl) | |
80 | EVT_SIZE(wxSpinButton::OnSize) | |
81 | END_EVENT_TABLE() | |
82 | ||
83 | wxSpinButton::wxSpinButton() | |
84 | { | |
85 | } | |
86 | ||
87 | bool wxSpinButton::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, | |
88 | long style, const wxString& name) | |
89 | { | |
83624f79 | 90 | m_needParent = TRUE; |
6380910c | 91 | |
83624f79 | 92 | wxSize new_size = size; |
19da4326 | 93 | new_size.x = 15; |
6380910c VZ |
94 | if (new_size.y == -1) |
95 | new_size.y = 30; | |
96 | ||
83624f79 | 97 | PreCreation( parent, id, pos, new_size, style, name ); |
6380910c | 98 | |
8e189077 RR |
99 | // SetValidator( validator ); |
100 | ||
83624f79 | 101 | m_oldPos = 0.0; |
8e189077 | 102 | |
83624f79 | 103 | m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0); |
6380910c | 104 | |
83624f79 | 105 | m_widget = gtk_spin_button_new( m_adjust, 0, 0 ); |
6380910c | 106 | |
83624f79 | 107 | gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), (m_windowStyle & wxSP_WRAP) ); |
6380910c VZ |
108 | |
109 | gtk_signal_connect( GTK_OBJECT (m_adjust), | |
83624f79 | 110 | "value_changed", |
6380910c VZ |
111 | (GtkSignalFunc) gtk_spinbutt_callback, |
112 | (gpointer) this ); | |
113 | ||
f03fc89f | 114 | m_parent->DoAddChild( this ); |
6380910c | 115 | |
83624f79 | 116 | PostCreation(); |
6380910c | 117 | |
83624f79 | 118 | SetBackgroundColour( parent->GetBackgroundColour() ); |
8e189077 | 119 | |
83624f79 | 120 | Show( TRUE ); |
6380910c | 121 | |
83624f79 | 122 | return TRUE; |
8e189077 RR |
123 | } |
124 | ||
125 | wxSpinButton::~wxSpinButton() | |
126 | { | |
127 | } | |
128 | ||
129 | int wxSpinButton::GetMin() const | |
130 | { | |
9f15c5fe | 131 | wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") ); |
6380910c | 132 | |
66d78146 | 133 | return (int)ceil(m_adjust->lower); |
8e189077 RR |
134 | } |
135 | ||
136 | int wxSpinButton::GetMax() const | |
137 | { | |
9f15c5fe | 138 | wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") ); |
6380910c | 139 | |
66d78146 | 140 | return (int)ceil(m_adjust->upper); |
8e189077 RR |
141 | } |
142 | ||
143 | int wxSpinButton::GetValue() const | |
144 | { | |
9f15c5fe | 145 | wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") ); |
6380910c | 146 | |
66d78146 | 147 | return (int)ceil(m_adjust->value); |
8e189077 RR |
148 | } |
149 | ||
150 | void wxSpinButton::SetValue( int value ) | |
151 | { | |
9f15c5fe | 152 | wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") ); |
6380910c | 153 | |
83624f79 RR |
154 | float fpos = (float)value; |
155 | m_oldPos = fpos; | |
6380910c VZ |
156 | if (fabs(fpos-m_adjust->value) < sensitivity) return; |
157 | ||
83624f79 | 158 | m_adjust->value = fpos; |
6380910c | 159 | |
83624f79 | 160 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); |
8e189077 RR |
161 | } |
162 | ||
163 | void wxSpinButton::SetRange(int minVal, int maxVal) | |
164 | { | |
9f15c5fe | 165 | wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") ); |
6380910c | 166 | |
83624f79 RR |
167 | float fmin = (float)minVal; |
168 | float fmax = (float)maxVal; | |
6380910c VZ |
169 | |
170 | if ((fabs(fmin-m_adjust->lower) < sensitivity) && | |
171 | (fabs(fmax-m_adjust->upper) < sensitivity)) | |
83624f79 RR |
172 | { |
173 | return; | |
174 | } | |
6380910c | 175 | |
83624f79 RR |
176 | m_adjust->lower = fmin; |
177 | m_adjust->upper = fmax; | |
8e189077 | 178 | |
83624f79 | 179 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
8e189077 RR |
180 | } |
181 | ||
182 | void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) ) | |
183 | { | |
9f15c5fe | 184 | wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") ); |
6380910c | 185 | |
19da4326 | 186 | m_width = 15; |
83624f79 | 187 | gtk_widget_set_usize( m_widget, m_width, m_height ); |
8e189077 RR |
188 | } |
189 | ||
190 | bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window ) | |
191 | { | |
83624f79 | 192 | return GTK_SPIN_BUTTON(m_widget)->panel == window; |
8e189077 RR |
193 | } |
194 | ||
195 | void wxSpinButton::ApplyWidgetStyle() | |
196 | { | |
83624f79 RR |
197 | SetWidgetStyle(); |
198 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
8e189077 RR |
199 | } |
200 | ||
201 | //----------------------------------------------------------------------------- | |
202 | // wxSpinEvent | |
203 | //----------------------------------------------------------------------------- | |
204 | ||
205 | IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxScrollEvent) | |
206 | ||
207 | wxSpinEvent::wxSpinEvent(wxEventType commandType, int id): | |
208 | wxScrollEvent(commandType, id) | |
209 | { | |
210 | } |