]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/spinbutt.cpp
gtk spinctrl code simplification
[wxWidgets.git] / src / gtk / spinbutt.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/spinbutt.cpp
3// Purpose: wxSpinButton
4// Author: Robert
5// Modified by:
6// RCS-ID: $Id$
7// Copyright: (c) Robert Roebling
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#if wxUSE_SPINBTN
15
16#include "wx/spinbutt.h"
17
18#ifndef WX_PRECOMP
19 #include "wx/utils.h"
20#endif
21
22#include "wx/gtk/private.h"
23
24//-----------------------------------------------------------------------------
25// data
26//-----------------------------------------------------------------------------
27
28extern bool g_blockEventsOnDrag;
29
30//-----------------------------------------------------------------------------
31// "value_changed"
32//-----------------------------------------------------------------------------
33
34extern "C" {
35static void
36gtk_value_changed(GtkSpinButton* spinbutton, wxSpinButton* win)
37{
38 if (g_isIdle) wxapp_install_idle_handler();
39
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 }
48
49 wxSpinEvent event(pos > oldPos ? wxEVT_SCROLL_LINEUP : wxEVT_SCROLL_LINEDOWN, win->GetId());
50 event.SetPosition(pos);
51 event.SetEventObject(win);
52
53 if ((win->GetEventHandler()->ProcessEvent( event )) &&
54 !event.IsAllowed() )
55 {
56 /* program has vetoed */
57 win->BlockScrollEvent();
58 gtk_spin_button_set_value(spinbutton, oldPos);
59 win->UnblockScrollEvent();
60 return;
61 }
62
63 win->m_pos = pos;
64
65 /* always send a thumbtrack event */
66 wxSpinEvent event2(wxEVT_SCROLL_THUMBTRACK, win->GetId());
67 event2.SetPosition(pos);
68 event2.SetEventObject(win);
69 win->GetEventHandler()->ProcessEvent(event2);
70}
71}
72
73//-----------------------------------------------------------------------------
74// wxSpinButton
75//-----------------------------------------------------------------------------
76
77IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
78IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
79
80BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
81 EVT_SIZE(wxSpinButton::OnSize)
82END_EVENT_TABLE()
83
84wxSpinButton::wxSpinButton()
85{
86 m_pos = 0;
87}
88
89bool wxSpinButton::Create(wxWindow *parent,
90 wxWindowID id,
91 const wxPoint& pos,
92 const wxSize& size,
93 long style,
94 const wxString& name)
95{
96 m_needParent = true;
97
98 wxSize new_size = size,
99 sizeBest = DoGetBestSize();
100 new_size.x = sizeBest.x; // override width always
101 if (new_size.y == -1)
102 new_size.y = sizeBest.y;
103
104 if (!PreCreation( parent, pos, new_size ) ||
105 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
106 {
107 wxFAIL_MSG( wxT("wxSpinButton creation failed") );
108 return false;
109 }
110
111 m_pos = 0;
112
113 m_widget = gtk_spin_button_new_with_range(0, 100, 1);
114
115 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
116 (int)(m_windowStyle & wxSP_WRAP) );
117
118 g_signal_connect_after(
119 m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this);
120
121 m_parent->DoAddChild( this );
122
123 PostCreation(new_size);
124
125 return true;
126}
127
128int wxSpinButton::GetMin() const
129{
130 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
131
132 double min;
133 gtk_spin_button_get_range((GtkSpinButton*)m_widget, &min, NULL);
134 return int(min);
135}
136
137int wxSpinButton::GetMax() const
138{
139 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
140
141 double max;
142 gtk_spin_button_get_range((GtkSpinButton*)m_widget, NULL, &max);
143 return int(max);
144}
145
146int wxSpinButton::GetValue() const
147{
148 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
149
150 return m_pos;
151}
152
153void wxSpinButton::SetValue( int value )
154{
155 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
156
157 BlockScrollEvent();
158 gtk_spin_button_set_value((GtkSpinButton*)m_widget, value);
159 UnblockScrollEvent();
160}
161
162void wxSpinButton::SetRange(int minVal, int maxVal)
163{
164 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
165
166 BlockScrollEvent();
167 gtk_spin_button_set_range((GtkSpinButton*)m_widget, minVal, maxVal);
168 UnblockScrollEvent();
169}
170
171void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
172{
173 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
174
175 m_width = DoGetBestSize().x;
176 gtk_widget_set_size_request( m_widget, m_width, m_height );
177}
178
179bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
180{
181 return GTK_SPIN_BUTTON(m_widget)->panel == window;
182}
183
184wxSize wxSpinButton::DoGetBestSize() const
185{
186 wxSize best(15, 26); // FIXME
187 CacheBestSize(best);
188 return best;
189}
190
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
200#endif