]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/scrolbar.cpp
Tought Systemettings that sysfont has 12 pts
[wxWidgets.git] / src / gtk / scrolbar.cpp
CommitLineData
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
22extern bool g_blockEventsOnDrag;
23
c801d85f 24//-----------------------------------------------------------------------------
b4071e91 25// "value_changed"
c801d85f
KB
26//-----------------------------------------------------------------------------
27
66bd6b93 28static 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
67static 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
80static 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
95IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
96
c801d85f
KB
97wxScrollBar::~wxScrollBar(void)
98{
6de97a3b 99}
c801d85f 100
debe6624 101bool 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
c801d85f
KB
129 PostCreation();
130
f96aa4d9
RR
131 SetBackgroundColour( parent->GetBackgroundColour() );
132
c801d85f
KB
133 Show( TRUE );
134
135 return TRUE;
6de97a3b 136}
c801d85f
KB
137
138int wxScrollBar::GetPosition(void) const
139{
140 return (int)(m_adjust->value+0.5);
6de97a3b 141}
c801d85f
KB
142
143int wxScrollBar::GetThumbSize() const
144{
145 return (int)(m_adjust->page_size+0.5);
6de97a3b 146}
c801d85f
KB
147
148int wxScrollBar::GetPageSize() const
149{
150 return (int)(m_adjust->page_increment+0.5);
6de97a3b 151}
c801d85f
KB
152
153int wxScrollBar::GetRange() const
154{
155 return (int)(m_adjust->upper+0.5);
6de97a3b 156}
c801d85f 157
debe6624 158void wxScrollBar::SetPosition( int viewStart )
c801d85f
KB
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;
cb43b372
RR
164
165 if (!m_isScrolling)
166 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
6de97a3b 167}
c801d85f 168
debe6624
JS
169void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize,
170 bool WXUNUSED(refresh) )
c801d85f
KB
171{
172 float fpos = (float)position;
c801d85f
KB
173 float frange = (float)range;
174 float fthumb = (float)thumbSize;
175 float fpage = (float)pageSize;
176
cb43b372 177 if ((fabs(frange-m_adjust->upper) < 0.2) &&
c801d85f
KB
178 (fabs(fthumb-m_adjust->page_size) < 0.2) &&
179 (fabs(fpage-m_adjust->page_increment) < 0.2))
cb43b372
RR
180 {
181 SetPosition( position );
182 return;
183 }
84efdbf1 184
cb43b372
RR
185 m_oldPos = fpos;
186
c801d85f
KB
187 m_adjust->lower = 0.0;
188 m_adjust->upper = frange;
189 m_adjust->value = fpos;
190 m_adjust->step_increment = 1.0;
84efdbf1 191 m_adjust->page_increment = (float)(wxMax(fpage,0));
c801d85f
KB
192 m_adjust->page_size = fthumb;
193
194 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 195}
c801d85f
KB
196
197// Backward compatibility
198int wxScrollBar::GetValue(void) const
199{
200 return GetPosition();
6de97a3b 201}
c801d85f 202
debe6624 203void wxScrollBar::SetValue( int viewStart )
c801d85f
KB
204{
205 SetPosition( viewStart );
6de97a3b 206}
c801d85f
KB
207
208void 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;
6de97a3b 219}
c801d85f
KB
220
221int wxScrollBar::GetViewLength() const
222{
223 return (int)(m_adjust->upper+0.5);
6de97a3b 224}
c801d85f
KB
225
226int wxScrollBar::GetObjectLength() const
227{
228 return (int)(m_adjust->page_size+0.5);
6de97a3b 229}
c801d85f 230
debe6624 231void wxScrollBar::SetPageSize( int pageLength )
c801d85f
KB
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 );
6de97a3b 237}
c801d85f 238
debe6624 239void wxScrollBar::SetObjectLength( int objectLength )
c801d85f
KB
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 );
6de97a3b 245}
c801d85f 246
debe6624 247void wxScrollBar::SetViewLength( int viewLength )
c801d85f
KB
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 );
6de97a3b 253}
c801d85f 254
b4071e91
RR
255bool 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}
58614078
RR
264
265void wxScrollBar::ApplyWidgetStyle()
266{
267 SetWidgetStyle();
268 gtk_widget_set_style( m_widget, m_widgetStyle );
269}
270