1 /*
2 *
3 * kkconsui textbrowser class
4 * $Id: textbrowser.cc,v 1.13 2002/11/23 15:42:08 konst Exp $
5 *
6 * Copyright (C) 1999-2001 by Konstantin Klyagin <k@thekonst.net>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * USA
22 *
23 */
24
25 #include "textbrowser.h"
26
textbrowser(int nx1,int ny1,int nx2,int ny2,int clr)27 textbrowser::textbrowser(int nx1, int ny1, int nx2, int ny2, int clr) {
28 setcoords(nx1, ny1, nx2, ny2);
29 setcolor(clr);
30 idle = 0;
31 otherkeys = 0;
32 }
33
textbrowser(int clr)34 textbrowser::textbrowser(int clr) {
35 setcolor(clr);
36 idle = 0;
37 otherkeys = 0;
38 }
39
~textbrowser()40 textbrowser::~textbrowser() {
41 }
42
setcolor(int clr)43 void textbrowser::setcolor(int clr) {
44 ncolor = clr;
45 }
46
setcoords(int nx1,int ny1,int nx2,int ny2)47 void textbrowser::setcoords(int nx1, int ny1, int nx2, int ny2) {
48 x1 = nx1;
49 x2 = nx2;
50 y1 = ny1;
51 y2 = ny2;
52 setbuf(buf);
53 }
54
redraw()55 void textbrowser::redraw() {
56 draw(line);
57 }
58
empty()59 bool textbrowser::empty() {
60 return lines.empty();
61 }
62
draw(int line)63 void textbrowser::draw(int line) {
64 int i, k;
65 string buf;
66 attrset(ncolor);
67
68 for(i = line; (i-line < y2-y1) && (i < lines.size()); i++) {
69 mvprintw(y1+i-line, x1, "");
70 buf = makebidi(lines[i], x2-x1);
71 printstring(buf);
72 for(k = buf.size(); k < x2-x1; k++) printw(" ");
73 }
74
75 for(; i-line < y2-y1; i++) mvhline(y1+i-line, x1, ' ', x2-x1);
76 refresh();
77 }
78
setbuf(const string & p)79 void textbrowser::setbuf(const string &p) {
80 line = 0;
81 buf = p;
82
83 if(x2-x1 > 1)
84 breakintolines(buf = p, lines, x2-x1);
85
86 if((endline = lines.size()-y2+y1+1) < 0) endline = 0;
87 }
88
open()89 int textbrowser::open() {
90 bool go;
91 int res, ch;
92
93 finished = aborted = false;
94
95 screenbuffer.save(x1, y1, x2, y2);
96 draw(line);
97
98 while(!finished) {
99 if(idle) go = keypressed(); else go = true;
100
101 if(go) switch(ch = getkey()) {
102 case KEY_UP:
103 case KEY_DOWN:
104 case KEY_PPAGE:
105 case KEY_NPAGE:
106 case KEY_END:
107 case KEY_HOME:
108 move(ch);
109 draw(line);
110 break;
111 case KEY_F(10):
112 case 27:
113 res = 0;
114 finished = true;
115 break;
116
117 default:
118 if(otherkeys) {
119 res = (*otherkeys)(*this, ch);
120 if(res >= 0) finished = true;
121 }
122
123 } else {
124 if(idle) (*idle)(*this);
125 }
126 }
127
128 return res;
129 }
130
move(int k)131 void textbrowser::move(int k) {
132 switch(k) {
133 case KEY_UP: if(--line < 0) line = 0; break;
134 case KEY_DOWN: if(++line > endline) line = endline; break;
135 case KEY_PPAGE: if((line -= y2-y1) < 0) line = 0; break;
136 case KEY_NPAGE: if((line += y2-y1) > endline) line = endline; break;
137 case KEY_END: line = endline; break;
138 case KEY_HOME: line = 0; break;
139 }
140 }
141