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