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