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