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