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