]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/spinbutt.cpp
Add wxAnyScrollHelperBase to reduce code duplication in wxVarScrollHelperBase.
[wxWidgets.git] / src / gtk / spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/spinbutt.cpp
3 // Purpose: wxSpinButton
4 // Author: Robert
5 // Modified by:
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_SPINBTN
14
15 #include "wx/spinbutt.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/utils.h"
19 #endif
20
21 #include <gtk/gtk.h>
22
23 //-----------------------------------------------------------------------------
24 // data
25 //-----------------------------------------------------------------------------
26
27 extern bool g_blockEventsOnDrag;
28
29 //-----------------------------------------------------------------------------
30 // "value_changed"
31 //-----------------------------------------------------------------------------
32
33 extern "C" {
34 static void
35 gtk_value_changed(GtkSpinButton* spinbutton, wxSpinButton* win)
36 {
37 const double value = gtk_spin_button_get_value(spinbutton);
38 const int pos = int(value);
39 const int oldPos = win->m_pos;
40 if (g_blockEventsOnDrag || pos == oldPos)
41 {
42 win->m_pos = pos;
43 return;
44 }
45
46 wxSpinEvent event(pos > oldPos ? wxEVT_SCROLL_LINEUP : wxEVT_SCROLL_LINEDOWN, win->GetId());
47 event.SetPosition(pos);
48 event.SetEventObject(win);
49
50 if ((win->HandleWindowEvent( event )) &&
51 !event.IsAllowed() )
52 {
53 /* program has vetoed */
54 // this will cause another "value_changed" signal,
55 // but because pos == oldPos nothing will happen
56 gtk_spin_button_set_value(spinbutton, oldPos);
57 return;
58 }
59
60 win->m_pos = pos;
61
62 /* always send a thumbtrack event */
63 wxSpinEvent event2(wxEVT_SCROLL_THUMBTRACK, win->GetId());
64 event2.SetPosition(pos);
65 event2.SetEventObject(win);
66 win->HandleWindowEvent(event2);
67 }
68 }
69
70 //-----------------------------------------------------------------------------
71 // wxSpinButton
72 //-----------------------------------------------------------------------------
73
74 BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
75 EVT_SIZE(wxSpinButton::OnSize)
76 END_EVENT_TABLE()
77
78 wxSpinButton::wxSpinButton()
79 {
80 m_pos = 0;
81 }
82
83 bool wxSpinButton::Create(wxWindow *parent,
84 wxWindowID id,
85 const wxPoint& pos,
86 const wxSize& size,
87 long style,
88 const wxString& name)
89 {
90 wxSize new_size = size,
91 sizeBest = DoGetBestSize();
92 new_size.x = sizeBest.x; // override width always
93 if (new_size.y == -1)
94 new_size.y = sizeBest.y;
95
96 if (!PreCreation( parent, pos, new_size ) ||
97 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
98 {
99 wxFAIL_MSG( wxT("wxSpinButton creation failed") );
100 return false;
101 }
102
103 m_pos = 0;
104
105 m_widget = gtk_spin_button_new_with_range(0, 100, 1);
106 g_object_ref(m_widget);
107
108 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
109 (int)(m_windowStyle & wxSP_WRAP) );
110
111 g_signal_connect_after(
112 m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this);
113
114 m_parent->DoAddChild( this );
115
116 PostCreation(new_size);
117
118 return true;
119 }
120
121 int wxSpinButton::GetMin() const
122 {
123 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
124
125 double min;
126 gtk_spin_button_get_range((GtkSpinButton*)m_widget, &min, NULL);
127 return int(min);
128 }
129
130 int wxSpinButton::GetMax() const
131 {
132 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
133
134 double max;
135 gtk_spin_button_get_range((GtkSpinButton*)m_widget, NULL, &max);
136 return int(max);
137 }
138
139 int wxSpinButton::GetValue() const
140 {
141 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
142
143 return m_pos;
144 }
145
146 void wxSpinButton::SetValue( int value )
147 {
148 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
149
150 GtkDisableEvents();
151 gtk_spin_button_set_value((GtkSpinButton*)m_widget, value);
152 m_pos = int(gtk_spin_button_get_value((GtkSpinButton*)m_widget));
153 GtkEnableEvents();
154 }
155
156 void wxSpinButton::SetRange(int minVal, int maxVal)
157 {
158 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
159
160 GtkDisableEvents();
161 gtk_spin_button_set_range((GtkSpinButton*)m_widget, minVal, maxVal);
162 m_pos = int(gtk_spin_button_get_value((GtkSpinButton*)m_widget));
163 GtkEnableEvents();
164 }
165
166 void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
167 {
168 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
169
170 m_width = DoGetBestSize().x;
171 gtk_widget_set_size_request( m_widget, m_width, m_height );
172 }
173
174 bool wxSpinButton::Enable( bool enable )
175 {
176 if (!base_type::Enable(enable))
177 return false;
178
179 // Work around lack of visual update when enabling
180 if (enable)
181 GTKFixSensitivity(false /* fix even if not under mouse */);
182
183 return true;
184 }
185
186 void wxSpinButton::GtkDisableEvents() const
187 {
188 g_signal_handlers_block_by_func(m_widget,
189 (gpointer)gtk_value_changed, (void*) this);
190 }
191
192 void wxSpinButton::GtkEnableEvents() const
193 {
194 g_signal_handlers_unblock_by_func(m_widget,
195 (gpointer)gtk_value_changed, (void*) this);
196 }
197
198 GdkWindow *wxSpinButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
199 {
200 #ifdef __WXGTK3__
201 // no access to internal GdkWindows
202 return NULL;
203 #else
204 return GTK_SPIN_BUTTON(m_widget)->panel;
205 #endif
206 }
207
208 wxSize wxSpinButton::DoGetBestSize() const
209 {
210 wxSize best(15, 26); // FIXME
211 CacheBestSize(best);
212 return best;
213 }
214
215 // static
216 wxVisualAttributes
217 wxSpinButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
218 {
219 return GetDefaultAttributesFromGTKWidget(gtk_spin_button_new_with_range(0, 100, 1));
220 }
221
222 #endif