xref: /haiku/src/apps/terminal/TermView.h (revision b0944c78b074a8110bd98e060415d0e8f38a7f65)
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				_UpdateModifiers();
255 
256 			void				_NextState(State* state);
257 
258 private:
259 			Listener*			fListener;
260 			Shell*				fShell;
261 
262 			BMessageRunner*		fWinchRunner;
263 			BMessageRunner*		fCursorBlinkRunner;
264 			BMessageRunner*		fAutoScrollRunner;
265 			BMessageRunner*		fResizeRunner;
266 			BStringView*		fResizeView;
267 			DefaultCharClassifier* fCharClassifier;
268 
269 			// Font and Width
270 			BFont				fHalfFont;
271 			BFont				fBoldFont;
272 			int					fFontWidth;
273 			int					fFontHeight;
274 			int					fFontAscent;
275 			struct escapement_delta fEscapement;
276 			bool				fEmulateBold;
277 
278 			// frame resized flag.
279 			bool				fFrameResized;
280 			int32				fResizeViewDisableCount;
281 
282 			// Cursor Blinking, draw flag.
283 			bigtime_t			fLastActivityTime;
284 			int32				fCursorState;
285 			int					fCursorStyle;
286 			bool				fCursorBlinking;
287 			bool				fCursorHidden;
288 
289 			// Cursor position.
290 			TermPos				fCursor;
291 
292 			// Terminal rows and columns.
293 			int					fColumns;
294 			int					fRows;
295 
296 			int					fEncoding;
297 			bool				fActive;
298 
299 			// Object pointer.
300 			TerminalBuffer*		fTextBuffer;
301 			BasicTerminalBuffer* fVisibleTextBuffer;
302 			bool				fVisibleTextBufferChanged;
303 			BScrollBar*			fScrollBar;
304 			InlineInput*		fInline;
305 
306 			// Color and Attribute.
307 			rgb_color			fTextForeColor;
308 			rgb_color			fTextBackColor;
309 			rgb_color			fCursorForeColor;
310 			rgb_color			fCursorBackColor;
311 			rgb_color			fSelectForeColor;
312 			rgb_color			fSelectBackColor;
313 
314 			// Scroll Region
315 			float				fScrollOffset;
316 			int32				fScrBufSize;
317 				// TODO: That's the history capacity -- only needed
318 				// until the text buffer is created.
319 			float				fAutoScrollSpeed;
320 
321 			// redraw management
322 			bigtime_t			fLastSyncTime;
323 			int32				fScrolledSinceLastSync;
324 			BMessageRunner*		fSyncRunner;
325 			bool				fConsiderClockedSync;
326 
327 			// selection
328 			Highlight			fSelection;
329 			TermPos				fInitialSelectionStart;
330 			TermPos				fInitialSelectionEnd;
331 			BPoint				fLastClickPoint;
332 
333 			HighlightList		fHighlights;
334 
335 			// mouse
336 			int32				fMouseButtons;
337 			int32				fModifiers;
338 			TermPos				fPrevPos;
339 			bool				fReportX10MouseEvent;
340 			bool				fReportNormalMouseEvent;
341 			bool				fReportButtonMouseEvent;
342 			bool				fReportAnyMouseEvent;
343 			BClipboard*			fMouseClipboard;
344 
345 			// states
346 			DefaultState*		fDefaultState;
347 			SelectState*		fSelectState;
348 			HyperLinkState*		fHyperLinkState;
349 			HyperLinkMenuState*	fHyperLinkMenuState;
350 			State*				fActiveState;
351 };
352 
353 
354 class TermView::Listener {
355 public:
356 	virtual						~Listener();
357 
358 	// all hooks called in the window thread
359 	virtual	void				NotifyTermViewQuit(TermView* view,
360 									int32 reason);
361 	virtual	void				SetTermViewTitle(TermView* view,
362 									const char* title);
363 	virtual	void				PreviousTermView(TermView* view);
364 	virtual	void				NextTermView(TermView* view);
365 };
366 
367 
368 #endif // TERMVIEW_H
369