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