]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/spinbutt.cpp
Use window position in screen coordinates in wxDisplay::GetFromWindow().
[wxWidgets.git] / src / gtk / spinbutt.cpp
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 <gtk/gtk.h>
23
24 //-----------------------------------------------------------------------------
25 // data
26 //-----------------------------------------------------------------------------
27
28 extern bool g_blockEventsOnDrag;
29
30 //-----------------------------------------------------------------------------
31 // "value_changed"
32 //-----------------------------------------------------------------------------
33
34 extern "C" {
35 static void
36 gtk_value_changed(GtkSpinButton* spinbutton, wxSpinButton* win)
37 {
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 || pos == oldPos)
42 {
43 win->m_pos = pos;
44 return;
45 }
46
47 wxSpinEvent event(pos > oldPos ? wxEVT_SCROLL_LINEUP : wxEVT_SCROLL_LINEDOWN, win->GetId());
48 event.SetPosition(pos);
49 event.SetEventObject(win);
50
51 if ((win->HandleWindowEvent( event )) &&
52 !event.IsAllowed() )
53 {
54 /* program has vetoed */
55 // this will cause another "value_changed" signal,
56 // but because pos == oldPos nothing will happen
57 gtk_spin_button_set_value(spinbutton, oldPos);
58 return;
59 }
60
61 win->m_pos = pos;
62
63 /* always send a thumbtrack event */
64 wxSpinEvent event2(wxEVT_SCROLL_THUMBTRACK, win->GetId());
65 event2.SetPosition(pos);
66 event2.SetEventObject(win);
67 win->HandleWindowEvent(event2);
68 }
69 }
70
71 //-----------------------------------------------------------------------------
72 // wxSpinButton
73 //-----------------------------------------------------------------------------
74
75 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
76 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
77
78 BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
79 EVT_SIZE(wxSpinButton::OnSize)
80 END_EVENT_TABLE()
81
82 wxSpinButton::wxSpinButton()
83 {
84 m_pos = 0;
85 }
86
87 bool wxSpinButton::Create(wxWindow *parent,
88 wxWindowID id,
89 const wxPoint& pos,
90 const wxSize& size,
91 long style,
92 const wxString& name)
93 {
94 wxSize new_size = size,
95 sizeBest = DoGetBestSize();
96 new_size.x = sizeBest.x; // override width always
97 if (new_size.y == -1)
98 new_size.y = sizeBest.y;
99
100 if (!PreCreation( parent, pos, new_size ) ||
101 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
102 {
103 wxFAIL_MSG( wxT("wxSpinButton creation failed") );
104 return false;
105 }
106
107 m_pos = 0;
108
109 m_widget = gtk_spin_button_new_with_range(0, 100, 1);
110 g_object_ref(m_widget);
111
112 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
113 (int)(m_windowStyle & wxSP_WRAP) );
114
115 g_signal_connect_after(
116 m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this);
117
118 m_parent->DoAddChild( this );
119
120 PostCreation(new_size);
121
122 return true;
123 }
124
125 int wxSpinButton::GetMin() const
126 {
127 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
128
129 double min;
130 gtk_spin_button_get_range((GtkSpinButton*)m_widget, &min, NULL);
131 return int(min);
132 }
133
134 int wxSpinButton::GetMax() const
135 {
136 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
137
138 double max;
139 gtk_spin_button_get_range((GtkSpinButton*)m_widget, NULL, &max);
140 return int(max);
141 }
142
143 int wxSpinButton::GetValue() const
144 {
145 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
146
147 return m_pos;
148 }
149
150 void wxSpinButton::SetValue( int value )
151 {
152 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
153
154 GtkDisableEvents();
155 gtk_spin_button_set_value((GtkSpinButton*)m_widget, value);
156 m_pos = int(gtk_spin_button_get_value((GtkSpinButton*)m_widget));
157 GtkEnableEvents();
158 }
159
160 void wxSpinButton::SetRange(int minVal, int maxVal)
161 {
162 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
163
164 GtkDisableEvents();
165 gtk_spin_button_set_range((GtkSpinButton*)m_widget, minVal, maxVal);
166 m_pos = int(gtk_spin_button_get_value((GtkSpinButton*)m_widget));
167 GtkEnableEvents();
168 }
169
170 void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
171 {
172 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
173
174 m_width = DoGetBestSize().x;
175 gtk_widget_set_size_request( m_widget, m_width, m_height );
176 }
177
178 bool wxSpinButton::Enable( bool enable )
179 {
180 if (!base_type::Enable(enable))
181 return false;
182
183 // Work around lack of visual update when enabling
184 if (enable)
185 GTKFixSensitivity(false /* fix even if not under mouse */);
186
187 return true;
188 }
189
190 void wxSpinButton::GtkDisableEvents() const
191 {
192 g_signal_handlers_block_by_func(m_widget,
193 (gpointer)gtk_value_changed, (void*) this);
194 }
195
196 void wxSpinButton::GtkEnableEvents() const
197 {
198 g_signal_handlers_unblock_by_func(m_widget,
199 (gpointer)gtk_value_changed, (void*) this);
200 }
201
202 GdkWindow *wxSpinButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
203 {
204 return GTK_SPIN_BUTTON(m_widget)->panel;
205 }
206
207 wxSize wxSpinButton::DoGetBestSize() const
208 {
209 wxSize best(15, 26); // FIXME
210 CacheBestSize(best);
211 return best;
212 }
213
214 // static
215 wxVisualAttributes
216 wxSpinButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
217 {
218 // TODO: overload to accept functions like gtk_spin_button_new?
219 // Until then use a similar type
220 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
221 }
222
223 #endif