xref: /haiku/src/apps/terminal/TermView.h (revision ba499cdc3336fb89429027418871bf263f1f5e14)
1 /*
2  * Copyright (c) 2003-4 Kian Duffy <myob@users.sourceforge.net>
3  * Parts Copyright (C) 1998,99 Kazuho Okui and Takashi Murai.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files or portions
7  * thereof (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so, subject
11  * to the following conditions:
12  *
13  *  * Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  *
16  *  * Redistributions in binary form must reproduce the above copyright notice
17  *    in the  binary, as well as this list of conditions and the following
18  *    disclaimer in the documentation and/or other materials provided with
19  *    the distribution.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27  * THE SOFTWARE.
28  *
29  */
30 
31 #ifndef TERMVIEW_H
32 #define TERMVIEW_H
33 
34 #include <View.h>
35 #include <String.h>
36 #include <MessageRunner.h>
37 #include "TermConst.h"
38 #include "TermWindow.h"
39 #include "CurPos.h"
40 
41 /* Cursor Blinking flag */
42 #define CUROFF 0
43 #define CURON  1
44 
45 #define VIEW_THR_CODE 'vtcd'
46 #define MOUSE_THR_CODE 'mtcd'
47 #define RECT_BUF_SIZE 32
48 
49 const unsigned char M_ADD_CURSOR [] = {
50   16,				// Size
51   1,				// Color depth
52   0,				// Hot spot y
53   1,				// Hot spot x
54   // Cursor image
55   0x70, 0x00,
56   0x48, 0x00,
57   0x48, 0x00,
58   0x27, 0xc0,
59   0x24, 0xb8,
60   0x12, 0x54,
61   0x10, 0x02,
62   0x78, 0x02,
63   0x98, 0x02,
64   0x84, 0x02,
65   0x60, 0x02,
66   0x18, 0x12,
67   0x04, 0x10,
68   0x02, 0x7c,
69   0x00, 0x10,
70   0x00, 0x10,
71   // Mask image
72   0x70, 0x00,
73   0x78, 0x00,
74   0x78, 0x00,
75   0x3f, 0xc0,
76   0x3f, 0xf8,
77   0x1f, 0xfc,
78   0x1f, 0xfe,
79   0x7f, 0xfe,
80   0xff, 0xfe,
81   0xff, 0xfe,
82   0x7f, 0xfe,
83   0x1f, 0xfe,
84   0x07, 0xfc,
85   0x03, 0xfc,
86   0x00, 0x10,
87   0x00, 0x10,
88 };
89 
90 class TermBuffer;
91 class CodeConv;
92 class BPopUpMenu;
93 class BScrollBar;
94 class BString;
95 
96 class TermView : public BView
97 {
98 public:
99   //
100   // Constructor, Destructor, and Initializer.
101   //
102   TermView (BRect frame, CodeConv *inCodeConv);
103   ~TermView ();
104 
105   //
106   // Initializer, Set function
107   //
108   void	SetTermFont(const BFont *halfFont, const BFont *fullFont);
109   void	GetFontSize (int *width, int *height);
110   BRect	SetTermSize (int rows, int cols, bool flag);
111   void	SetTermColor (void);
112   void	SetMouseButton (void);
113   void	SetMouseCursor (void);
114  // void  SetIMAware (bool);
115   void	SetScrollBar (BScrollBar *scrbar);
116   BScrollBar  *ScrollBar () const { return fScrollBar; };
117 
118   //
119   // Output Charactor.
120   //
121   void	PutChar (uchar *string, ushort attr, int width);
122   void	PutCR (void);
123   void	PutLF (void);
124   void	PutNL (int num);
125   void	SetInsertMode (int flag);
126   void	InsertSpace (int num);
127 
128   int TermDraw (const CurPos &start, const CurPos &end);
129   int TermDrawRegion (CurPos start, CurPos end);
130   int TermDrawSelectedRegion (CurPos start, CurPos end);
131 
132   //
133   // Delete Charactor.
134   //
135   void	EraseBelow (void);
136   void	DeleteChar (int num);
137   void	DeleteColumns (void);
138   void	DeleteLine (int num);
139 
140   //
141   // Get and Set Cursor position.
142   //
143   void	SetCurPos (int x, int y);
144   void	SetCurX (int x);
145   void	SetCurY (int y);
146 
147   void	GetCurPos (CurPos *inCurPos);
148   int	GetCurX (void);
149   int	GetCurY (void);
150 
151   void	SaveCursor (void);
152   void	RestoreCursor (void);
153 
154   //
155   // Move Cursor
156   //
157   void	MoveCurRight (int num);
158   void	MoveCurLeft (int num);
159   void	MoveCurUp (int num);
160   void	MoveCurDown (int num);
161 
162   //
163   // Cursor setting.
164   //
165   void	DrawCursor (void);
166   void	BlinkCursor (void);
167   void	SetCurDraw (bool flag);
168   void	SetCurBlinking (bool flag);
169 
170   //
171   // Scroll region.
172   //
173   void	ScrollRegion (int top, int bot, int dir, int num);
174   void	SetScrollRegion (int top, int bot);
175   void	ScrollAtCursor (void);
176 
177   //
178   // Other.
179   //
180   void	UpdateSIGWINCH();
181   void	DeviceStatusReport (int);
182   void	UpdateLine (void);
183   void	ScrollScreen (void);
184   void	ScrollScreenDraw (void);
185   void  GetFrameSize (float *width, float *height);
186   void  GetFontInfo (int *, int*);
187   bool  Find (const BString &str, bool forwardSearch, bool matchCase, bool matchWord);
188   void  GetSelection (BString &str);
189 
190   /*
191    *	PRIVATE MEMBER.
192    */
193 
194 private:
195   /*
196    * static Functions (Thread functions).
197    */
198 
199   static int32 ViewThread (void *);
200   static int32 MouseTracking (void *);
201 
202   /*
203    * Hook Functions.
204    */
205   void	AttachedToWindow (void);
206   void	Draw (BRect updateRect);
207   void	WindowActivated (bool active);
208   void	KeyDown (const char*, int32);
209   void	MouseDown (BPoint where);
210   void  MouseMoved (BPoint, uint32, const BMessage *);
211 
212   void	FrameResized(float width, float height);
213   void	MessageReceived(BMessage* message);
214 
215   /*
216    * Private Member Functions.
217    */
218 
219   thread_id InitViewThread (void);
220 
221   inline void DrawLines (int , int, ushort, uchar *, int, int, int, BView *);
222 
223   void DoPrint(BRect updateRect);
224 
225   void ResizeScrBarRange (void);
226 
227   void DoFileDrop(entry_ref &ref);
228 
229   // edit menu function.
230   void DoCopy (void);
231   void DoPaste (void);
232   void DoSelectAll (void);
233   void DoClearAll (void);
234 
235   int SubstMetaChar (const char *p, char *q);
236   void WritePTY (const uchar *text, int num_byteses);
237 
238   // Comunicate Input Method
239 //  void DoIMStart (BMessage* message);
240 //  void DoIMStop (BMessage* message);
241 //  void DoIMChange (BMessage* message);
242 //  void DoIMLocation (BMessage* message);
243 //  void DoIMConfirm (void);
244   void ConfirmString (const char *, int32);
245   int32 GetCharFromUTF8String (const char *, char *);
246   int32 GetWidthFromUTF8String (const char *);
247 
248   // mouse operation
249   void SetupPop (void);
250   int32 SetupMouseButton (const char *bname);
251 
252   // Mouse select
253   void  Select (CurPos start, CurPos end);
254   void  AddSelectRegion (CurPos);
255   void  ResizeSelectRegion (CurPos);
256 
257   void  DeSelect(void);
258 
259   // select word function
260   void  SelectWord (BPoint where, int mod);
261   void  SelectLine (BPoint where, int mod);
262 
263   // point and text offset conversion.
264   CurPos  BPointToCurPos (const BPoint &p);
265   BPoint  CurPosToBPoint (const CurPos &pos);
266 
267   bool	CheckSelectedRegion (const CurPos &pos);
268 
269   inline void SendDataToDrawEngine (int, int, int, int);
270 
271   /*
272    * DATA Member.
273    */
274 
275   //
276   // Font and Width.
277   //
278   BFont fHalfFont;
279   BFont fFullFont;
280 
281   int fFontWidth;
282   int fFontHeight;
283 
284   int fFontAscent;
285   struct escapement_delta fEscapement;
286 
287   //
288   // Flags.
289   //
290 
291   // Update flag (Set on PutChar).
292   bool fUpdateFlag;
293 
294   // Terminal insertmode flag (use PutChar).
295   bool fInsertModeFlag;
296 
297   // Scroll count, range.
298   int fScrollUpCount;
299   int fScrollBarRange;
300 
301   // Frame Resized flag.
302   bool fFrameResized;
303 
304   // Cursor Blinking, draw flag.
305   bool fCursorDrawFlag;
306   bool fCursorStatus;
307   bool fCursorBlinkingFlag;
308   bool fCursorRedrawFlag;
309 
310   int fCursorHeight;
311 
312   // terminal text attribute flag.
313   bool fInverseFlag;
314   bool fBoldFlag;
315   bool fUnderlineFlag;
316 
317   //
318   // Cursor position.
319   //
320   CurPos fCurPos;
321   CurPos fCurStack;
322 
323   int fBufferStartPos;
324 
325   //
326   // Terminal rows and columns.
327   //
328   int fTermRows;
329   int fTermColumns;
330 
331   //
332   // Terminal view pointer.
333   //
334   int fTop;
335 
336   //
337   // Object pointer.
338   //
339   TermBuffer	*fTextBuffer;
340   CodeConv	*fCodeConv;
341   BScrollBar	*fScrollBar;
342 
343   //
344   // Offscreen Bitmap and View.
345   //
346 
347   BRect fSrcRect;
348   BRect fDstRect;
349 
350   //
351   // Color and Attribute.
352   //
353 
354   // text fore and back color
355   rgb_color fTextForeColor, fTextBackColor;
356 
357   // cursor fore and back color
358   rgb_color fCursorForeColor, fCursorBackColor;
359 
360   // selected region color
361   rgb_color fSelectForeColor, fSelectBackColor;
362 
363   uchar fTermAttr;
364 
365   //
366   // Scroll Region
367   //
368   int fScrTop;
369   int fScrBot;
370   int fScrBufSize;
371   bool fScrRegionSet;
372 
373   BPopUpMenu *fPopMenu;
374   BMenu *fPopEncoding;
375   BMenu *fPopSize;
376 
377   // mouse
378   int32 mSelectButton;
379   int32 mSubMenuButton;
380   int32 mPasteMenuButton;
381 
382   bool	fMouseImage;
383 
384   BPoint fPreviousMousePoint;
385 
386   // view selection
387   CurPos fSelStart;
388   CurPos fSelEnd;
389   bool fSelected;
390   bool fMouseTracking;
391 
392   // thread ID / flags.
393   thread_id fViewThread;
394   thread_id fMouseThread;
395   bool fQuitting;
396 
397   // DrawEngine parameter.
398   sem_id fDrawRectSem;
399   sDrawRect fDrawRectBuffer[RECT_BUF_SIZE];
400   int fDrawRect_p;
401 
402   // Input Method parameter.
403   int fIMViewPtr;
404   CurPos fIMStartPos;
405   CurPos fIMEndPos;
406   BString fIMString;
407   bool fIMflag;
408   BMessenger fIMMessenger;
409 
410   int32 fImCodeState;
411 
412 };
413 
414 
415 #endif //TERMVIEW_H
416