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