1 /*
2 	miscobj.h	Various NCurses C++ Classes Header
3 	Copyright (c) 1996-8,2000,2007 Kriang Lerdsuwanakij
4 	email:		lerdsuwa@users.sourceforge.net
5 
6 	This program is free software; you can redistribute it and/or modify
7 	it under the terms of the GNU General Public License as published by
8 	the Free Software Foundation; either version 2 of the License, or
9 	(at your option) any later version.
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 __K_MISCOBJ_H
22 #define __K_MISCOBJ_H
23 
24 #include "config.h"
25 #include "setupcurses.h"
26 #include "cxxlib.h"
27 
28 USING_NAMESPACE_STD;
29 
30 /*************************************************************************
31 	ACS fallback support
32 *************************************************************************/
33 
34 enum	entity_id {
35 	EID_LT = 0, EID_GT, EID_AMP, EID_QUOT,
36 	EID_BOXUR, EID_BOXDR, EID_BOXDL, EID_BOXUL,
37 	EID_BOXVL, EID_BOXVR, EID_BOXHU, EID_BOXHD, EID_BOXH, EID_BOXV,
38 	EID_RARR, EID_LARR, EID_DARR, EID_UARR,
39 	EID_DEGREE, EID_PLMINUS, EID_LEQUAL, EID_GEQUAL, EID_NEQUAL,
40 	EID_BULLET, EID_DIAMOND,
41 	EID_CKBOARD, EID_BOARD, EID_LANTERN, EID_BLOCK,
42 	EID_PI, EID_STERLING, EID_S1, EID_S9, EID_S3, EID_S7,
43 	EID_SPACE };
44 
45 struct	entity_info {
46 	const char	*name;
47 	size_t		length;
48 	chtype		code;
49 	wchar_t		wc;
50 };
51 
52 extern entity_info entity_table[];
53 
54 void	init_entity_table();
55 chtype	get_entity(entity_id id);
56 wchar_t	get_wc_entity(entity_id id);
57 
58 void	draw_entity(WINDOW *win, entity_id id);
59 
60 extern bool	isACSFallBack;
61 
62 /*************************************************************************
63 	Unicode output support
64 *************************************************************************/
65 
66 int	k_waddch_int(WINDOW *win, const char *&s, int max_width = -1);
67 int	k_waddch(WINDOW *win, const char *s, int max_width = -1);
68 int	k_waddstr(WINDOW *win, const char *s);
69 int	k_waddnstr(WINDOW *win, const char *s, int max_width);
70 
k_waddstr(WINDOW * win,const string & s)71 inline int	k_waddstr(WINDOW *win, const string &s)
72 {
73 	return k_waddstr(win, s.c_str());
74 }
75 
k_waddnstr(WINDOW * win,const string & s,int max_width)76 inline int	k_waddnstr(WINDOW *win, const string &s, int max_width)
77 {
78 	return k_waddnstr(win, s.c_str(), max_width);
79 }
80 
81 /*************************************************************************
82 	HTML anchor class
83 *************************************************************************/
84 
85 class Anchor {
86 	public:
87 		string	name;
88 		string	linkText;
89 		int	rowFrom, colFrom, rowTo, colTo;
90 		int	type;
91 
Anchor(const char * str)92 		Anchor(const char *str) : name(str), linkText("") {}
Anchor(const char * str,int len)93 		Anchor(const char *str, int len) : name(str, len), linkText("") {}
Anchor(const string & str)94 		Anchor(const string &str) : name(str), linkText("") {}
95 		Anchor(const char *str, int len, int type_,
96 			int rowFrom_ = -1, int colFrom_ = -1,
97 			int rowTo_ = -1, int colTo_ = -1)
name(str,len)98 				: name(str, len), linkText(""),
99 				  rowFrom(rowFrom_), colFrom(colFrom_),
100 		  		  rowTo(rowTo_), colTo(colTo_), type(type_) {}
~Anchor()101 		~Anchor() {}
102 };
103 
104 /*************************************************************************
105 	Rectangle class
106 *************************************************************************/
107 
108 class	NCRect {
109 	private:
110 		int	x1, y1, x2, y2, row, col;
111 	public:
GetX1()112 		int	GetX1()		{ return x1; }
GetY1()113 		int	GetY1()		{ return y1; }
GetX2()114 		int	GetX2()		{ return x2; }
GetY2()115 		int	GetY2()		{ return y2; }
GetX()116 		int	GetX()		{ return x1; }
GetY()117 		int	GetY()		{ return y1; }
GetRow()118 		int	GetRow()	{ return row; }
GetCol()119 		int	GetCol()	{ return col; }
120 
SetX1(int x1_)121 		void	SetX1(int x1_)	{ x1 = x1_; col = x2-x1+1; }
SetY1(int y1_)122 		void	SetY1(int y1_)	{ y1 = y1_; row = y2-y1+1; }
SetX2(int x2_)123 		void	SetX2(int x2_)	{ x2 = x2_; col = x2-x1+1; }
SetY2(int y2_)124 		void	SetY2(int y2_)	{ y2 = y2_; row = y2-y1+1; }
SetX(int x_)125 		void	SetX(int x_)	{ x1 = x_; col = x2-x1+1; }
SetY(int y_)126 		void	SetY(int y_)	{ y1 = y_; row = y2-y1+1; }
SetRow(int row_)127 		void	SetRow(int row_){ row = row_; y2 = y1+row-1; }
SetCol(int col_)128 		void	SetCol(int col_){ col = col_; x2 = x1+col-1; }
129 
NCRect()130 		NCRect()		{ x1 = y1 = x2 = y2 = row = col = 0; }
NCRect(int x1_,int y1_,int x2_,int y2_)131 		NCRect(int x1_, int y1_, int x2_, int y2_)
132 				{ x1 = x1_; y1 = y1_; x2 = x2_; y2 = y2_;
133 				  row = y2-y1+1; col = x2-x1+1; }
134 
135 		// NCRect(const NCRect &) automatically generated by compiler
136 };
137 
IsRectXIntersect(NCRect & rect1,NCRect & rect2)138 inline int	IsRectXIntersect(NCRect &rect1, NCRect &rect2)
139 {
140 	return Max(rect1.GetX1(), rect2.GetX2()) <= Min(rect1.GetX1(), rect2.GetX2());
141 }
142 
IsRectYIntersect(NCRect & rect1,NCRect & rect2)143 inline int	IsRectYIntersect(NCRect &rect1, NCRect &rect2)
144 {
145 	return Max(rect1.GetY1(), rect2.GetY2()) <= Min(rect1.GetY1(), rect2.GetY2());
146 }
147 
IsRectIntersect(NCRect & rect1,NCRect & rect2)148 inline int	IsRectIntersect(NCRect &rect1, NCRect &rect2)
149 {
150 	return IsRectXIntersect(rect1, rect2) && IsRectYIntersect(rect1, rect2);
151 }
152 
153 /*************************************************************************
154 	Cursor state class
155 *************************************************************************/
156 
157 class	CursorState {
158 	private:
159 		int	isSave;
160 		int	oldCursor;
161 	public:
CursorState()162 		CursorState()	{ isSave = 0; }
163 		void	Hide();
164 		void	Show();
165 		void	Restore();
166 };
167 
168 /*************************************************************************
169 	ncurses screen setup and clean up class
170 *************************************************************************/
171 
172 typedef void (*notifyFuncType)();
173 
174 class	NCurses {
175 	protected:
176 		static int	isInitScr;
177 		static int	isInitScrMore;
178 
179 		static int	isRedirect;
180 
181 		static notifyFuncType	notifySize;
182 
183 		static void	CheckTerminal(int exitIfRedir = 1);
184 		static void	EventSIGINT(int /*sig*/);
185 		static void	EventSIGWINCH(int /*sig*/);
186 
187 	public:
188 		static void	Init(int exitIfRedir = 1);
189 		static void	InitMore(WINDOW *win);
190 		static void	SetNotify(notifyFuncType func);
191 		static void	End();
IsRedirect()192 		static int	IsRedirect() { return isRedirect; }
IsInitScr()193 		static int	IsInitScr() { return isInitScr; }
194 };
195 
196 #endif	/* __K_MISCOBJ_H */
197