]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/spinbutt.cpp
allow adjusting the combo button size (patch 1489452)
[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
463c4d71 22#include "wx/math.h"
9e691f46 23#include "wx/gtk/private.h"
83624f79 24
8e189077
RR
25//-----------------------------------------------------------------------------
26// data
27//-----------------------------------------------------------------------------
28
29extern bool g_blockEventsOnDrag;
30
738f9e5a 31static const float sensitivity = 0.02;
6380910c 32
8e189077
RR
33//-----------------------------------------------------------------------------
34// "value_changed"
35//-----------------------------------------------------------------------------
36
865bb325 37extern "C" {
8e189077 38static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win )
6380910c 39{
acfd422a
RR
40 if (g_isIdle) wxapp_install_idle_handler();
41
a2053b27 42 if (!win->m_hasVMT) return;
83624f79 43 if (g_blockEventsOnDrag) return;
828f655f 44
83624f79 45 float diff = win->m_adjust->value - win->m_oldPos;
6380910c 46 if (fabs(diff) < sensitivity) return;
6380910c 47
83624f79 48 wxEventType command = wxEVT_NULL;
6380910c 49
83624f79 50 float line_step = win->m_adjust->step_increment;
6380910c 51
0f42a871
RR
52 if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP;
53 else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN;
83624f79 54 else command = wxEVT_SCROLL_THUMBTRACK;
8e189077 55
66d78146 56 int value = (int)ceil(win->m_adjust->value);
6380910c 57
83624f79
RR
58 wxSpinEvent event( command, win->GetId());
59 event.SetPosition( value );
83624f79 60 event.SetEventObject( win );
0f42a871
RR
61
62 if ((win->GetEventHandler()->ProcessEvent( event )) &&
63 !event.IsAllowed() )
64 {
65 /* program has vetoed */
66 win->m_adjust->value = win->m_oldPos;
88d19775 67
9fa72bd2
MR
68 g_signal_handlers_disconnect_by_func (win->m_adjust,
69 (gpointer) gtk_spinbutt_callback,
70 win);
88d19775 71
9fa72bd2 72 g_signal_emit_by_name (win->m_adjust, "value_changed");
0f42a871 73
9fa72bd2
MR
74 g_signal_connect (win->m_adjust, "value_changed",
75 G_CALLBACK (gtk_spinbutt_callback), win);
0f42a871
RR
76 return;
77 }
88d19775 78
0f42a871 79 win->m_oldPos = win->m_adjust->value;
88d19775 80
e65cc56a
RR
81 /* always send a thumbtrack event */
82 if (command != wxEVT_SCROLL_THUMBTRACK)
83 {
84 command = wxEVT_SCROLL_THUMBTRACK;
85 wxSpinEvent event2( command, win->GetId());
86 event2.SetPosition( value );
87 event2.SetEventObject( win );
88 win->GetEventHandler()->ProcessEvent( event2 );
89 }
8e189077 90}
865bb325 91}
8e189077
RR
92
93//-----------------------------------------------------------------------------
94// wxSpinButton
95//-----------------------------------------------------------------------------
96
97IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
2fa7c206 98IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
8e189077
RR
99
100BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
101 EVT_SIZE(wxSpinButton::OnSize)
102END_EVENT_TABLE()
103
31528cd3
VZ
104bool wxSpinButton::Create(wxWindow *parent,
105 wxWindowID id,
106 const wxPoint& pos,
107 const wxSize& size,
108 long style,
109 const wxString& name)
8e189077 110{
de6185e2 111 m_needParent = true;
6380910c 112
9d9b7755
VZ
113 wxSize new_size = size,
114 sizeBest = DoGetBestSize();
115 new_size.x = sizeBest.x; // override width always
6380910c 116 if (new_size.y == -1)
9d9b7755 117 new_size.y = sizeBest.y;
6380910c 118
2259e007 119 if (!PreCreation( parent, pos, new_size ) ||
4dcaf11a
RR
120 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
121 {
223d09f6 122 wxFAIL_MSG( wxT("wxXX creation failed") );
de6185e2 123 return false;
4dcaf11a 124 }
8e189077 125
83624f79 126 m_oldPos = 0.0;
8e189077 127
83624f79 128 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
6380910c 129
83624f79 130 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
6380910c 131
b02da6b1
VZ
132 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
133 (int)(m_windowStyle & wxSP_WRAP) );
6380910c 134
9fa72bd2
MR
135 g_signal_connect (m_adjust, "value_changed",
136 G_CALLBACK (gtk_spinbutt_callback), this);
6380910c 137
f03fc89f 138 m_parent->DoAddChild( this );
6380910c 139
abdeb9e7 140 PostCreation(new_size);
6380910c 141
de6185e2 142 return true;
8e189077
RR
143}
144
8e189077
RR
145int wxSpinButton::GetMin() const
146{
223d09f6 147 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 148
66d78146 149 return (int)ceil(m_adjust->lower);
8e189077
RR
150}
151
152int wxSpinButton::GetMax() const
153{
223d09f6 154 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 155
66d78146 156 return (int)ceil(m_adjust->upper);
8e189077
RR
157}
158
159int wxSpinButton::GetValue() const
160{
223d09f6 161 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
6380910c 162
66d78146 163 return (int)ceil(m_adjust->value);
8e189077
RR
164}
165
166void wxSpinButton::SetValue( int value )
167{
223d09f6 168 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 169
83624f79
RR
170 float fpos = (float)value;
171 m_oldPos = fpos;
6380910c
VZ
172 if (fabs(fpos-m_adjust->value) < sensitivity) return;
173
83624f79 174 m_adjust->value = fpos;
6380910c 175
9fa72bd2 176 g_signal_emit_by_name (m_adjust, "value_changed");
8e189077
RR
177}
178
179void wxSpinButton::SetRange(int minVal, int maxVal)
180{
223d09f6 181 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 182
83624f79
RR
183 float fmin = (float)minVal;
184 float fmax = (float)maxVal;
6380910c
VZ
185
186 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
187 (fabs(fmax-m_adjust->upper) < sensitivity))
83624f79
RR
188 {
189 return;
190 }
6380910c 191
83624f79
RR
192 m_adjust->lower = fmin;
193 m_adjust->upper = fmax;
8e189077 194
9fa72bd2 195 g_signal_emit_by_name (m_adjust, "changed");
88d19775 196
65045edd
RR
197 // these two calls are required due to some bug in GTK
198 Refresh();
199 SetFocus();
8e189077
RR
200}
201
202void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
203{
223d09f6 204 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
6380910c 205
9d9b7755 206 m_width = DoGetBestSize().x;
370dc79c 207 gtk_widget_set_size_request( m_widget, m_width, m_height );
8e189077
RR
208}
209
210bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
211{
83624f79 212 return GTK_SPIN_BUTTON(m_widget)->panel == window;
8e189077
RR
213}
214
9d9b7755
VZ
215wxSize wxSpinButton::DoGetBestSize() const
216{
9f884528
RD
217 wxSize best(15, 26); // FIXME
218 CacheBestSize(best);
219 return best;
9d9b7755
VZ
220}
221
9d522606
RD
222// static
223wxVisualAttributes
224wxSpinButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
225{
226 // TODO: overload to accept functions like gtk_spin_button_new?
227 // Until then use a similar type
228 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
229}
230
31528cd3 231#endif