]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/scrolbar.cpp
wxMotif::wxFont supports encodings too (and shares 99% of font code with wxGTK)
[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( 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 if (!PreCreation( parent, pos, size ) ||
141 !CreateBase( parent, id, pos, size, style, validator, name ))
142 {
143 wxFAIL_MSG( _T("wxScrollBar creation failed") );
144 return FALSE;
145 }
146
147 m_oldPos = 0.0;
148
149 if ((style & wxSB_VERTICAL) == wxSB_VERTICAL)
150 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
151 else
152 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
153
154 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
155
156 gtk_signal_connect( GTK_OBJECT(m_adjust),
157 "value_changed",
158 (GtkSignalFunc) gtk_scrollbar_callback,
159 (gpointer) this );
160
161 gtk_signal_connect( GTK_OBJECT(m_widget),
162 "button_press_event",
163 (GtkSignalFunc)gtk_scrollbar_button_press_callback,
164 (gpointer) this );
165
166 gtk_signal_connect( GTK_OBJECT(m_widget),
167 "button_release_event",
168 (GtkSignalFunc)gtk_scrollbar_button_release_callback,
169 (gpointer) this );
170
171 m_parent->DoAddChild( this );
172
173 PostCreation();
174
175 SetBackgroundColour( parent->GetBackgroundColour() );
176
177 Show( TRUE );
178
179 return TRUE;
180 }
181
182 int wxScrollBar::GetThumbPosition(void) const
183 {
184 return (int)(m_adjust->value+0.5);
185 }
186
187 int wxScrollBar::GetThumbSize() const
188 {
189 return (int)(m_adjust->page_size+0.5);
190 }
191
192 int wxScrollBar::GetPageSize() const
193 {
194 return (int)(m_adjust->page_increment+0.5);
195 }
196
197 int wxScrollBar::GetRange() const
198 {
199 return (int)(m_adjust->upper+0.5);
200 }
201
202 void wxScrollBar::SetThumbPosition( int viewStart )
203 {
204 if (m_isScrolling) return;
205
206 float fpos = (float)viewStart;
207 m_oldPos = fpos;
208 if (fabs(fpos-m_adjust->value) < 0.2) return;
209 m_adjust->value = fpos;
210
211 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
212 }
213
214 void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize,
215 bool WXUNUSED(refresh) )
216 {
217 float fpos = (float)position;
218 float frange = (float)range;
219 float fthumb = (float)thumbSize;
220 float fpage = (float)pageSize;
221
222 if ((fabs(frange-m_adjust->upper) < 0.2) &&
223 (fabs(fthumb-m_adjust->page_size) < 0.2) &&
224 (fabs(fpage-m_adjust->page_increment) < 0.2))
225 {
226 SetThumbPosition( position );
227 return;
228 }
229
230 m_oldPos = fpos;
231
232 m_adjust->lower = 0.0;
233 m_adjust->upper = frange;
234 m_adjust->value = fpos;
235 m_adjust->step_increment = 1.0;
236 m_adjust->page_increment = (float)(wxMax(fpage,0));
237 m_adjust->page_size = fthumb;
238
239 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
240 }
241
242 /* Backward compatibility */
243 int wxScrollBar::GetValue(void) const
244 {
245 return GetThumbPosition();
246 }
247
248 void wxScrollBar::SetValue( int viewStart )
249 {
250 SetThumbPosition( viewStart );
251 }
252
253 void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const
254 {
255 int pos = (int)(m_adjust->value+0.5);
256 int thumb = (int)(m_adjust->page_size+0.5);
257 int page = (int)(m_adjust->page_increment+0.5);
258 int range = (int)(m_adjust->upper+0.5);
259
260 *viewStart = pos;
261 *viewLength = range;
262 *objectLength = thumb;
263 *pageLength = page;
264 }
265
266 int wxScrollBar::GetViewLength() const
267 {
268 return (int)(m_adjust->upper+0.5);
269 }
270
271 int wxScrollBar::GetObjectLength() const
272 {
273 return (int)(m_adjust->page_size+0.5);
274 }
275
276 void wxScrollBar::SetPageSize( int pageLength )
277 {
278 int pos = (int)(m_adjust->value+0.5);
279 int thumb = (int)(m_adjust->page_size+0.5);
280 int range = (int)(m_adjust->upper+0.5);
281 SetScrollbar( pos, thumb, range, pageLength );
282 }
283
284 void wxScrollBar::SetObjectLength( int objectLength )
285 {
286 int pos = (int)(m_adjust->value+0.5);
287 int page = (int)(m_adjust->page_increment+0.5);
288 int range = (int)(m_adjust->upper+0.5);
289 SetScrollbar( pos, objectLength, range, page );
290 }
291
292 void wxScrollBar::SetViewLength( int viewLength )
293 {
294 int pos = (int)(m_adjust->value+0.5);
295 int thumb = (int)(m_adjust->page_size+0.5);
296 int page = (int)(m_adjust->page_increment+0.5);
297 SetScrollbar( pos, thumb, viewLength, page );
298 }
299
300 bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window )
301 {
302 GtkRange *range = GTK_RANGE(m_widget);
303 return ( (window == GTK_WIDGET(range)->window) ||
304 (window == range->trough) ||
305 (window == range->slider) ||
306 (window == range->step_forw) ||
307 (window == range->step_back) );
308 }
309
310 void wxScrollBar::ApplyWidgetStyle()
311 {
312 SetWidgetStyle();
313 gtk_widget_set_style( m_widget, m_widgetStyle );
314 }
315
316 #endif