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