]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/spinctrl.cpp
fixed typo wxUSE_ENCODING shuold be wxUSE_UNICODE
[wxWidgets.git] / src / gtk / spinctrl.cpp
CommitLineData
738f9e5a
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: spinbutt.cpp
3// Purpose: wxSpinCtrl
4// Author: Robert
5// Modified by:
6// RCS-ID: $Id$
7// Copyright: (c) Robert Roebling
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
12#pragma implementation "spinctrl.h"
13#endif
14
15#include "wx/spinctrl.h"
16
0e0d8857 17#if wxUSE_SPINCTRL
738f9e5a
RR
18
19#include "wx/utils.h"
aec0ed2e 20
738f9e5a
RR
21#include <math.h>
22
aec0ed2e
RR
23#include <gdk/gdk.h>
24#include <gtk/gtk.h>
738f9e5a
RR
25
26//-----------------------------------------------------------------------------
27// idle system
28//-----------------------------------------------------------------------------
29
30extern void wxapp_install_idle_handler();
31extern bool g_isIdle;
32
0c623889
RR
33static const float sensitivity = 0.02;
34
738f9e5a
RR
35//-----------------------------------------------------------------------------
36// data
37//-----------------------------------------------------------------------------
38
39extern bool g_blockEventsOnDrag;
40
738f9e5a
RR
41//-----------------------------------------------------------------------------
42// "value_changed"
43//-----------------------------------------------------------------------------
44
45static void gtk_spinctrl_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win )
46{
47 if (g_isIdle) wxapp_install_idle_handler();
48
49 if (!win->m_hasVMT) return;
50 if (g_blockEventsOnDrag) return;
51
58c837a4 52 wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
738f9e5a 53 event.SetEventObject( win );
58c837a4 54 event.SetInt( win->GetValue() );
738f9e5a 55 win->GetEventHandler()->ProcessEvent( event );
738f9e5a
RR
56}
57
58//-----------------------------------------------------------------------------
59// wxSpinCtrl
60//-----------------------------------------------------------------------------
61
62IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl)
63
da048e3d
RR
64BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
65 EVT_CHAR(wxSpinCtrl::OnChar)
66END_EVENT_TABLE()
67
738f9e5a 68bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
ce89fdd2
VZ
69 const wxString& value,
70 const wxPoint& pos, const wxSize& size,
71 long style,
72 int min, int max, int initial,
73 const wxString& name)
738f9e5a
RR
74{
75 m_needParent = TRUE;
354aa1e3 76 m_acceptsFocus = TRUE;
738f9e5a 77
0279e844
RR
78 if (!PreCreation( parent, pos, size ) ||
79 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
738f9e5a
RR
80 {
81 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
9750fc42 82 return FALSE;
738f9e5a
RR
83 }
84
85 m_oldPos = initial;
86
87 m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0);
88
89 m_widget = gtk_spin_button_new( m_adjust, 1, 0 );
0279e844
RR
90
91 wxSize new_size = size,
92 sizeBest = DoGetBestSize();
93 if (new_size.x == -1)
94 new_size.x = sizeBest.x;
95 if (new_size.y == -1)
96 new_size.y = sizeBest.y;
97
98 if ((new_size.x != size.x) || (new_size.y != size.y))
99 SetSize( new_size.x, new_size.y );
738f9e5a 100
b02da6b1
VZ
101 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
102 (int)(m_windowStyle & wxSP_WRAP) );
738f9e5a
RR
103
104 gtk_signal_connect( GTK_OBJECT (m_adjust),
105 "value_changed",
106 (GtkSignalFunc) gtk_spinctrl_callback,
107 (gpointer) this );
108
109 m_parent->DoAddChild( this );
110
111 PostCreation();
112
113 SetBackgroundColour( parent->GetBackgroundColour() );
114
ce89fdd2
VZ
115 SetValue( value );
116
738f9e5a
RR
117 Show( TRUE );
118
119 return TRUE;
120}
121
122int wxSpinCtrl::GetMin() const
123{
124 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
125
126 return (int)ceil(m_adjust->lower);
127}
128
129int wxSpinCtrl::GetMax() const
130{
131 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
132
133 return (int)ceil(m_adjust->upper);
134}
135
136int wxSpinCtrl::GetValue() const
137{
138 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
139
140 return (int)ceil(m_adjust->value);
141}
142
ce89fdd2
VZ
143void wxSpinCtrl::SetValue( const wxString& value )
144{
145 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
146
147 int n;
148 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
149 {
150 // a number - set it
151 SetValue(n);
152 }
153 else
154 {
155 // invalid number - set text as is (wxMSW compatible)
7dd62924 156 gtk_entry_set_text( GTK_ENTRY(m_widget), value.mbc_str() );
ce89fdd2
VZ
157 }
158}
159
738f9e5a
RR
160void wxSpinCtrl::SetValue( int value )
161{
162 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
163
164 float fpos = (float)value;
165 m_oldPos = fpos;
166 if (fabs(fpos-m_adjust->value) < sensitivity) return;
167
168 m_adjust->value = fpos;
169
170 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
171}
172
173void wxSpinCtrl::SetRange(int minVal, int maxVal)
174{
175 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
176
177 float fmin = (float)minVal;
178 float fmax = (float)maxVal;
179
180 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
181 (fabs(fmax-m_adjust->upper) < sensitivity))
182 {
183 return;
184 }
185
186 m_adjust->lower = fmin;
187 m_adjust->upper = fmax;
188
189 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
0e0d8857 190
738f9e5a
RR
191 // these two calls are required due to some bug in GTK
192 Refresh();
193 SetFocus();
194}
195
da048e3d
RR
196void wxSpinCtrl::OnChar( wxKeyEvent &event )
197{
198 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
199
200 if (event.KeyCode() == WXK_RETURN)
201 {
202 wxWindow *top_frame = m_parent;
203 while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame))
204 top_frame = top_frame->GetParent();
9750fc42 205 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
0e0d8857 206
9750fc42 207 if (window->default_widget)
da048e3d
RR
208 {
209 gtk_widget_activate (window->default_widget);
9750fc42
VZ
210 return;
211 }
da048e3d
RR
212 }
213
214 event.Skip();
215}
216
738f9e5a
RR
217bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
218{
219 return GTK_SPIN_BUTTON(m_widget)->panel == window;
220}
221
222void wxSpinCtrl::ApplyWidgetStyle()
223{
224 SetWidgetStyle();
225 gtk_widget_set_style( m_widget, m_widgetStyle );
226}
227
9d9b7755
VZ
228wxSize wxSpinCtrl::DoGetBestSize() const
229{
0279e844
RR
230 wxSize ret( wxControl::DoGetBestSize() );
231 return wxSize(95, ret.y);
9d9b7755
VZ
232}
233
738f9e5a 234#endif
aec0ed2e 235 // wxUSE_SPINCTRL