]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: scrolbar.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6 | // Id: | |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "scrolbar.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/scrolbar.h" | |
17 | #include "wx/utils.h" | |
18 | ||
19 | //----------------------------------------------------------------------------- | |
20 | // data | |
21 | //----------------------------------------------------------------------------- | |
22 | ||
23 | extern bool g_blockEventsOnDrag; | |
24 | ||
25 | //----------------------------------------------------------------------------- | |
26 | // "value_changed" | |
27 | //----------------------------------------------------------------------------- | |
28 | ||
29 | static void gtk_scrollbar_callback( GtkWidget *WXUNUSED(widget), wxScrollBar *win ) | |
30 | { | |
31 | if (!win->HasVMT()) return; | |
32 | if (g_blockEventsOnDrag) return; | |
33 | ||
34 | float diff = win->m_adjust->value - win->m_oldPos; | |
35 | if (fabs(diff) < 0.2) return; | |
36 | ||
37 | wxEventType command = wxEVT_NULL; | |
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; | |
51 | if (win->GetWindowStyleFlag() & wxSB_VERTICAL == wxSB_VERTICAL) orient = wxVERTICAL; | |
52 | ||
53 | wxScrollEvent event( command, win->GetId(), value, orient ); | |
54 | event.SetEventObject( win ); | |
55 | win->GetEventHandler()->ProcessEvent( event ); | |
56 | ||
57 | /* | |
58 | wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() ); | |
59 | cevent.SetEventObject( win ); | |
60 | win->ProcessEvent( cevent ); | |
61 | */ | |
62 | } | |
63 | ||
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 | ||
92 | //----------------------------------------------------------------------------- | |
93 | // wxScrollBar | |
94 | //----------------------------------------------------------------------------- | |
95 | ||
96 | IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl) | |
97 | ||
98 | wxScrollBar::~wxScrollBar(void) | |
99 | { | |
100 | } | |
101 | ||
102 | bool wxScrollBar::Create(wxWindow *parent, wxWindowID id, | |
103 | const wxPoint& pos, const wxSize& size, | |
104 | long style, const wxValidator& validator, const wxString& name ) | |
105 | { | |
106 | m_needParent = TRUE; | |
107 | ||
108 | PreCreation( parent, id, pos, size, style, name ); | |
109 | ||
110 | SetValidator( validator ); | |
111 | ||
112 | m_oldPos = 0.0; | |
113 | ||
114 | if (style & wxSB_VERTICAL == wxSB_VERTICAL) | |
115 | m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL ); | |
116 | else | |
117 | m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL ); | |
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 | ||
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 | ||
130 | PostCreation(); | |
131 | ||
132 | Show( TRUE ); | |
133 | ||
134 | return TRUE; | |
135 | } | |
136 | ||
137 | int wxScrollBar::GetPosition(void) const | |
138 | { | |
139 | return (int)(m_adjust->value+0.5); | |
140 | } | |
141 | ||
142 | int wxScrollBar::GetThumbSize() const | |
143 | { | |
144 | return (int)(m_adjust->page_size+0.5); | |
145 | } | |
146 | ||
147 | int wxScrollBar::GetPageSize() const | |
148 | { | |
149 | return (int)(m_adjust->page_increment+0.5); | |
150 | } | |
151 | ||
152 | int wxScrollBar::GetRange() const | |
153 | { | |
154 | return (int)(m_adjust->upper+0.5); | |
155 | } | |
156 | ||
157 | void wxScrollBar::SetPosition( int viewStart ) | |
158 | { | |
159 | float fpos = (float)viewStart; | |
160 | m_oldPos = fpos; | |
161 | if (fabs(fpos-m_adjust->value) < 0.2) return; | |
162 | m_adjust->value = fpos; | |
163 | ||
164 | if (!m_isScrolling) | |
165 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); | |
166 | } | |
167 | ||
168 | void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize, | |
169 | bool WXUNUSED(refresh) ) | |
170 | { | |
171 | float fpos = (float)position; | |
172 | float frange = (float)range; | |
173 | float fthumb = (float)thumbSize; | |
174 | float fpage = (float)pageSize; | |
175 | ||
176 | if ((fabs(frange-m_adjust->upper) < 0.2) && | |
177 | (fabs(fthumb-m_adjust->page_size) < 0.2) && | |
178 | (fabs(fpage-m_adjust->page_increment) < 0.2)) | |
179 | { | |
180 | SetPosition( position ); | |
181 | return; | |
182 | } | |
183 | ||
184 | m_oldPos = fpos; | |
185 | ||
186 | m_adjust->lower = 0.0; | |
187 | m_adjust->upper = frange; | |
188 | m_adjust->value = fpos; | |
189 | m_adjust->step_increment = 1.0; | |
190 | m_adjust->page_increment = (float)(wxMax(fpage,0)); | |
191 | m_adjust->page_size = fthumb; | |
192 | ||
193 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); | |
194 | } | |
195 | ||
196 | // Backward compatibility | |
197 | int wxScrollBar::GetValue(void) const | |
198 | { | |
199 | return GetPosition(); | |
200 | } | |
201 | ||
202 | void wxScrollBar::SetValue( int viewStart ) | |
203 | { | |
204 | SetPosition( viewStart ); | |
205 | } | |
206 | ||
207 | void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const | |
208 | { | |
209 | int pos = (int)(m_adjust->value+0.5); | |
210 | int thumb = (int)(m_adjust->page_size+0.5); | |
211 | int page = (int)(m_adjust->page_increment+0.5); | |
212 | int range = (int)(m_adjust->upper+0.5); | |
213 | ||
214 | *viewStart = pos; | |
215 | *viewLength = range; | |
216 | *objectLength = thumb; | |
217 | *pageLength = page; | |
218 | } | |
219 | ||
220 | int wxScrollBar::GetViewLength() const | |
221 | { | |
222 | return (int)(m_adjust->upper+0.5); | |
223 | } | |
224 | ||
225 | int wxScrollBar::GetObjectLength() const | |
226 | { | |
227 | return (int)(m_adjust->page_size+0.5); | |
228 | } | |
229 | ||
230 | void wxScrollBar::SetPageSize( int pageLength ) | |
231 | { | |
232 | int pos = (int)(m_adjust->value+0.5); | |
233 | int thumb = (int)(m_adjust->page_size+0.5); | |
234 | int range = (int)(m_adjust->upper+0.5); | |
235 | SetScrollbar( pos, thumb, range, pageLength ); | |
236 | } | |
237 | ||
238 | void wxScrollBar::SetObjectLength( int objectLength ) | |
239 | { | |
240 | int pos = (int)(m_adjust->value+0.5); | |
241 | int page = (int)(m_adjust->page_increment+0.5); | |
242 | int range = (int)(m_adjust->upper+0.5); | |
243 | SetScrollbar( pos, objectLength, range, page ); | |
244 | } | |
245 | ||
246 | void wxScrollBar::SetViewLength( int viewLength ) | |
247 | { | |
248 | int pos = (int)(m_adjust->value+0.5); | |
249 | int thumb = (int)(m_adjust->page_size+0.5); | |
250 | int page = (int)(m_adjust->page_increment+0.5); | |
251 | SetScrollbar( pos, thumb, viewLength, page ); | |
252 | } | |
253 | ||
254 | bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window ) | |
255 | { | |
256 | GtkRange *range = GTK_RANGE(m_widget); | |
257 | return ( (window == GTK_WIDGET(range)->window) || | |
258 | (window == range->trough) || | |
259 | (window == range->slider) || | |
260 | (window == range->step_forw) || | |
261 | (window == range->step_back) ); | |
262 | } |