1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/scrolwin.cpp
3 // Purpose: wxScrolledWindow implementation
4 // Author: Robert Roebling
5 // Modified by: Ron Lee
6 // Vadim Zeitlin: removed 90% of duplicated common code
9 // Copyright: (c) Robert Roebling
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
20 #include "wx/scrolwin.h"
23 #include "wx/gtk/private/gtk2-compat.h"
25 // ----------------------------------------------------------------------------
26 // wxScrollHelper implementation
27 // ----------------------------------------------------------------------------
29 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
, int pixelsPerUnitY
,
30 int noUnitsX
, int noUnitsY
,
34 // prevent programmatic position changes from causing scroll events
35 m_win
->SetScrollPos(wxHORIZONTAL
, xPos
);
36 m_win
->SetScrollPos(wxVERTICAL
, yPos
);
38 base_type::SetScrollbars(
39 pixelsPerUnitX
, pixelsPerUnitY
, noUnitsX
, noUnitsY
, xPos
, yPos
, noRefresh
);
42 void wxScrollHelper::DoAdjustScrollbar(GtkRange
* range
,
55 if (pixelsPerLine
> 0 && winSize
> 0 && winSize
< virtSize
)
57 upper
= (virtSize
+ pixelsPerLine
- 1) / pixelsPerLine
;
58 page_size
= winSize
/ pixelsPerLine
;
60 *linesPerPage
= page_size
;
64 // GtkRange won't allow upper == lower, so for disabled state use [0,1]
65 // with a page size of 1. This will also clamp position to 0.
72 gtk_range_set_increments(range
, 1, page_size
);
73 gtk_adjustment_set_page_size(gtk_range_get_adjustment(range
), page_size
);
74 gtk_range_set_range(range
, 0, upper
);
76 // ensure that the scroll position is always in valid range
81 void wxScrollHelper::AdjustScrollbars()
84 m_targetWindow
->GetVirtualSize(&vw
, &vh
);
87 const wxSize availSize
= GetSizeAvailableForScrollTarget(
88 m_win
->GetSize() - m_win
->GetWindowBorderSize());
89 if ( availSize
.x
>= vw
&& availSize
.y
>= vh
)
94 // we know that the scrollbars will be removed
95 DoAdjustHScrollbar(w
, vw
);
96 DoAdjustVScrollbar(h
, vh
);
101 m_targetWindow
->GetClientSize(&w
, NULL
);
102 DoAdjustHScrollbar(w
, vw
);
104 m_targetWindow
->GetClientSize(NULL
, &h
);
105 DoAdjustVScrollbar(h
, vh
);
108 m_targetWindow
->GetClientSize(&w
, NULL
);
111 // It is necessary to repeat the calculations in this case to avoid an
112 // observed infinite series of size events, involving alternating
113 // changes in visibility of the scrollbars.
114 // At this point, GTK+ has already queued a resize, which will cause
115 // AdjustScrollbars() to be called again. If the scrollbar visibility
116 // is not correct before then, yet another resize will occur, possibly
117 // leading to an unending series if the sizes are just right.
118 DoAdjustHScrollbar(w
, vw
);
120 m_targetWindow
->GetClientSize(NULL
, &h
);
121 DoAdjustVScrollbar(h
, vh
);
125 void wxScrollHelper::DoScrollOneDir(int orient
,
130 if ( pos
!= -1 && pos
!= *posOld
&& pixelsPerLine
)
132 m_win
->SetScrollPos(orient
, pos
);
133 pos
= m_win
->GetScrollPos(orient
);
135 int diff
= (*posOld
- pos
)*pixelsPerLine
;
136 m_targetWindow
->ScrollWindow(orient
== wxHORIZONTAL
? diff
: 0,
137 orient
== wxHORIZONTAL
? 0 : diff
);
143 void wxScrollHelper::DoScroll( int x_pos
, int y_pos
)
145 wxCHECK_RET( m_targetWindow
!= 0, wxT("No target window") );
147 DoScrollOneDir(wxHORIZONTAL
, x_pos
, m_xScrollPixelsPerLine
, &m_xScrollPosition
);
148 DoScrollOneDir(wxVERTICAL
, y_pos
, m_yScrollPixelsPerLine
, &m_yScrollPosition
);
151 // ----------------------------------------------------------------------------
152 // scrollbars visibility
153 // ----------------------------------------------------------------------------
158 GtkPolicyType
GtkPolicyFromWX(wxScrollbarVisibility visibility
)
160 GtkPolicyType policy
;
161 switch ( visibility
)
163 case wxSHOW_SB_NEVER
:
164 policy
= GTK_POLICY_NEVER
;
167 case wxSHOW_SB_DEFAULT
:
168 policy
= GTK_POLICY_AUTOMATIC
;
172 wxFAIL_MSG( wxS("unknown scrollbar visibility") );
175 case wxSHOW_SB_ALWAYS
:
176 policy
= GTK_POLICY_ALWAYS
;
183 } // anonymous namespace
185 void wxScrollHelper::DoShowScrollbars(wxScrollbarVisibility horz
,
186 wxScrollbarVisibility vert
)
188 GtkScrolledWindow
* const scrolled
= GTK_SCROLLED_WINDOW(m_win
->m_widget
);
189 wxCHECK_RET( scrolled
, "window must be created" );
191 gtk_scrolled_window_set_policy(scrolled
,
192 GtkPolicyFromWX(horz
),
193 GtkPolicyFromWX(vert
));