1 /*
2  *	HT Editor
3  *	display.h
4  *
5  *	Copyright (C) 1999-2004 Stefan Weyergraf (stefan@weyergraf.de)
6  *
7  *	This program is free software; you can redistribute it and/or modify
8  *	it under the terms of the GNU General Public License version 2 as
9  *	published by the Free Software Foundation.
10  *
11  *	This program is distributed in the hope that it will be useful,
12  *	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *	GNU General Public License for more details.
15  *
16  *	You should have received a copy of the GNU General Public License
17  *	along with this program; if not, write to the Free Software
18  *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #ifndef __DISPLAY_H__
22 #define __DISPLAY_H__
23 
24 #include "bounds.h"
25 #include "types.h"
26 #include "data.h"
27 
28 /* codepages */
29 
30 enum Codepage {
31 	CP_INVALID = 0,
32 	CP_DEVICE,
33 	CP_GRAPHICAL,
34 	CP_WINDOWS,
35 	CP_UNICODE,
36 };
37 
38 /* "graphical" chars (ie. lines, corners and patterns like in ASCII) */
39 
40 // if a char has all 4 orthogonally possible alignments, we suffix its name
41 // by a number 0..3, counting clockwise (mathematically positive)
42 
43 #define	GC_TRANSPARENT		'0'		// transparent
44 
45 #define	GC_1VLINE		'|'		// single vertical line
46 #define	GC_1HLINE		'-'		// single horizontal line
47 #define	GC_1CORNER0		'l'		// right-upper corner, single
48 #define	GC_1CORNER1		'j'		// right-lower corner, single
49 #define	GC_1CORNER2		'L'		// left-lower corner, single
50 #define	GC_1CORNER3		'F'		// left-upper corner, single
51 
52 #define	GC_1UTEE		0x01		// 'T', with "nose" pointing up
53 #define	GC_1LTEE		0x02		// 'T', with "nose" pointing left
54 #define	GC_1DTEE		0x03		// 'T', with "nose" pointing down
55 #define	GC_1RTEE		0x04		// 'T', with "nose" pointing right
56 
57 #define	GC_1CROSS		0x05		// a cross like in '+', but bigger to fit with other line-drawing chars
58 
59 #define	GC_2VLINE		'H'		// double vertical line
60 #define	GC_2HLINE		'='		// double horizontal line
61 #define	GC_2CORNER0		0x06		// right-upper corner, double
62 #define	GC_2CORNER1		0x07		// right-lower corner, double
63 #define	GC_2CORNER2		0x08		// left-lower corner, double
64 #define	GC_2CORNER3		0x09		// left-upper corner, double
65 
66 #define GC_LOW			0x0a		// regular pattern, density: low
67 #define GC_MEDIUM		0x0b		// regular pattern, density: medium
68 #define GC_HIGH			0x0c		// regular pattern, density: high
69 #define GC_FULL			0x0d		// regular pattern, density: full
70 
71 #define GC_ARROW_UP		'^'		// a filled triangle, points up
72 #define GC_ARROW_DOWN		'v'		// a filled triangle, points down
73 #define GC_ARROW_LEFT		'<'		// a filled triangle, points left
74 #define GC_ARROW_RIGHT		'>'		// a filled triangle, points right
75 
76 #define GC_SMALL_ARROW_UP	'A'		// an arrow up
77 #define GC_SMALL_ARROW_DOWN	'V'		// an arrow down
78 
79 #define GC_FILLED_CIRCLE	'o'		// a filled and centered circle
80 #define GC_FILLED_QUAD		'x'		// a filled and centered quad
81 
82 #define GC_FILLED_UPPER		0x0e		// upper half filled
83 #define GC_FILLED_LOWER		0x0f		// lower half filled
84 
85 /* virtual colors */
86 
87 typedef int vc;
88 
89 #define NUM_VCS				15
90 
91 // real colors
92 #define VC_BLACK			0
93 #define VC_BLUE				1
94 #define VC_GREEN			2
95 #define VC_CYAN				3
96 #define VC_RED				4
97 #define VC_MAGENTA			5
98 #define VC_YELLOW			6
99 #define VC_WHITE			7
100 // functional colors
101 #define VC_TRANSPARENT			8
102 #define VC_LIGHTEN			9
103 #define VC_DARKEN			10
104 #define VC_MONOCHROME			11
105 #define VC_INVERSE			12
106 // like VC_TRANSPARENT, but always change 'this color' if it would equal the 'other color'
107 #define VC_TRANSPARENT_EXCLUSIVE	13
108 // like VC_TRANSPARENT, but always change the 'other color' if it would equal 'this color'
109 #define VC_TRANSPARENT_EXCLUSIVE_DOM	14
110 
111 #define VC_LIGHT(vc) ((vc) | 0x80)
112 
113 #define VC_GET_LIGHT(vc) ((vc) & 0x80)
114 #define VC_GET_BASECOLOR(vc) ((vc) & 0x7f)
115 
116 /* virtual color pairs (fg/bg) */
117 
118 typedef int vcp;
119 
120 #define VCP_INVALID	-1
121 #define VCP(vc_fg, vc_bg) (vcp)((vc_bg) | ((vc_fg)<<8))
122 #define VCP_BACKGROUND(v) ((v) & 0xff)
123 #define VCP_FOREGROUND(v) ((v>>8) & 0xff)
124 
125 vcp mixColors(vcp base, vcp layer);
126 
127 /*
128  *	Display (absolute/screen coordinates)
129  */
130 
131 struct AbstractChar {
132 	Codepage codepage;
133 	uint32 chr;
134 };
135 
136 struct AbstractColoredChar {
137 	Codepage codepage;
138 	vcp color;
139 	uint32 chr;
140 };
141 
142 enum CursorMode { CURSOR_OFF, CURSOR_NORMAL, CURSOR_BOLD };
143 
144 class Display: public Bounds {
145 public:
Display()146 				Display() {};
Display(const Bounds & b)147 				Display(const Bounds &b) : Bounds(b) {};
~Display()148 	virtual			~Display() {};
149 	/* extends Bounds */
150 	virtual	void		assign(int x, int y, int w, int h);
151 	virtual	void		move(int deltax, int deltay);
152 	virtual	void		resize(int deltaw, int deltah);
153 	/* new */
154 	virtual	void		fill(int x, int y, int w, int h, vcp color, char chr, Codepage cp = CP_DEVICE) = 0;
155      		void		fillAll(vcp color, char chr, Codepage cp = CP_DEVICE);
156 	virtual	void		getCursor(int &x, int &y) const = 0;
157 	virtual	CursorMode	getCursorMode() const = 0;
158 	virtual	int		nprint(int x, int y, vcp color, const char *str, int maxstrlen, Codepage cp = CP_DEVICE) = 0;
159 		int		nprintW(int x, int y, vcp color, const AbstractChar *widestr, int maxstrlen);
160 		int		nprintf(int x, int y, vcp color, int maxstrlen, Codepage cp, const char *format, ...);
161 		int		print(int x, int y, vcp color, const char *str, Codepage cp = CP_DEVICE);
162 		int		printW(int x, int y, vcp color, const AbstractChar *widestr);
163 		int		printChar(int x, int y, vcp color, char chr, Codepage cp = CP_DEVICE);
164 		int		printf(int x, int y, vcp color, Codepage cp, const char *format, ...);
165 	virtual	bool		read(uint &rawchar, vcp &color, int x, int y) const = 0;
166 	virtual	void		setBounds(const Bounds &b);
167 	virtual	void		setCursor(int x, int y, CursorMode mode = CURSOR_NORMAL) = 0;
168 	virtual	void		setCursorMode(CursorMode mode = CURSOR_NORMAL) = 0;
169 #if 0
170 	/* graphical extension */
171 	virtual	void		line(int px1, int py1, int px2, int py2, uint color);
172 	virtual	void		putPixel(int px, int py, uint color);
173 	virtual	void		textToPixelCoord(int tx, int ty, int &px, int &py) const;
174 	virtual	void		pixelToTextCoord(int px, int py, int &tx, int &ty) const;
175 #endif
176 };
177 
178 /*
179  *	RDisplay (relative coords)
180  */
181 typedef Display RDisplay;
182 
183 /*
184  *	NullRDisplay
185  */
186 class NullRDisplay: public RDisplay {
187 protected:
188 	uint cursorx, cursory;
189 	CursorMode cursorMode;
190 public:
191 				NullRDisplay(const Bounds &b);
192 	/* extends Display */
193 	virtual	void		fill(int x, int y, int w, int h, vcp color, char chr, Codepage cp = CP_DEVICE);
194 	virtual	void		getCursor(int &x, int &y) const;
195 	virtual	CursorMode	getCursorMode() const;
196 	virtual	int		nprint(int x, int y, vcp color, const char *str, int maxstrlen, Codepage cp = CP_DEVICE);
197 	virtual	bool		read(uint &rawchar, vcp &color, int x, int y) const;
198 	virtual	void		setCursor(int x, int y, CursorMode mode = CURSOR_NORMAL);
199 	virtual	void		setCursorMode(CursorMode mode = CURSOR_NORMAL);
200 };
201 
202 /*
203  *   BufferedRDisplay
204  */
205 struct ColoredChar {
206 	uint rawchar;
207 	vcp color;
208 };
209 
210 class BufferedRDisplay: public RDisplay {
211 protected:
212 	int cursorx, cursory;
213 	CursorMode cursorMode;
214 public:
215 	ColoredChar *buf;
216 
217 				BufferedRDisplay(const Bounds &b);
218 	virtual			~BufferedRDisplay();
219 	/* extends RDisplay */
220 	virtual	void		fill(int x, int y, int w, int h, vcp color, char chr, Codepage cp = CP_DEVICE);
221 	virtual	void		getCursor(int &x, int &y) const;
222 	virtual	CursorMode	getCursorMode() const;
223 	virtual	int		nprint(int x, int y, vcp color, const char *str, int maxstrlen, Codepage cp = CP_DEVICE);
224 	virtual	bool		read(uint &rawchar, vcp &color, int x, int y) const;
225 	virtual	void		setBounds(const Bounds &b);
226 	virtual	void		setCursor(int x, int y, CursorMode mode = CURSOR_NORMAL);
227 	virtual	void		setCursorMode(CursorMode mode = CURSOR_NORMAL);
228 };
229 
230 /* system-dependant (implementation in $MYSYSTEM/ *.cc) */
231 uint	mapCharToSystemCP(char chr, Codepage cp);
232 bool	sys_get_screen_size(int &w, int &h);
233 bool	sys_set_screen_size(int w, int h);
234 bool	sys_get_winch_flag();
235 void	sys_set_winch_flag(bool f);
236 
237 class SystemDisplay: public Display {
238 public:
239 				SystemDisplay();
240 	/* new */
241 	virtual	void		copyFromDisplay(const Display &display, int x, int y, const Bounds &clipping) = 0;
242 	virtual	void		show() = 0;
243 };
244 
245 SystemDisplay *allocSystemDisplay(const char *title);
246 
247 /*
248  *   SystemRDisplay
249  */
250 class SystemRDisplay: public RDisplay {
251 public:
252 	SystemDisplay *system_display;
253 
254 				SystemRDisplay(SystemDisplay *system_display, const Bounds &b);
255 	virtual			~SystemRDisplay();
256 	/* extends Display */
257 	virtual	void		fill(int x, int y, int w, int h, vcp color, char chr, Codepage cp = CP_DEVICE);
258 	virtual	void		getCursor(int &x, int &y) const;
259 	virtual	CursorMode	getCursorMode() const;
260 	virtual	int		nprint(int x, int y, vcp color, const char *str, int maxstrlen, Codepage cp = CP_DEVICE);
261 	virtual	bool		read(uint &rawchar, vcp &color, int x, int y) const;
262 	virtual	void		setBounds(const Bounds &b);
263 	virtual	void		setCursor(int x, int y, CursorMode mode = CURSOR_NORMAL);
264 	virtual	void		setCursorMode(CursorMode mode = CURSOR_NORMAL);
265 };
266 
267 #endif /* __DISPLAY_H__ */
268