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