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