This repository has been archived by the owner on Dec 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScrollPane.cpp
284 lines (233 loc) · 6.16 KB
/
ScrollPane.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright (C) 2007 by Mark Pustjens <pustjens@dds.nl>
* Copyright (C) 2010-2012 by CenterIM developers
*
* This file is part of CenterIM.
*
* CenterIM is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* CenterIM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* @file
* ScrollPane class implementation.
*
* @ingroup cppconsui
*/
#include "ScrollPane.h"
namespace CppConsUI
{
ScrollPane::ScrollPane(int w, int h, int scrollw, int scrollh)
: Container(w, h), scroll_xpos(0), scroll_ypos(0), scroll_width(scrollw)
, scroll_height(scrollh), update_screen_area(false), screen_area(NULL)
{
update_area = true;
}
ScrollPane::~ScrollPane()
{
if (screen_area)
delete screen_area;
}
void ScrollPane::Draw()
{
DrawEx(true);
}
int ScrollPane::GetRealWidth() const
{
if (!screen_area)
return 0;
return screen_area->getmaxx();
}
int ScrollPane::GetRealHeight() const
{
if (!screen_area)
return 0;
return screen_area->getmaxy();
}
Point ScrollPane::GetRelativePosition(const Container& ref,
const Widget& child) const
{
g_assert(child.GetParent() == this);
if (!parent || this == &ref)
return Point(child.GetLeft() - scroll_xpos, child.GetTop() - scroll_ypos);
Point p = parent->GetRelativePosition(ref, *this);
return Point(p.GetX() + child.GetLeft() - scroll_xpos,
p.GetY() + child.GetTop() - scroll_ypos);
}
Point ScrollPane::GetAbsolutePosition(const Widget& child) const
{
g_assert(child.GetParent() == this);
if (!parent)
return Point(child.GetLeft() - scroll_xpos, child.GetTop() - scroll_ypos);
Point p = parent->GetAbsolutePosition(*this);
return Point(p.GetX() + child.GetLeft() - scroll_xpos,
p.GetY() + child.GetTop() - scroll_ypos);
}
void ScrollPane::SetScrollSize(int swidth, int sheight)
{
if (swidth == scroll_width && sheight == scroll_height)
return;
scroll_width = swidth;
scroll_height = sheight;
UpdateVirtualArea();
signal_scrollarea_resize(*this, Size(scroll_width, scroll_height));
}
void ScrollPane::AdjustScroll(int newx, int newy)
{
bool scrolled = false;
if (scroll_xpos != newx || scroll_ypos != newy)
scrolled = true;
if (screen_area) {
scroll_xpos = newx;
scroll_ypos = newy;
int real_width = screen_area->getmaxx();
int real_height = screen_area->getmaxy();
if (scroll_xpos + real_width > scroll_width) {
scroll_xpos = scroll_width - real_width;
scrolled = true;
}
if (scroll_xpos < 0) {
scroll_xpos = 0;
scrolled = true;
}
if (scroll_ypos + real_height > scroll_height) {
scroll_ypos = scroll_height - real_height;
scrolled = true;
}
if (scroll_ypos < 0) {
scroll_ypos = 0;
scrolled = true;
}
}
else {
if (!scroll_xpos && !scroll_ypos)
scrolled = true;
scroll_xpos = 0;
scroll_ypos = 0;
}
if (scrolled) {
Redraw();
signal_scrollarea_scroll(*this, Point(scroll_xpos, scroll_ypos));
}
}
void ScrollPane::MakeVisible(int x, int y)
{
if (!screen_area) {
AdjustScroll(0, 0);
return;
}
if (!MakePointVisible(x, y))
return;
Redraw();
signal_scrollarea_scroll(*this, Point(scroll_xpos, scroll_ypos));
}
void ScrollPane::MakeVisible(int x, int y, int w, int h)
{
if (!screen_area) {
AdjustScroll(0, 0);
return;
}
bool scrolled = false;
if (MakePointVisible(x + w - 1, y + h - 1))
scrolled = true;
if (MakePointVisible(x, y))
scrolled = true;
if (!scrolled)
return;
Redraw();
signal_scrollarea_scroll(*this, Point(scroll_xpos, scroll_ypos));
}
void ScrollPane::UpdateArea()
{
update_screen_area = true;
Redraw();
}
void ScrollPane::ProceedUpdateArea()
{
g_assert(parent);
if (update_screen_area) {
if (screen_area)
delete screen_area;
screen_area = parent->GetSubPad(*this, xpos, ypos, width, height);
// fix scroll position if necessary
AdjustScroll(scroll_xpos, scroll_ypos);
update_screen_area = false;
}
}
void ScrollPane::UpdateVirtualArea()
{
if (!update_area)
for (Children::iterator i = children.begin(); i != children.end(); i++)
i->widget->UpdateArea();
update_area = true;
}
void ScrollPane::ProceedUpdateVirtualArea()
{
if (update_area) {
if (area)
delete area;
area = Curses::Window::newpad(scroll_width, scroll_height);
update_area = false;
}
}
void ScrollPane::DrawEx(bool container_draw)
{
ProceedUpdateArea();
ProceedUpdateVirtualArea();
if (!area || !screen_area) {
if (screen_area)
screen_area->fill(GetColorPair("container", "background"));
return;
}
if (container_draw)
Container::Draw();
/* If the defined scrollable area is smaller than the widget, make sure
* the copy works. */
int copyw = MIN(scroll_width, screen_area->getmaxx()) - 1;
int copyh = MIN(scroll_height, screen_area->getmaxy()) - 1;
area->copyto(screen_area, scroll_xpos, scroll_ypos, 0, 0, copyw, copyh, 0);
}
bool ScrollPane::MakePointVisible(int x, int y)
{
// fix parameters
if (x < 0)
x = 0;
else if (x >= scroll_width)
x = scroll_width - 1;
if (y < 0)
y = 0;
else if (y >= scroll_height)
y = scroll_height - 1;
int real_width = screen_area->getmaxx();
int real_height = screen_area->getmaxy();
bool scrolled = false;
if (x > scroll_xpos + real_width - 1) {
scroll_xpos = x - real_width + 1;
scrolled = true;
}
else if (x < scroll_xpos) {
scroll_xpos = x;
scrolled = true;
}
if (y > scroll_ypos + real_height - 1) {
scroll_ypos = y - real_height + 1;
scrolled = true;
}
else if (y < scroll_ypos) {
scroll_ypos = y;
scrolled = true;
}
return scrolled;
}
} // namespace CppConsUI
/* vim: set tabstop=2 shiftwidth=2 textwidth=78 expandtab : */