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