]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/scrolwin.cpp
use wxWindow::GetClientSize() instead of ::GetClientRect() in wxActiveXContainer...
[wxWidgets.git] / src / gtk / scrolwin.cpp
CommitLineData
30954328 1/////////////////////////////////////////////////////////////////////////////
e1437456 2// Name: gtk/scrolwin.cpp
30954328 3// Purpose: wxScrolledWindow implementation
47a3ff38 4// Author: Robert Roebling
566d84a7 5// Modified by: Ron Lee
d32e78bd 6// Vadim Zeitlin: removed 90% of duplicated common code
30954328
RR
7// Created: 01/02/97
8// RCS-ID: $Id$
47a3ff38 9// Copyright: (c) Robert Roebling
65571936 10// Licence: wxWindows licence
30954328
RR
11/////////////////////////////////////////////////////////////////////////////
12
13// ============================================================================
14// declarations
15// ============================================================================
16
17// ----------------------------------------------------------------------------
18// headers
19// ----------------------------------------------------------------------------
20
30954328
RR
21// For compilers that support precompilation, includes "wx.h".
22#include "wx/wxprec.h"
23
24#ifdef __BORLANDC__
25 #pragma hdrstop
26#endif
27
e1bf3ad3 28#include "wx/scrolwin.h"
9e691f46 29#include "wx/gtk/private.h"
30954328
RR
30
31// ============================================================================
32// implementation
33// ============================================================================
34
30954328 35// ----------------------------------------------------------------------------
d32e78bd 36// wxScrollHelper implementation
30954328
RR
37// ----------------------------------------------------------------------------
38
29e1398f
VZ
39void wxScrollHelper::SetScrollbars(int pixelsPerUnitX, int pixelsPerUnitY,
40 int noUnitsX, int noUnitsY,
41 int xPos, int yPos,
42 bool noRefresh)
30954328 43{
1adff2f5 44 int xs, ys;
d32e78bd 45 GetViewStart(& xs, & ys);
4e115ed2 46
1adff2f5
JS
47 int old_x = m_xScrollPixelsPerLine * xs;
48 int old_y = m_yScrollPixelsPerLine * ys;
30486297 49
30954328
RR
50 m_xScrollPixelsPerLine = pixelsPerUnitX;
51 m_yScrollPixelsPerLine = pixelsPerUnitY;
39c869a6 52
cd38dd5b 53 m_win->m_scrollBar[wxWindow::ScrollDir_Horz]->adjustment->value =
add7cadd 54 m_xScrollPosition = xPos;
cd38dd5b 55 m_win->m_scrollBar[wxWindow::ScrollDir_Vert]->adjustment->value =
add7cadd 56 m_yScrollPosition = yPos;
30954328 57
2b5f62a0
VZ
58 // Setting hints here should arguably be deprecated, but without it
59 // a sizer might override this manual scrollbar setting in old code.
878ddad5 60 // m_targetWindow->SetVirtualSizeHints( noUnitsX * pixelsPerUnitX, noUnitsY * pixelsPerUnitY );
30486297 61
f18f464c
VZ
62 int w = noUnitsX * pixelsPerUnitX;
63 int h = noUnitsY * pixelsPerUnitY;
64 m_targetWindow->SetVirtualSize( w ? w : wxDefaultCoord,
65 h ? h : wxDefaultCoord);
2b5f62a0 66
add7cadd
PC
67 // If the target is not the same as the window with the scrollbars,
68 // then we need to update the scrollbars here, since they won't have
69 // been updated by SetVirtualSize().
70 if (m_targetWindow != m_win)
71 {
72 AdjustScrollbars();
73 }
74
ec7482df
RR
75 if (!noRefresh)
76 {
77 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
78 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
30486297 79
566d84a7 80 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
ec7482df 81 }
30954328
RR
82}
83
29e1398f
VZ
84void wxScrollHelper::DoAdjustScrollbar(GtkRange* range,
85 int pixelsPerLine,
86 int winSize,
87 int virtSize,
88 int *pos,
89 int *lines,
90 int *linesPerPage)
30954328 91{
f6814d01
PC
92 int upper;
93 int page_size;
fb18afdd 94 if (pixelsPerLine > 0 && winSize > 0 && winSize < virtSize)
4ca24f18 95 {
f6814d01
PC
96 upper = (virtSize + pixelsPerLine - 1) / pixelsPerLine;
97 page_size = winSize / pixelsPerLine;
5713b349
RR
98 *lines = upper;
99 *linesPerPage = page_size;
5713b349
RR
100 }
101 else
102 {
103 // GtkRange won't allow upper == lower, so for disabled state use [0,1]
104 // with a page size of 1. This will also clamp position to 0.
f6814d01
PC
105 upper = 1;
106 page_size = 1;
5713b349
RR
107 *lines = 0;
108 *linesPerPage = 0;
4ca24f18 109 }
9ec927f8 110
f6814d01
PC
111 GtkAdjustment* adj = range->adjustment;
112 adj->step_increment = 1;
113 adj->page_increment =
114 adj->page_size = page_size;
115 gtk_range_set_range(range, 0, upper);
ae4c09a8
PC
116
117 // ensure that the scroll position is always in valid range
118 if (*pos > *lines)
119 *pos = *lines;
30954328
RR
120}
121
29e1398f 122void wxScrollHelper::AdjustScrollbars()
566d84a7 123{
9ec927f8 124 int vw, vh;
3d9ecb87
VZ
125 m_targetWindow->GetVirtualSize(&vw, &vh);
126
127 int w, h;
128 const wxSize availSize = GetSizeAvailableForScrollTarget(
129 m_win->GetSize() - m_win->GetWindowBorderSize());
130 if ( availSize.x >= vw && availSize.y >= vh )
131 {
132 w = availSize.x;
133 h = availSize.y;
134
135 // we know that the scrollbars will be removed
136 DoAdjustHScrollbar(w, vw);
137 DoAdjustVScrollbar(h, vh);
138
139 return;
140 }
830ed6d9 141
cd38dd5b 142 m_targetWindow->GetClientSize(&w, NULL);
9ec927f8
VZ
143 DoAdjustHScrollbar(w, vw);
144
cd38dd5b 145 m_targetWindow->GetClientSize(NULL, &h);
9ec927f8 146 DoAdjustVScrollbar(h, vh);
cd38dd5b
PC
147
148 const int w_old = w;
149 m_targetWindow->GetClientSize(&w, NULL);
9ec927f8 150 if ( w != w_old )
cd38dd5b
PC
151 {
152 // It is necessary to repeat the calculations in this case to avoid an
153 // observed infinite series of size events, involving alternating
154 // changes in visibility of the scrollbars.
155 // At this point, GTK+ has already queued a resize, which will cause
156 // AdjustScrollbars() to be called again. If the scrollbar visibility
157 // is not correct before then, yet another resize will occur, possibly
158 // leading to an unending series if the sizes are just right.
9ec927f8
VZ
159 DoAdjustHScrollbar(w, vw);
160
cd38dd5b 161 m_targetWindow->GetClientSize(NULL, &h);
9ec927f8 162 DoAdjustVScrollbar(h, vh);
cd38dd5b 163 }
830ed6d9
RR
164}
165
29e1398f
VZ
166void wxScrollHelper::DoScrollOneDir(int orient,
167 int pos,
168 int pixelsPerLine,
169 int *posOld)
30486297 170{
d32e78bd 171 if ( pos != -1 && pos != *posOld && pixelsPerLine )
30486297 172 {
add7cadd
PC
173 m_win->SetScrollPos(orient, pos);
174 pos = m_win->GetScrollPos(orient);
30486297 175
d32e78bd
VZ
176 int diff = (*posOld - pos)*pixelsPerLine;
177 m_targetWindow->ScrollWindow(orient == wxHORIZONTAL ? diff : 0,
178 orient == wxHORIZONTAL ? 0 : diff);
30954328 179
d32e78bd 180 *posOld = pos;
2b5f62a0 181 }
30954328
RR
182}
183
29e1398f 184void wxScrollHelper::DoScroll( int x_pos, int y_pos )
30954328 185{
d32e78bd 186 wxCHECK_RET( m_targetWindow != 0, _T("No target window") );
30954328 187
0b0f6f87
VZ
188 DoScrollOneDir(wxHORIZONTAL, x_pos, m_xScrollPixelsPerLine, &m_xScrollPosition);
189 DoScrollOneDir(wxVERTICAL, y_pos, m_yScrollPixelsPerLine, &m_yScrollPosition);
30954328 190}
6362d82b
VZ
191
192// ----------------------------------------------------------------------------
193// scrollbars visibility
194// ----------------------------------------------------------------------------
195
196namespace
197{
198
199GtkPolicyType GtkPolicyFromWX(wxScrollbarVisibility visibility)
200{
201 GtkPolicyType policy;
202 switch ( visibility )
203 {
204 case wxSHOW_SB_NEVER:
205 policy = GTK_POLICY_NEVER;
206 break;
207
208 case wxSHOW_SB_DEFAULT:
209 policy = GTK_POLICY_AUTOMATIC;
210 break;
211
212 case wxSHOW_SB_ALWAYS:
213 policy = GTK_POLICY_ALWAYS;
214 break;
215 }
216
217 return policy;
218}
219
220} // anonymous namespace
221
29e1398f
VZ
222void wxScrollHelper::DoShowScrollbars(wxScrollbarVisibility horz,
223 wxScrollbarVisibility vert)
6362d82b
VZ
224{
225 GtkScrolledWindow * const scrolled = GTK_SCROLLED_WINDOW(m_win->m_widget);
226 wxCHECK_RET( scrolled, "window must be created" );
227
228 gtk_scrolled_window_set_policy(scrolled,
229 GtkPolicyFromWX(horz),
230 GtkPolicyFromWX(vert));
231}
232