]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/scrolbar.cpp
Bit fiddling.
[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 #include "wx/utils.h"
17
18 //-----------------------------------------------------------------------------
19 // data
20 //-----------------------------------------------------------------------------
21
22 extern bool g_blockEventsOnDrag;
23
24 //-----------------------------------------------------------------------------
25 // "value_changed"
26 //-----------------------------------------------------------------------------
27
28 static void gtk_scrollbar_callback( GtkWidget *WXUNUSED(widget), wxScrollBar *win )
29 {
30 if (!win->HasVMT()) return;
31 if (g_blockEventsOnDrag) return;
32
33 float diff = win->m_adjust->value - win->m_oldPos;
34 if (fabs(diff) < 0.2) return;
35
36 wxEventType command = wxEVT_NULL;
37
38 float line_step = win->m_adjust->step_increment;
39 float page_step = win->m_adjust->page_increment;
40
41 if (fabs(diff-line_step) < 0.2) command = wxEVT_SCROLL_LINEDOWN;
42 else if (fabs(diff+line_step) < 0.2) command = wxEVT_SCROLL_LINEUP;
43 else if (fabs(diff-page_step) < 0.2) command = wxEVT_SCROLL_PAGEDOWN;
44 else if (fabs(diff+page_step) < 0.2) command = wxEVT_SCROLL_PAGEUP;
45 else command = wxEVT_SCROLL_THUMBTRACK;
46
47 int value = (int)(win->m_adjust->value+0.5);
48
49 int orient = wxHORIZONTAL;
50 if (win->GetWindowStyleFlag() & wxSB_VERTICAL == wxSB_VERTICAL) orient = wxVERTICAL;
51
52 wxScrollEvent event( command, win->GetId(), value, orient );
53 event.SetEventObject( win );
54 win->GetEventHandler()->ProcessEvent( event );
55
56 /*
57 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
58 cevent.SetEventObject( win );
59 win->ProcessEvent( cevent );
60 */
61 }
62
63 //-----------------------------------------------------------------------------
64 // "button_press_event" from slider
65 //-----------------------------------------------------------------------------
66
67 static gint gtk_scrollbar_button_press_callback( GtkRange *widget, GdkEventButton *gdk_event, wxScrollBar *win )
68 {
69 if (gdk_event->window != widget->slider) return FALSE;
70
71 win->m_isScrolling = TRUE;
72
73 return FALSE;
74 }
75
76 //-----------------------------------------------------------------------------
77 // "button_release_event" from slider
78 //-----------------------------------------------------------------------------
79
80 static gint gtk_scrollbar_button_release_callback( GtkRange *widget, GdkEventButton *gdk_event, wxScrollBar *win )
81 {
82 if (gdk_event->window != widget->slider) return FALSE;
83
84 gtk_signal_emit_by_name( GTK_OBJECT(win->m_adjust), "value_changed" );
85
86 win->m_isScrolling = FALSE;
87
88 return FALSE;
89 }
90
91 //-----------------------------------------------------------------------------
92 // wxScrollBar
93 //-----------------------------------------------------------------------------
94
95 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
96
97 wxScrollBar::~wxScrollBar(void)
98 {
99 }
100
101 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
102 const wxPoint& pos, const wxSize& size,
103 long style, const wxValidator& validator, const wxString& name )
104 {
105 m_needParent = TRUE;
106
107 PreCreation( parent, id, pos, size, style, name );
108
109 SetValidator( validator );
110
111 m_oldPos = 0.0;
112
113 if (style & wxSB_VERTICAL == wxSB_VERTICAL)
114 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
115 else
116 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
117
118 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
119
120 gtk_signal_connect (GTK_OBJECT (m_adjust), "value_changed",
121 (GtkSignalFunc) gtk_scrollbar_callback, (gpointer) this );
122
123 gtk_signal_connect( GTK_OBJECT(m_widget), "button_press_event",
124 (GtkSignalFunc)gtk_scrollbar_button_press_callback, (gpointer) this );
125
126 gtk_signal_connect( GTK_OBJECT(m_widget), "button_release_event",
127 (GtkSignalFunc)gtk_scrollbar_button_release_callback, (gpointer) this );
128
129 m_parent->AddChild( this );
130
131 (m_parent->m_insertCallback)( m_parent, this );
132
133 PostCreation();
134
135 SetBackgroundColour( parent->GetBackgroundColour() );
136
137 Show( TRUE );
138
139 return TRUE;
140 }
141
142 int wxScrollBar::GetPosition(void) const
143 {
144 return (int)(m_adjust->value+0.5);
145 }
146
147 int wxScrollBar::GetThumbSize() const
148 {
149 return (int)(m_adjust->page_size+0.5);
150 }
151
152 int wxScrollBar::GetPageSize() const
153 {
154 return (int)(m_adjust->page_increment+0.5);
155 }
156
157 int wxScrollBar::GetRange() const
158 {
159 return (int)(m_adjust->upper+0.5);
160 }
161
162 void wxScrollBar::SetPosition( int viewStart )
163 {
164 float fpos = (float)viewStart;
165 m_oldPos = fpos;
166 if (fabs(fpos-m_adjust->value) < 0.2) return;
167 m_adjust->value = fpos;
168
169 if (!m_isScrolling)
170 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
171 }
172
173 void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize,
174 bool WXUNUSED(refresh) )
175 {
176 float fpos = (float)position;
177 float frange = (float)range;
178 float fthumb = (float)thumbSize;
179 float fpage = (float)pageSize;
180
181 if ((fabs(frange-m_adjust->upper) < 0.2) &&
182 (fabs(fthumb-m_adjust->page_size) < 0.2) &&
183 (fabs(fpage-m_adjust->page_increment) < 0.2))
184 {
185 SetPosition( position );
186 return;
187 }
188
189 m_oldPos = fpos;
190
191 m_adjust->lower = 0.0;
192 m_adjust->upper = frange;
193 m_adjust->value = fpos;
194 m_adjust->step_increment = 1.0;
195 m_adjust->page_increment = (float)(wxMax(fpage,0));
196 m_adjust->page_size = fthumb;
197
198 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
199 }
200
201 // Backward compatibility
202 int wxScrollBar::GetValue(void) const
203 {
204 return GetPosition();
205 }
206
207 void wxScrollBar::SetValue( int viewStart )
208 {
209 SetPosition( viewStart );
210 }
211
212 void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const
213 {
214 int pos = (int)(m_adjust->value+0.5);
215 int thumb = (int)(m_adjust->page_size+0.5);
216 int page = (int)(m_adjust->page_increment+0.5);
217 int range = (int)(m_adjust->upper+0.5);
218
219 *viewStart = pos;
220 *viewLength = range;
221 *objectLength = thumb;
222 *pageLength = page;
223 }
224
225 int wxScrollBar::GetViewLength() const
226 {
227 return (int)(m_adjust->upper+0.5);
228 }
229
230 int wxScrollBar::GetObjectLength() const
231 {
232 return (int)(m_adjust->page_size+0.5);
233 }
234
235 void wxScrollBar::SetPageSize( int pageLength )
236 {
237 int pos = (int)(m_adjust->value+0.5);
238 int thumb = (int)(m_adjust->page_size+0.5);
239 int range = (int)(m_adjust->upper+0.5);
240 SetScrollbar( pos, thumb, range, pageLength );
241 }
242
243 void wxScrollBar::SetObjectLength( int objectLength )
244 {
245 int pos = (int)(m_adjust->value+0.5);
246 int page = (int)(m_adjust->page_increment+0.5);
247 int range = (int)(m_adjust->upper+0.5);
248 SetScrollbar( pos, objectLength, range, page );
249 }
250
251 void wxScrollBar::SetViewLength( int viewLength )
252 {
253 int pos = (int)(m_adjust->value+0.5);
254 int thumb = (int)(m_adjust->page_size+0.5);
255 int page = (int)(m_adjust->page_increment+0.5);
256 SetScrollbar( pos, thumb, viewLength, page );
257 }
258
259 bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window )
260 {
261 GtkRange *range = GTK_RANGE(m_widget);
262 return ( (window == GTK_WIDGET(range)->window) ||
263 (window == range->trough) ||
264 (window == range->slider) ||
265 (window == range->step_forw) ||
266 (window == range->step_back) );
267 }
268
269 void wxScrollBar::ApplyWidgetStyle()
270 {
271 SetWidgetStyle();
272 gtk_widget_set_style( m_widget, m_widgetStyle );
273 }
274