1 /*
2 *
3 * kkconsui textwindow class
4 * $Id: textwindow.cc,v 1.9 2002/11/23 15:42:09 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 "textwindow.h"
26 
textwindow()27 textwindow::textwindow() {
28     x1 = x2 = y1 = y2 = options = 0;
29 }
30 
textwindow(int xx1,int yy1,int xx2,int yy2,int wcolor,int noptions,int tcolor,const char * tfmt,...)31 textwindow::textwindow(int xx1, int yy1, int xx2, int yy2, int wcolor,
32 int noptions, int tcolor, const char *tfmt, ...) {
33     char buf[10240];
34 
35     setoptions(noptions);
36     setcoords(xx1, yy1, xx2, yy2);
37     setcolor(wcolor);
38 
39     if(tfmt) {
40 	va_list ap;
41 	va_start(ap, tfmt);
42 	vsprintf(buf, tfmt, ap);
43 	va_end(ap);
44 	set_title(tcolor, buf);
45     }
46 }
47 
textwindow(const textwindow & aw)48 textwindow::textwindow(const textwindow &aw): abstractuicontrol(aw) {
49     title = aw.title;
50     wc = aw.wc;
51     tc = aw.tc;
52     options = aw.options;
53 }
54 
~textwindow()55 textwindow::~textwindow() {
56 }
57 
setcoords(int ax1,int ay1,int ax2,int ay2)58 void textwindow::setcoords(int ax1, int ay1, int ax2, int ay2) {
59     x1 = ax1;
60     x2 = ax2;
61     y1 = ay1;
62     y2 = ay2;
63 
64     if(options & TW_CENTERED) {
65 	int h = y2-y1, w = x2-x1;
66 	y1 = (LINES-h)/2;
67 	y2 = y1 + h;
68 	x1 = (COLS-w)/2;
69 	x2 = x1 + w;
70     }
71 }
72 
setcolor(int awc)73 void textwindow::setcolor(int awc) {
74     wc = awc;
75 }
76 
setoptions(int aoptions)77 void textwindow::setoptions(int aoptions) {
78     options = aoptions;
79     setcoords(x1, y1, x2, y2);
80 }
81 
set_titlef(int color,const char * fmt,...)82 void textwindow::set_titlef(int color, const char *fmt, ...) {
83     va_list ap;
84     char buf[10240];
85 
86     va_start(ap, fmt);
87     vsprintf(buf, fmt, ap);
88     va_end(ap);
89 
90     set_title(color, buf);
91 }
92 
set_title(int color,const string & atitle)93 void textwindow::set_title(int color, const string &atitle) {
94     title = atitle;
95     tc = color;
96 
97     if(fisopen) {
98 	attrset(wc);
99 	mvhline(y1, x1+1, HLINE, x2-x1-1);
100 	write(((x2 - x1) - title.size())/2, 0, tc, title);
101     }
102 }
103 
writef(int x,int y,const char * fmt,...)104 void textwindow::writef(int x, int y, const char *fmt, ...) {
105     char buf[10240];
106     va_list ap;
107 
108     va_start(ap, fmt);
109     vsprintf(buf, fmt, ap);
110     va_end(ap);
111 
112     write(x, y, wc, buf);
113 }
114 
writef(int x,int y,int c,const char * fmt,...)115 void textwindow::writef(int x, int y, int c, const char *fmt, ...) {
116     char buf[10240];
117     va_list ap;
118 
119     va_start(ap, fmt);
120     vsprintf(buf, fmt, ap);
121     va_end(ap);
122 
123     write(x, y, c, buf);
124 }
125 
write(int x,int y,const string & text)126 void textwindow::write(int x, int y, const string &text) {
127     write(x, y, wc, text);
128 }
129 
write(int x,int y,int c,const string & text)130 void textwindow::write(int x, int y, int c, const string &text) {
131     int i;
132     string dtext;
133 
134     if(fisopen && (y < y2-y1)) {
135 	for(i = 0; (i < text.size()) && (i < x2-x1-x); i++)
136 	    dtext += KT_DISP_FILTER(text[i]);
137 
138 	attrset(c);
139 	mvprintw(y1 + y, x1 + x, "%s", dtext.c_str());
140 	refresh();
141     }
142 }
143 
gotoxy(int x,int y)144 void textwindow::gotoxy(int x, int y) {
145     if(fisopen) kgotoxy(x1 + x, y1 + y);
146 }
147 
redraw()148 void textwindow::redraw() {
149     if(fisopen) {
150 	int i;
151 	attrset(wc);
152 
153 	if(options & TW_NOBORDER) {
154 	    for(i = y1; i < y2; i++) mvhline(i, x1, ' ', x2-x1);
155 	} else {
156 	    for(i = y1; i <= y2; i++) mvhline(i, x1, ' ', x2-x1+1);
157 
158 	    if(!(options & TW_SPACEBORDER)) {
159 		mvaddch(y1, x1, ULCORNER); mvaddch(y2, x2, LRCORNER);
160 		mvaddch(y1, x2, URCORNER); mvaddch(y2, x1, LLCORNER);
161 
162 		mvvline(y1 + 1, x1, VLINE, y2-y1-1);
163 		mvvline(y1 + 1, x2, VLINE, y2-y1-1);
164 		mvhline(y1, x1 + 1, HLINE, x2-x1-1);
165 		mvhline(y2, x1 + 1, HLINE, x2-x1-1);
166 	    }
167 	}
168 
169 	if(!title.empty()) {
170 	    attrset(tc);
171 	    mvprintw(y1, x1+((x2-x1)-title.size())/2, "%s", title.c_str());
172 	}
173 
174 	refresh();
175     }
176 }
177 
open()178 void textwindow::open() {
179     if(!fisopen) {
180 	fisopen = true;
181 	screenbuffer.save(x1, y1, x2, y2);
182 	redraw();
183     }
184 }
185 
separatey(int y)186 void textwindow::separatey(int y) {
187     attrset(wc);
188     mvhline(y1 + y, x1 + 1, HLINE, x2 - x1 - 1);
189     mvaddch(y1 + y, x1, LTEE);
190     mvaddch(y1 + y, x2, RTEE);
191     refresh();
192 }
193 
separatex(int x)194 void textwindow::separatex(int x) {
195     attrset(wc);
196     mvvline(y1 + 1, x1 + x, VLINE, y2 - y1 - 1);
197     mvaddch(y1, x1 + x, TTEE);
198     mvaddch(y2, x1 + x, BTEE);
199     refresh();
200 }
201 
isbordered()202 bool textwindow::isbordered() {
203     return !(options & TW_NOBORDER);
204 }
205 
close()206 void textwindow::close() {
207     abstractuicontrol::close();
208 }
209