2 * Copyright 2013, Haiku, Inc. All rights reserved.
3 * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
4 * Distributed under the terms of the MIT License.
7 * Ingo Weinhold, ingo_weinhold@gmx.de
8 * Siarzhuk Zharski, zharik@gmx.li
10 #ifndef BASIC_TERMINAL_BUFFER_H
11 #define BASIC_TERMINAL_BUFFER_H
16 #include "HistoryBuffer.h"
22 class TerminalCharClassifier
;
26 struct TerminalBufferDirtyInfo
{
27 int32 linesScrolled
; // number of lines added to the history
28 int32 dirtyTop
; // dirty line range
31 bool messageSent
; // listener has been notified
33 bool IsDirtyRegionValid() const
35 return dirtyTop
<= dirtyBottom
;
38 void ExtendDirtyRegion(int32 top
, int32 bottom
)
42 if (bottom
> dirtyBottom
)
50 dirtyBottom
= INT_MIN
;
51 invalidateAll
= false;
55 TerminalBufferDirtyInfo()
62 class BasicTerminalBuffer
{
64 BasicTerminalBuffer();
65 virtual ~BasicTerminalBuffer();
67 status_t
Init(int32 width
, int32 height
,
68 int32 historyCapacity
);
70 int32
Width() const { return fWidth
; }
71 int32
Height() const { return fHeight
; }
72 inline int32
HistorySize() const;
73 inline int32
HistoryCapacity() const;
75 bool IsAlternateScreenActive() const
76 { return fAlternateScreenActive
; }
78 TerminalBufferDirtyInfo
& DirtyInfo() { return fDirtyInfo
; }
80 virtual status_t
ResizeTo(int32 width
, int32 height
);
81 virtual status_t
ResizeTo(int32 width
, int32 height
,
82 int32 historyCapacity
);
83 status_t
SetHistoryCapacity(int32 historyCapacity
);
84 void Clear(bool resetCursor
);
87 const BasicTerminalBuffer
* other
,
88 int32 offset
, int32 dirtyTop
,
91 bool IsFullWidthChar(int32 row
, int32 column
) const;
92 int GetChar(int32 row
, int32 column
,
94 uint32
& attributes
) const;
95 void GetCellAttributes(int32 row
, int32 column
,
96 uint32
& attributes
, uint32
& count
) const;
97 int32
GetString(int32 row
, int32 firstColumn
,
98 int32 lastColumn
, char* buffer
,
99 uint32
& attributes
) const;
100 void GetStringFromRegion(BString
& string
,
101 const TermPos
& start
,
102 const TermPos
& end
) const;
103 bool FindWord(const TermPos
& pos
,
104 TerminalCharClassifier
* classifier
,
105 bool findNonWords
, TermPos
& start
,
107 int32
LineLength(int32 index
) const;
108 int32
GetLineColor(int32 index
) const;
110 bool PreviousLinePos(TermPos
& pos
) const;
111 bool NextLinePos(TermPos
& pos
, bool normalize
) const;
112 // normalize specifies that the returned
113 // position must be a valid position, i.e.
114 // actually point to a character (as opposed
115 // to just pointing to the position after a
118 bool Find(const char* pattern
, const TermPos
& start
,
119 bool forward
, bool caseSensitive
,
120 bool matchWord
, TermPos
& matchStart
,
121 TermPos
& matchEnd
) const;
123 inline uint32
GetAttributes();
124 inline void SetAttributes(uint32 attributes
);
126 // snapshots and data capture for debugging
127 void MakeLinesSnapshots(time_t timeStamp
,
128 const char* fileName
);
129 void StartStopDebugCapture();
130 void CaptureChar(char ch
);
132 // insert chars/lines
133 void InsertChar(UTF8Char c
);
134 void FillScreen(UTF8Char c
, uint32 attr
);
140 void InsertCursorBackTab(int32 numTabs
);
141 void SetInsertMode(int flag
);
142 void InsertSpace(int32 num
);
143 void InsertLines(int32 numLines
);
145 // delete chars/lines
146 inline void EraseChars(int32 numChars
);
147 void EraseCharsFrom(int32 first
, int32 numChars
);
151 void DeleteChars(int32 numChars
);
152 inline void DeleteColumns();
153 void DeleteColumnsFrom(int32 first
);
154 void DeleteLines(int32 numLines
);
156 // get and set cursor position
157 inline void SetCursor(int32 x
, int32 y
);
158 inline void SetCursorX(int32 x
);
159 inline void SetCursorY(int32 y
);
160 inline TermPos
Cursor() const { return fCursor
; }
162 void RestoreCursor();
165 inline void MoveCursorRight(int32 num
);
166 inline void MoveCursorLeft(int32 num
);
167 inline void MoveCursorUp(int32 num
);
168 inline void MoveCursorDown(int32 num
);
169 inline void NextLine();
172 inline void ScrollBy(int32 numLines
);
173 void SetScrollRegion(int32 top
, int32 bottom
);
174 void SetOriginMode(bool enabled
);
175 void SaveOriginMode();
176 void RestoreOriginMode();
177 void SetTabStop(int32 x
);
178 void ClearTabStop(int32 x
);
179 void ClearAllTabStops();
182 virtual void NotifyListener();
184 inline int32
_LineIndex(int32 index
) const;
185 inline TerminalLine
* _LineAt(int32 index
) const;
186 inline TerminalLine
* _HistoryLineAt(int32 index
,
187 TerminalLine
* lineBuffer
) const;
189 inline void _Invalidate(int32 top
, int32 bottom
);
190 inline void _CursorChanged();
191 void _SetCursor(int32 x
, int32 y
, bool absolute
);
192 void _InvalidateAll();
194 static TerminalLine
** _AllocateLines(int32 width
, int32 count
);
195 static void _FreeLines(TerminalLine
** lines
, int32 count
);
196 void _ClearLines(int32 first
, int32 last
);
198 status_t
_ResizeHistory(int32 width
,
199 int32 historyCapacity
);
200 status_t
_ResizeSimple(int32 width
, int32 height
,
201 int32 historyCapacity
);
202 status_t
_ResizeRewrap(int32 width
, int32 height
,
203 int32 historyCapacity
);
204 status_t
_ResetTabStops(int32 width
);
206 void _Scroll(int32 top
, int32 bottom
,
208 void _SoftBreakLine();
209 void _PadLineToCursor();
210 static void _TruncateLine(TerminalLine
* line
, int32 length
);
211 void _InsertGap(int32 width
);
212 TerminalLine
* _GetPartialLineString(BString
& string
,
213 int32 row
, int32 startColumn
,
214 int32 endColumn
) const;
215 bool _PreviousChar(TermPos
& pos
, UTF8Char
& c
) const;
216 bool _NextChar(TermPos
& pos
, UTF8Char
& c
) const;
218 bool _PreviousLinePos(TerminalLine
* lineBuffer
,
219 TerminalLine
*& line
, TermPos
& pos
) const;
220 bool _NormalizeLinePos(TerminalLine
* lineBuffer
,
221 TerminalLine
*& line
, TermPos
& pos
) const;
224 // screen width/height
228 // scroll region top/bottom
229 int32 fScrollTop
; // first line to scroll
230 int32 fScrollBottom
; // last line to scroll (incl.)
232 // line buffers for the history (ring buffer)
233 TerminalLine
** fScreen
;
234 int32 fScreenOffset
; // index of screen line 0
235 HistoryBuffer
* fHistory
;
239 // cursor position (origin: (0, 0))
241 std::stack
<TermPos
> fSavedCursors
;
242 bool fSoftWrappedCursor
;
244 bool fOverwriteMode
; // false for insert
245 bool fAlternateScreenActive
;
247 bool fSavedOriginMode
;
253 // listener/dirty region management
254 TerminalBufferDirtyInfo fDirtyInfo
;
259 BasicTerminalBuffer::HistorySize() const
261 return fHistory
!= NULL
? fHistory
->Size() : 0;
266 BasicTerminalBuffer::HistoryCapacity() const
268 return fHistory
!= NULL
? fHistory
->Capacity() : 0;
273 BasicTerminalBuffer::GetAttributes()
280 BasicTerminalBuffer::SetAttributes(uint32 attributes
)
282 fAttributes
= attributes
;
287 BasicTerminalBuffer::EraseChars(int32 numChars
)
289 EraseCharsFrom(fCursor
.x
, numChars
);
294 BasicTerminalBuffer::DeleteColumns()
296 DeleteColumnsFrom(fCursor
.x
);
301 BasicTerminalBuffer::SetCursor(int32 x
, int32 y
)
303 _SetCursor(x
, y
, false);
308 BasicTerminalBuffer::SetCursorX(int32 x
)
310 SetCursor(x
, fCursor
.y
);
315 BasicTerminalBuffer::SetCursorY(int32 y
)
317 SetCursor(fCursor
.x
, y
);
322 BasicTerminalBuffer::MoveCursorRight(int32 num
)
324 SetCursor(fCursor
.x
+ num
, fCursor
.y
);
329 BasicTerminalBuffer::MoveCursorLeft(int32 num
)
331 SetCursor(fCursor
.x
- num
, fCursor
.y
);
336 BasicTerminalBuffer::MoveCursorUp(int32 num
)
338 SetCursor(fCursor
.x
, fCursor
.y
- num
);
343 BasicTerminalBuffer::MoveCursorDown(int32 num
)
345 SetCursor(fCursor
.x
, fCursor
.y
+ num
);
350 BasicTerminalBuffer::ScrollBy(int32 numLines
)
352 _Scroll(fScrollTop
, fScrollBottom
, numLines
);
357 BasicTerminalBuffer::NextLine()
359 SetCursor(0, fCursor
.y
+ 1);
362 #endif // BASIC_TERMINAL_BUFFER_H