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