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