]>
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 | ||
30954328 RR |
13 | // For compilers that support precompilation, includes "wx.h". |
14 | #include "wx/wxprec.h" | |
15 | ||
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
e1bf3ad3 | 20 | #include "wx/scrolwin.h" |
30954328 | 21 | |
8aa40b04 | 22 | #include <gtk/gtk.h> |
30954328 | 23 | |
30954328 | 24 | // ---------------------------------------------------------------------------- |
d32e78bd | 25 | // wxScrollHelper implementation |
30954328 RR |
26 | // ---------------------------------------------------------------------------- |
27 | ||
29e1398f VZ |
28 | void wxScrollHelper::SetScrollbars(int pixelsPerUnitX, int pixelsPerUnitY, |
29 | int noUnitsX, int noUnitsY, | |
30 | int xPos, int yPos, | |
8aa40b04 | 31 | bool noRefresh) |
30954328 | 32 | { |
8aa40b04 PC |
33 | m_win->m_scrollBar[wxWindow::ScrollDir_Horz]->adjustment->value = xPos; |
34 | m_win->m_scrollBar[wxWindow::ScrollDir_Vert]->adjustment->value = yPos; | |
35 | base_type::SetScrollbars( | |
36 | pixelsPerUnitX, pixelsPerUnitY, noUnitsX, noUnitsY, xPos, yPos, noRefresh); | |
30954328 RR |
37 | } |
38 | ||
29e1398f VZ |
39 | void wxScrollHelper::DoAdjustScrollbar(GtkRange* range, |
40 | int pixelsPerLine, | |
41 | int winSize, | |
42 | int virtSize, | |
43 | int *pos, | |
44 | int *lines, | |
45 | int *linesPerPage) | |
30954328 | 46 | { |
f6814d01 PC |
47 | int upper; |
48 | int page_size; | |
fb18afdd | 49 | if (pixelsPerLine > 0 && winSize > 0 && winSize < virtSize) |
4ca24f18 | 50 | { |
f6814d01 PC |
51 | upper = (virtSize + pixelsPerLine - 1) / pixelsPerLine; |
52 | page_size = winSize / pixelsPerLine; | |
5713b349 RR |
53 | *lines = upper; |
54 | *linesPerPage = page_size; | |
5713b349 RR |
55 | } |
56 | else | |
57 | { | |
58 | // GtkRange won't allow upper == lower, so for disabled state use [0,1] | |
59 | // with a page size of 1. This will also clamp position to 0. | |
f6814d01 PC |
60 | upper = 1; |
61 | page_size = 1; | |
5713b349 RR |
62 | *lines = 0; |
63 | *linesPerPage = 0; | |
4ca24f18 | 64 | } |
9ec927f8 | 65 | |
f6814d01 PC |
66 | GtkAdjustment* adj = range->adjustment; |
67 | adj->step_increment = 1; | |
8aa40b04 | 68 | adj->page_increment = |
f6814d01 PC |
69 | adj->page_size = page_size; |
70 | gtk_range_set_range(range, 0, upper); | |
ae4c09a8 PC |
71 | |
72 | // ensure that the scroll position is always in valid range | |
73 | if (*pos > *lines) | |
74 | *pos = *lines; | |
30954328 RR |
75 | } |
76 | ||
29e1398f | 77 | void wxScrollHelper::AdjustScrollbars() |
566d84a7 | 78 | { |
9ec927f8 | 79 | int vw, vh; |
3d9ecb87 VZ |
80 | m_targetWindow->GetVirtualSize(&vw, &vh); |
81 | ||
82 | int w, h; | |
83 | const wxSize availSize = GetSizeAvailableForScrollTarget( | |
84 | m_win->GetSize() - m_win->GetWindowBorderSize()); | |
85 | if ( availSize.x >= vw && availSize.y >= vh ) | |
86 | { | |
87 | w = availSize.x; | |
88 | h = availSize.y; | |
89 | ||
90 | // we know that the scrollbars will be removed | |
91 | DoAdjustHScrollbar(w, vw); | |
92 | DoAdjustVScrollbar(h, vh); | |
93 | ||
94 | return; | |
95 | } | |
830ed6d9 | 96 | |
cd38dd5b | 97 | m_targetWindow->GetClientSize(&w, NULL); |
9ec927f8 VZ |
98 | DoAdjustHScrollbar(w, vw); |
99 | ||
cd38dd5b | 100 | m_targetWindow->GetClientSize(NULL, &h); |
9ec927f8 | 101 | DoAdjustVScrollbar(h, vh); |
cd38dd5b PC |
102 | |
103 | const int w_old = w; | |
104 | m_targetWindow->GetClientSize(&w, NULL); | |
9ec927f8 | 105 | if ( w != w_old ) |
cd38dd5b PC |
106 | { |
107 | // It is necessary to repeat the calculations in this case to avoid an | |
108 | // observed infinite series of size events, involving alternating | |
109 | // changes in visibility of the scrollbars. | |
110 | // At this point, GTK+ has already queued a resize, which will cause | |
111 | // AdjustScrollbars() to be called again. If the scrollbar visibility | |
112 | // is not correct before then, yet another resize will occur, possibly | |
113 | // leading to an unending series if the sizes are just right. | |
9ec927f8 VZ |
114 | DoAdjustHScrollbar(w, vw); |
115 | ||
cd38dd5b | 116 | m_targetWindow->GetClientSize(NULL, &h); |
9ec927f8 | 117 | DoAdjustVScrollbar(h, vh); |
cd38dd5b | 118 | } |
830ed6d9 RR |
119 | } |
120 | ||
29e1398f VZ |
121 | void wxScrollHelper::DoScrollOneDir(int orient, |
122 | int pos, | |
123 | int pixelsPerLine, | |
124 | int *posOld) | |
30486297 | 125 | { |
d32e78bd | 126 | if ( pos != -1 && pos != *posOld && pixelsPerLine ) |
30486297 | 127 | { |
add7cadd PC |
128 | m_win->SetScrollPos(orient, pos); |
129 | pos = m_win->GetScrollPos(orient); | |
30486297 | 130 | |
d32e78bd VZ |
131 | int diff = (*posOld - pos)*pixelsPerLine; |
132 | m_targetWindow->ScrollWindow(orient == wxHORIZONTAL ? diff : 0, | |
133 | orient == wxHORIZONTAL ? 0 : diff); | |
30954328 | 134 | |
d32e78bd | 135 | *posOld = pos; |
2b5f62a0 | 136 | } |
30954328 RR |
137 | } |
138 | ||
29e1398f | 139 | void wxScrollHelper::DoScroll( int x_pos, int y_pos ) |
30954328 | 140 | { |
d32e78bd | 141 | wxCHECK_RET( m_targetWindow != 0, _T("No target window") ); |
30954328 | 142 | |
0b0f6f87 VZ |
143 | DoScrollOneDir(wxHORIZONTAL, x_pos, m_xScrollPixelsPerLine, &m_xScrollPosition); |
144 | DoScrollOneDir(wxVERTICAL, y_pos, m_yScrollPixelsPerLine, &m_yScrollPosition); | |
30954328 | 145 | } |
6362d82b VZ |
146 | |
147 | // ---------------------------------------------------------------------------- | |
148 | // scrollbars visibility | |
149 | // ---------------------------------------------------------------------------- | |
150 | ||
151 | namespace | |
152 | { | |
153 | ||
154 | GtkPolicyType GtkPolicyFromWX(wxScrollbarVisibility visibility) | |
155 | { | |
156 | GtkPolicyType policy; | |
157 | switch ( visibility ) | |
158 | { | |
159 | case wxSHOW_SB_NEVER: | |
160 | policy = GTK_POLICY_NEVER; | |
161 | break; | |
162 | ||
163 | case wxSHOW_SB_DEFAULT: | |
164 | policy = GTK_POLICY_AUTOMATIC; | |
165 | break; | |
166 | ||
167 | case wxSHOW_SB_ALWAYS: | |
168 | policy = GTK_POLICY_ALWAYS; | |
169 | break; | |
170 | } | |
171 | ||
172 | return policy; | |
173 | } | |
174 | ||
175 | } // anonymous namespace | |
176 | ||
29e1398f VZ |
177 | void wxScrollHelper::DoShowScrollbars(wxScrollbarVisibility horz, |
178 | wxScrollbarVisibility vert) | |
6362d82b VZ |
179 | { |
180 | GtkScrolledWindow * const scrolled = GTK_SCROLLED_WINDOW(m_win->m_widget); | |
181 | wxCHECK_RET( scrolled, "window must be created" ); | |
182 | ||
183 | gtk_scrolled_window_set_policy(scrolled, | |
184 | GtkPolicyFromWX(horz), | |
185 | GtkPolicyFromWX(vert)); | |
186 | } |