1. more wxMotif fixes
[wxWidgets.git] / src / gtk / 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 #include <math.h>
21
22 #include "gdk/gdk.h"
23 #include "gtk/gtk.h"
24
25 //-----------------------------------------------------------------------------
26 // idle system
27 //-----------------------------------------------------------------------------
28
29 extern void wxapp_install_idle_handler();
30 extern bool g_isIdle;
31
32 //-----------------------------------------------------------------------------
33 // data
34 //-----------------------------------------------------------------------------
35
36 extern bool g_blockEventsOnDrag;
37
38 static const float sensitivity = 0.2;
39
40 //-----------------------------------------------------------------------------
41 // "value_changed"
42 //-----------------------------------------------------------------------------
43
44 static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win )
45 {
46 if (g_isIdle) wxapp_install_idle_handler();
47
48 if (!win->m_hasVMT) return;
49 if (g_blockEventsOnDrag) return;
50
51 float diff = win->m_adjust->value - win->m_oldPos;
52 if (fabs(diff) < sensitivity) return;
53 win->m_oldPos = win->m_adjust->value;
54
55 wxEventType command = wxEVT_NULL;
56
57 float line_step = win->m_adjust->step_increment;
58 float page_step = win->m_adjust->page_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 if (fabs(diff-page_step) < sensitivity) command = wxEVT_SCROLL_PAGEDOWN;
63 else if (fabs(diff+page_step) < sensitivity) command = wxEVT_SCROLL_PAGEUP;
64 else command = wxEVT_SCROLL_THUMBTRACK;
65
66 int value = (int)ceil(win->m_adjust->value);
67
68 wxSpinEvent event( command, win->GetId());
69 event.SetPosition( value );
70 event.SetOrientation( wxVERTICAL );
71 event.SetEventObject( win );
72
73 win->GetEventHandler()->ProcessEvent( event );
74 }
75
76 //-----------------------------------------------------------------------------
77 // wxSpinButton
78 //-----------------------------------------------------------------------------
79
80 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
81 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxScrollEvent);
82
83 BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
84 EVT_SIZE(wxSpinButton::OnSize)
85 END_EVENT_TABLE()
86
87 bool wxSpinButton::Create(wxWindow *parent,
88 wxWindowID id,
89 const wxPoint& pos,
90 const wxSize& size,
91 long style,
92 const wxString& name)
93 {
94 m_needParent = TRUE;
95
96 wxSize new_size = size;
97 new_size.x = 15;
98 if (new_size.y == -1)
99 new_size.y = 30;
100
101 PreCreation( parent, id, pos, new_size, style, name );
102
103 // SetValidator( validator );
104
105 m_oldPos = 0.0;
106
107 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
108
109 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
110
111 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), (m_windowStyle & wxSP_WRAP) );
112
113 gtk_signal_connect( GTK_OBJECT (m_adjust),
114 "value_changed",
115 (GtkSignalFunc) gtk_spinbutt_callback,
116 (gpointer) this );
117
118 m_parent->DoAddChild( this );
119
120 PostCreation();
121
122 SetBackgroundColour( parent->GetBackgroundColour() );
123
124 Show( TRUE );
125
126 return TRUE;
127 }
128
129 wxSpinButton::~wxSpinButton()
130 {
131 }
132
133 int wxSpinButton::GetMin() const
134 {
135 wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") );
136
137 return (int)ceil(m_adjust->lower);
138 }
139
140 int wxSpinButton::GetMax() const
141 {
142 wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") );
143
144 return (int)ceil(m_adjust->upper);
145 }
146
147 int wxSpinButton::GetValue() const
148 {
149 wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") );
150
151 return (int)ceil(m_adjust->value);
152 }
153
154 void wxSpinButton::SetValue( int value )
155 {
156 wxCHECK_RET( (m_widget != NULL), _T("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 wxSpinButton::SetRange(int minVal, int maxVal)
168 {
169 wxCHECK_RET( (m_widget != NULL), _T("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
186 void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
187 {
188 wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") );
189
190 m_width = 15;
191 gtk_widget_set_usize( m_widget, m_width, m_height );
192 }
193
194 bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
195 {
196 return GTK_SPIN_BUTTON(m_widget)->panel == window;
197 }
198
199 void wxSpinButton::ApplyWidgetStyle()
200 {
201 SetWidgetStyle();
202 gtk_widget_set_style( m_widget, m_widgetStyle );
203 }
204
205 #endif