]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/spinbutt.cpp
GUI thread update and event corrections.
[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
83624f79 102 wxSize new_size = size;
19da4326 103 new_size.x = 15;
6380910c 104 if (new_size.y == -1)
738f9e5a 105 new_size.y = 26;
6380910c 106
2259e007 107 if (!PreCreation( parent, pos, new_size ) ||
4dcaf11a
RR
108 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
109 {
223d09f6 110 wxFAIL_MSG( wxT("wxXX creation failed") );
4dcaf11a
RR
111 return FALSE;
112 }
8e189077 113
83624f79 114 m_oldPos = 0.0;
8e189077 115
83624f79 116 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
6380910c 117
83624f79 118 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
6380910c 119
b02da6b1
VZ
120 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
121 (int)(m_windowStyle & wxSP_WRAP) );
6380910c
VZ
122
123 gtk_signal_connect( GTK_OBJECT (m_adjust),
83624f79 124 "value_changed",
6380910c
VZ
125 (GtkSignalFunc) gtk_spinbutt_callback,
126 (gpointer) this );
127
f03fc89f 128 m_parent->DoAddChild( this );
6380910c 129
83624f79 130 PostCreation();
6380910c 131
83624f79 132 SetBackgroundColour( parent->GetBackgroundColour() );
8e189077 133
83624f79 134 Show( TRUE );
6380910c 135
83624f79 136 return TRUE;
8e189077
RR
137}
138
8e189077
RR
139int wxSpinButton::GetMin() const
140{
223d09f6 141 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 142
66d78146 143 return (int)ceil(m_adjust->lower);
8e189077
RR
144}
145
146int wxSpinButton::GetMax() const
147{
223d09f6 148 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 149
66d78146 150 return (int)ceil(m_adjust->upper);
8e189077
RR
151}
152
153int wxSpinButton::GetValue() const
154{
223d09f6 155 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 156
66d78146 157 return (int)ceil(m_adjust->value);
8e189077
RR
158}
159
160void wxSpinButton::SetValue( int value )
161{
223d09f6 162 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 163
83624f79
RR
164 float fpos = (float)value;
165 m_oldPos = fpos;
6380910c
VZ
166 if (fabs(fpos-m_adjust->value) < sensitivity) return;
167
83624f79 168 m_adjust->value = fpos;
6380910c 169
83624f79 170 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
8e189077
RR
171}
172
173void wxSpinButton::SetRange(int minVal, int maxVal)
174{
223d09f6 175 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 176
83624f79
RR
177 float fmin = (float)minVal;
178 float fmax = (float)maxVal;
6380910c
VZ
179
180 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
181 (fabs(fmax-m_adjust->upper) < sensitivity))
83624f79
RR
182 {
183 return;
184 }
6380910c 185
83624f79
RR
186 m_adjust->lower = fmin;
187 m_adjust->upper = fmax;
8e189077 188
83624f79 189 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
65045edd
RR
190
191 // these two calls are required due to some bug in GTK
192 Refresh();
193 SetFocus();
8e189077
RR
194}
195
196void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
197{
223d09f6 198 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 199
19da4326 200 m_width = 15;
83624f79 201 gtk_widget_set_usize( m_widget, m_width, m_height );
8e189077
RR
202}
203
204bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
205{
83624f79 206 return GTK_SPIN_BUTTON(m_widget)->panel == window;
8e189077
RR
207}
208
209void wxSpinButton::ApplyWidgetStyle()
210{
83624f79
RR
211 SetWidgetStyle();
212 gtk_widget_set_style( m_widget, m_widgetStyle );
8e189077
RR
213}
214
31528cd3 215#endif