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