1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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"
24 // ----------------------------------------------------------------------------
25 // wxScrollHelper implementation
26 // ----------------------------------------------------------------------------
28 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
, int pixelsPerUnitY
,
29 int noUnitsX
, int noUnitsY
,
33 m_win
->m_scrollPos
[wxWindow::ScrollDir_Horz
] =
34 m_win
->m_scrollBar
[wxWindow::ScrollDir_Horz
]->adjustment
->value
= xPos
;
35 m_win
->m_scrollPos
[wxWindow::ScrollDir_Vert
] =
36 m_win
->m_scrollBar
[wxWindow::ScrollDir_Vert
]->adjustment
->value
= yPos
;
37 base_type::SetScrollbars(
38 pixelsPerUnitX
, pixelsPerUnitY
, noUnitsX
, noUnitsY
, xPos
, yPos
, noRefresh
);
41 void wxScrollHelper::DoAdjustScrollbar(GtkRange
* range
,
54 if (pixelsPerLine
> 0 && winSize
> 0 && winSize
< virtSize
)
56 upper
= (virtSize
+ pixelsPerLine
- 1) / pixelsPerLine
;
57 page_size
= winSize
/ pixelsPerLine
;
59 *linesPerPage
= page_size
;
63 // GtkRange won't allow upper == lower, so for disabled state use [0,1]
64 // with a page size of 1. This will also clamp position to 0.
71 GtkAdjustment
* adj
= range
->adjustment
;
72 adj
->step_increment
= 1;
74 adj
->page_size
= page_size
;
75 gtk_range_set_range(range
, 0, upper
);
77 // ensure that the scroll position is always in valid range
82 void wxScrollHelper::AdjustScrollbars()
85 m_targetWindow
->GetVirtualSize(&vw
, &vh
);
88 const wxSize availSize
= GetSizeAvailableForScrollTarget(
89 m_win
->GetSize() - m_win
->GetWindowBorderSize());
90 if ( availSize
.x
>= vw
&& availSize
.y
>= vh
)
95 // we know that the scrollbars will be removed
96 DoAdjustHScrollbar(w
, vw
);
97 DoAdjustVScrollbar(h
, vh
);
102 m_targetWindow
->GetClientSize(&w
, NULL
);
103 DoAdjustHScrollbar(w
, vw
);
105 m_targetWindow
->GetClientSize(NULL
, &h
);
106 DoAdjustVScrollbar(h
, vh
);
109 m_targetWindow
->GetClientSize(&w
, NULL
);
112 // It is necessary to repeat the calculations in this case to avoid an
113 // observed infinite series of size events, involving alternating
114 // changes in visibility of the scrollbars.
115 // At this point, GTK+ has already queued a resize, which will cause
116 // AdjustScrollbars() to be called again. If the scrollbar visibility
117 // is not correct before then, yet another resize will occur, possibly
118 // leading to an unending series if the sizes are just right.
119 DoAdjustHScrollbar(w
, vw
);
121 m_targetWindow
->GetClientSize(NULL
, &h
);
122 DoAdjustVScrollbar(h
, vh
);
126 void wxScrollHelper::DoScrollOneDir(int orient
,
131 if ( pos
!= -1 && pos
!= *posOld
&& pixelsPerLine
)
133 m_win
->SetScrollPos(orient
, pos
);
134 pos
= m_win
->GetScrollPos(orient
);
136 int diff
= (*posOld
- pos
)*pixelsPerLine
;
137 m_targetWindow
->ScrollWindow(orient
== wxHORIZONTAL
? diff
: 0,
138 orient
== wxHORIZONTAL
? 0 : diff
);
144 void wxScrollHelper::DoScroll( int x_pos
, int y_pos
)
146 wxCHECK_RET( m_targetWindow
!= 0, wxT("No target window") );
148 DoScrollOneDir(wxHORIZONTAL
, x_pos
, m_xScrollPixelsPerLine
, &m_xScrollPosition
);
149 DoScrollOneDir(wxVERTICAL
, y_pos
, m_yScrollPixelsPerLine
, &m_yScrollPosition
);
152 // ----------------------------------------------------------------------------
153 // scrollbars visibility
154 // ----------------------------------------------------------------------------
159 GtkPolicyType
GtkPolicyFromWX(wxScrollbarVisibility visibility
)
161 GtkPolicyType policy
;
162 switch ( visibility
)
164 case wxSHOW_SB_NEVER
:
165 policy
= GTK_POLICY_NEVER
;
168 case wxSHOW_SB_DEFAULT
:
169 policy
= GTK_POLICY_AUTOMATIC
;
173 wxFAIL_MSG( wxS("unknown scrollbar visibility") );
176 case wxSHOW_SB_ALWAYS
:
177 policy
= GTK_POLICY_ALWAYS
;
184 } // anonymous namespace
186 void wxScrollHelper::DoShowScrollbars(wxScrollbarVisibility horz
,
187 wxScrollbarVisibility vert
)
189 GtkScrolledWindow
* const scrolled
= GTK_SCROLLED_WINDOW(m_win
->m_widget
);
190 wxCHECK_RET( scrolled
, "window must be created" );
192 gtk_scrolled_window_set_policy(scrolled
,
193 GtkPolicyFromWX(horz
),
194 GtkPolicyFromWX(vert
));