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