]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/spinbutt.cpp
Use __WINDOWS__ for OS checks and __WXMSW__ for GUI checks (round 2).
[wxWidgets.git] / src / gtk1 / spinbutt.cpp
CommitLineData
8e189077 1/////////////////////////////////////////////////////////////////////////////
de6185e2 2// Name: src/gtk1/spinbutt.cpp
8e189077
RR
3// Purpose: wxSpinButton
4// Author: Robert
5// Modified by:
6// RCS-ID: $Id$
7// Copyright: (c) Robert Roebling
65571936 8// Licence: wxWindows licence
8e189077
RR
9/////////////////////////////////////////////////////////////////////////////
10
14f355c2
VS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
de6185e2
WS
14#if wxUSE_SPINBTN
15
8e189077 16#include "wx/spinbutt.h"
dcf924a3 17
de6185e2
WS
18#ifndef WX_PRECOMP
19 #include "wx/utils.h"
18680f86 20 #include "wx/math.h"
de6185e2 21#endif
dcf924a3 22
3cbab641 23#include "wx/gtk1/private.h"
83624f79 24
acfd422a
RR
25//-----------------------------------------------------------------------------
26// idle system
27//-----------------------------------------------------------------------------
28
29extern void wxapp_install_idle_handler();
30extern bool g_isIdle;
31
8e189077
RR
32//-----------------------------------------------------------------------------
33// data
34//-----------------------------------------------------------------------------
35
36extern bool g_blockEventsOnDrag;
37
738f9e5a 38static const float sensitivity = 0.02;
6380910c 39
8e189077
RR
40//-----------------------------------------------------------------------------
41// "value_changed"
42//-----------------------------------------------------------------------------
43
865bb325 44extern "C" {
8e189077 45static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win )
6380910c 46{
acfd422a
RR
47 if (g_isIdle) wxapp_install_idle_handler();
48
a2053b27 49 if (!win->m_hasVMT) return;
83624f79 50 if (g_blockEventsOnDrag) return;
828f655f 51
83624f79 52 float diff = win->m_adjust->value - win->m_oldPos;
6380910c 53 if (fabs(diff) < sensitivity) return;
6380910c 54
83624f79 55 wxEventType command = wxEVT_NULL;
6380910c 56
83624f79 57 float line_step = win->m_adjust->step_increment;
6380910c 58
0f42a871
RR
59 if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP;
60 else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN;
83624f79 61 else command = wxEVT_SCROLL_THUMBTRACK;
8e189077 62
66d78146 63 int value = (int)ceil(win->m_adjust->value);
6380910c 64
83624f79
RR
65 wxSpinEvent event( command, win->GetId());
66 event.SetPosition( value );
83624f79 67 event.SetEventObject( win );
0f42a871 68
937013e0 69 if ((win->HandleWindowEvent( event )) &&
0f42a871
RR
70 !event.IsAllowed() )
71 {
72 /* program has vetoed */
73 win->m_adjust->value = win->m_oldPos;
88d19775 74
0f42a871 75 gtk_signal_disconnect_by_func( GTK_OBJECT (win->m_adjust),
88d19775
MR
76 (GtkSignalFunc) gtk_spinbutt_callback,
77 (gpointer) win );
78
0f42a871
RR
79 gtk_signal_emit_by_name( GTK_OBJECT(win->m_adjust), "value_changed" );
80
81 gtk_signal_connect( GTK_OBJECT (win->m_adjust),
88d19775
MR
82 "value_changed",
83 (GtkSignalFunc) gtk_spinbutt_callback,
84 (gpointer) win );
0f42a871
RR
85 return;
86 }
88d19775 87
0f42a871 88 win->m_oldPos = win->m_adjust->value;
88d19775 89
e65cc56a
RR
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 );
937013e0 97 win->HandleWindowEvent( event2 );
e65cc56a 98 }
8e189077 99}
865bb325 100}
8e189077
RR
101
102//-----------------------------------------------------------------------------
103// wxSpinButton
104//-----------------------------------------------------------------------------
105
8e189077
RR
106BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
107 EVT_SIZE(wxSpinButton::OnSize)
108END_EVENT_TABLE()
109
31528cd3
VZ
110bool wxSpinButton::Create(wxWindow *parent,
111 wxWindowID id,
112 const wxPoint& pos,
113 const wxSize& size,
114 long style,
115 const wxString& name)
8e189077 116{
de6185e2 117 m_needParent = true;
6380910c 118
9d9b7755
VZ
119 wxSize new_size = size,
120 sizeBest = DoGetBestSize();
121 new_size.x = sizeBest.x; // override width always
6380910c 122 if (new_size.y == -1)
9d9b7755 123 new_size.y = sizeBest.y;
6380910c 124
2259e007 125 if (!PreCreation( parent, pos, new_size ) ||
4dcaf11a
RR
126 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
127 {
223d09f6 128 wxFAIL_MSG( wxT("wxXX creation failed") );
de6185e2 129 return false;
4dcaf11a 130 }
8e189077 131
83624f79 132 m_oldPos = 0.0;
8e189077 133
83624f79 134 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
6380910c 135
83624f79 136 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
6380910c 137
b02da6b1
VZ
138 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
139 (int)(m_windowStyle & wxSP_WRAP) );
6380910c
VZ
140
141 gtk_signal_connect( GTK_OBJECT (m_adjust),
83624f79 142 "value_changed",
6380910c
VZ
143 (GtkSignalFunc) gtk_spinbutt_callback,
144 (gpointer) this );
145
f03fc89f 146 m_parent->DoAddChild( this );
6380910c 147
abdeb9e7 148 PostCreation(new_size);
6380910c 149
de6185e2 150 return true;
8e189077
RR
151}
152
8e189077
RR
153int wxSpinButton::GetMin() const
154{
223d09f6 155 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 156
66d78146 157 return (int)ceil(m_adjust->lower);
8e189077
RR
158}
159
160int wxSpinButton::GetMax() const
161{
223d09f6 162 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 163
66d78146 164 return (int)ceil(m_adjust->upper);
8e189077
RR
165}
166
167int wxSpinButton::GetValue() const
168{
223d09f6 169 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 170
66d78146 171 return (int)ceil(m_adjust->value);
8e189077
RR
172}
173
174void wxSpinButton::SetValue( int value )
175{
223d09f6 176 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 177
83624f79
RR
178 float fpos = (float)value;
179 m_oldPos = fpos;
6380910c
VZ
180 if (fabs(fpos-m_adjust->value) < sensitivity) return;
181
83624f79 182 m_adjust->value = fpos;
6380910c 183
83624f79 184 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
8e189077
RR
185}
186
187void wxSpinButton::SetRange(int minVal, int maxVal)
188{
223d09f6 189 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 190
83624f79
RR
191 float fmin = (float)minVal;
192 float fmax = (float)maxVal;
6380910c
VZ
193
194 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
195 (fabs(fmax-m_adjust->upper) < sensitivity))
83624f79
RR
196 {
197 return;
198 }
6380910c 199
83624f79
RR
200 m_adjust->lower = fmin;
201 m_adjust->upper = fmax;
8e189077 202
83624f79 203 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
88d19775 204
65045edd
RR
205 // these two calls are required due to some bug in GTK
206 Refresh();
207 SetFocus();
8e189077
RR
208}
209
210void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
211{
223d09f6 212 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 213
9d9b7755 214 m_width = DoGetBestSize().x;
83624f79 215 gtk_widget_set_usize( m_widget, m_width, m_height );
8e189077
RR
216}
217
218bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
219{
83624f79 220 return GTK_SPIN_BUTTON(m_widget)->panel == window;
8e189077
RR
221}
222
9d9b7755
VZ
223wxSize wxSpinButton::DoGetBestSize() const
224{
9f884528
RD
225 wxSize best(15, 26); // FIXME
226 CacheBestSize(best);
227 return best;
9d9b7755
VZ
228}
229
9d522606
RD
230// static
231wxVisualAttributes
232wxSpinButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
233{
234 // TODO: overload to accept functions like gtk_spin_button_new?
235 // Until then use a similar type
236 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
237}
238
de6185e2 239#endif // wxUSE_SPINBTN