]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/spinbutt.cpp
Compile fixes.
[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__
12#pragma implementation "spinbutt.h"
13#endif
14
15#include "wx/spinbutt.h"
dcf924a3
RR
16
17#ifdef wxUSE_SPINBTN
18
8e189077 19#include "wx/utils.h"
f5368809 20#include <math.h>
8e189077 21
83624f79
RR
22#include "gdk/gdk.h"
23#include "gtk/gtk.h"
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
6380910c
VZ
38static const float sensitivity = 0.2;
39
8e189077
RR
40//-----------------------------------------------------------------------------
41// "value_changed"
42//-----------------------------------------------------------------------------
43
44static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win )
6380910c 45{
acfd422a
RR
46 if (g_isIdle) wxapp_install_idle_handler();
47
a2053b27 48 if (!win->m_hasVMT) return;
83624f79 49 if (g_blockEventsOnDrag) return;
828f655f 50
83624f79 51 float diff = win->m_adjust->value - win->m_oldPos;
6380910c 52 if (fabs(diff) < sensitivity) return;
828f655f 53 win->m_oldPos = win->m_adjust->value;
6380910c 54
83624f79 55 wxEventType command = wxEVT_NULL;
6380910c 56
83624f79
RR
57 float line_step = win->m_adjust->step_increment;
58 float page_step = win->m_adjust->page_increment;
6380910c
VZ
59
60 if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN;
61 else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP;
62 else if (fabs(diff-page_step) < sensitivity) command = wxEVT_SCROLL_PAGEDOWN;
63 else if (fabs(diff+page_step) < sensitivity) command = wxEVT_SCROLL_PAGEUP;
83624f79 64 else command = wxEVT_SCROLL_THUMBTRACK;
8e189077 65
66d78146 66 int value = (int)ceil(win->m_adjust->value);
6380910c 67
83624f79
RR
68 wxSpinEvent event( command, win->GetId());
69 event.SetPosition( value );
70 event.SetOrientation( wxVERTICAL );
71 event.SetEventObject( win );
6380910c 72
828f655f 73 win->GetEventHandler()->ProcessEvent( event );
8e189077
RR
74}
75
76//-----------------------------------------------------------------------------
77// wxSpinButton
78//-----------------------------------------------------------------------------
79
80IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
81
82BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
83 EVT_SIZE(wxSpinButton::OnSize)
84END_EVENT_TABLE()
85
86wxSpinButton::wxSpinButton()
87{
88}
89
90bool wxSpinButton::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
91 long style, const wxString& name)
92{
83624f79 93 m_needParent = TRUE;
6380910c 94
83624f79 95 wxSize new_size = size;
19da4326 96 new_size.x = 15;
6380910c
VZ
97 if (new_size.y == -1)
98 new_size.y = 30;
99
83624f79 100 PreCreation( parent, id, pos, new_size, style, name );
6380910c 101
8e189077
RR
102// SetValidator( validator );
103
83624f79 104 m_oldPos = 0.0;
8e189077 105
83624f79 106 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
6380910c 107
83624f79 108 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
6380910c 109
83624f79 110 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), (m_windowStyle & wxSP_WRAP) );
6380910c
VZ
111
112 gtk_signal_connect( GTK_OBJECT (m_adjust),
83624f79 113 "value_changed",
6380910c
VZ
114 (GtkSignalFunc) gtk_spinbutt_callback,
115 (gpointer) this );
116
f03fc89f 117 m_parent->DoAddChild( this );
6380910c 118
83624f79 119 PostCreation();
6380910c 120
83624f79 121 SetBackgroundColour( parent->GetBackgroundColour() );
8e189077 122
83624f79 123 Show( TRUE );
6380910c 124
83624f79 125 return TRUE;
8e189077
RR
126}
127
128wxSpinButton::~wxSpinButton()
129{
130}
131
132int wxSpinButton::GetMin() const
133{
9f15c5fe 134 wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") );
6380910c 135
66d78146 136 return (int)ceil(m_adjust->lower);
8e189077
RR
137}
138
139int wxSpinButton::GetMax() const
140{
9f15c5fe 141 wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") );
6380910c 142
66d78146 143 return (int)ceil(m_adjust->upper);
8e189077
RR
144}
145
146int wxSpinButton::GetValue() const
147{
9f15c5fe 148 wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") );
6380910c 149
66d78146 150 return (int)ceil(m_adjust->value);
8e189077
RR
151}
152
153void wxSpinButton::SetValue( int value )
154{
9f15c5fe 155 wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") );
6380910c 156
83624f79
RR
157 float fpos = (float)value;
158 m_oldPos = fpos;
6380910c
VZ
159 if (fabs(fpos-m_adjust->value) < sensitivity) return;
160
83624f79 161 m_adjust->value = fpos;
6380910c 162
83624f79 163 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
8e189077
RR
164}
165
166void wxSpinButton::SetRange(int minVal, int maxVal)
167{
9f15c5fe 168 wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") );
6380910c 169
83624f79
RR
170 float fmin = (float)minVal;
171 float fmax = (float)maxVal;
6380910c
VZ
172
173 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
174 (fabs(fmax-m_adjust->upper) < sensitivity))
83624f79
RR
175 {
176 return;
177 }
6380910c 178
83624f79
RR
179 m_adjust->lower = fmin;
180 m_adjust->upper = fmax;
8e189077 181
83624f79 182 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
8e189077
RR
183}
184
185void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
186{
9f15c5fe 187 wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") );
6380910c 188
19da4326 189 m_width = 15;
83624f79 190 gtk_widget_set_usize( m_widget, m_width, m_height );
8e189077
RR
191}
192
193bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
194{
83624f79 195 return GTK_SPIN_BUTTON(m_widget)->panel == window;
8e189077
RR
196}
197
198void wxSpinButton::ApplyWidgetStyle()
199{
83624f79
RR
200 SetWidgetStyle();
201 gtk_widget_set_style( m_widget, m_widgetStyle );
8e189077
RR
202}
203
204//-----------------------------------------------------------------------------
205// wxSpinEvent
206//-----------------------------------------------------------------------------
207
208IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxScrollEvent)
209
210wxSpinEvent::wxSpinEvent(wxEventType commandType, int id):
211 wxScrollEvent(commandType, id)
212{
213}
dcf924a3
RR
214
215#endif