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