]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/spinbutt.cpp
1. wxMotif toolbar works; the toggle buttons have the same width as the other
[wxWidgets.git] / src / gtk1 / spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: spinbutt.cpp
3 // Purpose: wxSpinButton
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 "spinbutt.h"
13 #endif
14
15 #include "wx/spinbutt.h"
16
17 #ifdef wxUSE_SPINBTN
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 //-----------------------------------------------------------------------------
34 // data
35 //-----------------------------------------------------------------------------
36
37 extern bool g_blockEventsOnDrag;
38
39 static const float sensitivity = 0.02;
40
41 //-----------------------------------------------------------------------------
42 // "value_changed"
43 //-----------------------------------------------------------------------------
44
45 static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win )
46 {
47 if (g_isIdle) wxapp_install_idle_handler();
48
49 if (!win->m_hasVMT) return;
50 if (g_blockEventsOnDrag) return;
51
52 float diff = win->m_adjust->value - win->m_oldPos;
53 if (fabs(diff) < sensitivity) return;
54 win->m_oldPos = win->m_adjust->value;
55
56 wxEventType command = wxEVT_NULL;
57
58 float line_step = win->m_adjust->step_increment;
59
60 if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN;
61 else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP;
62 else command = wxEVT_SCROLL_THUMBTRACK;
63
64 int value = (int)ceil(win->m_adjust->value);
65
66 wxSpinEvent event( command, win->GetId());
67 event.SetPosition( value );
68 event.SetEventObject( win );
69 win->GetEventHandler()->ProcessEvent( event );
70
71 /* always send a thumbtrack event */
72 if (command != wxEVT_SCROLL_THUMBTRACK)
73 {
74 command = wxEVT_SCROLL_THUMBTRACK;
75 wxSpinEvent event2( command, win->GetId());
76 event2.SetPosition( value );
77 event2.SetEventObject( win );
78 win->GetEventHandler()->ProcessEvent( event2 );
79 }
80 }
81
82 //-----------------------------------------------------------------------------
83 // wxSpinButton
84 //-----------------------------------------------------------------------------
85
86 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
87 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxScrollEvent);
88
89 BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
90 EVT_SIZE(wxSpinButton::OnSize)
91 END_EVENT_TABLE()
92
93 bool wxSpinButton::Create(wxWindow *parent,
94 wxWindowID id,
95 const wxPoint& pos,
96 const wxSize& size,
97 long style,
98 const wxString& name)
99 {
100 m_needParent = TRUE;
101
102 wxSize new_size = size;
103 new_size.x = 15;
104 if (new_size.y == -1)
105 new_size.y = 26;
106
107 if (!PreCreation( parent, pos, new_size ) ||
108 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
109 {
110 wxFAIL_MSG( wxT("wxXX creation failed") );
111 return FALSE;
112 }
113
114 m_oldPos = 0.0;
115
116 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
117
118 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
119
120 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
121 (int)(m_windowStyle & wxSP_WRAP) );
122
123 gtk_signal_connect( GTK_OBJECT (m_adjust),
124 "value_changed",
125 (GtkSignalFunc) gtk_spinbutt_callback,
126 (gpointer) this );
127
128 m_parent->DoAddChild( this );
129
130 PostCreation();
131
132 SetBackgroundColour( parent->GetBackgroundColour() );
133
134 Show( TRUE );
135
136 return TRUE;
137 }
138
139 int wxSpinButton::GetMin() const
140 {
141 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
142
143 return (int)ceil(m_adjust->lower);
144 }
145
146 int wxSpinButton::GetMax() const
147 {
148 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
149
150 return (int)ceil(m_adjust->upper);
151 }
152
153 int wxSpinButton::GetValue() const
154 {
155 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
156
157 return (int)ceil(m_adjust->value);
158 }
159
160 void wxSpinButton::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
173 void wxSpinButton::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" );
190
191 // these two calls are required due to some bug in GTK
192 Refresh();
193 SetFocus();
194 }
195
196 void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
197 {
198 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
199
200 m_width = 15;
201 gtk_widget_set_usize( m_widget, m_width, m_height );
202 }
203
204 bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
205 {
206 return GTK_SPIN_BUTTON(m_widget)->panel == window;
207 }
208
209 void wxSpinButton::ApplyWidgetStyle()
210 {
211 SetWidgetStyle();
212 gtk_widget_set_style( m_widget, m_widgetStyle );
213 }
214
215 #endif