]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/scrolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "scrolbar.h"
16 #include "wx/scrolbar.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 extern bool g_blockEventsOnDrag
;
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 static void gtk_scrollbar_callback( GtkWidget
*WXUNUSED(widget
), wxScrollBar
*win
)
31 if (!win
->HasVMT()) return;
32 if (g_blockEventsOnDrag
) return;
34 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
35 if (fabs(diff
) < 0.2) return;
37 wxEventType command
= wxEVT_NULL
;
39 float line_step
= win
->m_adjust
->step_increment
;
40 float page_step
= win
->m_adjust
->page_increment
;
42 if (fabs(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
43 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
44 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
45 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
46 else command
= wxEVT_SCROLL_THUMBTRACK
;
48 int value
= (int)(win
->m_adjust
->value
+0.5);
50 int orient
= wxHORIZONTAL
;
51 if (win
->GetWindowStyleFlag() & wxSB_VERTICAL
== wxSB_VERTICAL
) orient
= wxHORIZONTAL
;
53 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
54 event
.SetEventObject( win
);
55 win
->GetEventHandler()->ProcessEvent( event
);
58 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
59 cevent.SetEventObject( win );
60 win->ProcessEvent( cevent );
64 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
66 wxScrollBar::~wxScrollBar(void)
70 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
71 const wxPoint
& pos
, const wxSize
& size
,
72 long style
, const wxValidator
& validator
, const wxString
& name
)
76 PreCreation( parent
, id
, pos
, size
, style
, name
);
78 SetValidator( validator
);
82 if (style
& wxSB_VERTICAL
== wxSB_VERTICAL
)
83 m_widget
= gtk_hscrollbar_new( NULL
);
85 m_widget
= gtk_vscrollbar_new( NULL
);
87 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
89 gtk_signal_connect (GTK_OBJECT (m_adjust
), "value_changed",
90 (GtkSignalFunc
) gtk_scrollbar_callback
, (gpointer
) this );
99 int wxScrollBar::GetPosition(void) const
101 return (int)(m_adjust
->value
+0.5);
104 int wxScrollBar::GetThumbSize() const
106 return (int)(m_adjust
->page_size
+0.5);
109 int wxScrollBar::GetPageSize() const
111 return (int)(m_adjust
->page_increment
+0.5);
114 int wxScrollBar::GetRange() const
116 return (int)(m_adjust
->upper
+0.5);
119 void wxScrollBar::SetPosition( int viewStart
)
121 float fpos
= (float)viewStart
;
123 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
124 m_adjust
->value
= fpos
;
126 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
129 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
130 bool WXUNUSED(refresh
) )
132 float fpos
= (float)position
;
134 float frange
= (float)range
;
135 float fthumb
= (float)thumbSize
;
136 float fpage
= (float)pageSize
;
138 if ((fabs(fpos
-m_adjust
->value
) < 0.2) &&
139 (fabs(frange
-m_adjust
->upper
) < 0.2) &&
140 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
141 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
144 m_adjust
->lower
= 0.0;
145 m_adjust
->upper
= frange
;
146 m_adjust
->value
= fpos
;
147 m_adjust
->step_increment
= 1.0;
148 m_adjust
->page_increment
= (float)(wxMax(fpage
-2,0));
149 m_adjust
->page_size
= fthumb
;
151 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
154 // Backward compatibility
155 int wxScrollBar::GetValue(void) const
157 return GetPosition();
160 void wxScrollBar::SetValue( int viewStart
)
162 SetPosition( viewStart
);
165 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
167 int pos
= (int)(m_adjust
->value
+0.5);
168 int thumb
= (int)(m_adjust
->page_size
+0.5);
169 int page
= (int)(m_adjust
->page_increment
+0.5);
170 int range
= (int)(m_adjust
->upper
+0.5);
174 *objectLength
= thumb
;
178 int wxScrollBar::GetViewLength() const
180 return (int)(m_adjust
->upper
+0.5);
183 int wxScrollBar::GetObjectLength() const
185 return (int)(m_adjust
->page_size
+0.5);
188 void wxScrollBar::SetPageSize( int pageLength
)
190 int pos
= (int)(m_adjust
->value
+0.5);
191 int thumb
= (int)(m_adjust
->page_size
+0.5);
192 int range
= (int)(m_adjust
->upper
+0.5);
193 SetScrollbar( pos
, thumb
, range
, pageLength
);
196 void wxScrollBar::SetObjectLength( int objectLength
)
198 int pos
= (int)(m_adjust
->value
+0.5);
199 int page
= (int)(m_adjust
->page_increment
+0.5);
200 int range
= (int)(m_adjust
->upper
+0.5);
201 SetScrollbar( pos
, objectLength
, range
, page
);
204 void wxScrollBar::SetViewLength( int viewLength
)
206 int pos
= (int)(m_adjust
->value
+0.5);
207 int thumb
= (int)(m_adjust
->page_size
+0.5);
208 int page
= (int)(m_adjust
->page_increment
+0.5);
209 SetScrollbar( pos
, thumb
, viewLength
, page
);