]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: spinbutt.cpp | |
3 | // Purpose: wxSpinButton | |
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 "spinbutt.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/spinbutt.h" | |
16 | #include "wx/utils.h" | |
17 | #include <math.h> | |
18 | ||
19 | #include "gdk/gdk.h" | |
20 | #include "gtk/gtk.h" | |
21 | ||
22 | //----------------------------------------------------------------------------- | |
23 | // idle system | |
24 | //----------------------------------------------------------------------------- | |
25 | ||
26 | extern void wxapp_install_idle_handler(); | |
27 | extern bool g_isIdle; | |
28 | ||
29 | //----------------------------------------------------------------------------- | |
30 | // data | |
31 | //----------------------------------------------------------------------------- | |
32 | ||
33 | extern bool g_blockEventsOnDrag; | |
34 | ||
35 | static const float sensitivity = 0.2; | |
36 | ||
37 | //----------------------------------------------------------------------------- | |
38 | // "value_changed" | |
39 | //----------------------------------------------------------------------------- | |
40 | ||
41 | static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win ) | |
42 | { | |
43 | if (g_isIdle) wxapp_install_idle_handler(); | |
44 | ||
45 | if (!win->HasVMT()) return; | |
46 | if (g_blockEventsOnDrag) return; | |
47 | ||
48 | float diff = win->m_adjust->value - win->m_oldPos; | |
49 | if (fabs(diff) < sensitivity) return; | |
50 | win->m_oldPos = win->m_adjust->value; | |
51 | ||
52 | wxEventType command = wxEVT_NULL; | |
53 | ||
54 | float line_step = win->m_adjust->step_increment; | |
55 | float page_step = win->m_adjust->page_increment; | |
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; | |
61 | else command = wxEVT_SCROLL_THUMBTRACK; | |
62 | ||
63 | int value = (int)ceil(win->m_adjust->value); | |
64 | ||
65 | wxSpinEvent event( command, win->GetId()); | |
66 | event.SetPosition( value ); | |
67 | event.SetOrientation( wxVERTICAL ); | |
68 | event.SetEventObject( win ); | |
69 | ||
70 | win->GetEventHandler()->ProcessEvent( event ); | |
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 | { | |
90 | m_needParent = TRUE; | |
91 | ||
92 | wxSize new_size = size; | |
93 | new_size.x = 15; | |
94 | if (new_size.y == -1) | |
95 | new_size.y = 30; | |
96 | ||
97 | PreCreation( parent, id, pos, new_size, style, name ); | |
98 | ||
99 | // SetValidator( validator ); | |
100 | ||
101 | m_oldPos = 0.0; | |
102 | ||
103 | m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0); | |
104 | ||
105 | m_widget = gtk_spin_button_new( m_adjust, 0, 0 ); | |
106 | ||
107 | gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), (m_windowStyle & wxSP_WRAP) ); | |
108 | ||
109 | gtk_signal_connect( GTK_OBJECT (m_adjust), | |
110 | "value_changed", | |
111 | (GtkSignalFunc) gtk_spinbutt_callback, | |
112 | (gpointer) this ); | |
113 | ||
114 | m_parent->AddChild( this ); | |
115 | ||
116 | (m_parent->m_insertCallback)( m_parent, this ); | |
117 | ||
118 | PostCreation(); | |
119 | ||
120 | SetBackgroundColour( parent->GetBackgroundColour() ); | |
121 | ||
122 | Show( TRUE ); | |
123 | ||
124 | return TRUE; | |
125 | } | |
126 | ||
127 | wxSpinButton::~wxSpinButton() | |
128 | { | |
129 | } | |
130 | ||
131 | int wxSpinButton::GetMin() const | |
132 | { | |
133 | wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") ); | |
134 | ||
135 | return (int)ceil(m_adjust->lower); | |
136 | } | |
137 | ||
138 | int wxSpinButton::GetMax() const | |
139 | { | |
140 | wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") ); | |
141 | ||
142 | return (int)ceil(m_adjust->upper); | |
143 | } | |
144 | ||
145 | int wxSpinButton::GetValue() const | |
146 | { | |
147 | wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") ); | |
148 | ||
149 | return (int)ceil(m_adjust->value); | |
150 | } | |
151 | ||
152 | void wxSpinButton::SetValue( int value ) | |
153 | { | |
154 | wxCHECK_RET( (m_widget != NULL), _T("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 wxSpinButton::SetRange(int minVal, int maxVal) | |
166 | { | |
167 | wxCHECK_RET( (m_widget != NULL), _T("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" ); | |
182 | } | |
183 | ||
184 | void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) ) | |
185 | { | |
186 | wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") ); | |
187 | ||
188 | m_width = 15; | |
189 | gtk_widget_set_usize( m_widget, m_width, m_height ); | |
190 | } | |
191 | ||
192 | bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window ) | |
193 | { | |
194 | return GTK_SPIN_BUTTON(m_widget)->panel == window; | |
195 | } | |
196 | ||
197 | void wxSpinButton::ApplyWidgetStyle() | |
198 | { | |
199 | SetWidgetStyle(); | |
200 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
201 | } | |
202 | ||
203 | //----------------------------------------------------------------------------- | |
204 | // wxSpinEvent | |
205 | //----------------------------------------------------------------------------- | |
206 | ||
207 | IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxScrollEvent) | |
208 | ||
209 | wxSpinEvent::wxSpinEvent(wxEventType commandType, int id): | |
210 | wxScrollEvent(commandType, id) | |
211 | { | |
212 | } |