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