]>
Commit | Line | Data |
---|---|---|
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 |
39 | void wxScrollHelper::SetScrollbars(int pixelsPerUnitX, int pixelsPerUnitY, |
40 | int noUnitsX, int noUnitsY, | |
41 | int xPos, int yPos, | |
ff8c74b9 | 42 | bool WXUNUSED(noRefresh)) |
30954328 RR |
43 | { |
44 | m_xScrollPixelsPerLine = pixelsPerUnitX; | |
45 | m_yScrollPixelsPerLine = pixelsPerUnitY; | |
39c869a6 | 46 | |
f18f464c VZ |
47 | int w = noUnitsX * pixelsPerUnitX; |
48 | int h = noUnitsY * pixelsPerUnitY; | |
49 | m_targetWindow->SetVirtualSize( w ? w : wxDefaultCoord, | |
50 | h ? h : wxDefaultCoord); | |
2b5f62a0 | 51 | |
ff8c74b9 RR |
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); | |
56 | ||
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 ); | |
80e8b5de | 59 | |
add7cadd PC |
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) | |
64 | { | |
65 | AdjustScrollbars(); | |
66 | } | |
0295d448 | 67 | |
ff8c74b9 | 68 | #if 0 |
0295d448 PC |
69 | if (!noRefresh) |
70 | { | |
71 | int new_x = m_xScrollPixelsPerLine * m_xScrollPosition; | |
72 | int new_y = m_yScrollPixelsPerLine * m_yScrollPosition; | |
73 | ||
74 | m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y ); | |
75 | } | |
ff8c74b9 | 76 | #endif |
30954328 RR |
77 | } |
78 | ||
29e1398f VZ |
79 | void wxScrollHelper::DoAdjustScrollbar(GtkRange* range, |
80 | int pixelsPerLine, | |
81 | int winSize, | |
82 | int virtSize, | |
83 | int *pos, | |
84 | int *lines, | |
85 | int *linesPerPage) | |
30954328 | 86 | { |
f6814d01 PC |
87 | int upper; |
88 | int page_size; | |
fb18afdd | 89 | if (pixelsPerLine > 0 && winSize > 0 && winSize < virtSize) |
4ca24f18 | 90 | { |
f6814d01 PC |
91 | upper = (virtSize + pixelsPerLine - 1) / pixelsPerLine; |
92 | page_size = winSize / pixelsPerLine; | |
5713b349 RR |
93 | *lines = upper; |
94 | *linesPerPage = page_size; | |
5713b349 RR |
95 | } |
96 | else | |
97 | { | |
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. | |
f6814d01 PC |
100 | upper = 1; |
101 | page_size = 1; | |
5713b349 RR |
102 | *lines = 0; |
103 | *linesPerPage = 0; | |
4ca24f18 | 104 | } |
9ec927f8 | 105 | |
f6814d01 PC |
106 | GtkAdjustment* adj = range->adjustment; |
107 | adj->step_increment = 1; | |
80e8b5de | 108 | adj->page_increment = |
f6814d01 PC |
109 | adj->page_size = page_size; |
110 | gtk_range_set_range(range, 0, upper); | |
ae4c09a8 PC |
111 | |
112 | // ensure that the scroll position is always in valid range | |
113 | if (*pos > *lines) | |
114 | *pos = *lines; | |
30954328 RR |
115 | } |
116 | ||
29e1398f | 117 | void wxScrollHelper::AdjustScrollbars() |
566d84a7 | 118 | { |
9ec927f8 | 119 | int vw, vh; |
3d9ecb87 VZ |
120 | m_targetWindow->GetVirtualSize(&vw, &vh); |
121 | ||
122 | int w, h; | |
123 | const wxSize availSize = GetSizeAvailableForScrollTarget( | |
124 | m_win->GetSize() - m_win->GetWindowBorderSize()); | |
125 | if ( availSize.x >= vw && availSize.y >= vh ) | |
126 | { | |
127 | w = availSize.x; | |
128 | h = availSize.y; | |
129 | ||
130 | // we know that the scrollbars will be removed | |
131 | DoAdjustHScrollbar(w, vw); | |
132 | DoAdjustVScrollbar(h, vh); | |
133 | ||
134 | return; | |
135 | } | |
830ed6d9 | 136 | |
cd38dd5b | 137 | m_targetWindow->GetClientSize(&w, NULL); |
9ec927f8 VZ |
138 | DoAdjustHScrollbar(w, vw); |
139 | ||
cd38dd5b | 140 | m_targetWindow->GetClientSize(NULL, &h); |
9ec927f8 | 141 | DoAdjustVScrollbar(h, vh); |
cd38dd5b PC |
142 | |
143 | const int w_old = w; | |
144 | m_targetWindow->GetClientSize(&w, NULL); | |
9ec927f8 | 145 | if ( w != w_old ) |
cd38dd5b PC |
146 | { |
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. | |
9ec927f8 VZ |
154 | DoAdjustHScrollbar(w, vw); |
155 | ||
cd38dd5b | 156 | m_targetWindow->GetClientSize(NULL, &h); |
9ec927f8 | 157 | DoAdjustVScrollbar(h, vh); |
cd38dd5b | 158 | } |
830ed6d9 RR |
159 | } |
160 | ||
29e1398f VZ |
161 | void wxScrollHelper::DoScrollOneDir(int orient, |
162 | int pos, | |
163 | int pixelsPerLine, | |
164 | int *posOld) | |
30486297 | 165 | { |
d32e78bd | 166 | if ( pos != -1 && pos != *posOld && pixelsPerLine ) |
30486297 | 167 | { |
add7cadd PC |
168 | m_win->SetScrollPos(orient, pos); |
169 | pos = m_win->GetScrollPos(orient); | |
30486297 | 170 | |
d32e78bd VZ |
171 | int diff = (*posOld - pos)*pixelsPerLine; |
172 | m_targetWindow->ScrollWindow(orient == wxHORIZONTAL ? diff : 0, | |
173 | orient == wxHORIZONTAL ? 0 : diff); | |
30954328 | 174 | |
d32e78bd | 175 | *posOld = pos; |
2b5f62a0 | 176 | } |
30954328 RR |
177 | } |
178 | ||
29e1398f | 179 | void wxScrollHelper::DoScroll( int x_pos, int y_pos ) |
30954328 | 180 | { |
d32e78bd | 181 | wxCHECK_RET( m_targetWindow != 0, _T("No target window") ); |
30954328 | 182 | |
0b0f6f87 VZ |
183 | DoScrollOneDir(wxHORIZONTAL, x_pos, m_xScrollPixelsPerLine, &m_xScrollPosition); |
184 | DoScrollOneDir(wxVERTICAL, y_pos, m_yScrollPixelsPerLine, &m_yScrollPosition); | |
30954328 | 185 | } |
6362d82b VZ |
186 | |
187 | // ---------------------------------------------------------------------------- | |
188 | // scrollbars visibility | |
189 | // ---------------------------------------------------------------------------- | |
190 | ||
191 | namespace | |
192 | { | |
193 | ||
194 | GtkPolicyType GtkPolicyFromWX(wxScrollbarVisibility visibility) | |
195 | { | |
196 | GtkPolicyType policy; | |
197 | switch ( visibility ) | |
198 | { | |
199 | case wxSHOW_SB_NEVER: | |
200 | policy = GTK_POLICY_NEVER; | |
201 | break; | |
202 | ||
203 | case wxSHOW_SB_DEFAULT: | |
204 | policy = GTK_POLICY_AUTOMATIC; | |
205 | break; | |
206 | ||
207 | case wxSHOW_SB_ALWAYS: | |
208 | policy = GTK_POLICY_ALWAYS; | |
209 | break; | |
210 | } | |
211 | ||
212 | return policy; | |
213 | } | |
214 | ||
215 | } // anonymous namespace | |
216 | ||
29e1398f VZ |
217 | void wxScrollHelper::DoShowScrollbars(wxScrollbarVisibility horz, |
218 | wxScrollbarVisibility vert) | |
6362d82b VZ |
219 | { |
220 | GtkScrolledWindow * const scrolled = GTK_SCROLLED_WINDOW(m_win->m_widget); | |
221 | wxCHECK_RET( scrolled, "window must be created" ); | |
222 | ||
223 | gtk_scrolled_window_set_policy(scrolled, | |
224 | GtkPolicyFromWX(horz), | |
225 | GtkPolicyFromWX(vert)); | |
226 | } | |
227 |