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