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