summaryrefslogtreecommitdiffstats
path: root/src/textviewwidget.cpp
blob: 12edf335e4827bf11909cf512f049ba9d94019e7 (plain)
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
#include "textviewwidget.h"

#include <algorithm>

#include "utils.h"

namespace newsboat {

TextviewWidget::TextviewWidget(const std::string& textview_name,
	Stfl::Form& form)
	: textview_name(textview_name)
	, form(form)
	, num_lines(0)
{
}

void TextviewWidget::stfl_replace_textview(std::uint32_t number_of_lines,
	std::string stfl)
{
	num_lines = number_of_lines;
	form.modify(textview_name, "replace", stfl);
}

void TextviewWidget::stfl_replace_lines(std::uint32_t number_of_lines,
	std::string stfl)
{
	num_lines = number_of_lines;
	form.modify(textview_name, "replace_inner", stfl);
}

void TextviewWidget::scroll_up()
{
	const std::uint32_t offset = get_scroll_offset();
	if (offset > 0) {
		set_scroll_offset(offset - 1);
	}
}

void TextviewWidget::scroll_down()
{
	if (num_lines == 0) {
		// Ignore if list is empty
	}
	const std::uint32_t maxoffset = num_lines - 1;
	const std::uint32_t offset = get_scroll_offset();
	if (offset < maxoffset) {
		set_scroll_offset(offset + 1);
	}
}
void TextviewWidget::scroll_to_top()
{
	set_scroll_offset(0);
}

void TextviewWidget::scroll_to_bottom()
{
	if (num_lines == 0) {
		// Ignore if list is empty
	}
	const std::uint32_t maxoffset = num_lines - 1;
	const std::uint32_t widget_height = get_height();
	if (maxoffset + 2 < widget_height) {
		set_scroll_offset(0);
	} else {
		set_scroll_offset((maxoffset + 2) - widget_height);
	}
}

void TextviewWidget::scroll_page_up()
{
	const std::uint32_t offset = get_scroll_offset();
	const std::uint32_t widget_height = get_height();
	if (offset + 1 > widget_height) {
		set_scroll_offset((offset + 1) - widget_height);
	} else {
		set_scroll_offset(0);
	}
}

void TextviewWidget::scroll_page_down()
{
	if (num_lines == 0) {
		// Ignore if list is empty
	}
	const std::uint32_t maxoffset = num_lines - 1;
	const std::uint32_t offset = get_scroll_offset();
	const std::uint32_t widget_height = get_height();
	if (offset + widget_height - 1 < maxoffset) {
		set_scroll_offset(offset + widget_height - 1);
	} else {
		set_scroll_offset(maxoffset);
	}
}

std::uint32_t TextviewWidget::get_scroll_offset()
{
	const std::string offset = form.get(textview_name + "_offset");
	if (!offset.empty()) {
		return std::max(0, std::stoi(offset));
	}
	return 0;
}

void TextviewWidget::set_scroll_offset(std::uint32_t offset)
{
	form.set(textview_name + "_offset", std::to_string(offset));
}

std::uint32_t TextviewWidget::get_width()
{
	return utils::to_u(form.get(textview_name + ":w"));
}

std::uint32_t TextviewWidget::get_height()
{
	return utils::to_u(form.get(textview_name + ":h"));
}

} // namespace newsboat