1 // Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
3 // This file is part of CenterIM.
5 // CenterIM is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // CenterIM is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with CenterIM. If not, see <http://www.gnu.org/licenses/>.
21 /// @ingroup cppconsui
32 /// Line oriented text view widget.
33 class TextView
: public Widget
{
35 TextView(int w
, int h
, bool autoscroll_
= false, bool scrollbar_
= false);
36 virtual ~TextView() override
;
39 virtual int draw(Curses::ViewPort area
, Error
&error
) override
;
41 /// Appends text after the last line.
42 virtual void append(const char *text
, int color
= 0);
44 /// Inserts text before specified line number. Text can contain multiple lines
45 /// and should end with '\\n' character just in front of '\\0' character.
46 virtual void insert(std::size_t line_num
, const char *text
, int color
= 0);
48 /// Removes a specified line.
49 virtual void erase(std::size_t line_num
);
51 /// Removes specified range of lines. Parameter end_line represents the line
52 /// after the last removed line, thus range of <start_line, end_line) lines is
54 virtual void erase(std::size_t start_line
, std::size_t end_line
);
56 /// Removes all lines.
59 /// Returns string for a specified line number.
60 virtual const char *getLine(std::size_t line_num
) const;
62 /// Returns count of all lines.
63 virtual std::size_t getLinesNumber() const;
65 virtual void setAutoScroll(bool new_autoscroll
);
66 virtual bool hasAutoScroll() const { return autoscroll_
; }
68 virtual void setScrollBar(bool new_scrollbar
);
69 virtual bool hasScrollBar() const { return scrollbar_
; }
72 /// Struct Line saves a real line. All text added into TextView is split on
73 /// '\\n' character and stored into Line objects.
75 /// UTF-8 encoded text. Note: Newline character is not part of text.
78 /// Text length in characters.
84 Line(const char *text_
, std::size_t bytes
, int color_
);
88 /// ScreenLine represents an on-screen line.
90 /// Parent Line that this ScreenLine shows.
93 /// Pointer into parent's text where this ScreenLine starts.
96 /// Length in characters (Unicode).
99 ScreenLine(Line
&parent_
, const char *text_
, int length_
);
102 typedef std::deque
<Line
*> Lines
;
103 typedef std::deque
<ScreenLine
> ScreenLines
;
105 std::size_t view_top_
;
107 bool autoscroll_suspended_
;
110 /// Array of real lines.
113 /// Array of on-screen lines.
114 ScreenLines screen_lines_
;
117 virtual void updateArea() override
;
119 virtual const char *proceedLine(
120 const char *text
, int area_width
, int *res_length
) const;
122 /// Recalculates on-screen lines for a specified line number.
123 virtual std::size_t updateScreenLines(
124 std::size_t line_num
, std::size_t start
= 0);
126 /// Recalculates all screen lines.
127 virtual void updateAllScreenLines();
129 virtual std::size_t eraseScreenLines(std::size_t line_num
,
130 std::size_t start
= 0, std::size_t *deleted
= nullptr);
133 CONSUI_DISABLE_COPY(TextView
);
135 void actionScroll(int direction
);
137 void declareBindables();
140 } // namespace CppConsUI
144 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: