]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/spinbutt.cpp
Made wxGTK dcps.cpp generic.
[wxWidgets.git] / src / gtk1 / spinbutt.cpp
CommitLineData
8e189077
RR
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#include "wx/utils.h"
f5368809 17#include <math.h>
8e189077 18
83624f79
RR
19#include "gdk/gdk.h"
20#include "gtk/gtk.h"
21
8e189077
RR
22//-----------------------------------------------------------------------------
23// data
24//-----------------------------------------------------------------------------
25
26extern bool g_blockEventsOnDrag;
27
28//-----------------------------------------------------------------------------
29// "value_changed"
30//-----------------------------------------------------------------------------
31
32static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win )
33{
83624f79
RR
34 if (!win->HasVMT()) return;
35 if (g_blockEventsOnDrag) return;
8e189077 36
83624f79
RR
37 float diff = win->m_adjust->value - win->m_oldPos;
38 if (fabs(diff) < 0.2) return;
8e189077 39
83624f79 40 wxEventType command = wxEVT_NULL;
8e189077 41
83624f79
RR
42 float line_step = win->m_adjust->step_increment;
43 float page_step = win->m_adjust->page_increment;
8e189077 44
83624f79
RR
45 if (fabs(diff-line_step) < 0.2) command = wxEVT_SCROLL_LINEDOWN;
46 else if (fabs(diff+line_step) < 0.2) command = wxEVT_SCROLL_LINEUP;
47 else if (fabs(diff-page_step) < 0.2) command = wxEVT_SCROLL_PAGEDOWN;
48 else if (fabs(diff+page_step) < 0.2) command = wxEVT_SCROLL_PAGEUP;
49 else command = wxEVT_SCROLL_THUMBTRACK;
8e189077 50
83624f79 51 int value = (int)(win->m_adjust->value+0.5);
8e189077 52
83624f79
RR
53 wxSpinEvent event( command, win->GetId());
54 event.SetPosition( value );
55 event.SetOrientation( wxVERTICAL );
56 event.SetEventObject( win );
8e189077 57
83624f79 58 win->ProcessEvent( event );
8e189077
RR
59}
60
61//-----------------------------------------------------------------------------
62// wxSpinButton
63//-----------------------------------------------------------------------------
64
65IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
66
67BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
68 EVT_SIZE(wxSpinButton::OnSize)
69END_EVENT_TABLE()
70
71wxSpinButton::wxSpinButton()
72{
73}
74
75bool wxSpinButton::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
76 long style, const wxString& name)
77{
83624f79 78 m_needParent = TRUE;
8e189077 79
83624f79
RR
80 wxSize new_size = size;
81 new_size.x = 16;
82 if (new_size.y == -1) new_size.y = 30;
8e189077 83
83624f79 84 PreCreation( parent, id, pos, new_size, style, name );
8e189077
RR
85
86// SetValidator( validator );
87
83624f79 88 m_oldPos = 0.0;
8e189077 89
83624f79 90 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
8e189077 91
83624f79 92 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
8e189077 93
83624f79 94 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), (m_windowStyle & wxSP_WRAP) );
8e189077 95
83624f79
RR
96 gtk_signal_connect( GTK_OBJECT (m_adjust),
97 "value_changed",
98 (GtkSignalFunc) gtk_spinbutt_callback,
99 (gpointer) this );
8e189077 100
83624f79 101 m_parent->AddChild( this );
8e189077 102
83624f79 103 (m_parent->m_insertCallback)( m_parent, this );
8e189077 104
83624f79 105 PostCreation();
8e189077 106
83624f79 107 SetBackgroundColour( parent->GetBackgroundColour() );
8e189077 108
83624f79 109 Show( TRUE );
8e189077 110
83624f79 111 return TRUE;
8e189077
RR
112}
113
114wxSpinButton::~wxSpinButton()
115{
116}
117
118int wxSpinButton::GetMin() const
119{
83624f79 120 wxCHECK_MSG( (m_widget != NULL), 0, "invalid spin button" );
8e189077 121
83624f79 122 return (int)(m_adjust->lower+0.5);
8e189077
RR
123}
124
125int wxSpinButton::GetMax() const
126{
83624f79 127 wxCHECK_MSG( (m_widget != NULL), 0, "invalid spin button" );
8e189077 128
83624f79 129 return (int)(m_adjust->upper+0.5);
8e189077
RR
130}
131
132int wxSpinButton::GetValue() const
133{
83624f79 134 wxCHECK_MSG( (m_widget != NULL), 0, "invalid spin button" );
8e189077 135
83624f79 136 return (int)(m_adjust->value+0.5);
8e189077
RR
137}
138
139void wxSpinButton::SetValue( int value )
140{
83624f79 141 wxCHECK_RET( (m_widget != NULL), "invalid spin button" );
8e189077 142
83624f79
RR
143 float fpos = (float)value;
144 m_oldPos = fpos;
145 if (fabs(fpos-m_adjust->value) < 0.2) return;
146
147 m_adjust->value = fpos;
8e189077 148
83624f79 149 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
8e189077
RR
150}
151
152void wxSpinButton::SetRange(int minVal, int maxVal)
153{
83624f79 154 wxCHECK_RET( (m_widget != NULL), "invalid spin button" );
8e189077 155
83624f79
RR
156 float fmin = (float)minVal;
157 float fmax = (float)maxVal;
8e189077 158
83624f79
RR
159 if ((fabs(fmin-m_adjust->lower) < 0.2) &&
160 (fabs(fmax-m_adjust->upper) < 0.2))
161 {
162 return;
163 }
8e189077 164
83624f79
RR
165 m_adjust->lower = fmin;
166 m_adjust->upper = fmax;
8e189077 167
83624f79 168 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
8e189077
RR
169}
170
171void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
172{
83624f79 173 wxCHECK_RET( (m_widget != NULL), "invalid spin button" );
8e189077 174
83624f79
RR
175 m_width = 16;
176 gtk_widget_set_usize( m_widget, m_width, m_height );
8e189077
RR
177}
178
179bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
180{
83624f79 181 return GTK_SPIN_BUTTON(m_widget)->panel == window;
8e189077
RR
182}
183
184void wxSpinButton::ApplyWidgetStyle()
185{
83624f79
RR
186 SetWidgetStyle();
187 gtk_widget_set_style( m_widget, m_widgetStyle );
8e189077
RR
188}
189
190//-----------------------------------------------------------------------------
191// wxSpinEvent
192//-----------------------------------------------------------------------------
193
194IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxScrollEvent)
195
196wxSpinEvent::wxSpinEvent(wxEventType commandType, int id):
197 wxScrollEvent(commandType, id)
198{
199}
200
201