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