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 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
28 #include "wx/scrolwin.h"
29 #include "wx/gtk/private.h"
31 // ============================================================================
33 // ============================================================================
35 // ----------------------------------------------------------------------------
36 // wxScrollHelper implementation
37 // ----------------------------------------------------------------------------
39 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX
, int pixelsPerUnitY
,
40 int noUnitsX
, int noUnitsY
,
42 bool WXUNUSED(noRefresh
))
44 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
45 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
47 int w
= noUnitsX
* pixelsPerUnitX
;
48 int h
= noUnitsY
* pixelsPerUnitY
;
49 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
50 h
? h
: wxDefaultCoord
);
52 GtkRange
*sb
= m_win
->m_scrollBar
[wxWindow::ScrollDir_Vert
];
53 gtk_range_set_value(sb
, yPos
);
54 sb
= m_win
->m_scrollBar
[wxWindow::ScrollDir_Horz
];
55 gtk_range_set_value(sb
, xPos
);
57 m_xScrollPosition
= wxRound( m_win
->m_scrollBar
[wxWindow::ScrollDir_Horz
]->adjustment
->value
);
58 m_yScrollPosition
= wxRound( m_win
->m_scrollBar
[wxWindow::ScrollDir_Vert
]->adjustment
->value
);
60 // If the target is not the same as the window with the scrollbars,
61 // then we need to update the scrollbars here, since they won't have
62 // been updated by SetVirtualSize().
63 if (m_targetWindow
!= m_win
)
71 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
72 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
74 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
79 void wxScrollHelper::DoAdjustScrollbar(GtkRange
* range
,
89 if (pixelsPerLine
> 0 && winSize
> 0 && winSize
< virtSize
)
91 upper
= (virtSize
+ pixelsPerLine
- 1) / pixelsPerLine
;
92 page_size
= winSize
/ pixelsPerLine
;
94 *linesPerPage
= page_size
;
98 // GtkRange won't allow upper == lower, so for disabled state use [0,1]
99 // with a page size of 1. This will also clamp position to 0.
106 GtkAdjustment
* adj
= range
->adjustment
;
107 adj
->step_increment
= 1;
108 adj
->page_increment
=
109 adj
->page_size
= page_size
;
110 gtk_range_set_range(range
, 0, upper
);
112 // ensure that the scroll position is always in valid range
117 void wxScrollHelper::AdjustScrollbars()
120 m_targetWindow
->GetVirtualSize(&vw
, &vh
);
123 const wxSize availSize
= GetSizeAvailableForScrollTarget(
124 m_win
->GetSize() - m_win
->GetWindowBorderSize());
125 if ( availSize
.x
>= vw
&& availSize
.y
>= vh
)
130 // we know that the scrollbars will be removed
131 DoAdjustHScrollbar(w
, vw
);
132 DoAdjustVScrollbar(h
, vh
);
137 m_targetWindow
->GetClientSize(&w
, NULL
);
138 DoAdjustHScrollbar(w
, vw
);
140 m_targetWindow
->GetClientSize(NULL
, &h
);
141 DoAdjustVScrollbar(h
, vh
);
144 m_targetWindow
->GetClientSize(&w
, NULL
);
147 // It is necessary to repeat the calculations in this case to avoid an
148 // observed infinite series of size events, involving alternating
149 // changes in visibility of the scrollbars.
150 // At this point, GTK+ has already queued a resize, which will cause
151 // AdjustScrollbars() to be called again. If the scrollbar visibility
152 // is not correct before then, yet another resize will occur, possibly
153 // leading to an unending series if the sizes are just right.
154 DoAdjustHScrollbar(w
, vw
);
156 m_targetWindow
->GetClientSize(NULL
, &h
);
157 DoAdjustVScrollbar(h
, vh
);
161 void wxScrollHelper::DoScrollOneDir(int orient
,
166 if ( pos
!= -1 && pos
!= *posOld
&& pixelsPerLine
)
168 m_win
->SetScrollPos(orient
, pos
);
169 pos
= m_win
->GetScrollPos(orient
);
171 int diff
= (*posOld
- pos
)*pixelsPerLine
;
172 m_targetWindow
->ScrollWindow(orient
== wxHORIZONTAL
? diff
: 0,
173 orient
== wxHORIZONTAL
? 0 : diff
);
179 void wxScrollHelper::DoScroll( int x_pos
, int y_pos
)
181 wxCHECK_RET( m_targetWindow
!= 0, _T("No target window") );
183 DoScrollOneDir(wxHORIZONTAL
, x_pos
, m_xScrollPixelsPerLine
, &m_xScrollPosition
);
184 DoScrollOneDir(wxVERTICAL
, y_pos
, m_yScrollPixelsPerLine
, &m_yScrollPosition
);
187 // ----------------------------------------------------------------------------
188 // scrollbars visibility
189 // ----------------------------------------------------------------------------
194 GtkPolicyType
GtkPolicyFromWX(wxScrollbarVisibility visibility
)
196 GtkPolicyType policy
;
197 switch ( visibility
)
199 case wxSHOW_SB_NEVER
:
200 policy
= GTK_POLICY_NEVER
;
203 case wxSHOW_SB_DEFAULT
:
204 policy
= GTK_POLICY_AUTOMATIC
;
207 case wxSHOW_SB_ALWAYS
:
208 policy
= GTK_POLICY_ALWAYS
;
215 } // anonymous namespace
217 void wxScrollHelper::DoShowScrollbars(wxScrollbarVisibility horz
,
218 wxScrollbarVisibility vert
)
220 GtkScrolledWindow
* const scrolled
= GTK_SCROLLED_WINDOW(m_win
->m_widget
);
221 wxCHECK_RET( scrolled
, "window must be created" );
223 gtk_scrolled_window_set_policy(scrolled
,
224 GtkPolicyFromWX(horz
),
225 GtkPolicyFromWX(vert
));