]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/spinctrl.cpp
Corrected miniframe.
[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
9d9b7755
VZ
78 wxSize new_size = size,
79 sizeBest = DoGetBestSize();
80 if (new_size.x == -1)
81 new_size.x = sizeBest.x;
738f9e5a 82 if (new_size.y == -1)
9d9b7755 83 new_size.y = sizeBest.y;
738f9e5a
RR
84
85 if (!PreCreation( parent, pos, new_size ) ||
86 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
87 {
88 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
9750fc42 89 return FALSE;
738f9e5a
RR
90 }
91
92 m_oldPos = initial;
93
94 m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0);
95
96 m_widget = gtk_spin_button_new( m_adjust, 1, 0 );
97
b02da6b1
VZ
98 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
99 (int)(m_windowStyle & wxSP_WRAP) );
738f9e5a
RR
100
101 gtk_signal_connect( GTK_OBJECT (m_adjust),
102 "value_changed",
103 (GtkSignalFunc) gtk_spinctrl_callback,
104 (gpointer) this );
105
106 m_parent->DoAddChild( this );
107
108 PostCreation();
109
110 SetBackgroundColour( parent->GetBackgroundColour() );
111
ce89fdd2
VZ
112 SetValue( value );
113
738f9e5a
RR
114 Show( TRUE );
115
116 return TRUE;
117}
118
119int wxSpinCtrl::GetMin() const
120{
121 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
122
123 return (int)ceil(m_adjust->lower);
124}
125
126int wxSpinCtrl::GetMax() const
127{
128 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
129
130 return (int)ceil(m_adjust->upper);
131}
132
133int wxSpinCtrl::GetValue() const
134{
135 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
136
137 return (int)ceil(m_adjust->value);
138}
139
ce89fdd2
VZ
140void wxSpinCtrl::SetValue( const wxString& value )
141{
142 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
143
144 int n;
145 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
146 {
147 // a number - set it
148 SetValue(n);
149 }
150 else
151 {
152 // invalid number - set text as is (wxMSW compatible)
7dd62924 153 gtk_entry_set_text( GTK_ENTRY(m_widget), value.mbc_str() );
ce89fdd2
VZ
154 }
155}
156
738f9e5a
RR
157void wxSpinCtrl::SetValue( int value )
158{
159 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
160
161 float fpos = (float)value;
162 m_oldPos = fpos;
163 if (fabs(fpos-m_adjust->value) < sensitivity) return;
164
165 m_adjust->value = fpos;
166
167 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
168}
169
170void wxSpinCtrl::SetRange(int minVal, int maxVal)
171{
172 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
173
174 float fmin = (float)minVal;
175 float fmax = (float)maxVal;
176
177 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
178 (fabs(fmax-m_adjust->upper) < sensitivity))
179 {
180 return;
181 }
182
183 m_adjust->lower = fmin;
184 m_adjust->upper = fmax;
185
186 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
0e0d8857 187
738f9e5a
RR
188 // these two calls are required due to some bug in GTK
189 Refresh();
190 SetFocus();
191}
192
da048e3d
RR
193void wxSpinCtrl::OnChar( wxKeyEvent &event )
194{
195 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
196
197 if (event.KeyCode() == WXK_RETURN)
198 {
199 wxWindow *top_frame = m_parent;
200 while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame))
201 top_frame = top_frame->GetParent();
9750fc42 202 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
0e0d8857 203
9750fc42 204 if (window->default_widget)
da048e3d
RR
205 {
206 gtk_widget_activate (window->default_widget);
9750fc42
VZ
207 return;
208 }
da048e3d
RR
209 }
210
211 event.Skip();
212}
213
738f9e5a
RR
214bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
215{
216 return GTK_SPIN_BUTTON(m_widget)->panel == window;
217}
218
219void wxSpinCtrl::ApplyWidgetStyle()
220{
221 SetWidgetStyle();
222 gtk_widget_set_style( m_widget, m_widgetStyle );
223}
224
9d9b7755
VZ
225wxSize wxSpinCtrl::DoGetBestSize() const
226{
227 return wxSize(95, 26);
228}
229
738f9e5a 230#endif
aec0ed2e 231 // wxUSE_SPINCTRL