]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/spinbutt.cpp
Fix cvs conflict.
[wxWidgets.git] / src / gtk1 / spinbutt.cpp
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 #pragma implementation "spinbutbase.h"
14 #endif
15
16 #include "wx/spinbutt.h"
17
18 #if wxUSE_SPINBTN
19
20 #include "wx/utils.h"
21
22 #include <math.h>
23
24 #include <gdk/gdk.h>
25 #include <gtk/gtk.h>
26
27 //-----------------------------------------------------------------------------
28 // idle system
29 //-----------------------------------------------------------------------------
30
31 extern void wxapp_install_idle_handler();
32 extern bool g_isIdle;
33
34 //-----------------------------------------------------------------------------
35 // data
36 //-----------------------------------------------------------------------------
37
38 extern bool g_blockEventsOnDrag;
39
40 static const float sensitivity = 0.02;
41
42 //-----------------------------------------------------------------------------
43 // "value_changed"
44 //-----------------------------------------------------------------------------
45
46 static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win )
47 {
48 if (g_isIdle) wxapp_install_idle_handler();
49
50 if (!win->m_hasVMT) return;
51 if (g_blockEventsOnDrag) return;
52
53 float diff = win->m_adjust->value - win->m_oldPos;
54 if (fabs(diff) < sensitivity) return;
55
56 wxEventType command = wxEVT_NULL;
57
58 float line_step = win->m_adjust->step_increment;
59
60 if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP;
61 else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN;
62 else command = wxEVT_SCROLL_THUMBTRACK;
63
64 int value = (int)ceil(win->m_adjust->value);
65
66 wxSpinEvent event( command, win->GetId());
67 event.SetPosition( value );
68 event.SetEventObject( win );
69
70 if ((win->GetEventHandler()->ProcessEvent( event )) &&
71 !event.IsAllowed() )
72 {
73 /* program has vetoed */
74 win->m_adjust->value = win->m_oldPos;
75
76 gtk_signal_disconnect_by_func( GTK_OBJECT (win->m_adjust),
77 (GtkSignalFunc) gtk_spinbutt_callback,
78 (gpointer) win );
79
80 gtk_signal_emit_by_name( GTK_OBJECT(win->m_adjust), "value_changed" );
81
82 gtk_signal_connect( GTK_OBJECT (win->m_adjust),
83 "value_changed",
84 (GtkSignalFunc) gtk_spinbutt_callback,
85 (gpointer) win );
86 return;
87 }
88
89 win->m_oldPos = win->m_adjust->value;
90
91 /* always send a thumbtrack event */
92 if (command != wxEVT_SCROLL_THUMBTRACK)
93 {
94 command = wxEVT_SCROLL_THUMBTRACK;
95 wxSpinEvent event2( command, win->GetId());
96 event2.SetPosition( value );
97 event2.SetEventObject( win );
98 win->GetEventHandler()->ProcessEvent( event2 );
99 }
100 }
101
102 //-----------------------------------------------------------------------------
103 // wxSpinButton
104 //-----------------------------------------------------------------------------
105
106 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
107 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
108
109 BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
110 EVT_SIZE(wxSpinButton::OnSize)
111 END_EVENT_TABLE()
112
113 bool wxSpinButton::Create(wxWindow *parent,
114 wxWindowID id,
115 const wxPoint& pos,
116 const wxSize& size,
117 long style,
118 const wxString& name)
119 {
120 m_needParent = TRUE;
121
122 wxSize new_size = size,
123 sizeBest = DoGetBestSize();
124 new_size.x = sizeBest.x; // override width always
125 if (new_size.y == -1)
126 new_size.y = sizeBest.y;
127
128 if (!PreCreation( parent, pos, new_size ) ||
129 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
130 {
131 wxFAIL_MSG( wxT("wxXX creation failed") );
132 return FALSE;
133 }
134
135 m_oldPos = 0.0;
136
137 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
138
139 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
140
141 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
142 (int)(m_windowStyle & wxSP_WRAP) );
143
144 gtk_signal_connect( GTK_OBJECT (m_adjust),
145 "value_changed",
146 (GtkSignalFunc) gtk_spinbutt_callback,
147 (gpointer) this );
148
149 m_parent->DoAddChild( this );
150
151 PostCreation();
152
153 SetBackgroundColour( parent->GetBackgroundColour() );
154
155 Show( TRUE );
156
157 return TRUE;
158 }
159
160 int wxSpinButton::GetMin() const
161 {
162 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
163
164 return (int)ceil(m_adjust->lower);
165 }
166
167 int wxSpinButton::GetMax() const
168 {
169 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
170
171 return (int)ceil(m_adjust->upper);
172 }
173
174 int wxSpinButton::GetValue() const
175 {
176 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
177
178 return (int)ceil(m_adjust->value);
179 }
180
181 void wxSpinButton::SetValue( int value )
182 {
183 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
184
185 float fpos = (float)value;
186 m_oldPos = fpos;
187 if (fabs(fpos-m_adjust->value) < sensitivity) return;
188
189 m_adjust->value = fpos;
190
191 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
192 }
193
194 void wxSpinButton::SetRange(int minVal, int maxVal)
195 {
196 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
197
198 float fmin = (float)minVal;
199 float fmax = (float)maxVal;
200
201 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
202 (fabs(fmax-m_adjust->upper) < sensitivity))
203 {
204 return;
205 }
206
207 m_adjust->lower = fmin;
208 m_adjust->upper = fmax;
209
210 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
211
212 // these two calls are required due to some bug in GTK
213 Refresh();
214 SetFocus();
215 }
216
217 void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
218 {
219 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
220
221 m_width = DoGetBestSize().x;
222 gtk_widget_set_usize( m_widget, m_width, m_height );
223 }
224
225 bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
226 {
227 return GTK_SPIN_BUTTON(m_widget)->panel == window;
228 }
229
230 void wxSpinButton::ApplyWidgetStyle()
231 {
232 SetWidgetStyle();
233 gtk_widget_set_style( m_widget, m_widgetStyle );
234 }
235
236 wxSize wxSpinButton::DoGetBestSize() const
237 {
238 return wxSize(15, 26);
239 }
240
241 #endif