]>
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" | |
17 | ||
66bd6b93 RR |
18 | //----------------------------------------------------------------------------- |
19 | // data | |
20 | //----------------------------------------------------------------------------- | |
21 | ||
22 | extern bool g_blockEventsOnDrag; | |
23 | ||
c801d85f | 24 | //----------------------------------------------------------------------------- |
b4071e91 | 25 | // "value_changed" |
c801d85f KB |
26 | //----------------------------------------------------------------------------- |
27 | ||
66bd6b93 | 28 | static void gtk_scrollbar_callback( GtkWidget *WXUNUSED(widget), wxScrollBar *win ) |
c801d85f | 29 | { |
c801d85f | 30 | if (!win->HasVMT()) return; |
66bd6b93 | 31 | if (g_blockEventsOnDrag) return; |
c801d85f KB |
32 | |
33 | float diff = win->m_adjust->value - win->m_oldPos; | |
34 | if (fabs(diff) < 0.2) return; | |
35 | ||
7798a18e | 36 | wxEventType command = wxEVT_NULL; |
c801d85f KB |
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; | |
84efdbf1 | 50 | if (win->GetWindowStyleFlag() & wxSB_VERTICAL == wxSB_VERTICAL) orient = wxVERTICAL; |
c801d85f KB |
51 | |
52 | wxScrollEvent event( command, win->GetId(), value, orient ); | |
53 | event.SetEventObject( win ); | |
47908e25 | 54 | win->GetEventHandler()->ProcessEvent( event ); |
c801d85f KB |
55 | |
56 | /* | |
57 | wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() ); | |
58 | cevent.SetEventObject( win ); | |
59 | win->ProcessEvent( cevent ); | |
60 | */ | |
6de97a3b | 61 | } |
c801d85f | 62 | |
cb43b372 RR |
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 | ||
b4071e91 RR |
91 | //----------------------------------------------------------------------------- |
92 | // wxScrollBar | |
93 | //----------------------------------------------------------------------------- | |
94 | ||
c801d85f KB |
95 | IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl) |
96 | ||
c801d85f KB |
97 | wxScrollBar::~wxScrollBar(void) |
98 | { | |
6de97a3b | 99 | } |
c801d85f | 100 | |
debe6624 | 101 | bool wxScrollBar::Create(wxWindow *parent, wxWindowID id, |
c801d85f | 102 | const wxPoint& pos, const wxSize& size, |
6de97a3b | 103 | long style, const wxValidator& validator, const wxString& name ) |
c801d85f KB |
104 | { |
105 | m_needParent = TRUE; | |
106 | ||
107 | PreCreation( parent, id, pos, size, style, name ); | |
108 | ||
6de97a3b RR |
109 | SetValidator( validator ); |
110 | ||
c801d85f KB |
111 | m_oldPos = 0.0; |
112 | ||
113 | if (style & wxSB_VERTICAL == wxSB_VERTICAL) | |
c67daf87 | 114 | m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL ); |
c801d85f | 115 | else |
c67daf87 | 116 | m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL ); |
c801d85f KB |
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 | ||
cb43b372 RR |
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 | ||
6ca41e57 RR |
129 | m_parent->AddChild( this ); |
130 | ||
131 | (m_parent->m_insertCallback)( m_parent, this ); | |
132 | ||
c801d85f KB |
133 | PostCreation(); |
134 | ||
f96aa4d9 RR |
135 | SetBackgroundColour( parent->GetBackgroundColour() ); |
136 | ||
c801d85f KB |
137 | Show( TRUE ); |
138 | ||
139 | return TRUE; | |
6de97a3b | 140 | } |
c801d85f KB |
141 | |
142 | int wxScrollBar::GetPosition(void) const | |
143 | { | |
144 | return (int)(m_adjust->value+0.5); | |
6de97a3b | 145 | } |
c801d85f KB |
146 | |
147 | int wxScrollBar::GetThumbSize() const | |
148 | { | |
149 | return (int)(m_adjust->page_size+0.5); | |
6de97a3b | 150 | } |
c801d85f KB |
151 | |
152 | int wxScrollBar::GetPageSize() const | |
153 | { | |
154 | return (int)(m_adjust->page_increment+0.5); | |
6de97a3b | 155 | } |
c801d85f KB |
156 | |
157 | int wxScrollBar::GetRange() const | |
158 | { | |
159 | return (int)(m_adjust->upper+0.5); | |
6de97a3b | 160 | } |
c801d85f | 161 | |
debe6624 | 162 | void wxScrollBar::SetPosition( int viewStart ) |
c801d85f KB |
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; | |
cb43b372 RR |
168 | |
169 | if (!m_isScrolling) | |
170 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); | |
6de97a3b | 171 | } |
c801d85f | 172 | |
debe6624 JS |
173 | void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize, |
174 | bool WXUNUSED(refresh) ) | |
c801d85f KB |
175 | { |
176 | float fpos = (float)position; | |
c801d85f KB |
177 | float frange = (float)range; |
178 | float fthumb = (float)thumbSize; | |
179 | float fpage = (float)pageSize; | |
180 | ||
cb43b372 | 181 | if ((fabs(frange-m_adjust->upper) < 0.2) && |
c801d85f KB |
182 | (fabs(fthumb-m_adjust->page_size) < 0.2) && |
183 | (fabs(fpage-m_adjust->page_increment) < 0.2)) | |
cb43b372 RR |
184 | { |
185 | SetPosition( position ); | |
186 | return; | |
187 | } | |
84efdbf1 | 188 | |
cb43b372 RR |
189 | m_oldPos = fpos; |
190 | ||
c801d85f KB |
191 | m_adjust->lower = 0.0; |
192 | m_adjust->upper = frange; | |
193 | m_adjust->value = fpos; | |
194 | m_adjust->step_increment = 1.0; | |
84efdbf1 | 195 | m_adjust->page_increment = (float)(wxMax(fpage,0)); |
c801d85f KB |
196 | m_adjust->page_size = fthumb; |
197 | ||
198 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); | |
6de97a3b | 199 | } |
c801d85f KB |
200 | |
201 | // Backward compatibility | |
202 | int wxScrollBar::GetValue(void) const | |
203 | { | |
204 | return GetPosition(); | |
6de97a3b | 205 | } |
c801d85f | 206 | |
debe6624 | 207 | void wxScrollBar::SetValue( int viewStart ) |
c801d85f KB |
208 | { |
209 | SetPosition( viewStart ); | |
6de97a3b | 210 | } |
c801d85f KB |
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; | |
6de97a3b | 223 | } |
c801d85f KB |
224 | |
225 | int wxScrollBar::GetViewLength() const | |
226 | { | |
227 | return (int)(m_adjust->upper+0.5); | |
6de97a3b | 228 | } |
c801d85f KB |
229 | |
230 | int wxScrollBar::GetObjectLength() const | |
231 | { | |
232 | return (int)(m_adjust->page_size+0.5); | |
6de97a3b | 233 | } |
c801d85f | 234 | |
debe6624 | 235 | void wxScrollBar::SetPageSize( int pageLength ) |
c801d85f KB |
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 ); | |
6de97a3b | 241 | } |
c801d85f | 242 | |
debe6624 | 243 | void wxScrollBar::SetObjectLength( int objectLength ) |
c801d85f KB |
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 ); | |
6de97a3b | 249 | } |
c801d85f | 250 | |
debe6624 | 251 | void wxScrollBar::SetViewLength( int viewLength ) |
c801d85f KB |
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 ); | |
6de97a3b | 257 | } |
c801d85f | 258 | |
b4071e91 RR |
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 | } | |
58614078 RR |
268 | |
269 | void wxScrollBar::ApplyWidgetStyle() | |
270 | { | |
271 | SetWidgetStyle(); | |
272 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
273 | } | |
274 |