1 /*
2 *
3 * a class for saving and restoring screen areas
4 * $Id: screenarea.cc,v 1.1 2001/06/27 13:42:07 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 "screenarea.h"
26 
screenarea()27 screenarea::screenarea() {
28 }
29 
screenarea(int fx1,int fy1,int fx2,int fy2)30 screenarea::screenarea(int fx1, int fy1, int fx2, int fy2) {
31     save(fx1, fy1, fx2, fy2);
32 }
33 
~screenarea()34 screenarea::~screenarea() {
35     freebuffer();
36 }
37 
save()38 void screenarea::save() {
39     save(0, 0, COLS, LINES);
40 }
41 
save(int fx1,int fy1,int fx2,int fy2)42 void screenarea::save(int fx1, int fy1, int fx2, int fy2) {
43     int i;
44     chtype *line;
45 #ifdef HAVE_NCURSESW
46     wchar_t *line2;
47 #endif
48 
49     freebuffer();
50 
51     for(i = 0; i <= fy2-fy1; i++) {
52 	line = new chtype[fx2-fx1+2];
53 #ifdef HAVE_NCURSESW
54 	line2 = new wchar_t[fx2-fx1+2];
55 #endif
56 	mvinchnstr(fy1+i, fx1, line, fx2-fx1+1);
57 #ifdef HAVE_NCURSESW
58 	mvinnwstr(fy1+i, fx1, line2, fx2-fx1+1);
59 #endif
60 	buffer.push_back(line);
61 #ifdef HAVE_NCURSESW
62 	buffer2.push_back(line2);
63 #endif
64     }
65 
66     x1 = fx1;
67     y1 = fy1;
68     x2 = fx2;
69     y2 = fy2;
70 }
71 
restore()72 void screenarea::restore() {
73     restore(x1, y1, x2, y2);
74 }
75 
restore(int fx1,int fy1,int fx2,int fy2)76 void screenarea::restore(int fx1, int fy1, int fx2, int fy2) {
77     vector<chtype *>::iterator i;
78 #ifdef HAVE_NCURSESW
79     vector<wchar_t *>::iterator j;
80 #endif
81     int k = fy1;
82     chtype *line;
83 #ifdef HAVE_NCURSESW
84     wchar_t *line2;
85 #endif
86     int l;
87     if(!buffer.empty()) {
88 #ifdef HAVE_NCURSESW
89 	for(i = buffer.begin(), j = buffer2.begin(); i != buffer.end(); i++, j++) {
90 #else
91 	for(i = buffer.begin(); i != buffer.end(); i++) {
92 #endif
93 	    line = *i;
94 #ifdef HAVE_NCURSESW
95 	    line2 = *j;
96 	    const chtype *line_ptr = line;
97 	    const wchar_t *line2_ptr = line2;
98 	    for(l = 0; l < fx2-fx1+1; l++, line_ptr++, line2_ptr++ )
99 	    {
100 		    attrset( (*line_ptr & A_COLOR) | (*line_ptr & A_ATTRIBUTES) );
101 		    mvaddnwstr(k, fx1+l, line2_ptr, 1);
102 	    }
103 	    k++;
104 #else
105 	    mvaddchnstr(k++, fx1, line, fx2-fx1+1);
106 #endif
107 
108 	}
109 
110 	refresh();
111     }
112 
113     freebuffer();
114 }
115 
116 void screenarea::freebuffer() {
117     while(!buffer.empty()) {
118 	delete[] buffer.front();
119 	buffer.erase(buffer.begin());
120     }
121 #ifdef HAVE_NCURSESW
122     while(!buffer2.empty()) {
123 	delete[] buffer2.front();
124 	buffer2.erase(buffer2.begin());
125     }
126 #endif
127 }
128 
129 bool screenarea::empty() {
130     return buffer.empty();
131 }
132