1. wxCalendarCtrl
[wxWidgets.git] / src / gtk1 / spinctrl.cpp
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
17 #if wxUSE_SPINCTRL
18
19 #include "wx/utils.h"
20
21 #include <math.h>
22
23 #include <gdk/gdk.h>
24 #include <gtk/gtk.h>
25
26 //-----------------------------------------------------------------------------
27 // idle system
28 //-----------------------------------------------------------------------------
29
30 extern void wxapp_install_idle_handler();
31 extern bool g_isIdle;
32
33 static const float sensitivity = 0.02;
34
35 //-----------------------------------------------------------------------------
36 // data
37 //-----------------------------------------------------------------------------
38
39 extern bool g_blockEventsOnDrag;
40
41 //-----------------------------------------------------------------------------
42 // "value_changed"
43 //-----------------------------------------------------------------------------
44
45 static 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
52 wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
53 event.SetEventObject( win );
54 event.SetInt( win->GetValue() );
55 win->GetEventHandler()->ProcessEvent( event );
56 }
57
58 //-----------------------------------------------------------------------------
59 // wxSpinCtrl
60 //-----------------------------------------------------------------------------
61
62 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl)
63
64 BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
65 EVT_CHAR(wxSpinCtrl::OnChar)
66 END_EVENT_TABLE()
67
68 bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
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)
74 {
75 m_needParent = TRUE;
76 m_acceptsFocus = TRUE;
77
78 wxSize new_size = size,
79 sizeBest = DoGetBestSize();
80 if (new_size.x == -1)
81 new_size.x = sizeBest.x;
82 if (new_size.y == -1)
83 new_size.y = sizeBest.y;
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") );
89 return FALSE;
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
98 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
99 (int)(m_windowStyle & wxSP_WRAP) );
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
112 SetValue( value );
113
114 Show( TRUE );
115
116 return TRUE;
117 }
118
119 int 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
126 int 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
133 int 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
140 void 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)
153 gtk_entry_set_text( GTK_ENTRY(m_widget), value.mbc_str() );
154 }
155 }
156
157 void 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
170 void 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" );
187
188 // these two calls are required due to some bug in GTK
189 Refresh();
190 SetFocus();
191 }
192
193 void 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();
202 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
203
204 if (window->default_widget)
205 {
206 gtk_widget_activate (window->default_widget);
207 return;
208 }
209 }
210
211 event.Skip();
212 }
213
214 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
215 {
216 return GTK_SPIN_BUTTON(m_widget)->panel == window;
217 }
218
219 void wxSpinCtrl::ApplyWidgetStyle()
220 {
221 SetWidgetStyle();
222 gtk_widget_set_style( m_widget, m_widgetStyle );
223 }
224
225 wxSize wxSpinCtrl::DoGetBestSize() const
226 {
227 return wxSize(95, 26);
228 }
229
230 #endif
231 // wxUSE_SPINCTRL