]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/scrolbar.cpp
Added wizard sample.
[wxWidgets.git] / src / gtk / scrolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: scrolbar.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "scrolbar.h"
13 #endif
14
15 #include "wx/scrolbar.h"
16
17 #if wxUSE_SCROLLBAR
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
29 extern void wxapp_install_idle_handler();
30 extern bool g_isIdle;
31
32 //-----------------------------------------------------------------------------
33 // data
34 //-----------------------------------------------------------------------------
35
36 extern bool g_blockEventsOnDrag;
37 extern bool g_blockEventsOnScroll;
38
39 //-----------------------------------------------------------------------------
40 // "value_changed"
41 //-----------------------------------------------------------------------------
42
43 static void gtk_scrollbar_callback( GtkAdjustment *adjust, wxScrollBar *win )
44 {
45 if (g_isIdle) wxapp_install_idle_handler();
46
47 if (!win->m_hasVMT) return;
48 if (g_blockEventsOnDrag) return;
49
50 float diff = adjust->value - win->m_oldPos;
51 if (fabs(diff) < 0.2) return;
52
53 win->m_oldPos = adjust->value;
54
55 GtkRange *range = GTK_RANGE( win->m_widget );
56
57 wxEventType command = wxEVT_SCROLL_THUMBTRACK;
58 if (range->scroll_type == GTK_SCROLL_STEP_BACKWARD) command = wxEVT_SCROLL_LINEUP;
59 else if (range->scroll_type == GTK_SCROLL_STEP_FORWARD) command = wxEVT_SCROLL_LINEDOWN;
60 else if (range->scroll_type == GTK_SCROLL_PAGE_BACKWARD) command = wxEVT_SCROLL_PAGEUP;
61 else if (range->scroll_type == GTK_SCROLL_PAGE_FORWARD) command = wxEVT_SCROLL_PAGEDOWN;
62
63 int value = (int)ceil(adjust->value);
64
65 int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
66
67 wxScrollEvent event( command, win->GetId(), value, orient );
68 event.SetEventObject( win );
69 win->GetEventHandler()->ProcessEvent( event );
70
71 /*
72 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
73 cevent.SetEventObject( win );
74 win->ProcessEvent( cevent );
75 */
76 }
77
78 //-----------------------------------------------------------------------------
79 // "button_press_event" from slider
80 //-----------------------------------------------------------------------------
81
82 static gint gtk_scrollbar_button_press_callback( GtkRange *WXUNUSED(widget),
83 GdkEventButton *WXUNUSED(gdk_event),
84 wxScrollBar *win )
85 {
86 if (g_isIdle) wxapp_install_idle_handler();
87
88 win->SetScrolling(TRUE);
89 // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd
90
91 return FALSE;
92 }
93
94 //-----------------------------------------------------------------------------
95 // "button_release_event" from slider
96 //-----------------------------------------------------------------------------
97
98 static gint gtk_scrollbar_button_release_callback( GtkRange *WXUNUSED(widget),
99 GdkEventButton *WXUNUSED(gdk_event),
100 wxScrollBar *win )
101 {
102 if (g_isIdle) wxapp_install_idle_handler();
103
104 win->SetScrolling(FALSE);
105 // g_blockEventsOnScroll = FALSE;
106
107 gtk_signal_emit_by_name( GTK_OBJECT(win->m_adjust), "value_changed" );
108
109 return FALSE;
110 }
111
112 //-----------------------------------------------------------------------------
113 // wxScrollBar
114 //-----------------------------------------------------------------------------
115
116 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
117
118 wxScrollBar::~wxScrollBar(void)
119 {
120 }
121
122 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
123 const wxPoint& pos, const wxSize& size,
124 long style, const wxValidator& validator, const wxString& name )
125 {
126 m_needParent = TRUE;
127 m_acceptsFocus = TRUE;
128
129 if (!PreCreation( parent, pos, size ) ||
130 !CreateBase( parent, id, pos, size, style, validator, name ))
131 {
132 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
133 return FALSE;
134 }
135
136 m_oldPos = 0.0;
137
138 if ((style & wxSB_VERTICAL) == wxSB_VERTICAL)
139 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
140 else
141 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
142
143 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
144
145 gtk_signal_connect( GTK_OBJECT(m_adjust),
146 "value_changed",
147 (GtkSignalFunc) gtk_scrollbar_callback,
148 (gpointer) this );
149
150 gtk_signal_connect( GTK_OBJECT(m_widget),
151 "button_press_event",
152 (GtkSignalFunc)gtk_scrollbar_button_press_callback,
153 (gpointer) this );
154
155 gtk_signal_connect( GTK_OBJECT(m_widget),
156 "button_release_event",
157 (GtkSignalFunc)gtk_scrollbar_button_release_callback,
158 (gpointer) this );
159
160 m_parent->DoAddChild( this );
161
162 PostCreation();
163
164 SetBackgroundColour( parent->GetBackgroundColour() );
165
166 Show( TRUE );
167
168 return TRUE;
169 }
170
171 int wxScrollBar::GetThumbPosition(void) const
172 {
173 return (int)(m_adjust->value+0.5);
174 }
175
176 int wxScrollBar::GetThumbSize() const
177 {
178 return (int)(m_adjust->page_size+0.5);
179 }
180
181 int wxScrollBar::GetPageSize() const
182 {
183 return (int)(m_adjust->page_increment+0.5);
184 }
185
186 int wxScrollBar::GetRange() const
187 {
188 return (int)(m_adjust->upper+0.5);
189 }
190
191 void wxScrollBar::SetThumbPosition( int viewStart )
192 {
193 if (m_isScrolling) return;
194
195 float fpos = (float)viewStart;
196 m_oldPos = fpos;
197 if (fabs(fpos-m_adjust->value) < 0.2) return;
198 m_adjust->value = fpos;
199
200 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
201 }
202
203 void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize,
204 bool WXUNUSED(refresh) )
205 {
206 float fpos = (float)position;
207 float frange = (float)range;
208 float fthumb = (float)thumbSize;
209 float fpage = (float)pageSize;
210
211 if ((fabs(frange-m_adjust->upper) < 0.2) &&
212 (fabs(fthumb-m_adjust->page_size) < 0.2) &&
213 (fabs(fpage-m_adjust->page_increment) < 0.2))
214 {
215 SetThumbPosition( position );
216 return;
217 }
218
219 m_oldPos = fpos;
220
221 m_adjust->lower = 0.0;
222 m_adjust->upper = frange;
223 m_adjust->value = fpos;
224 m_adjust->step_increment = 1.0;
225 m_adjust->page_increment = (float)(wxMax(fpage,0));
226 m_adjust->page_size = fthumb;
227
228 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
229 }
230
231 /* Backward compatibility */
232 int wxScrollBar::GetValue(void) const
233 {
234 return GetThumbPosition();
235 }
236
237 void wxScrollBar::SetValue( int viewStart )
238 {
239 SetThumbPosition( viewStart );
240 }
241
242 void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const
243 {
244 int pos = (int)(m_adjust->value+0.5);
245 int thumb = (int)(m_adjust->page_size+0.5);
246 int page = (int)(m_adjust->page_increment+0.5);
247 int range = (int)(m_adjust->upper+0.5);
248
249 *viewStart = pos;
250 *viewLength = range;
251 *objectLength = thumb;
252 *pageLength = page;
253 }
254
255 int wxScrollBar::GetViewLength() const
256 {
257 return (int)(m_adjust->upper+0.5);
258 }
259
260 int wxScrollBar::GetObjectLength() const
261 {
262 return (int)(m_adjust->page_size+0.5);
263 }
264
265 void wxScrollBar::SetPageSize( int pageLength )
266 {
267 int pos = (int)(m_adjust->value+0.5);
268 int thumb = (int)(m_adjust->page_size+0.5);
269 int range = (int)(m_adjust->upper+0.5);
270 SetScrollbar( pos, thumb, range, pageLength );
271 }
272
273 void wxScrollBar::SetObjectLength( int objectLength )
274 {
275 int pos = (int)(m_adjust->value+0.5);
276 int page = (int)(m_adjust->page_increment+0.5);
277 int range = (int)(m_adjust->upper+0.5);
278 SetScrollbar( pos, objectLength, range, page );
279 }
280
281 void wxScrollBar::SetViewLength( int viewLength )
282 {
283 int pos = (int)(m_adjust->value+0.5);
284 int thumb = (int)(m_adjust->page_size+0.5);
285 int page = (int)(m_adjust->page_increment+0.5);
286 SetScrollbar( pos, thumb, viewLength, page );
287 }
288
289 bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window )
290 {
291 GtkRange *range = GTK_RANGE(m_widget);
292 return ( (window == GTK_WIDGET(range)->window) ||
293 (window == range->trough) ||
294 (window == range->slider) ||
295 (window == range->step_forw) ||
296 (window == range->step_back) );
297 }
298
299 void wxScrollBar::ApplyWidgetStyle()
300 {
301 SetWidgetStyle();
302 gtk_widget_set_style( m_widget, m_widgetStyle );
303 }
304
305 #endif