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