xref: /haiku/src/apps/terminal/TermView.h (revision 0dfceb2b8af9cd27312407bf35879ae38980a664)
1 /*
2  * Copyright 2001-2009, 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  * Distributed under the terms of the MIT license.
7  * Authors:
8  *		Stefano Ceccherini <stefano.ceccherini@gmail.com>
9  *		Kian Duffy, myob@users.sourceforge.net
10  */
11 #ifndef TERMVIEW_H
12 #define TERMVIEW_H
13 
14 #include <Autolock.h>
15 #include <Messenger.h>
16 #include <String.h>
17 #include <View.h>
18 
19 #include "TermPos.h"
20 
21 
22 class BClipboard;
23 class BMessageRunner;
24 class BScrollBar;
25 class BScrollView;
26 class BString;
27 class BStringView;
28 class BasicTerminalBuffer;
29 class TermBuffer;
30 class TerminalBuffer;
31 class ResizeWindow;
32 class Shell;
33 
34 class TermView : public BView {
35 public:
36 							TermView(BRect frame, int32 argc, const char** argv,
37 								int32 historySize);
38 							TermView(int rows, int columns, int32 argc,
39 								const char** argv, int32 historySize);
40 							TermView(BMessage* archive);
41 							~TermView();
42 
43 	static	BArchivable*	Instantiate(BMessage* data);
44 	virtual status_t		Archive(BMessage* data, bool deep = true) const;
45 
46 	virtual void			GetPreferredSize(float* _width, float* _height);
47 
48 			const char*		TerminalName() const;
49 
50 	inline	TerminalBuffer* TextBuffer() const	{ return fTextBuffer; }
51 
52 			void			GetTermFont(BFont* font) const;
53 			void			SetTermFont(const BFont* font);
54 
55 			void			GetFontSize(int* width, int* height);
56 			int				Rows() const;
57 			int				Columns() const;
58 			BRect			SetTermSize(int rows, int cols);
59 			void			SetTermSize(BRect rect);
60 			void			GetTermSizeFromRect(const BRect &rect,
61 								int *rows, int *columns);
62 
63 			void			SetTextColor(rgb_color fore, rgb_color back);
64 			void			SetSelectColor(rgb_color fore, rgb_color back);
65 			void			SetCursorColor(rgb_color fore, rgb_color back);
66 
67 			int				Encoding() const;
68 			void			SetEncoding(int encoding);
69 
70 			void			SetScrollBar(BScrollBar* scrollBar);
71 			BScrollBar*		ScrollBar() const { return fScrollBar; };
72 
73 			void			SetMouseClipboard(BClipboard *);
74 
75 	virtual void			SetTitle(const char* title);
76 	virtual void			NotifyQuit(int32 reason);
77 
78 			// edit functions
79 			void			Copy(BClipboard* clipboard);
80 			void			Paste(BClipboard* clipboard);
81 			void			SelectAll();
82 			void			Clear();
83 
84 			// Other
85 			void			GetFrameSize(float* width, float* height);
86 			bool			Find(const BString& str, bool forwardSearch,
87 								bool matchCase, bool matchWord);
88 			void			GetSelection(BString& string);
89 
90 			void			CheckShellGone();
91 
92 			void			InitiateDrag();
93 
94 			void			DisableResizeView(int32 disableCount = 1);
95 	static	void			AboutRequested();
96 
97 protected:
98 	virtual void			AttachedToWindow();
99 	virtual void			DetachedFromWindow();
100 	virtual void			Draw(BRect updateRect);
101 	virtual void			WindowActivated(bool active);
102 	virtual void			MakeFocus(bool focusState = true);
103 	virtual void			KeyDown(const char* bytes, int32 numBytes);
104 
105 	virtual void			MouseDown(BPoint where);
106 	virtual void			MouseMoved(BPoint where, uint32 transit,
107 								const BMessage* message);
108 	virtual void			MouseUp(BPoint where);
109 
110 	virtual void			FrameResized(float width, float height);
111 	virtual void			MessageReceived(BMessage* message);
112 
113 	virtual void			ScrollTo(BPoint where);
114 	virtual void			TargetedByScrollView(BScrollView *scrollView);
115 
116 	virtual status_t		GetSupportedSuites(BMessage* msg);
117 	virtual BHandler*		ResolveSpecifier(BMessage* msg, int32 index,
118 								BMessage* specifier, int32 form,
119 								const char* property);
120 
121 private:
122 			// point and text offset conversion
123 	inline	int32			_LineAt(float y);
124 	inline	float			_LineOffset(int32 index);
125 	inline	TermPos			_ConvertToTerminal(const BPoint& point);
126 	inline	BPoint			_ConvertFromTerminal(const TermPos& pos);
127 
128 	inline	void			_InvalidateTextRect(int32 x1, int32 y1, int32 x2,
129 								int32 y2);
130 
131 			status_t		_InitObject(int32 argc, const char** argv);
132 
133 			status_t		_AttachShell(Shell* shell);
134 			void			_DetachShell();
135 
136 			void			_Activate();
137 			void			_Deactivate();
138 
139 			void			_DrawLinePart(int32 x1, int32 y1, uint16 attr,
140 								char* buffer, int32 width, bool mouse,
141 								bool cursor, BView* inView);
142 			void			_DrawCursor();
143 			void			_InvalidateTextRange(TermPos start, TermPos end);
144 
145 			bool			_IsCursorVisible() const;
146 			void			_BlinkCursor();
147 			void			_ActivateCursor(bool invalidate);
148 
149 			void			_DoPrint(BRect updateRect);
150 			void			_UpdateScrollBarRange();
151 			void			_SecondaryMouseButtonDropped(BMessage* msg);
152 			void			_DoSecondaryMouseDropAction(BMessage* msg);
153 			void			_DoFileDrop(entry_ref &ref);
154 
155 			void			_SynchronizeWithTextBuffer(int32 visibleDirtyTop,
156 								int32 visibleDirtyBottom);
157 
158 			void			_WritePTY(const char* text, int32 numBytes);
159 
160 			// Comunicate Input Method
161 			//  void _DoIMStart (BMessage* message);
162 			//  void _DoIMStop (BMessage* message);
163 			//  void _DoIMChange (BMessage* message);
164 			//  void _DoIMLocation (BMessage* message);
165 			//  void _DoIMConfirm (void);
166 			//	void _ConfirmString(const char *, int32);
167 
168 			// selection
169 			float			_MouseDistanceSinceLastClick(BPoint where);
170 			void			_Select(TermPos start, TermPos end, bool inclusive,
171 								bool setInitialSelection);
172 			void			_ExtendSelection(TermPos, bool inclusive,
173 								bool useInitialSelection);
174 			void			_Deselect();
175 			bool			_HasSelection() const;
176 			void			_SelectWord(BPoint where, bool extend,
177 								bool useInitialSelection);
178 			void			_SelectLine(BPoint where, bool extend,
179 								bool useInitialSelection);
180 
181 			void			_AutoScrollUpdate();
182 
183 			bool			_CheckSelectedRegion(const TermPos& pos) const;
184 			bool			_CheckSelectedRegion(int32 row, int32 firstColumn,
185 								int32& lastColumn) const;
186 
187 			void			_UpdateSIGWINCH();
188 
189 			void			_ScrollTo(float y, bool scrollGfx);
190 			void			_ScrollToRange(TermPos start, TermPos end);
191 
192 			void			_SendMouseEvent(int32 button, int32 mode, int32 x,
193 								int32 y, bool motion);
194 private:
195 	class CharClassifier;
196 
197 			Shell*			fShell;
198 
199 			BMessageRunner*	fWinchRunner;
200 			BMessageRunner*	fCursorBlinkRunner;
201 			BMessageRunner*	fAutoScrollRunner;
202 			BMessageRunner*	fResizeRunner;
203 			BStringView*	fResizeView;
204 			CharClassifier*	fCharClassifier;
205 
206 			// Font and Width
207 			BFont			fHalfFont;
208 			int				fFontWidth;
209 			int				fFontHeight;
210 			int				fFontAscent;
211 			struct escapement_delta fEscapement;
212 
213 			// frame resized flag.
214 			bool			fFrameResized;
215 			int32			fResizeViewDisableCount;
216 
217 			// Cursor Blinking, draw flag.
218 			bigtime_t		fLastActivityTime;
219 			int32			fCursorState;
220 			int				fCursorHeight;
221 
222 			// Cursor position.
223 			TermPos			fCursor;
224 
225 			int32			fMouseButtons;
226 
227 			// Terminal rows and columns.
228 			int				fColumns;
229 			int				fRows;
230 
231 			int				fEncoding;
232 			bool				fActive;
233 			// Object pointer.
234 			TerminalBuffer*	fTextBuffer;
235 			BasicTerminalBuffer* fVisibleTextBuffer;
236 			BScrollBar*		fScrollBar;
237 
238 			// Color and Attribute.
239 			rgb_color		fTextForeColor;
240 			rgb_color		fTextBackColor;
241 			rgb_color		fCursorForeColor;
242 			rgb_color		fCursorBackColor;
243 			rgb_color		fSelectForeColor;
244 			rgb_color		fSelectBackColor;
245 
246 			// Scroll Region
247 			float			fScrollOffset;
248 			int32			fScrBufSize;
249 				// TODO: That's the history capacity -- only needed
250 				// until the text buffer is created.
251 			float			fAutoScrollSpeed;
252 
253 			// redraw management
254 			bigtime_t		fLastSyncTime;
255 			int32			fScrolledSinceLastSync;
256 			BMessageRunner*	fSyncRunner;
257 			bool			fConsiderClockedSync;
258 
259 			// selection
260 			TermPos			fSelStart;
261 			TermPos			fSelEnd;
262 			TermPos			fInitialSelectionStart;
263 			TermPos			fInitialSelectionEnd;
264 			bool			fMouseTracking;
265 			bool			fCheckMouseTracking;
266 			int				fSelectGranularity;
267 			BPoint			fLastClickPoint;
268 
269 			// mouse
270 			TermPos			fPrevPos;
271 			bool			fReportX10MouseEvent;
272 			bool			fReportNormalMouseEvent;
273 			bool			fReportButtonMouseEvent;
274 			bool			fReportAnyMouseEvent;
275 			BClipboard*		fMouseClipboard;
276 
277 			// Input Method parameter.
278 			int				fIMViewPtr;
279 			TermPos			fIMStartPos;
280 			TermPos			fIMEndPos;
281 			BString			fIMString;
282 			bool			fIMflag;
283 			BMessenger		fIMMessenger;
284 			int32			fImCodeState;
285 };
286 
287 
288 #endif // TERMVIEW_H
289