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