xref: /haiku/src/apps/terminal/TermView.h (revision c90684742e7361651849be4116d0e5de3a817194)
1 /*
2  * Copyright 2001-2009, Haiku.
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  */
11 #ifndef TERMVIEW_H
12 #define TERMVIEW_H
13 
14 #include <Autolock.h>
15 #include <Messenger.h>
16 #include <String.h>
17 #include <View.h>
18 
19 #include "TermPos.h"
20 
21 
22 class BClipboard;
23 class BMessageRunner;
24 class BScrollBar;
25 class BScrollView;
26 class BString;
27 class BStringView;
28 class BasicTerminalBuffer;
29 class InlineInput;
30 class ResizeWindow;
31 class TermBuffer;
32 class TerminalBuffer;
33 class Shell;
34 
35 class TermView : public BView {
36 public:
37 							TermView(BRect frame, int32 argc, const char** argv,
38 								int32 historySize);
39 							TermView(int rows, int columns, int32 argc,
40 								const char** argv, int32 historySize);
41 							TermView(BMessage* archive);
42 							~TermView();
43 
44 	static	BArchivable*	Instantiate(BMessage* data);
45 	virtual status_t		Archive(BMessage* data, bool deep = true) const;
46 
47 	virtual void			GetPreferredSize(float* _width, float* _height);
48 
49 			bool			IsShellBusy() const;
50 
51 			const char*		TerminalName() const;
52 
53 	inline	TerminalBuffer* TextBuffer() const	{ return fTextBuffer; }
54 
55 			void			GetTermFont(BFont* font) const;
56 			void			SetTermFont(const BFont* font);
57 
58 			void			GetFontSize(int* width, int* height);
59 			int				Rows() const;
60 			int				Columns() const;
61 			BRect			SetTermSize(int rows, int cols);
62 			void			SetTermSize(BRect rect);
63 			void			GetTermSizeFromRect(const BRect &rect,
64 								int *rows, int *columns);
65 
66 			void			SetTextColor(rgb_color fore, rgb_color back);
67 			void			SetSelectColor(rgb_color fore, rgb_color back);
68 			void			SetCursorColor(rgb_color fore, rgb_color back);
69 
70 			int				Encoding() const;
71 			void			SetEncoding(int encoding);
72 
73 			void			SetScrollBar(BScrollBar* scrollBar);
74 			BScrollBar*		ScrollBar() const { return fScrollBar; };
75 
76 			void			SetMouseClipboard(BClipboard *);
77 
78 	virtual void			SetTitle(const char* title);
79 	virtual void			NotifyQuit(int32 reason);
80 
81 			// edit functions
82 			void			Copy(BClipboard* clipboard);
83 			void			Paste(BClipboard* clipboard);
84 			void			SelectAll();
85 			void			Clear();
86 
87 			// Other
88 			void			GetFrameSize(float* width, float* height);
89 			bool			Find(const BString& str, bool forwardSearch,
90 								bool matchCase, bool matchWord);
91 			void			GetSelection(BString& string);
92 
93 			void			CheckShellGone();
94 
95 			void			InitiateDrag();
96 
97 			void			DisableResizeView(int32 disableCount = 1);
98 	static	void			AboutRequested();
99 
100 protected:
101 	virtual void			AttachedToWindow();
102 	virtual void			DetachedFromWindow();
103 	virtual void			Draw(BRect updateRect);
104 	virtual void			WindowActivated(bool active);
105 	virtual void			MakeFocus(bool focusState = true);
106 	virtual void			KeyDown(const char* bytes, int32 numBytes);
107 
108 	virtual void			MouseDown(BPoint where);
109 	virtual void			MouseMoved(BPoint where, uint32 transit,
110 								const BMessage* message);
111 	virtual void			MouseUp(BPoint where);
112 
113 	virtual void			FrameResized(float width, float height);
114 	virtual void			MessageReceived(BMessage* message);
115 
116 	virtual void			ScrollTo(BPoint where);
117 	virtual void			TargetedByScrollView(BScrollView *scrollView);
118 
119 	virtual status_t		GetSupportedSuites(BMessage* msg);
120 	virtual BHandler*		ResolveSpecifier(BMessage* msg, int32 index,
121 								BMessage* specifier, int32 form,
122 								const char* property);
123 
124 private:
125 			// point and text offset conversion
126 	inline	int32			_LineAt(float y);
127 	inline	float			_LineOffset(int32 index);
128 	inline	TermPos			_ConvertToTerminal(const BPoint& point);
129 	inline	BPoint			_ConvertFromTerminal(const TermPos& pos);
130 
131 	inline	void			_InvalidateTextRect(int32 x1, int32 y1, int32 x2,
132 								int32 y2);
133 
134 			status_t		_InitObject(int32 argc, const char** argv);
135 
136 			status_t		_AttachShell(Shell* shell);
137 			void			_DetachShell();
138 
139 			void			_Activate();
140 			void			_Deactivate();
141 
142 			void			_DrawLinePart(int32 x1, int32 y1, uint32 attr,
143 								char* buffer, int32 width, bool mouse,
144 								bool cursor, BView* inView);
145 			void			_DrawCursor();
146 			void			_InvalidateTextRange(TermPos start, TermPos end);
147 
148 			bool			_IsCursorVisible() const;
149 			void			_BlinkCursor();
150 			void			_ActivateCursor(bool invalidate);
151 
152 			void			_DoPrint(BRect updateRect);
153 			void			_UpdateScrollBarRange();
154 			void			_SecondaryMouseButtonDropped(BMessage* msg);
155 			void			_DoSecondaryMouseDropAction(BMessage* msg);
156 			void			_DoFileDrop(entry_ref &ref);
157 
158 			void			_SynchronizeWithTextBuffer(int32 visibleDirtyTop,
159 								int32 visibleDirtyBottom);
160 
161 			void			_WritePTY(const char* text, int32 numBytes);
162 
163 			// selection
164 			float			_MouseDistanceSinceLastClick(BPoint where);
165 			void			_Select(TermPos start, TermPos end, bool inclusive,
166 								bool setInitialSelection);
167 			void			_ExtendSelection(TermPos, bool inclusive,
168 								bool useInitialSelection);
169 			void			_Deselect();
170 			bool			_HasSelection() const;
171 			void			_SelectWord(BPoint where, bool extend,
172 								bool useInitialSelection);
173 			void			_SelectLine(BPoint where, bool extend,
174 								bool useInitialSelection);
175 
176 			void			_AutoScrollUpdate();
177 
178 			bool			_CheckSelectedRegion(const TermPos& pos) const;
179 			bool			_CheckSelectedRegion(int32 row, int32 firstColumn,
180 								int32& lastColumn) const;
181 
182 			void			_UpdateSIGWINCH();
183 
184 			void			_ScrollTo(float y, bool scrollGfx);
185 			void			_ScrollToRange(TermPos start, TermPos end);
186 
187 			void			_SendMouseEvent(int32 button, int32 mode, int32 x,
188 								int32 y, bool motion);
189 
190 			void			_DrawInlineMethodString();
191 			void			_HandleInputMethodChanged(BMessage* message);
192 			void			_HandleInputMethodLocationRequest();
193 			void			_CancelInputMethod();
194 private:
195 	class CharClassifier;
196 
197 			Shell*			fShell;
198 
199 			BMessageRunner*	fWinchRunner;
200 			BMessageRunner*	fCursorBlinkRunner;
201 			BMessageRunner*	fAutoScrollRunner;
202 			BMessageRunner*	fResizeRunner;
203 			BStringView*	fResizeView;
204 			CharClassifier*	fCharClassifier;
205 
206 			// Font and Width
207 			BFont			fHalfFont;
208 			int				fFontWidth;
209 			int				fFontHeight;
210 			int				fFontAscent;
211 			struct escapement_delta fEscapement;
212 
213 			// frame resized flag.
214 			bool			fFrameResized;
215 			int32			fResizeViewDisableCount;
216 
217 			// Cursor Blinking, draw flag.
218 			bigtime_t		fLastActivityTime;
219 			int32			fCursorState;
220 			int				fCursorHeight;
221 
222 			// Cursor position.
223 			TermPos			fCursor;
224 
225 			int32			fMouseButtons;
226 
227 			// Terminal rows and columns.
228 			int				fColumns;
229 			int				fRows;
230 
231 			int				fEncoding;
232 			bool				fActive;
233 
234 			// Object pointer.
235 			TerminalBuffer*	fTextBuffer;
236 			BasicTerminalBuffer* fVisibleTextBuffer;
237 			BScrollBar*		fScrollBar;
238 			InlineInput*		fInline;
239 
240 			// Color and Attribute.
241 			rgb_color		fCursorForeColor;
242 			rgb_color		fCursorBackColor;
243 			rgb_color		fSelectForeColor;
244 			rgb_color		fSelectBackColor;
245 
246 			// Scroll Region
247 			float			fScrollOffset;
248 			int32			fScrBufSize;
249 				// TODO: That's the history capacity -- only needed
250 				// until the text buffer is created.
251 			float			fAutoScrollSpeed;
252 
253 			// redraw management
254 			bigtime_t		fLastSyncTime;
255 			int32			fScrolledSinceLastSync;
256 			BMessageRunner*	fSyncRunner;
257 			bool			fConsiderClockedSync;
258 
259 			// selection
260 			TermPos			fSelStart;
261 			TermPos			fSelEnd;
262 			TermPos			fInitialSelectionStart;
263 			TermPos			fInitialSelectionEnd;
264 			bool			fMouseTracking;
265 			bool			fCheckMouseTracking;
266 			int				fSelectGranularity;
267 			BPoint			fLastClickPoint;
268 
269 			// mouse
270 			TermPos			fPrevPos;
271 			bool			fReportX10MouseEvent;
272 			bool			fReportNormalMouseEvent;
273 			bool			fReportButtonMouseEvent;
274 			bool			fReportAnyMouseEvent;
275 			BClipboard*		fMouseClipboard;
276 };
277 
278 
279 #endif // TERMVIEW_H
280