]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/spinctrl.cpp
wxMenuBarBase for MSW (untested)
[wxWidgets.git] / src / gtk / 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 #ifdef wxUSE_SPINBTN
18
19 #include "wx/utils.h"
20 #include "wx/spinbutt.h"
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_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 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 // wxSpinCtrl
84 //-----------------------------------------------------------------------------
85
86 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl)
87
88 bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
89 const wxPoint& pos, const wxSize& size,
90 long style,
91 int min, int max, int initial,
92 const wxString& name)
93 {
94 m_needParent = TRUE;
95
96 wxSize new_size = size;
97 if (new_size.y == -1)
98 new_size.y = 26;
99
100 if (!PreCreation( parent, pos, new_size ) ||
101 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
102 {
103 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
104 return FALSE;
105 }
106
107 m_oldPos = initial;
108
109 m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0);
110
111 m_widget = gtk_spin_button_new( m_adjust, 1, 0 );
112
113 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), (m_windowStyle & wxSP_WRAP) );
114
115 gtk_signal_connect( GTK_OBJECT (m_adjust),
116 "value_changed",
117 (GtkSignalFunc) gtk_spinctrl_callback,
118 (gpointer) this );
119
120 m_parent->DoAddChild( this );
121
122 PostCreation();
123
124 SetBackgroundColour( parent->GetBackgroundColour() );
125
126 Show( TRUE );
127
128 return TRUE;
129 }
130
131 int wxSpinCtrl::GetMin() const
132 {
133 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
134
135 return (int)ceil(m_adjust->lower);
136 }
137
138 int wxSpinCtrl::GetMax() const
139 {
140 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
141
142 return (int)ceil(m_adjust->upper);
143 }
144
145 int wxSpinCtrl::GetValue() const
146 {
147 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
148
149 return (int)ceil(m_adjust->value);
150 }
151
152 void wxSpinCtrl::SetValue( int value )
153 {
154 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
155
156 float fpos = (float)value;
157 m_oldPos = fpos;
158 if (fabs(fpos-m_adjust->value) < sensitivity) return;
159
160 m_adjust->value = fpos;
161
162 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
163 }
164
165 void wxSpinCtrl::SetRange(int minVal, int maxVal)
166 {
167 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
168
169 float fmin = (float)minVal;
170 float fmax = (float)maxVal;
171
172 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
173 (fabs(fmax-m_adjust->upper) < sensitivity))
174 {
175 return;
176 }
177
178 m_adjust->lower = fmin;
179 m_adjust->upper = fmax;
180
181 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
182
183 // these two calls are required due to some bug in GTK
184 Refresh();
185 SetFocus();
186 }
187
188 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
189 {
190 return GTK_SPIN_BUTTON(m_widget)->panel == window;
191 }
192
193 void wxSpinCtrl::ApplyWidgetStyle()
194 {
195 SetWidgetStyle();
196 gtk_widget_set_style( m_widget, m_widgetStyle );
197 }
198
199 #endif