]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/spinbutt.cpp
Fixed toolbar crash for MinGW/Cygwin
[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__
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
aec0ed2e
RR
24#include <gdk/gdk.h>
25#include <gtk/gtk.h>
83624f79 26
acfd422a
RR
27//-----------------------------------------------------------------------------
28// idle system
29//-----------------------------------------------------------------------------
30
31extern void wxapp_install_idle_handler();
32extern bool g_isIdle;
33
8e189077
RR
34//-----------------------------------------------------------------------------
35// data
36//-----------------------------------------------------------------------------
37
38extern bool g_blockEventsOnDrag;
39
738f9e5a 40static const float sensitivity = 0.02;
6380910c 41
8e189077
RR
42//-----------------------------------------------------------------------------
43// "value_changed"
44//-----------------------------------------------------------------------------
45
46static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win )
6380910c 47{
acfd422a
RR
48 if (g_isIdle) wxapp_install_idle_handler();
49
a2053b27 50 if (!win->m_hasVMT) return;
83624f79 51 if (g_blockEventsOnDrag) return;
828f655f 52
83624f79 53 float diff = win->m_adjust->value - win->m_oldPos;
6380910c 54 if (fabs(diff) < sensitivity) return;
6380910c 55
83624f79 56 wxEventType command = wxEVT_NULL;
6380910c 57
83624f79 58 float line_step = win->m_adjust->step_increment;
6380910c 59
0f42a871
RR
60 if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP;
61 else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN;
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 );
0f42a871
RR
69
70 if ((win->GetEventHandler()->ProcessEvent( event )) &&
71 !event.IsAllowed() )
72 {
73 /* program has vetoed */
74 win->m_adjust->value = win->m_oldPos;
75
76 gtk_signal_disconnect_by_func( GTK_OBJECT (win->m_adjust),
77 (GtkSignalFunc) gtk_spinbutt_callback,
78 (gpointer) win );
79
80 gtk_signal_emit_by_name( GTK_OBJECT(win->m_adjust), "value_changed" );
81
82 gtk_signal_connect( GTK_OBJECT (win->m_adjust),
83 "value_changed",
84 (GtkSignalFunc) gtk_spinbutt_callback,
85 (gpointer) win );
86 return;
87 }
88
89 win->m_oldPos = win->m_adjust->value;
e65cc56a
RR
90
91 /* always send a thumbtrack event */
92 if (command != wxEVT_SCROLL_THUMBTRACK)
93 {
94 command = wxEVT_SCROLL_THUMBTRACK;
95 wxSpinEvent event2( command, win->GetId());
96 event2.SetPosition( value );
97 event2.SetEventObject( win );
98 win->GetEventHandler()->ProcessEvent( event2 );
99 }
8e189077
RR
100}
101
102//-----------------------------------------------------------------------------
103// wxSpinButton
104//-----------------------------------------------------------------------------
105
106IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
2fa7c206 107IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
8e189077
RR
108
109BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
110 EVT_SIZE(wxSpinButton::OnSize)
111END_EVENT_TABLE()
112
31528cd3
VZ
113bool wxSpinButton::Create(wxWindow *parent,
114 wxWindowID id,
115 const wxPoint& pos,
116 const wxSize& size,
117 long style,
118 const wxString& name)
8e189077 119{
83624f79 120 m_needParent = TRUE;
6380910c 121
9d9b7755
VZ
122 wxSize new_size = size,
123 sizeBest = DoGetBestSize();
124 new_size.x = sizeBest.x; // override width always
6380910c 125 if (new_size.y == -1)
9d9b7755 126 new_size.y = sizeBest.y;
6380910c 127
2259e007 128 if (!PreCreation( parent, pos, new_size ) ||
4dcaf11a
RR
129 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
130 {
223d09f6 131 wxFAIL_MSG( wxT("wxXX creation failed") );
0f42a871 132 return FALSE;
4dcaf11a 133 }
8e189077 134
83624f79 135 m_oldPos = 0.0;
8e189077 136
83624f79 137 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
6380910c 138
83624f79 139 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
6380910c 140
b02da6b1
VZ
141 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
142 (int)(m_windowStyle & wxSP_WRAP) );
6380910c
VZ
143
144 gtk_signal_connect( GTK_OBJECT (m_adjust),
83624f79 145 "value_changed",
6380910c
VZ
146 (GtkSignalFunc) gtk_spinbutt_callback,
147 (gpointer) this );
148
f03fc89f 149 m_parent->DoAddChild( this );
6380910c 150
83624f79 151 PostCreation();
6380910c 152
83624f79 153 SetBackgroundColour( parent->GetBackgroundColour() );
8e189077 154
83624f79 155 Show( TRUE );
6380910c 156
83624f79 157 return TRUE;
8e189077
RR
158}
159
8e189077
RR
160int wxSpinButton::GetMin() const
161{
223d09f6 162 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 163
66d78146 164 return (int)ceil(m_adjust->lower);
8e189077
RR
165}
166
167int wxSpinButton::GetMax() const
168{
223d09f6 169 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 170
66d78146 171 return (int)ceil(m_adjust->upper);
8e189077
RR
172}
173
174int wxSpinButton::GetValue() const
175{
223d09f6 176 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 177
66d78146 178 return (int)ceil(m_adjust->value);
8e189077
RR
179}
180
181void wxSpinButton::SetValue( int value )
182{
223d09f6 183 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 184
83624f79
RR
185 float fpos = (float)value;
186 m_oldPos = fpos;
6380910c
VZ
187 if (fabs(fpos-m_adjust->value) < sensitivity) return;
188
83624f79 189 m_adjust->value = fpos;
6380910c 190
83624f79 191 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
8e189077
RR
192}
193
194void wxSpinButton::SetRange(int minVal, int maxVal)
195{
223d09f6 196 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 197
83624f79
RR
198 float fmin = (float)minVal;
199 float fmax = (float)maxVal;
6380910c
VZ
200
201 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
202 (fabs(fmax-m_adjust->upper) < sensitivity))
83624f79
RR
203 {
204 return;
205 }
6380910c 206
83624f79
RR
207 m_adjust->lower = fmin;
208 m_adjust->upper = fmax;
8e189077 209
83624f79 210 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
65045edd
RR
211
212 // these two calls are required due to some bug in GTK
213 Refresh();
214 SetFocus();
8e189077
RR
215}
216
217void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
218{
223d09f6 219 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 220
9d9b7755 221 m_width = DoGetBestSize().x;
83624f79 222 gtk_widget_set_usize( m_widget, m_width, m_height );
8e189077
RR
223}
224
225bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
226{
83624f79 227 return GTK_SPIN_BUTTON(m_widget)->panel == window;
8e189077
RR
228}
229
230void wxSpinButton::ApplyWidgetStyle()
231{
83624f79
RR
232 SetWidgetStyle();
233 gtk_widget_set_style( m_widget, m_widgetStyle );
8e189077
RR
234}
235
9d9b7755
VZ
236wxSize wxSpinButton::DoGetBestSize() const
237{
238 return wxSize(15, 26);
239}
240
31528cd3 241#endif