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