]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/spinbutt.cpp
SetMenuBar potential memory leak fixed; added wxTAB_TRAVERSAL as dummy (0) style
[wxWidgets.git] / src / gtk1 / spinbutt.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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#ifdef __GNUG__
12#pragma implementation "spinbutt.h"
13#endif
14
15#include "wx/spinbutt.h"
16
17#ifdef wxUSE_SPINBTN
18
19#include "wx/utils.h"
20#include <math.h>
21
22#include "gdk/gdk.h"
23#include "gtk/gtk.h"
24
25//-----------------------------------------------------------------------------
26// idle system
27//-----------------------------------------------------------------------------
28
29extern void wxapp_install_idle_handler();
30extern bool g_isIdle;
31
32//-----------------------------------------------------------------------------
33// data
34//-----------------------------------------------------------------------------
35
36extern bool g_blockEventsOnDrag;
37
38static const float sensitivity = 0.2;
39
40//-----------------------------------------------------------------------------
41// "value_changed"
42//-----------------------------------------------------------------------------
43
44static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win )
45{
46 if (g_isIdle) wxapp_install_idle_handler();
47
48 if (!win->m_hasVMT) return;
49 if (g_blockEventsOnDrag) return;
50
51 float diff = win->m_adjust->value - win->m_oldPos;
52 if (fabs(diff) < sensitivity) return;
53 win->m_oldPos = win->m_adjust->value;
54
55 wxEventType command = wxEVT_NULL;
56
57 float line_step = win->m_adjust->step_increment;
58
59 if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN;
60 else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP;
61 else command = wxEVT_SCROLL_THUMBTRACK;
62
63 int value = (int)ceil(win->m_adjust->value);
64
65 wxSpinEvent event( command, win->GetId());
66 event.SetPosition( value );
67 event.SetEventObject( win );
68 win->GetEventHandler()->ProcessEvent( event );
69
70 /* always send a thumbtrack event */
71 if (command != wxEVT_SCROLL_THUMBTRACK)
72 {
73 command = wxEVT_SCROLL_THUMBTRACK;
74 wxSpinEvent event2( command, win->GetId());
75 event2.SetPosition( value );
76 event2.SetEventObject( win );
77 win->GetEventHandler()->ProcessEvent( event2 );
78 }
79}
80
81//-----------------------------------------------------------------------------
82// wxSpinButton
83//-----------------------------------------------------------------------------
84
85IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
86IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxScrollEvent);
87
88BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
89 EVT_SIZE(wxSpinButton::OnSize)
90END_EVENT_TABLE()
91
92bool wxSpinButton::Create(wxWindow *parent,
93 wxWindowID id,
94 const wxPoint& pos,
95 const wxSize& size,
96 long style,
97 const wxString& name)
98{
99 m_needParent = TRUE;
100
101 wxSize new_size = size;
102 new_size.x = 15;
103 if (new_size.y == -1)
104 new_size.y = 30;
105
106 if (!PreCreation( parent, pos, new_size ) ||
107 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
108 {
109 wxFAIL_MSG( _T("wxXX creation failed") );
110 return FALSE;
111 }
112
113 m_oldPos = 0.0;
114
115 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
116
117 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
118
119 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), (m_windowStyle & wxSP_WRAP) );
120
121 gtk_signal_connect( GTK_OBJECT (m_adjust),
122 "value_changed",
123 (GtkSignalFunc) gtk_spinbutt_callback,
124 (gpointer) this );
125
126 m_parent->DoAddChild( this );
127
128 PostCreation();
129
130 SetBackgroundColour( parent->GetBackgroundColour() );
131
132 Show( TRUE );
133
134 return TRUE;
135}
136
137wxSpinButton::~wxSpinButton()
138{
139}
140
141int wxSpinButton::GetMin() const
142{
143 wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") );
144
145 return (int)ceil(m_adjust->lower);
146}
147
148int wxSpinButton::GetMax() const
149{
150 wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") );
151
152 return (int)ceil(m_adjust->upper);
153}
154
155int wxSpinButton::GetValue() const
156{
157 wxCHECK_MSG( (m_widget != NULL), 0, _T("invalid spin button") );
158
159 return (int)ceil(m_adjust->value);
160}
161
162void wxSpinButton::SetValue( int value )
163{
164 wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") );
165
166 float fpos = (float)value;
167 m_oldPos = fpos;
168 if (fabs(fpos-m_adjust->value) < sensitivity) return;
169
170 m_adjust->value = fpos;
171
172 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
173}
174
175void wxSpinButton::SetRange(int minVal, int maxVal)
176{
177 wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") );
178
179 float fmin = (float)minVal;
180 float fmax = (float)maxVal;
181
182 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
183 (fabs(fmax-m_adjust->upper) < sensitivity))
184 {
185 return;
186 }
187
188 m_adjust->lower = fmin;
189 m_adjust->upper = fmax;
190
191 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
192
193 // these two calls are required due to some bug in GTK
194 Refresh();
195 SetFocus();
196}
197
198void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
199{
200 wxCHECK_RET( (m_widget != NULL), _T("invalid spin button") );
201
202 m_width = 15;
203 gtk_widget_set_usize( m_widget, m_width, m_height );
204}
205
206bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
207{
208 return GTK_SPIN_BUTTON(m_widget)->panel == window;
209}
210
211void wxSpinButton::ApplyWidgetStyle()
212{
213 SetWidgetStyle();
214 gtk_widget_set_style( m_widget, m_widgetStyle );
215}
216
217#endif