]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/spinbutt.cpp
Prevents crashes caused by negative line count being passed to wrapping functions...
[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
9e691f46 22#include "wx/gtk/private.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{
acfd422a
RR
38 if (g_isIdle) wxapp_install_idle_handler();
39
a01ed326
PC
40 const double value = gtk_spin_button_get_value(spinbutton);
41 const int pos = int(value);
42 const int oldPos = win->m_pos;
43 if (!win->m_hasVMT || g_blockEventsOnDrag || win->m_blockScrollEvent || pos == oldPos)
44 {
45 win->m_pos = pos;
46 return;
47 }
6380910c 48
a01ed326
PC
49 wxSpinEvent event(pos > oldPos ? wxEVT_SCROLL_LINEUP : wxEVT_SCROLL_LINEDOWN, win->GetId());
50 event.SetPosition(pos);
51 event.SetEventObject(win);
0f42a871
RR
52
53 if ((win->GetEventHandler()->ProcessEvent( event )) &&
54 !event.IsAllowed() )
55 {
56 /* program has vetoed */
a01ed326
PC
57 win->BlockScrollEvent();
58 gtk_spin_button_set_value(spinbutton, oldPos);
59 win->UnblockScrollEvent();
0f42a871
RR
60 return;
61 }
88d19775 62
a01ed326 63 win->m_pos = pos;
88d19775 64
e65cc56a 65 /* always send a thumbtrack event */
a01ed326
PC
66 wxSpinEvent event2(wxEVT_SCROLL_THUMBTRACK, win->GetId());
67 event2.SetPosition(pos);
68 event2.SetEventObject(win);
69 win->GetEventHandler()->ProcessEvent(event2);
8e189077 70}
865bb325 71}
8e189077
RR
72
73//-----------------------------------------------------------------------------
74// wxSpinButton
75//-----------------------------------------------------------------------------
76
77IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
2fa7c206 78IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
8e189077
RR
79
80BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
81 EVT_SIZE(wxSpinButton::OnSize)
82END_EVENT_TABLE()
83
a01ed326
PC
84wxSpinButton::wxSpinButton()
85{
86 m_pos = 0;
87}
88
31528cd3
VZ
89bool wxSpinButton::Create(wxWindow *parent,
90 wxWindowID id,
91 const wxPoint& pos,
92 const wxSize& size,
93 long style,
94 const wxString& name)
8e189077 95{
de6185e2 96 m_needParent = true;
6380910c 97
9d9b7755
VZ
98 wxSize new_size = size,
99 sizeBest = DoGetBestSize();
100 new_size.x = sizeBest.x; // override width always
6380910c 101 if (new_size.y == -1)
9d9b7755 102 new_size.y = sizeBest.y;
6380910c 103
2259e007 104 if (!PreCreation( parent, pos, new_size ) ||
4dcaf11a
RR
105 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
106 {
a01ed326 107 wxFAIL_MSG( wxT("wxSpinButton creation failed") );
de6185e2 108 return false;
4dcaf11a 109 }
8e189077 110
a01ed326 111 m_pos = 0;
6380910c 112
a01ed326 113 m_widget = gtk_spin_button_new_with_range(0, 100, 1);
6380910c 114
b02da6b1
VZ
115 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
116 (int)(m_windowStyle & wxSP_WRAP) );
6380910c 117
a01ed326
PC
118 g_signal_connect_after(
119 m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this);
6380910c 120
f03fc89f 121 m_parent->DoAddChild( this );
6380910c 122
abdeb9e7 123 PostCreation(new_size);
6380910c 124
de6185e2 125 return true;
8e189077
RR
126}
127
8e189077
RR
128int wxSpinButton::GetMin() const
129{
223d09f6 130 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 131
a01ed326
PC
132 double min;
133 gtk_spin_button_get_range((GtkSpinButton*)m_widget, &min, NULL);
134 return int(min);
8e189077
RR
135}
136
137int wxSpinButton::GetMax() const
138{
223d09f6 139 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 140
a01ed326
PC
141 double max;
142 gtk_spin_button_get_range((GtkSpinButton*)m_widget, NULL, &max);
143 return int(max);
8e189077
RR
144}
145
146int wxSpinButton::GetValue() const
147{
223d09f6 148 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 149
a01ed326 150 return m_pos;
8e189077
RR
151}
152
153void wxSpinButton::SetValue( int value )
154{
223d09f6 155 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 156
a01ed326
PC
157 BlockScrollEvent();
158 gtk_spin_button_set_value((GtkSpinButton*)m_widget, value);
159 UnblockScrollEvent();
8e189077
RR
160}
161
162void wxSpinButton::SetRange(int minVal, int maxVal)
163{
223d09f6 164 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 165
a01ed326
PC
166 BlockScrollEvent();
167 gtk_spin_button_set_range((GtkSpinButton*)m_widget, minVal, maxVal);
168 UnblockScrollEvent();
8e189077
RR
169}
170
171void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
172{
223d09f6 173 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 174
9d9b7755 175 m_width = DoGetBestSize().x;
370dc79c 176 gtk_widget_set_size_request( m_widget, m_width, m_height );
8e189077
RR
177}
178
ef5c70f9 179GdkWindow *wxSpinButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
8e189077 180{
ef5c70f9 181 return GTK_SPIN_BUTTON(m_widget)->panel;
8e189077
RR
182}
183
9d9b7755
VZ
184wxSize wxSpinButton::DoGetBestSize() const
185{
9f884528
RD
186 wxSize best(15, 26); // FIXME
187 CacheBestSize(best);
188 return best;
9d9b7755
VZ
189}
190
9d522606
RD
191// static
192wxVisualAttributes
193wxSpinButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
194{
195 // TODO: overload to accept functions like gtk_spin_button_new?
196 // Until then use a similar type
197 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
198}
199
31528cd3 200#endif