]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/spinbutt.cpp
added support for gcc precompiled headers
[wxWidgets.git] / src / gtk1 / spinbutt.cpp
CommitLineData
8e189077
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: spinbutt.cpp
3// Purpose: wxSpinButton
4// Author: Robert
5// Modified by:
6// RCS-ID: $Id$
7// Copyright: (c) Robert Roebling
6380910c 8// Licence: wxWindows licence
8e189077
RR
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
510fc784
RR
12 #pragma implementation "spinbutt.h"
13 #pragma implementation "spinbutbase.h"
8e189077
RR
14#endif
15
16#include "wx/spinbutt.h"
dcf924a3 17
2fa7c206 18#if wxUSE_SPINBTN
dcf924a3 19
8e189077 20#include "wx/utils.h"
aec0ed2e 21
f5368809 22#include <math.h>
8e189077 23
9e691f46 24#include "wx/gtk/private.h"
83624f79 25
acfd422a
RR
26//-----------------------------------------------------------------------------
27// idle system
28//-----------------------------------------------------------------------------
29
30extern void wxapp_install_idle_handler();
31extern bool g_isIdle;
32
8e189077
RR
33//-----------------------------------------------------------------------------
34// data
35//-----------------------------------------------------------------------------
36
37extern bool g_blockEventsOnDrag;
38
738f9e5a 39static const float sensitivity = 0.02;
6380910c 40
8e189077
RR
41//-----------------------------------------------------------------------------
42// "value_changed"
43//-----------------------------------------------------------------------------
44
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
RR
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;
e65cc56a
RR
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 }
8e189077
RR
99}
100
101//-----------------------------------------------------------------------------
102// wxSpinButton
103//-----------------------------------------------------------------------------
104
105IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
2fa7c206 106IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
8e189077
RR
107
108BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
109 EVT_SIZE(wxSpinButton::OnSize)
110END_EVENT_TABLE()
111
31528cd3
VZ
112bool wxSpinButton::Create(wxWindow *parent,
113 wxWindowID id,
114 const wxPoint& pos,
115 const wxSize& size,
116 long style,
117 const wxString& name)
8e189077 118{
83624f79 119 m_needParent = TRUE;
6380910c 120
9d9b7755
VZ
121 wxSize new_size = size,
122 sizeBest = DoGetBestSize();
123 new_size.x = sizeBest.x; // override width always
6380910c 124 if (new_size.y == -1)
9d9b7755 125 new_size.y = sizeBest.y;
6380910c 126
2259e007 127 if (!PreCreation( parent, pos, new_size ) ||
4dcaf11a
RR
128 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
129 {
223d09f6 130 wxFAIL_MSG( wxT("wxXX creation failed") );
0f42a871 131 return FALSE;
4dcaf11a 132 }
8e189077 133
83624f79 134 m_oldPos = 0.0;
8e189077 135
83624f79 136 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
6380910c 137
83624f79 138 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
6380910c 139
b02da6b1
VZ
140 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
141 (int)(m_windowStyle & wxSP_WRAP) );
6380910c
VZ
142
143 gtk_signal_connect( GTK_OBJECT (m_adjust),
83624f79 144 "value_changed",
6380910c
VZ
145 (GtkSignalFunc) gtk_spinbutt_callback,
146 (gpointer) this );
147
f03fc89f 148 m_parent->DoAddChild( this );
6380910c 149
83624f79 150 PostCreation();
6380910c 151
83624f79 152 SetBackgroundColour( parent->GetBackgroundColour() );
8e189077 153
83624f79 154 Show( TRUE );
6380910c 155
83624f79 156 return TRUE;
8e189077
RR
157}
158
8e189077
RR
159int wxSpinButton::GetMin() const
160{
223d09f6 161 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 162
66d78146 163 return (int)ceil(m_adjust->lower);
8e189077
RR
164}
165
166int wxSpinButton::GetMax() const
167{
223d09f6 168 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 169
66d78146 170 return (int)ceil(m_adjust->upper);
8e189077
RR
171}
172
173int wxSpinButton::GetValue() const
174{
223d09f6 175 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 176
66d78146 177 return (int)ceil(m_adjust->value);
8e189077
RR
178}
179
180void wxSpinButton::SetValue( int value )
181{
223d09f6 182 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 183
83624f79
RR
184 float fpos = (float)value;
185 m_oldPos = fpos;
6380910c
VZ
186 if (fabs(fpos-m_adjust->value) < sensitivity) return;
187
83624f79 188 m_adjust->value = fpos;
6380910c 189
83624f79 190 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
8e189077
RR
191}
192
193void wxSpinButton::SetRange(int minVal, int maxVal)
194{
223d09f6 195 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 196
83624f79
RR
197 float fmin = (float)minVal;
198 float fmax = (float)maxVal;
6380910c
VZ
199
200 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
201 (fabs(fmax-m_adjust->upper) < sensitivity))
83624f79
RR
202 {
203 return;
204 }
6380910c 205
83624f79
RR
206 m_adjust->lower = fmin;
207 m_adjust->upper = fmax;
8e189077 208
83624f79 209 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
65045edd
RR
210
211 // these two calls are required due to some bug in GTK
212 Refresh();
213 SetFocus();
8e189077
RR
214}
215
216void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
217{
223d09f6 218 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 219
9d9b7755 220 m_width = DoGetBestSize().x;
83624f79 221 gtk_widget_set_usize( m_widget, m_width, m_height );
8e189077
RR
222}
223
224bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
225{
83624f79 226 return GTK_SPIN_BUTTON(m_widget)->panel == window;
8e189077
RR
227}
228
229void wxSpinButton::ApplyWidgetStyle()
230{
83624f79
RR
231 SetWidgetStyle();
232 gtk_widget_set_style( m_widget, m_widgetStyle );
8e189077
RR
233}
234
9d9b7755
VZ
235wxSize wxSpinButton::DoGetBestSize() const
236{
237 return wxSize(15, 26);
238}
239
31528cd3 240#endif