File: scrollbar.cpp

package info (click to toggle)
warzone2100 4.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,348 kB
  • sloc: cpp: 675,711; ansic: 387,204; javascript: 75,107; python: 16,628; php: 4,294; sh: 3,941; makefile: 2,330; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (125 lines) | stat: -rw-r--r-- 3,237 bytes parent folder | download | duplicates (2)
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
/*
	This file is part of Warzone 2100.
	Copyright (C) 2020  Warzone 2100 Project

	Warzone 2100 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.

	Warzone 2100 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 Warzone 2100; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
 * @file
 * Functions for scroll bar.
 */

#include <memory>
#include "scrollbar.h"
#include "lib/ivis_opengl/pieblitfunc.h"
#include "src/intimage.h"

static void displayScrollBar(WIDGET *widget, UDWORD xOffset, UDWORD yOffset)
{
	auto slider = (W_SLIDER *)widget;

	int x0 = slider->x() + xOffset;
	int y0 = slider->y() + yOffset;

	RenderWindowFrame(FRAME_NORMAL, x0, y0, slider->width(), slider->height());
	// pie_UniTransBoxFill(x0, y0, x0 + slider->width(), y0 + slider->height(), WZCOL_DBLUE);

	auto sliderY = slider->numStops > 0 ? (slider->height() - slider->barSize) * slider->pos / slider->numStops: 0;
	int sliderDrawY0 = y0 + sliderY+2;
	int sliderDrawY1 = y0 + sliderY + std::max<int>(slider->barSize,5)-2;
	if (sliderDrawY1 - sliderDrawY0 <= 2)
	{
		--sliderDrawY0;
		++sliderDrawY1;
	}
	pie_UniTransBoxFill(x0+2, sliderDrawY0, x0 + slider->width()-2, sliderDrawY1, slider->isEnabled() ? WZCOL_LBLUE : WZCOL_FORM_DISABLE);
}

void ScrollBarWidget::initialize()
{
	W_SLDINIT sliderInit;
	attach(slider = std::make_shared<W_SLIDER>(&sliderInit));
	slider->numStops = 0;
	slider->barSize = 0;
	slider->orientation = WSLD_TOP;
	slider->displayFunction = displayScrollBar;
}

void ScrollBarWidget::geometryChanged()
{
	slider->setGeometry(0, 0, width(), height());
}

uint16_t ScrollBarWidget::position() const
{
	return slider->pos;
}

void ScrollBarWidget::setPosition(uint16_t newPosition)
{
	slider->pos = std::min(newPosition, slider->numStops);
}

void ScrollBarWidget::incrementPosition(int32_t amount)
{
	if (isEnabled()) {
		auto pos = amount + slider->pos;
		CLIP(pos, 0, slider->numStops);
		slider->pos = pos;
	}
}

void ScrollBarWidget::setScrollableSize(uint16_t value)
{
	auto moveToBottom = stickToBottom && slider->pos == slider->numStops;
	scrollableSize = value;
	updateSlider();
	if (moveToBottom) {
		slider->pos = slider->numStops;
	}
}

void ScrollBarWidget::setViewSize(uint16_t value)
{
	viewSize = value;
	updateSlider();
}

void ScrollBarWidget::setStickToBottom(bool value)
{
	stickToBottom = value;
}

void ScrollBarWidget::updateSlider()
{
	slider->barSize = scrollableSize > 0 ? viewSize * height() / scrollableSize : 0;
	slider->numStops = MAX(0, scrollableSize - viewSize);
	slider->pos = std::min(slider->pos, slider->numStops);
}

void ScrollBarWidget::enable()
{
	slider->enable();
}

void ScrollBarWidget::disable()
{
	slider->disable();
}

bool ScrollBarWidget::isEnabled() const
{
	return slider->isEnabled();
}