summaryrefslogtreecommitdiff
path: root/scrollbar.cc
blob: 9b42899d01de11e9172499cae2f7dca85835d591 (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
// Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
//
// This program 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.
//
// This program 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, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.

#include <config.h>

#include "scrollbar.h"
#include "themes.h"

Scrollbar::Scrollbar()
{
    create_window();
    scrollok(wnd, 0);
    total_size = 0;
    page_size  = 0;
    page_pos   = 0;
    dirty = true;
}

void Scrollbar::set_page_size(int sz)
{
    if (sz != page_size) {
	page_size = sz;
	invalidate_view();
    }
}

void Scrollbar::set_total_size(int sz)
{
    if (sz != total_size) {
	total_size = sz;
	invalidate_view();
    }
}

void Scrollbar::set_page_pos(int pos)
{
    if (pos != page_pos) {
	page_pos = pos;
	invalidate_view();
    }
}

void Scrollbar::update()
{
    if (!dirty)
	return;

    int thumb_pos = 0;
    if (page_pos > 0)
	thumb_pos = (int)((double(page_pos)/double(total_size))*window_height());
    int thumb_size = (int)((double)page_size/double(total_size)*window_height()+1);
    if (thumb_size > window_height())
	thumb_size = window_height();
    if (page_size >= total_size) // don't show if we don't have a reason
	thumb_size = 0;
    if (thumb_size == window_height())
	thumb_size--;
    if (page_pos + page_size >= total_size)
	thumb_pos = window_height() - thumb_size;
    
    werase(wnd);
    attribute_t base_attr  = get_attr(SCROLLBAR_ATTR);
    attribute_t thumb_attr = get_attr(SCROLLBAR_THUMB_ATTR);
    for (int i = 0; i < window_height(); i++) {
	if (i >= thumb_pos && i < thumb_pos + thumb_size) {
	    wattrset(wnd, thumb_attr);
	    mvwhline(wnd, i, 0, ' ', 1);
	} else {
	    wattrset(wnd, base_attr);
	    mvwhline(wnd, i, 0, terminal::graphical_boxes ? ACS_VLINE : '|', 1);
	}
    }    

    wnoutrefresh(wnd);
    dirty = false;
}

void Scrollbar::resize(int lines, int columns, int y, int x)
{
    Widget::resize(lines, columns, y, x);
    invalidate_view();
}