xref: /haiku/src/apps/terminal/TermView.h (revision 5115ca085884f7b604a3d607688f0ca20fb7cf57)
1 /*
2  * Copyright 2001-2007, Haiku.
3  * Copyright (c) 2003-4 Kian Duffy <myob@users.sourceforge.net>
4  * Parts Copyright (C) 1998,99 Kazuho Okui and Takashi Murai.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files or portions
8  * thereof (the "Software"), to deal in the Software without restriction,
9  * including without limitation the rights to use, copy, modify, merge,
10  * publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject
12  * to the following conditions:
13  *
14  *  * Redistributions of source code must retain the above copyright notice,
15  *    this list of conditions and the following disclaimer.
16  *
17  *  * Redistributions in binary form must reproduce the above copyright notice
18  *    in the  binary, as well as this list of conditions and the following
19  *    disclaimer in the documentation and/or other materials provided with
20  *    the distribution.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28  * THE SOFTWARE.
29  *
30  */
31 #ifndef TERMVIEW_H
32 #define TERMVIEW_H
33 
34 
35 #include "CurPos.h"
36 #include "TermConst.h"
37 #include "TermWindow.h"
38 
39 #include <View.h>
40 #include <String.h>
41 #include <MessageRunner.h>
42 
43 /* Cursor Blinking flag */
44 #define CUROFF 0
45 #define CURON  1
46 
47 #define VIEW_THR_CODE 'vtcd'
48 #define MOUSE_THR_CODE 'mtcd'
49 #define RECT_BUF_SIZE 32
50 
51 const unsigned char M_ADD_CURSOR [] = {
52 	16,	// Size
53 	1,	// Color depth
54 	0,	// Hot spot y
55 	1,	// Hot spot x
56 
57 	// Cursor image
58 	0x70, 0x00,
59 	0x48, 0x00,
60 	0x48, 0x00,
61 	0x27, 0xc0,
62 	0x24, 0xb8,
63 	0x12, 0x54,
64 	0x10, 0x02,
65 	0x78, 0x02,
66 	0x98, 0x02,
67 	0x84, 0x02,
68 	0x60, 0x02,
69 	0x18, 0x12,
70 	0x04, 0x10,
71 	0x02, 0x7c,
72 	0x00, 0x10,
73 	0x00, 0x10,
74 	// Mask image
75 	0x70, 0x00,
76 	0x78, 0x00,
77 	0x78, 0x00,
78 	0x3f, 0xc0,
79 	0x3f, 0xf8,
80 	0x1f, 0xfc,
81 	0x1f, 0xfe,
82 	0x7f, 0xfe,
83 	0xff, 0xfe,
84 	0xff, 0xfe,
85 	0x7f, 0xfe,
86 	0x1f, 0xfe,
87 	0x07, 0xfc,
88 	0x03, 0xfc,
89 	0x00, 0x10,
90 	0x00, 0x10,
91 };
92 
93 class TermBuffer;
94 class CodeConv;
95 class BPopUpMenu;
96 class BScrollBar;
97 class BString;
98 
99 class TermView : public BView {
100 	public:
101 		TermView(BRect frame, CodeConv *inCodeConv, int fd);
102 		~TermView();
103 
104 		void	SetTermFont(const BFont *halfFont, const BFont *fullFont);
105 		void	GetFontSize(int *width, int *height);
106 		BRect	SetTermSize(int rows, int cols, bool flag);
107 		void	SetTermColor();
108 		void	SetMouseCursor();
109 		// void  SetIMAware (bool);
110 		void	SetScrollBar(BScrollBar *scrbar);
111 		BScrollBar  *ScrollBar() const { return fScrollBar; };
112 
113 		// Output Charactor
114 		void	PutChar(uchar *string, ushort attr, int width);
115 		void	PutCR(void);
116 		void	PutLF(void);
117 		void	PutNL(int num);
118 		void	SetInsertMode(int flag);
119 		void	InsertSpace(int num);
120 
121 		int		TermDraw(const CurPos &start, const CurPos &end);
122 		int		TermDrawRegion(CurPos start, CurPos end);
123 		int		TermDrawSelectedRegion(CurPos start, CurPos end);
124 
125 		// Delete Charactor
126 		void	EraseBelow();
127 		void	DeleteChar(int num);
128 		void	DeleteColumns();
129 		void	DeleteLine(int num);
130 
131 		// Get and Set Cursor position
132 		void	SetCurPos(int x, int y);
133 		void	SetCurX(int x);
134 		void	SetCurY(int y);
135 
136 		void	GetCurPos(CurPos *inCurPos);
137 		int		GetCurX();
138 		int		GetCurY();
139 
140 		void	SaveCursor();
141 		void	RestoreCursor();
142 
143 		// Move Cursor
144 		void	MoveCurRight(int num);
145 		void	MoveCurLeft(int num);
146 		void	MoveCurUp(int num);
147 		void	MoveCurDown(int num);
148 
149 		// Cursor setting
150 		void	DrawCursor();
151 		void	BlinkCursor();
152 		void	SetCurDraw(bool flag);
153 		void	SetCurBlinking(bool flag);
154 
155 		// Scroll region
156 		void	ScrollRegion(int top, int bot, int dir, int num);
157 		void	SetScrollRegion(int top, int bot);
158 		void	ScrollAtCursor();
159 
160 		// Other
161 		void	UpdateSIGWINCH();
162 		void	DeviceStatusReport(int);
163 		void	UpdateLine();
164 		void	ScrollScreen();
165 		void	ScrollScreenDraw();
166 		void	GetFrameSize(float *width, float *height);
167 		void	GetFontInfo(int *, int*);
168 		bool	Find(const BString &str, bool forwardSearch, bool matchCase, bool matchWord);
169 		void	GetSelection(BString &str);
170 
171 	protected:
172 		virtual void	AttachedToWindow(void);
173 		virtual void	Draw(BRect updateRect);
174 		virtual void	WindowActivated(bool active);
175 		virtual void	KeyDown(const char*, int32);
176 		virtual void	MouseDown(BPoint where);
177 		virtual void	MouseMoved(BPoint, uint32, const BMessage *);
178 
179 		virtual void	FrameResized(float width, float height);
180 		virtual void	MessageReceived(BMessage* message);
181 
182 	private:
183 		static int32	ViewThread(void *);
184 		static int32	MouseTracking(void *);
185 
186 		thread_id		_InitViewThread(void);
187 		void DrawLines(int , int, ushort, uchar *, int, int, int, BView *);
188 		void DoPrint(BRect updateRect);
189 		void ResizeScrBarRange (void);
190 		void DoFileDrop(entry_ref &ref);
191 
192 		// edit menu function.
193 		void DoCopy();
194 		void DoPaste();
195 		void DoSelectAll();
196 		void DoClearAll();
197 
198 		void WritePTY (const uchar *text, int num_byteses);
199 
200 		// Comunicate Input Method
201 		//  void DoIMStart (BMessage* message);
202 		//  void DoIMStop (BMessage* message);
203 		//  void DoIMChange (BMessage* message);
204 		//  void DoIMLocation (BMessage* message);
205 		//  void DoIMConfirm (void);
206 		void ConfirmString (const char *, int32);
207 		int32 GetCharFromUTF8String (const char *, char *);
208 		int32 GetWidthFromUTF8String (const char *);
209 
210 		// Mouse select
211 		void  Select(CurPos start, CurPos end);
212 		void  AddSelectRegion(CurPos);
213 		void  ResizeSelectRegion(CurPos);
214 
215 		void  DeSelect();
216 
217 		// select word function
218 		void  SelectWord(BPoint where, int mod);
219 		void  SelectLine(BPoint where, int mod);
220 
221 		// point and text offset conversion.
222 		CurPos  BPointToCurPos(const BPoint &p);
223 		BPoint  CurPosToBPoint(const CurPos &pos);
224 
225 		bool	CheckSelectedRegion(const CurPos &pos);
226 		inline void SendDataToDrawEngine(int, int, int, int);
227 
228 		int fTerminalFd;
229 
230 		// Font and Width
231 		BFont fHalfFont;
232 		BFont fFullFont;
233 		int fFontWidth;
234 		int fFontHeight;
235 		int fFontAscent;
236 		struct escapement_delta fEscapement;
237 
238 		// Flags
239 
240 		// Update flag (Set on PutChar).
241 		bool fUpdateFlag;
242 
243 		// Terminal insertmode flag (use PutChar).
244 		bool fInsertModeFlag;
245 
246 		// Scroll count, range.
247 		int fScrollUpCount;
248 		int fScrollBarRange;
249 
250 		// Frame Resized flag.
251 		bool fFrameResized;
252 
253 		// Cursor Blinking, draw flag.
254 		bool fCursorDrawFlag;
255 		bool fCursorStatus;
256 		bool fCursorBlinkingFlag;
257 		bool fCursorRedrawFlag;
258 
259 		int fCursorHeight;
260 
261 		// terminal text attribute flag.
262 		bool fInverseFlag;
263 		bool fBoldFlag;
264 		bool fUnderlineFlag;
265 
266 		// Cursor position.
267 		CurPos fCurPos;
268 		CurPos fCurStack;
269 
270 		int fBufferStartPos;
271 
272 		// Terminal rows and columns.
273 		int fTermRows;
274 		int fTermColumns;
275 
276 		// Terminal view pointer.
277 		int fTop;
278 
279 		// Object pointer.
280 
281 		TermBuffer	*fTextBuffer;
282 		CodeConv	*fCodeConv;
283 		BScrollBar	*fScrollBar;
284 
285 		// Offscreen Bitmap and View.
286 
287 		BRect fSrcRect;
288 		BRect fDstRect;
289 
290 		// Color and Attribute.
291 
292 		rgb_color fTextForeColor, fTextBackColor;
293 		rgb_color fCursorForeColor, fCursorBackColor;
294 		rgb_color fSelectForeColor, fSelectBackColor;
295 		uchar fTermAttr;
296 
297 		// Scroll Region
298 		int fScrTop;
299 		int fScrBot;
300 		int fScrBufSize;
301 		bool fScrRegionSet;
302 
303 		bool	fMouseImage;
304 
305 		BPoint fPreviousMousePoint;
306 
307 		// view selection
308 		CurPos fSelStart;
309 		CurPos fSelEnd;
310 		bool fSelected;
311 		bool fMouseTracking;
312 
313 		// thread ID / flags.
314 		thread_id fViewThread;
315 		thread_id fMouseThread;
316 		bool fQuitting;
317 
318 		// DrawEngine parameter.
319 		sem_id fDrawRectSem;
320 		sDrawRect fDrawRectBuffer[RECT_BUF_SIZE];
321 		int fDrawRect_p;
322 
323 		// Input Method parameter.
324 		int fIMViewPtr;
325 		CurPos fIMStartPos;
326 		CurPos fIMEndPos;
327 		BString fIMString;
328 		bool fIMflag;
329 		BMessenger fIMMessenger;
330 
331 		int32 fImCodeState;
332 };
333 
334 
335 #endif //TERMVIEW_H
336