xref: /haiku/src/apps/terminal/TermView.h (revision 20d4ee1d599525956b5db69157c174bbaf98d5d7)
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   /*
192    *	PRIVATE MEMBER.
193    */
194 
195 private:
196   /*
197    * static Functions (Thread functions).
198    */
199 
200   static int32 ViewThread (void *);
201   static int32 MouseTracking (void *);
202 
203   /*
204    * Hook Functions.
205    */
206   void	AttachedToWindow (void);
207   void	Draw (BRect updateRect);
208   void	WindowActivated (bool active);
209   void	KeyDown (const char*, int32);
210   void	MouseDown (BPoint where);
211   void  MouseMoved (BPoint, uint32, const BMessage *);
212 
213   void	FrameResized(float width, float height);
214   void	MessageReceived(BMessage* message);
215 
216   /*
217    * Private Member Functions.
218    */
219 
220   thread_id InitViewThread (void);
221 
222   inline void DrawLines (int , int, ushort, uchar *, int, int, int, BView *);
223 
224   void DoPrint(BRect updateRect);
225 
226   void ResizeScrBarRange (void);
227 
228   void DoFileDrop(entry_ref &ref);
229 
230   // edit menu function.
231   void DoCopy (void);
232   void DoPaste (void);
233   void DoSelectAll (void);
234   void DoClearAll (void);
235 
236   int SubstMetaChar (const char *p, char *q);
237   void WritePTY (const uchar *text, int num_byteses);
238 
239   // Comunicate Input Method
240 //  void DoIMStart (BMessage* message);
241 //  void DoIMStop (BMessage* message);
242 //  void DoIMChange (BMessage* message);
243 //  void DoIMLocation (BMessage* message);
244 //  void DoIMConfirm (void);
245   void ConfirmString (const char *, int32);
246   int32 GetCharFromUTF8String (const char *, char *);
247   int32 GetWidthFromUTF8String (const char *);
248 
249   // mouse operation
250   void SetupPop (void);
251   int32 SetupMouseButton (const char *bname);
252 
253   // Mouse select
254   void  Select (CurPos start, CurPos end);
255   void  AddSelectRegion (CurPos);
256   void  ResizeSelectRegion (CurPos);
257 
258   void  DeSelect(void);
259 
260   // select word function
261   void  SelectWord (BPoint where, int mod);
262   void  SelectLine (BPoint where, int mod);
263 
264   // point and text offset conversion.
265   CurPos  BPointToCurPos (const BPoint &p);
266   BPoint  CurPosToBPoint (const CurPos &pos);
267 
268   bool	CheckSelectedRegion (const CurPos &pos);
269 
270   inline void SendDataToDrawEngine (int, int, int, int);
271 
272   /*
273    * DATA Member.
274    */
275 
276   //
277   // Font and Width.
278   //
279   BFont fHalfFont;
280   BFont fFullFont;
281 
282   int fFontWidth;
283   int fFontHeight;
284 
285   int fFontAscent;
286   struct escapement_delta fEscapement;
287 
288   //
289   // Flags.
290   //
291 
292   // Update flag (Set on PutChar).
293   bool fUpdateFlag;
294 
295   // Terminal insertmode flag (use PutChar).
296   bool fInsertModeFlag;
297 
298   // Scroll count, range.
299   int fScrollUpCount;
300   int fScrollBarRange;
301 
302   // Frame Resized flag.
303   bool fFrameResized;
304 
305   // Cursor Blinking, draw flag.
306   bool fCursorDrawFlag;
307   bool fCursorStatus;
308   bool fCursorBlinkingFlag;
309   bool fCursorRedrawFlag;
310 
311   int fCursorHeight;
312 
313   // terminal text attribute flag.
314   bool fInverseFlag;
315   bool fBoldFlag;
316   bool fUnderlineFlag;
317 
318   //
319   // Cursor position.
320   //
321   CurPos fCurPos;
322   CurPos fCurStack;
323 
324   int fBufferStartPos;
325 
326   //
327   // Terminal rows and columns.
328   //
329   int fTermRows;
330   int fTermColumns;
331 
332   //
333   // Terminal view pointer.
334   //
335   int fTop;
336 
337   //
338   // Object pointer.
339   //
340   TermBuffer	*fTextBuffer;
341   CodeConv	*fCodeConv;
342   BScrollBar	*fScrollBar;
343 
344   //
345   // Offscreen Bitmap and View.
346   //
347 
348   BRect fSrcRect;
349   BRect fDstRect;
350 
351   //
352   // Color and Attribute.
353   //
354 
355   // text fore and back color
356   rgb_color fTextForeColor, fTextBackColor;
357 
358   // cursor fore and back color
359   rgb_color fCursorForeColor, fCursorBackColor;
360 
361   // selected region color
362   rgb_color fSelectForeColor, fSelectBackColor;
363 
364   uchar fTermAttr;
365 
366   //
367   // Scroll Region
368   //
369   int fScrTop;
370   int fScrBot;
371   int fScrBufSize;
372   bool fScrRegionSet;
373 
374   BPopUpMenu *fPopMenu;
375   BMenu *fPopEncoding;
376   BMenu *fPopSize;
377 
378   // mouse
379   int32 mSelectButton;
380   int32 mSubMenuButton;
381   int32 mPasteMenuButton;
382 
383   bool	fMouseImage;
384 
385   BPoint fPreviousMousePoint;
386 
387   // view selection
388   CurPos fSelStart;
389   CurPos fSelEnd;
390   int fSelected;
391   int fMouseTracking;
392 
393   // thread ID / flags.
394   thread_id fViewThread;
395   thread_id fMouseThread;
396   int fQuitting;
397 
398   // DrawEngine parameter.
399   sem_id fDrawRectSem;
400   sDrawRect fDrawRectBuffer[RECT_BUF_SIZE];
401   int fDrawRect_p;
402 
403   // Input Method parameter.
404   int fIMViewPtr;
405   CurPos fIMStartPos;
406   CurPos fIMEndPos;
407   BString fIMString;
408   bool fIMflag;
409   BMessenger fIMMessenger;
410 
411   int32 fImCodeState;
412 
413 };
414 
415 
416 #endif //TERMVIEW_H
417