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