]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/scrolbar.cpp
scrollbar handling simplification
[wxWidgets.git] / src / gtk / scrolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/scrolbar.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_SCROLLBAR
14
15 #include "wx/scrolbar.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/utils.h"
19 #endif
20
21 #include "wx/gtk/private.h"
22
23 //-----------------------------------------------------------------------------
24 // "value_changed" from scrollbar
25 //-----------------------------------------------------------------------------
26
27 extern "C" {
28 static void
29 gtk_value_changed(GtkRange* range, wxScrollBar* win)
30 {
31 wxEventType eventType = win->GetScrollEventType(range);
32 if (eventType != wxEVT_NULL)
33 {
34 const int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
35 const int value = win->GetThumbPosition();
36 wxScrollEvent event(eventType, win->GetId(), value, orient);
37 event.SetEventObject(win);
38 win->GetEventHandler()->ProcessEvent(event);
39 if (!win->m_isScrolling)
40 {
41 wxScrollEvent event(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
42 event.SetEventObject(win);
43 win->GetEventHandler()->ProcessEvent(event);
44 }
45 }
46 }
47 }
48
49 //-----------------------------------------------------------------------------
50 // "button_press_event" from scrollbar
51 //-----------------------------------------------------------------------------
52
53 extern "C" {
54 static gboolean
55 gtk_button_press_event(GtkRange*, GdkEventButton*, wxScrollBar* win)
56 {
57 if (g_isIdle) wxapp_install_idle_handler();
58
59 win->m_mouseButtonDown = true;
60 return false;
61 }
62 }
63
64 //-----------------------------------------------------------------------------
65 // "button_release_event" from scrollbar
66 //-----------------------------------------------------------------------------
67
68 extern "C" {
69 static gboolean
70 gtk_button_release_event(GtkRange*, GdkEventButton*, wxScrollBar* win)
71 {
72 if (g_isIdle)
73 wxapp_install_idle_handler();
74
75 win->m_mouseButtonDown = false;
76 // If thumb tracking
77 if (win->m_isScrolling)
78 {
79 win->m_isScrolling = false;
80 const int value = win->GetThumbPosition();
81 const int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
82
83 wxScrollEvent event(wxEVT_SCROLL_THUMBRELEASE, win->GetId(), value, orient);
84 event.SetEventObject(win);
85 win->GetEventHandler()->ProcessEvent(event);
86
87 wxScrollEvent event2(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
88 event2.SetEventObject(win);
89 win->GetEventHandler()->ProcessEvent(event2);
90 }
91
92 return false;
93 }
94 }
95
96 //-----------------------------------------------------------------------------
97 // wxScrollBar
98 //-----------------------------------------------------------------------------
99
100 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
101
102 wxScrollBar::wxScrollBar()
103 {
104 }
105
106 wxScrollBar::~wxScrollBar()
107 {
108 }
109
110 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
111 const wxPoint& pos, const wxSize& size,
112 long style, const wxValidator& validator, const wxString& name )
113 {
114 m_needParent = true;
115 m_acceptsFocus = true;
116
117 if (!PreCreation( parent, pos, size ) ||
118 !CreateBase( parent, id, pos, size, style, validator, name ))
119 {
120 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
121 return false;
122 }
123
124 const bool isVertical = (style & wxSB_VERTICAL) != 0;
125 if (isVertical)
126 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
127 else
128 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
129
130 m_scrollBar[int(isVertical)] = (GtkRange*)m_widget;
131
132 g_signal_connect(m_widget, "value_changed",
133 G_CALLBACK(gtk_value_changed), this);
134 g_signal_connect(m_widget, "button_press_event",
135 G_CALLBACK(gtk_button_press_event), this);
136 g_signal_connect(m_widget, "button_release_event",
137 G_CALLBACK(gtk_button_release_event), this);
138
139 m_parent->DoAddChild( this );
140
141 PostCreation(size);
142
143 return true;
144 }
145
146 int wxScrollBar::GetThumbPosition() const
147 {
148 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
149 return int(adj->value + 0.5);
150 }
151
152 int wxScrollBar::GetThumbSize() const
153 {
154 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
155 return int(adj->page_size);
156 }
157
158 int wxScrollBar::GetPageSize() const
159 {
160 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
161 return int(adj->page_increment);
162 }
163
164 int wxScrollBar::GetRange() const
165 {
166 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
167 return int(adj->upper);
168 }
169
170 void wxScrollBar::SetThumbPosition( int viewStart )
171 {
172 if (GetThumbPosition() != viewStart)
173 {
174 BlockScrollEvent();
175 gtk_range_set_value((GtkRange*)m_widget, viewStart);
176 UnblockScrollEvent();
177 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
178 const int i = HasFlag(wxSB_VERTICAL);
179 m_scrollPos[i] = adj->value;
180 }
181 }
182
183 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize, bool)
184 {
185 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
186 adj->value = position;
187 adj->step_increment = 1;
188 adj->page_increment = pageSize;
189 adj->page_size = thumbSize;
190 BlockScrollEvent();
191 gtk_range_set_range((GtkRange*)m_widget, 0, range);
192 UnblockScrollEvent();
193 const int i = HasFlag(wxSB_VERTICAL);
194 m_scrollPos[i] = adj->value;
195 }
196
197 void wxScrollBar::SetPageSize( int pageLength )
198 {
199 SetScrollbar(GetThumbPosition(), GetThumbSize(), GetRange(), pageLength);
200 }
201
202 void wxScrollBar::SetRange(int range)
203 {
204 SetScrollbar(GetThumbPosition(), GetThumbSize(), range, GetPageSize());
205 }
206
207 bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window )
208 {
209 GtkRange *range = GTK_RANGE(m_widget);
210 return ( (window == GTK_WIDGET(range)->window) );
211 }
212
213 // static
214 wxVisualAttributes
215 wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
216 {
217 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new);
218 }
219
220 #endif // wxUSE_SCROLLBAR