]>
Commit | Line | Data |
---|---|---|
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->GTKGetScrollEventType(range); | |
32 | if (eventType != wxEVT_NULL) | |
33 | { | |
34 | const int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL; | |
35 | const int value = win->GetThumbPosition(); | |
36 | const int id = win->GetId(); | |
37 | ||
38 | // first send the specific event for the user action | |
39 | wxScrollEvent evtSpec(eventType, id, value, orient); | |
40 | evtSpec.SetEventObject(win); | |
41 | win->HandleWindowEvent(evtSpec); | |
42 | ||
43 | if (!win->m_isScrolling) | |
44 | { | |
45 | // and if it's over also send a general "changed" event | |
46 | wxScrollEvent evtChanged(wxEVT_SCROLL_CHANGED, id, value, orient); | |
47 | evtChanged.SetEventObject(win); | |
48 | win->HandleWindowEvent(evtChanged); | |
49 | } | |
50 | } | |
51 | } | |
52 | } | |
53 | ||
54 | //----------------------------------------------------------------------------- | |
55 | // "button_press_event" from scrollbar | |
56 | //----------------------------------------------------------------------------- | |
57 | ||
58 | extern "C" { | |
59 | static gboolean | |
60 | gtk_button_press_event(GtkRange*, GdkEventButton*, wxScrollBar* win) | |
61 | { | |
62 | win->m_mouseButtonDown = true; | |
63 | return false; | |
64 | } | |
65 | } | |
66 | ||
67 | //----------------------------------------------------------------------------- | |
68 | // "event_after" from scrollbar | |
69 | //----------------------------------------------------------------------------- | |
70 | ||
71 | extern "C" { | |
72 | static void | |
73 | gtk_event_after(GtkRange* range, GdkEvent* event, wxScrollBar* win) | |
74 | { | |
75 | if (event->type == GDK_BUTTON_RELEASE) | |
76 | { | |
77 | g_signal_handlers_block_by_func(range, (void*)gtk_event_after, win); | |
78 | ||
79 | const int value = win->GetThumbPosition(); | |
80 | const int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL; | |
81 | const int id = win->GetId(); | |
82 | ||
83 | wxScrollEvent evtRel(wxEVT_SCROLL_THUMBRELEASE, id, value, orient); | |
84 | evtRel.SetEventObject(win); | |
85 | win->HandleWindowEvent(evtRel); | |
86 | ||
87 | wxScrollEvent evtChanged(wxEVT_SCROLL_CHANGED, id, value, orient); | |
88 | evtChanged.SetEventObject(win); | |
89 | win->HandleWindowEvent(evtChanged); | |
90 | } | |
91 | } | |
92 | } | |
93 | ||
94 | //----------------------------------------------------------------------------- | |
95 | // "button_release_event" from scrollbar | |
96 | //----------------------------------------------------------------------------- | |
97 | ||
98 | extern "C" { | |
99 | static gboolean | |
100 | gtk_button_release_event(GtkRange* range, GdkEventButton*, wxScrollBar* win) | |
101 | { | |
102 | win->m_mouseButtonDown = false; | |
103 | // If thumb tracking | |
104 | if (win->m_isScrolling) | |
105 | { | |
106 | win->m_isScrolling = false; | |
107 | // Hook up handler to send thumb release event after this emission is finished. | |
108 | // To allow setting scroll position from event handler, sending event must | |
109 | // be deferred until after the GtkRange handler for this signal has run | |
110 | g_signal_handlers_unblock_by_func(range, (void*)gtk_event_after, win); | |
111 | } | |
112 | ||
113 | return false; | |
114 | } | |
115 | } | |
116 | ||
117 | //----------------------------------------------------------------------------- | |
118 | // wxScrollBar | |
119 | //----------------------------------------------------------------------------- | |
120 | ||
121 | wxScrollBar::wxScrollBar() | |
122 | { | |
123 | } | |
124 | ||
125 | wxScrollBar::~wxScrollBar() | |
126 | { | |
127 | } | |
128 | ||
129 | bool wxScrollBar::Create(wxWindow *parent, wxWindowID id, | |
130 | const wxPoint& pos, const wxSize& size, | |
131 | long style, const wxValidator& validator, const wxString& name ) | |
132 | { | |
133 | if (!PreCreation( parent, pos, size ) || | |
134 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
135 | { | |
136 | wxFAIL_MSG( wxT("wxScrollBar creation failed") ); | |
137 | return false; | |
138 | } | |
139 | ||
140 | const bool isVertical = (style & wxSB_VERTICAL) != 0; | |
141 | if (isVertical) | |
142 | m_widget = gtk_vscrollbar_new( NULL ); | |
143 | else | |
144 | m_widget = gtk_hscrollbar_new( NULL ); | |
145 | g_object_ref(m_widget); | |
146 | ||
147 | m_scrollBar[0] = (GtkRange*)m_widget; | |
148 | ||
149 | g_signal_connect_after(m_widget, "value_changed", | |
150 | G_CALLBACK(gtk_value_changed), this); | |
151 | g_signal_connect(m_widget, "button_press_event", | |
152 | G_CALLBACK(gtk_button_press_event), this); | |
153 | g_signal_connect(m_widget, "button_release_event", | |
154 | G_CALLBACK(gtk_button_release_event), this); | |
155 | ||
156 | gulong handler_id; | |
157 | handler_id = g_signal_connect( | |
158 | m_widget, "event_after", G_CALLBACK(gtk_event_after), this); | |
159 | g_signal_handler_block(m_widget, handler_id); | |
160 | ||
161 | m_parent->DoAddChild( this ); | |
162 | ||
163 | PostCreation(size); | |
164 | ||
165 | return true; | |
166 | } | |
167 | ||
168 | int wxScrollBar::GetThumbPosition() const | |
169 | { | |
170 | GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment; | |
171 | return int(adj->value + 0.5); | |
172 | } | |
173 | ||
174 | int wxScrollBar::GetThumbSize() const | |
175 | { | |
176 | GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment; | |
177 | return int(adj->page_size); | |
178 | } | |
179 | ||
180 | int wxScrollBar::GetPageSize() const | |
181 | { | |
182 | GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment; | |
183 | return int(adj->page_increment); | |
184 | } | |
185 | ||
186 | int wxScrollBar::GetRange() const | |
187 | { | |
188 | GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment; | |
189 | return int(adj->upper); | |
190 | } | |
191 | ||
192 | void wxScrollBar::SetThumbPosition( int viewStart ) | |
193 | { | |
194 | if (GetThumbPosition() != viewStart) | |
195 | { | |
196 | g_signal_handlers_block_by_func(m_widget, | |
197 | (gpointer)gtk_value_changed, this); | |
198 | ||
199 | gtk_range_set_value((GtkRange*)m_widget, viewStart); | |
200 | m_scrollPos[0] = gtk_range_get_value((GtkRange*)m_widget); | |
201 | ||
202 | g_signal_handlers_unblock_by_func(m_widget, | |
203 | (gpointer)gtk_value_changed, this); | |
204 | } | |
205 | } | |
206 | ||
207 | void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize, bool) | |
208 | { | |
209 | if (range == 0) | |
210 | { | |
211 | // GtkRange requires upper > lower | |
212 | range = | |
213 | thumbSize = 1; | |
214 | } | |
215 | GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment; | |
216 | adj->step_increment = 1; | |
217 | adj->page_increment = pageSize; | |
218 | adj->page_size = thumbSize; | |
219 | adj->value = position; | |
220 | g_signal_handlers_block_by_func(m_widget, (void*)gtk_value_changed, this); | |
221 | gtk_range_set_range((GtkRange*)m_widget, 0, range); | |
222 | m_scrollPos[0] = adj->value; | |
223 | g_signal_handlers_unblock_by_func(m_widget, (void*)gtk_value_changed, this); | |
224 | } | |
225 | ||
226 | void wxScrollBar::SetPageSize( int pageLength ) | |
227 | { | |
228 | SetScrollbar(GetThumbPosition(), GetThumbSize(), GetRange(), pageLength); | |
229 | } | |
230 | ||
231 | void wxScrollBar::SetRange(int range) | |
232 | { | |
233 | SetScrollbar(GetThumbPosition(), GetThumbSize(), range, GetPageSize()); | |
234 | } | |
235 | ||
236 | GdkWindow *wxScrollBar::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const | |
237 | { | |
238 | return m_widget->window; | |
239 | } | |
240 | ||
241 | // static | |
242 | wxVisualAttributes | |
243 | wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
244 | { | |
245 | return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new); | |
246 | } | |
247 | ||
248 | #endif // wxUSE_SCROLLBAR |