]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
1e6feb95 | 2 | // Name: src/gtk/scrolbar.cpp |
c801d85f KB |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
f96aa4d9 RR |
5 | // Id: $Id$ |
6 | // Copyright: (c) 1998 Robert Roebling | |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
dcf924a3 RR |
12 | |
13 | #if wxUSE_SCROLLBAR | |
14 | ||
1e6feb95 VZ |
15 | #include "wx/scrolbar.h" |
16 | ||
de6185e2 WS |
17 | #ifndef WX_PRECOMP |
18 | #include "wx/utils.h" | |
19 | #endif | |
20 | ||
9e691f46 | 21 | #include "wx/gtk/private.h" |
83624f79 | 22 | |
66bd6b93 | 23 | //----------------------------------------------------------------------------- |
add7cadd | 24 | // "value_changed" from scrollbar |
66bd6b93 RR |
25 | //----------------------------------------------------------------------------- |
26 | ||
865bb325 | 27 | extern "C" { |
add7cadd PC |
28 | static void |
29 | gtk_value_changed(GtkRange* range, wxScrollBar* win) | |
a2615ebc | 30 | { |
71ead4bf | 31 | wxEventType eventType = win->GTKGetScrollEventType(range); |
add7cadd | 32 | if (eventType != wxEVT_NULL) |
7c2f14ee | 33 | { |
add7cadd PC |
34 | const int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL; |
35 | const int value = win->GetThumbPosition(); | |
74ab5f5b VZ |
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); | |
937013e0 | 41 | win->HandleWindowEvent(evtSpec); |
74ab5f5b | 42 | |
add7cadd PC |
43 | if (!win->m_isScrolling) |
44 | { | |
74ab5f5b VZ |
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); | |
937013e0 | 48 | win->HandleWindowEvent(evtChanged); |
add7cadd | 49 | } |
91af0895 | 50 | } |
6de97a3b | 51 | } |
865bb325 | 52 | } |
c801d85f | 53 | |
cb43b372 | 54 | //----------------------------------------------------------------------------- |
add7cadd | 55 | // "button_press_event" from scrollbar |
cb43b372 | 56 | //----------------------------------------------------------------------------- |
add7cadd | 57 | |
865bb325 | 58 | extern "C" { |
add7cadd PC |
59 | static gboolean |
60 | gtk_button_press_event(GtkRange*, GdkEventButton*, wxScrollBar* win) | |
cb43b372 | 61 | { |
add7cadd PC |
62 | win->m_mouseButtonDown = true; |
63 | return false; | |
cb43b372 | 64 | } |
865bb325 | 65 | } |
cb43b372 | 66 | |
c918b2cd PC |
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; | |
74ab5f5b | 81 | const int id = win->GetId(); |
c918b2cd | 82 | |
74ab5f5b VZ |
83 | wxScrollEvent evtRel(wxEVT_SCROLL_THUMBRELEASE, id, value, orient); |
84 | evtRel.SetEventObject(win); | |
937013e0 | 85 | win->HandleWindowEvent(evtRel); |
c918b2cd | 86 | |
74ab5f5b VZ |
87 | wxScrollEvent evtChanged(wxEVT_SCROLL_CHANGED, id, value, orient); |
88 | evtChanged.SetEventObject(win); | |
937013e0 | 89 | win->HandleWindowEvent(evtChanged); |
c918b2cd PC |
90 | } |
91 | } | |
92 | } | |
93 | ||
cb43b372 | 94 | //----------------------------------------------------------------------------- |
add7cadd | 95 | // "button_release_event" from scrollbar |
cb43b372 RR |
96 | //----------------------------------------------------------------------------- |
97 | ||
865bb325 | 98 | extern "C" { |
add7cadd | 99 | static gboolean |
c918b2cd | 100 | gtk_button_release_event(GtkRange* range, GdkEventButton*, wxScrollBar* win) |
cb43b372 | 101 | { |
add7cadd PC |
102 | win->m_mouseButtonDown = false; |
103 | // If thumb tracking | |
2a23d363 RR |
104 | if (win->m_isScrolling) |
105 | { | |
add7cadd | 106 | win->m_isScrolling = false; |
c918b2cd | 107 | // Hook up handler to send thumb release event after this emission is finished. |
8ea30e36 PC |
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 | |
c918b2cd | 110 | g_signal_handlers_unblock_by_func(range, (void*)gtk_event_after, win); |
add7cadd | 111 | } |
7c2f14ee | 112 | |
add7cadd | 113 | return false; |
cb43b372 | 114 | } |
865bb325 | 115 | } |
cb43b372 | 116 | |
b4071e91 RR |
117 | //----------------------------------------------------------------------------- |
118 | // wxScrollBar | |
119 | //----------------------------------------------------------------------------- | |
120 | ||
add7cadd PC |
121 | wxScrollBar::wxScrollBar() |
122 | { | |
123 | } | |
124 | ||
0a07a7d8 | 125 | wxScrollBar::~wxScrollBar() |
c801d85f | 126 | { |
6de97a3b | 127 | } |
c801d85f | 128 | |
debe6624 | 129 | bool wxScrollBar::Create(wxWindow *parent, wxWindowID id, |
c801d85f | 130 | const wxPoint& pos, const wxSize& size, |
6de97a3b | 131 | long style, const wxValidator& validator, const wxString& name ) |
c801d85f | 132 | { |
4dcaf11a RR |
133 | if (!PreCreation( parent, pos, size ) || |
134 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
135 | { | |
223d09f6 | 136 | wxFAIL_MSG( wxT("wxScrollBar creation failed") ); |
91af0895 | 137 | return false; |
4dcaf11a | 138 | } |
6de97a3b | 139 | |
add7cadd | 140 | const bool isVertical = (style & wxSB_VERTICAL) != 0; |
1897abe1 | 141 | m_widget = gtk_scrollbar_new(GtkOrientation(isVertical), NULL); |
9ff9d30c | 142 | g_object_ref(m_widget); |
d9bd1494 | 143 | |
63c95f27 | 144 | m_scrollBar[0] = (GtkRange*)m_widget; |
a2615ebc | 145 | |
40e5ebbf | 146 | g_signal_connect_after(m_widget, "value_changed", |
add7cadd PC |
147 | G_CALLBACK(gtk_value_changed), this); |
148 | g_signal_connect(m_widget, "button_press_event", | |
149 | G_CALLBACK(gtk_button_press_event), this); | |
150 | g_signal_connect(m_widget, "button_release_event", | |
151 | G_CALLBACK(gtk_button_release_event), this); | |
cb43b372 | 152 | |
c918b2cd PC |
153 | gulong handler_id; |
154 | handler_id = g_signal_connect( | |
155 | m_widget, "event_after", G_CALLBACK(gtk_event_after), this); | |
156 | g_signal_handler_block(m_widget, handler_id); | |
157 | ||
f03fc89f | 158 | m_parent->DoAddChild( this ); |
a2615ebc | 159 | |
abdeb9e7 | 160 | PostCreation(size); |
a2615ebc | 161 | |
91af0895 | 162 | return true; |
6de97a3b | 163 | } |
c801d85f | 164 | |
0a07a7d8 | 165 | int wxScrollBar::GetThumbPosition() const |
c801d85f | 166 | { |
385e8575 | 167 | return wxRound(gtk_range_get_value(GTK_RANGE(m_widget))); |
6de97a3b | 168 | } |
c801d85f KB |
169 | |
170 | int wxScrollBar::GetThumbSize() const | |
171 | { | |
385e8575 PC |
172 | GtkAdjustment* adj = gtk_range_get_adjustment(GTK_RANGE(m_widget)); |
173 | return int(gtk_adjustment_get_page_size(adj)); | |
6de97a3b | 174 | } |
c801d85f KB |
175 | |
176 | int wxScrollBar::GetPageSize() const | |
177 | { | |
385e8575 PC |
178 | GtkAdjustment* adj = gtk_range_get_adjustment(GTK_RANGE(m_widget)); |
179 | return int(gtk_adjustment_get_page_increment(adj)); | |
6de97a3b | 180 | } |
c801d85f KB |
181 | |
182 | int wxScrollBar::GetRange() const | |
183 | { | |
385e8575 PC |
184 | GtkAdjustment* adj = gtk_range_get_adjustment(GTK_RANGE(m_widget)); |
185 | return int(gtk_adjustment_get_upper(adj)); | |
6de97a3b | 186 | } |
c801d85f | 187 | |
4fabb575 | 188 | void wxScrollBar::SetThumbPosition( int viewStart ) |
c801d85f | 189 | { |
add7cadd | 190 | if (GetThumbPosition() != viewStart) |
2d17d68f | 191 | { |
98264520 PC |
192 | g_signal_handlers_block_by_func(m_widget, |
193 | (gpointer)gtk_value_changed, this); | |
40e5ebbf | 194 | |
63c95f27 PC |
195 | gtk_range_set_value((GtkRange*)m_widget, viewStart); |
196 | m_scrollPos[0] = gtk_range_get_value((GtkRange*)m_widget); | |
98264520 PC |
197 | |
198 | g_signal_handlers_unblock_by_func(m_widget, | |
199 | (gpointer)gtk_value_changed, this); | |
2d17d68f | 200 | } |
6de97a3b | 201 | } |
c801d85f | 202 | |
add7cadd | 203 | void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize, bool) |
c801d85f | 204 | { |
de7bb802 PC |
205 | if (range == 0) |
206 | { | |
207 | // GtkRange requires upper > lower | |
208 | range = | |
209 | thumbSize = 1; | |
210 | } | |
63c95f27 | 211 | g_signal_handlers_block_by_func(m_widget, (void*)gtk_value_changed, this); |
385e8575 PC |
212 | GtkRange* widget = GTK_RANGE(m_widget); |
213 | gtk_adjustment_set_page_size(gtk_range_get_adjustment(widget), thumbSize); | |
214 | gtk_range_set_increments(widget, 1, pageSize); | |
215 | gtk_range_set_range(widget, 0, range); | |
216 | gtk_range_set_value(widget, position); | |
217 | m_scrollPos[0] = gtk_range_get_value(widget); | |
63c95f27 | 218 | g_signal_handlers_unblock_by_func(m_widget, (void*)gtk_value_changed, this); |
6de97a3b | 219 | } |
c801d85f | 220 | |
debe6624 | 221 | void wxScrollBar::SetPageSize( int pageLength ) |
c801d85f | 222 | { |
add7cadd | 223 | SetScrollbar(GetThumbPosition(), GetThumbSize(), GetRange(), pageLength); |
6de97a3b | 224 | } |
c801d85f | 225 | |
add7cadd | 226 | void wxScrollBar::SetRange(int range) |
c801d85f | 227 | { |
add7cadd | 228 | SetScrollbar(GetThumbPosition(), GetThumbSize(), range, GetPageSize()); |
6de97a3b | 229 | } |
c801d85f | 230 | |
9d522606 RD |
231 | // static |
232 | wxVisualAttributes | |
233 | wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
234 | { | |
1897abe1 | 235 | return GetDefaultAttributesFromGTKWidget(gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, NULL)); |
9d522606 RD |
236 | } |
237 | ||
de6185e2 | 238 | #endif // wxUSE_SCROLLBAR |