]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/spinbutt.cpp
Corrected some things in, and some thing revealed by
[wxWidgets.git] / src / gtk / 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"
aec0ed2e 20
f5368809 21#include <math.h>
8e189077 22
aec0ed2e
RR
23#include <gdk/gdk.h>
24#include <gtk/gtk.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;
828f655f 54 win->m_oldPos = win->m_adjust->value;
6380910c 55
83624f79 56 wxEventType command = wxEVT_NULL;
6380910c 57
83624f79 58 float line_step = win->m_adjust->step_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;
83624f79 62 else command = wxEVT_SCROLL_THUMBTRACK;
8e189077 63
66d78146 64 int value = (int)ceil(win->m_adjust->value);
6380910c 65
83624f79
RR
66 wxSpinEvent event( command, win->GetId());
67 event.SetPosition( value );
83624f79 68 event.SetEventObject( win );
828f655f 69 win->GetEventHandler()->ProcessEvent( event );
e65cc56a
RR
70
71 /* always send a thumbtrack event */
72 if (command != wxEVT_SCROLL_THUMBTRACK)
73 {
74 command = wxEVT_SCROLL_THUMBTRACK;
75 wxSpinEvent event2( command, win->GetId());
76 event2.SetPosition( value );
77 event2.SetEventObject( win );
78 win->GetEventHandler()->ProcessEvent( event2 );
79 }
8e189077
RR
80}
81
82//-----------------------------------------------------------------------------
83// wxSpinButton
84//-----------------------------------------------------------------------------
85
86IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
31528cd3 87IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxScrollEvent);
8e189077
RR
88
89BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
90 EVT_SIZE(wxSpinButton::OnSize)
91END_EVENT_TABLE()
92
31528cd3
VZ
93bool wxSpinButton::Create(wxWindow *parent,
94 wxWindowID id,
95 const wxPoint& pos,
96 const wxSize& size,
97 long style,
98 const wxString& name)
8e189077 99{
83624f79 100 m_needParent = TRUE;
6380910c 101
9d9b7755
VZ
102 wxSize new_size = size,
103 sizeBest = DoGetBestSize();
104 new_size.x = sizeBest.x; // override width always
6380910c 105 if (new_size.y == -1)
9d9b7755 106 new_size.y = sizeBest.y;
6380910c 107
2259e007 108 if (!PreCreation( parent, pos, new_size ) ||
4dcaf11a
RR
109 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
110 {
223d09f6 111 wxFAIL_MSG( wxT("wxXX creation failed") );
4dcaf11a
RR
112 return FALSE;
113 }
8e189077 114
83624f79 115 m_oldPos = 0.0;
8e189077 116
83624f79 117 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
6380910c 118
83624f79 119 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
6380910c 120
b02da6b1
VZ
121 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
122 (int)(m_windowStyle & wxSP_WRAP) );
6380910c
VZ
123
124 gtk_signal_connect( GTK_OBJECT (m_adjust),
83624f79 125 "value_changed",
6380910c
VZ
126 (GtkSignalFunc) gtk_spinbutt_callback,
127 (gpointer) this );
128
f03fc89f 129 m_parent->DoAddChild( this );
6380910c 130
83624f79 131 PostCreation();
6380910c 132
83624f79 133 SetBackgroundColour( parent->GetBackgroundColour() );
8e189077 134
83624f79 135 Show( TRUE );
6380910c 136
83624f79 137 return TRUE;
8e189077
RR
138}
139
8e189077
RR
140int wxSpinButton::GetMin() const
141{
223d09f6 142 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 143
66d78146 144 return (int)ceil(m_adjust->lower);
8e189077
RR
145}
146
147int wxSpinButton::GetMax() const
148{
223d09f6 149 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 150
66d78146 151 return (int)ceil(m_adjust->upper);
8e189077
RR
152}
153
154int wxSpinButton::GetValue() const
155{
223d09f6 156 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 157
66d78146 158 return (int)ceil(m_adjust->value);
8e189077
RR
159}
160
161void wxSpinButton::SetValue( int value )
162{
223d09f6 163 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 164
83624f79
RR
165 float fpos = (float)value;
166 m_oldPos = fpos;
6380910c
VZ
167 if (fabs(fpos-m_adjust->value) < sensitivity) return;
168
83624f79 169 m_adjust->value = fpos;
6380910c 170
83624f79 171 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
8e189077
RR
172}
173
174void wxSpinButton::SetRange(int minVal, int maxVal)
175{
223d09f6 176 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 177
83624f79
RR
178 float fmin = (float)minVal;
179 float fmax = (float)maxVal;
6380910c
VZ
180
181 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
182 (fabs(fmax-m_adjust->upper) < sensitivity))
83624f79
RR
183 {
184 return;
185 }
6380910c 186
83624f79
RR
187 m_adjust->lower = fmin;
188 m_adjust->upper = fmax;
8e189077 189
83624f79 190 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
65045edd
RR
191
192 // these two calls are required due to some bug in GTK
193 Refresh();
194 SetFocus();
8e189077
RR
195}
196
197void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
198{
223d09f6 199 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 200
9d9b7755 201 m_width = DoGetBestSize().x;
83624f79 202 gtk_widget_set_usize( m_widget, m_width, m_height );
8e189077
RR
203}
204
205bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
206{
83624f79 207 return GTK_SPIN_BUTTON(m_widget)->panel == window;
8e189077
RR
208}
209
210void wxSpinButton::ApplyWidgetStyle()
211{
83624f79
RR
212 SetWidgetStyle();
213 gtk_widget_set_style( m_widget, m_widgetStyle );
8e189077
RR
214}
215
9d9b7755
VZ
216wxSize wxSpinButton::DoGetBestSize() const
217{
218 return wxSize(15, 26);
219}
220
31528cd3 221#endif