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