1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/scrolbar.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/scrolbar.h"
21 #include "wx/gtk/private.h"
23 //-----------------------------------------------------------------------------
24 // "value_changed" from scrollbar
25 //-----------------------------------------------------------------------------
29 gtk_value_changed(GtkRange
* range
, wxScrollBar
* win
)
31 wxEventType eventType
= win
->GetScrollEventType(range
);
32 if (eventType
!= wxEVT_NULL
)
34 const int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
35 const int i
= orient
== wxVERTICAL
;
36 const int value
= win
->GetThumbPosition();
37 wxScrollEvent
event(eventType
, win
->GetId(), value
, orient
);
38 event
.SetEventObject(win
);
39 win
->m_blockValueChanged
[i
] = true;
40 win
->GetEventHandler()->ProcessEvent(event
);
41 if (!win
->m_isScrolling
)
43 wxScrollEvent
event(wxEVT_SCROLL_CHANGED
, win
->GetId(), value
, orient
);
44 event
.SetEventObject(win
);
45 win
->GetEventHandler()->ProcessEvent(event
);
47 win
->m_blockValueChanged
[i
] = false;
52 //-----------------------------------------------------------------------------
53 // "button_press_event" from scrollbar
54 //-----------------------------------------------------------------------------
58 gtk_button_press_event(GtkRange
*, GdkEventButton
*, wxScrollBar
* win
)
60 // don't need to install idle handler, its done from "event" signal
62 win
->m_mouseButtonDown
= true;
67 //-----------------------------------------------------------------------------
68 // "event_after" from scrollbar
69 //-----------------------------------------------------------------------------
73 gtk_event_after(GtkRange
* range
, GdkEvent
* event
, wxScrollBar
* win
)
75 if (event
->type
== GDK_BUTTON_RELEASE
)
77 g_signal_handlers_block_by_func(range
, (void*)gtk_event_after
, win
);
79 const int value
= win
->GetThumbPosition();
80 const int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
82 wxScrollEvent
event(wxEVT_SCROLL_THUMBRELEASE
, win
->GetId(), value
, orient
);
83 event
.SetEventObject(win
);
84 win
->GetEventHandler()->ProcessEvent(event
);
86 wxScrollEvent
event2(wxEVT_SCROLL_CHANGED
, win
->GetId(), value
, orient
);
87 event2
.SetEventObject(win
);
88 win
->GetEventHandler()->ProcessEvent(event2
);
93 //-----------------------------------------------------------------------------
94 // "button_release_event" from scrollbar
95 //-----------------------------------------------------------------------------
99 gtk_button_release_event(GtkRange
* range
, GdkEventButton
*, wxScrollBar
* win
)
101 // don't need to install idle handler, its done from "event" signal
103 win
->m_mouseButtonDown
= false;
105 if (win
->m_isScrolling
)
107 win
->m_isScrolling
= false;
108 // Hook up handler to send thumb release event after this emission is finished.
109 // To allow setting scroll position from event handler, sending event must
110 // be deferred until after the GtkRange handler for this signal has run
111 g_signal_handlers_unblock_by_func(range
, (void*)gtk_event_after
, win
);
118 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
122 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
124 wxScrollBar::wxScrollBar()
128 wxScrollBar::~wxScrollBar()
132 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
133 const wxPoint
& pos
, const wxSize
& size
,
134 long style
, const wxValidator
& validator
, const wxString
& name
)
138 if (!PreCreation( parent
, pos
, size
) ||
139 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
141 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
145 const bool isVertical
= (style
& wxSB_VERTICAL
) != 0;
147 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
149 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
151 m_scrollBar
[int(isVertical
)] = (GtkRange
*)m_widget
;
153 g_signal_connect(m_widget
, "value_changed",
154 G_CALLBACK(gtk_value_changed
), this);
155 g_signal_connect(m_widget
, "button_press_event",
156 G_CALLBACK(gtk_button_press_event
), this);
157 g_signal_connect(m_widget
, "button_release_event",
158 G_CALLBACK(gtk_button_release_event
), this);
161 handler_id
= g_signal_connect(
162 m_widget
, "event_after", G_CALLBACK(gtk_event_after
), this);
163 g_signal_handler_block(m_widget
, handler_id
);
165 m_parent
->DoAddChild( this );
172 int wxScrollBar::GetThumbPosition() const
174 GtkAdjustment
* adj
= ((GtkRange
*)m_widget
)->adjustment
;
175 return int(adj
->value
+ 0.5);
178 int wxScrollBar::GetThumbSize() const
180 GtkAdjustment
* adj
= ((GtkRange
*)m_widget
)->adjustment
;
181 return int(adj
->page_size
);
184 int wxScrollBar::GetPageSize() const
186 GtkAdjustment
* adj
= ((GtkRange
*)m_widget
)->adjustment
;
187 return int(adj
->page_increment
);
190 int wxScrollBar::GetRange() const
192 GtkAdjustment
* adj
= ((GtkRange
*)m_widget
)->adjustment
;
193 return int(adj
->upper
);
196 void wxScrollBar::SetThumbPosition( int viewStart
)
198 if (GetThumbPosition() != viewStart
)
200 GtkAdjustment
* adj
= ((GtkRange
*)m_widget
)->adjustment
;
201 const int i
= (GtkRange
*)m_widget
== m_scrollBar
[1];
202 const int max
= int(adj
->upper
- adj
->page_size
);
209 adj
->value
= viewStart
;
210 // If a "value_changed" signal emission is not already in progress
211 if (!m_blockValueChanged
[i
])
213 gtk_adjustment_value_changed(adj
);
218 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
, bool)
222 // GtkRange requires upper > lower
226 if (position
> range
- thumbSize
)
227 position
= range
- thumbSize
;
230 GtkAdjustment
* adj
= ((GtkRange
*)m_widget
)->adjustment
;
231 adj
->step_increment
= 1;
232 adj
->page_increment
= pageSize
;
233 adj
->page_size
= thumbSize
;
235 SetThumbPosition(position
);
236 gtk_adjustment_changed(adj
);
239 void wxScrollBar::SetPageSize( int pageLength
)
241 SetScrollbar(GetThumbPosition(), GetThumbSize(), GetRange(), pageLength
);
244 void wxScrollBar::SetRange(int range
)
246 SetScrollbar(GetThumbPosition(), GetThumbSize(), range
, GetPageSize());
249 GdkWindow
*wxScrollBar::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
251 return GTK_WIDGET(GTK_RANGE(m_widget
))->window
;
256 wxScrollBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
258 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new
);
261 #endif // wxUSE_SCROLLBAR