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