1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/gtk/scrolbar.cpp
3 // Purpose:
4 // Author:      Robert Roebling
5 // Id:          $Id: scrolbar.cpp 46665 2007-06-23 16:26:28Z RR $
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
gtk_value_changed(GtkRange * range,wxScrollBar * win)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
gtk_button_press_event(GtkRange *,GdkEventButton *,wxScrollBar * win)55 gtk_button_press_event(GtkRange*, GdkEventButton*, wxScrollBar* win)
56 {
57     // don't need to install idle handler, its done from "event" signal
58 
59     win->m_mouseButtonDown = true;
60     return false;
61 }
62 }
63 
64 //-----------------------------------------------------------------------------
65 // "event_after" from scrollbar
66 //-----------------------------------------------------------------------------
67 
68 extern "C" {
69 static void
gtk_event_after(GtkRange * range,GdkEvent * event,wxScrollBar * win)70 gtk_event_after(GtkRange* range, GdkEvent* event, wxScrollBar* win)
71 {
72     if (event->type == GDK_BUTTON_RELEASE)
73     {
74         g_signal_handlers_block_by_func(range, (void*)gtk_event_after, win);
75 
76         const int value = win->GetThumbPosition();
77         const int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
78 
79         wxScrollEvent event(wxEVT_SCROLL_THUMBRELEASE, win->GetId(), value, orient);
80         event.SetEventObject(win);
81         win->GetEventHandler()->ProcessEvent(event);
82 
83         wxScrollEvent event2(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
84         event2.SetEventObject(win);
85         win->GetEventHandler()->ProcessEvent(event2);
86     }
87 }
88 }
89 
90 //-----------------------------------------------------------------------------
91 // "button_release_event" from scrollbar
92 //-----------------------------------------------------------------------------
93 
94 extern "C" {
95 static gboolean
gtk_button_release_event(GtkRange * range,GdkEventButton *,wxScrollBar * win)96 gtk_button_release_event(GtkRange* range, GdkEventButton*, wxScrollBar* win)
97 {
98     // don't need to install idle handler, its done from "event" signal
99 
100     win->m_mouseButtonDown = false;
101     // If thumb tracking
102     if (win->m_isScrolling)
103     {
104         win->m_isScrolling = false;
105         // Hook up handler to send thumb release event after this emission is finished.
106         // To allow setting scroll position from event handler, sending event must
107         // be deferred until after the GtkRange handler for this signal has run
108         g_signal_handlers_unblock_by_func(range, (void*)gtk_event_after, win);
109     }
110 
111     return false;
112 }
113 }
114 
115 //-----------------------------------------------------------------------------
116 // wxScrollBar
117 //-----------------------------------------------------------------------------
118 
IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)119 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
120 
121 wxScrollBar::wxScrollBar()
122 {
123 }
124 
~wxScrollBar()125 wxScrollBar::~wxScrollBar()
126 {
127 }
128 
Create(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,long style,const wxValidator & validator,const wxString & name)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     m_needParent = true;
134     m_acceptsFocus = true;
135 
136     if (!PreCreation( parent, pos, size ) ||
137         !CreateBase( parent, id, pos, size, style, validator, name ))
138     {
139         wxFAIL_MSG( wxT("wxScrollBar creation failed") );
140         return false;
141     }
142 
143     const bool isVertical = (style & wxSB_VERTICAL) != 0;
144     if (isVertical)
145         m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
146     else
147         m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
148 
149     m_scrollBar[int(isVertical)] = (GtkRange*)m_widget;
150 
151     g_signal_connect(m_widget, "value_changed",
152                      G_CALLBACK(gtk_value_changed), this);
153     g_signal_connect(m_widget, "button_press_event",
154                      G_CALLBACK(gtk_button_press_event), this);
155     g_signal_connect(m_widget, "button_release_event",
156                      G_CALLBACK(gtk_button_release_event), this);
157 
158     gulong handler_id;
159     handler_id = g_signal_connect(
160         m_widget, "event_after", G_CALLBACK(gtk_event_after), this);
161     g_signal_handler_block(m_widget, handler_id);
162 
163     m_parent->DoAddChild( this );
164 
165     PostCreation(size);
166 
167     return true;
168 }
169 
GetThumbPosition() const170 int wxScrollBar::GetThumbPosition() const
171 {
172     GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
173     return int(adj->value + 0.5);
174 }
175 
GetThumbSize() const176 int wxScrollBar::GetThumbSize() const
177 {
178     GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
179     return int(adj->page_size);
180 }
181 
GetPageSize() const182 int wxScrollBar::GetPageSize() const
183 {
184     GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
185     return int(adj->page_increment);
186 }
187 
GetRange() const188 int wxScrollBar::GetRange() const
189 {
190     GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
191     return int(adj->upper);
192 }
193 
SetThumbPosition(int viewStart)194 void wxScrollBar::SetThumbPosition( int viewStart )
195 {
196     if (GetThumbPosition() != viewStart)
197     {
198         GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
199         const int i = (GtkRange*)m_widget == m_scrollBar[1];
200         const int max = int(adj->upper - adj->page_size);
201         if (viewStart > max)
202             viewStart = max;
203         if (viewStart < 0)
204             viewStart = 0;
205 
206         m_scrollPos[i] =
207         adj->value = viewStart;
208 
209         g_signal_handlers_disconnect_by_func( m_widget,
210                               (gpointer)gtk_value_changed, this);
211 
212         gtk_adjustment_value_changed(adj);
213 
214         g_signal_connect_after(m_widget, "value_changed",
215                      G_CALLBACK(gtk_value_changed), this);
216     }
217 }
218 
SetScrollbar(int position,int thumbSize,int range,int pageSize,bool)219 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize, bool)
220 {
221     if (range == 0)
222     {
223         // GtkRange requires upper > lower
224         range =
225         thumbSize = 1;
226     }
227     if (position > range - thumbSize)
228         position = range - thumbSize;
229     if (position < 0)
230         position = 0;
231     GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
232     adj->step_increment = 1;
233     adj->page_increment = pageSize;
234     adj->page_size = thumbSize;
235     adj->upper = range;
236     SetThumbPosition(position);
237     gtk_adjustment_changed(adj);
238 }
239 
SetPageSize(int pageLength)240 void wxScrollBar::SetPageSize( int pageLength )
241 {
242     SetScrollbar(GetThumbPosition(), GetThumbSize(), GetRange(), pageLength);
243 }
244 
SetRange(int range)245 void wxScrollBar::SetRange(int range)
246 {
247     SetScrollbar(GetThumbPosition(), GetThumbSize(), range, GetPageSize());
248 }
249 
GTKGetWindow(wxArrayGdkWindows & WXUNUSED (windows)) const250 GdkWindow *wxScrollBar::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
251 {
252     return GTK_WIDGET(GTK_RANGE(m_widget))->window;
253 }
254 
255 // static
256 wxVisualAttributes
GetClassDefaultAttributes(wxWindowVariant WXUNUSED (variant))257 wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
258 {
259     return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new);
260 }
261 
262 #endif // wxUSE_SCROLLBAR
263