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