/
canvas.h
141 lines (107 loc) · 6.01 KB
/
canvas.h
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
// rTorrent - BitTorrent client
// Copyright (C) 2005-2011, Jari Sundell
//
// 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-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_DISPLAY_CANVAS_H
#define RTORRENT_DISPLAY_CANVAS_H
#include <string>
#include <vector>
#include "attributes.h"
namespace display {
class Canvas {
public:
typedef std::vector<Attributes> attributes_list;
Canvas(int x = 0, int y = 0, int width = 0, int height = 0);
~Canvas() { delwin(m_window); }
void refresh() { wnoutrefresh(m_window); }
static void refresh_std() { wnoutrefresh(stdscr); }
void redraw() { redrawwin(m_window); }
static void redraw_std() { redrawwin(stdscr); }
void resize(int w, int h) { wresize(m_window, h, w); }
void resize(int x, int y, int w, int h);
static void resize_term(int x, int y) { resizeterm(y, x); }
static void resize_term(std::pair<int, int> dim) { resizeterm(dim.second, dim.first); }
unsigned int get_x() { int x, __UNUSED y; getyx(m_window, y, x); return x; }
unsigned int get_y() { int x, y; getyx(m_window, y, x); return y; }
unsigned int width() { int x, __UNUSED y; getmaxyx(m_window, y, x); return x; }
unsigned int height() { int x, y; getmaxyx(m_window, y, x); return y; }
void move(unsigned int x, unsigned int y) { wmove(m_window, y, x); }
chtype get_background() { return getbkgd(m_window); }
void set_background(chtype c) { return wbkgdset(m_window, c); }
void erase() { werase(m_window); }
static void erase_std() { werase(stdscr); }
void print_border(chtype ls, chtype rs,
chtype ts, chtype bs,
chtype tl, chtype tr,
chtype bl, chtype br) { wborder(m_window, ls, rs, ts, bs, tl, tr, bl, br); }
// The format string is non-const, but that will not be a problem
// since the string shall always be a C string choosen at
// compiletime. Might cause extra copying of the string?
void print(const char* str, ...);
void print(unsigned int x, unsigned int y, const char* str, ...);
void print_attributes(unsigned int x, unsigned int y, const char* first, const char* last, const attributes_list* attributes);
void print_char(const chtype ch) { waddch(m_window, ch); }
void print_char(unsigned int x, unsigned int y, const chtype ch) { mvwaddch(m_window, y, x, ch); }
void set_attr(unsigned int x, unsigned int y, unsigned int n, int attr, int color) { mvwchgat(m_window, y, x, n, attr, color, NULL); }
void set_default_attributes(int attr) { (void)wattrset(m_window, attr); }
// Initialize stdscr.
static void initialize();
static void cleanup();
static int get_screen_width() { int x, __UNUSED y; getmaxyx(stdscr, y, x); return x; }
static int get_screen_height() { int x, y; getmaxyx(stdscr, y, x); return y; }
static std::pair<int, int> term_size();
static void do_update() { doupdate(); }
private:
Canvas(const Canvas&);
void operator = (const Canvas&);
static bool m_isInitialized;
WINDOW* m_window;
};
inline void
Canvas::print(const char* str, ...) {
va_list arglist;
va_start(arglist, str);
vw_printw(m_window, const_cast<char*>(str), arglist);
va_end(arglist);
}
inline void
Canvas::print(unsigned int x, unsigned int y, const char* str, ...) {
va_list arglist;
va_start(arglist, str);
wmove(m_window, y, x);
vw_printw(m_window, const_cast<char*>(str), arglist);
va_end(arglist);
}
}
#endif