]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/scrolbar.cpp
All functionality I know hot implement already implemented.
[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
18 //-----------------------------------------------------------------------------
19 // data
20 //-----------------------------------------------------------------------------
21
22 extern bool g_blockEventsOnDrag;
23
24 //-----------------------------------------------------------------------------
25 // "value_changed"
26 //-----------------------------------------------------------------------------
27
28 static void gtk_scrollbar_callback( GtkWidget *WXUNUSED(widget), wxScrollBar *win )
29 {
30 if (!win->HasVMT()) return;
31 if (g_blockEventsOnDrag) return;
32
33 float diff = win->m_adjust->value - win->m_oldPos;
34 if (fabs(diff) < 0.2) return;
35
36 wxEventType command = wxEVT_NULL;
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;
50 if (win->GetWindowStyleFlag() & wxSB_VERTICAL == wxSB_VERTICAL) orient = wxVERTICAL;
51
52 wxScrollEvent event( command, win->GetId(), value, orient );
53 event.SetEventObject( win );
54 win->GetEventHandler()->ProcessEvent( event );
55
56 /*
57 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
58 cevent.SetEventObject( win );
59 win->ProcessEvent( cevent );
60 */
61 }
62
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
91 //-----------------------------------------------------------------------------
92 // wxScrollBar
93 //-----------------------------------------------------------------------------
94
95 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
96
97 wxScrollBar::~wxScrollBar(void)
98 {
99 }
100
101 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
102 const wxPoint& pos, const wxSize& size,
103 long style, const wxValidator& validator, const wxString& name )
104 {
105 m_needParent = TRUE;
106
107 PreCreation( parent, id, pos, size, style, name );
108
109 SetValidator( validator );
110
111 m_oldPos = 0.0;
112
113 if (style & wxSB_VERTICAL == wxSB_VERTICAL)
114 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
115 else
116 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
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
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
129 PostCreation();
130
131 SetBackgroundColour( parent->GetBackgroundColour() );
132
133 Show( TRUE );
134
135 return TRUE;
136 }
137
138 int wxScrollBar::GetPosition(void) const
139 {
140 return (int)(m_adjust->value+0.5);
141 }
142
143 int wxScrollBar::GetThumbSize() const
144 {
145 return (int)(m_adjust->page_size+0.5);
146 }
147
148 int wxScrollBar::GetPageSize() const
149 {
150 return (int)(m_adjust->page_increment+0.5);
151 }
152
153 int wxScrollBar::GetRange() const
154 {
155 return (int)(m_adjust->upper+0.5);
156 }
157
158 void wxScrollBar::SetPosition( int viewStart )
159 {
160 float fpos = (float)viewStart;
161 m_oldPos = fpos;
162 if (fabs(fpos-m_adjust->value) < 0.2) return;
163 m_adjust->value = fpos;
164
165 if (!m_isScrolling)
166 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
167 }
168
169 void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize,
170 bool WXUNUSED(refresh) )
171 {
172 float fpos = (float)position;
173 float frange = (float)range;
174 float fthumb = (float)thumbSize;
175 float fpage = (float)pageSize;
176
177 if ((fabs(frange-m_adjust->upper) < 0.2) &&
178 (fabs(fthumb-m_adjust->page_size) < 0.2) &&
179 (fabs(fpage-m_adjust->page_increment) < 0.2))
180 {
181 SetPosition( position );
182 return;
183 }
184
185 m_oldPos = fpos;
186
187 m_adjust->lower = 0.0;
188 m_adjust->upper = frange;
189 m_adjust->value = fpos;
190 m_adjust->step_increment = 1.0;
191 m_adjust->page_increment = (float)(wxMax(fpage,0));
192 m_adjust->page_size = fthumb;
193
194 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
195 }
196
197 // Backward compatibility
198 int wxScrollBar::GetValue(void) const
199 {
200 return GetPosition();
201 }
202
203 void wxScrollBar::SetValue( int viewStart )
204 {
205 SetPosition( viewStart );
206 }
207
208 void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const
209 {
210 int pos = (int)(m_adjust->value+0.5);
211 int thumb = (int)(m_adjust->page_size+0.5);
212 int page = (int)(m_adjust->page_increment+0.5);
213 int range = (int)(m_adjust->upper+0.5);
214
215 *viewStart = pos;
216 *viewLength = range;
217 *objectLength = thumb;
218 *pageLength = page;
219 }
220
221 int wxScrollBar::GetViewLength() const
222 {
223 return (int)(m_adjust->upper+0.5);
224 }
225
226 int wxScrollBar::GetObjectLength() const
227 {
228 return (int)(m_adjust->page_size+0.5);
229 }
230
231 void wxScrollBar::SetPageSize( int pageLength )
232 {
233 int pos = (int)(m_adjust->value+0.5);
234 int thumb = (int)(m_adjust->page_size+0.5);
235 int range = (int)(m_adjust->upper+0.5);
236 SetScrollbar( pos, thumb, range, pageLength );
237 }
238
239 void wxScrollBar::SetObjectLength( int objectLength )
240 {
241 int pos = (int)(m_adjust->value+0.5);
242 int page = (int)(m_adjust->page_increment+0.5);
243 int range = (int)(m_adjust->upper+0.5);
244 SetScrollbar( pos, objectLength, range, page );
245 }
246
247 void wxScrollBar::SetViewLength( int viewLength )
248 {
249 int pos = (int)(m_adjust->value+0.5);
250 int thumb = (int)(m_adjust->page_size+0.5);
251 int page = (int)(m_adjust->page_increment+0.5);
252 SetScrollbar( pos, thumb, viewLength, page );
253 }
254
255 bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window )
256 {
257 GtkRange *range = GTK_RANGE(m_widget);
258 return ( (window == GTK_WIDGET(range)->window) ||
259 (window == range->trough) ||
260 (window == range->slider) ||
261 (window == range->step_forw) ||
262 (window == range->step_back) );
263 }
264
265 void wxScrollBar::ApplyWidgetStyle()
266 {
267 SetWidgetStyle();
268 gtk_widget_set_style( m_widget, m_widgetStyle );
269 }
270