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