Avoid use of curses subpads
[centerim5.git] / cppconsui / TextEdit.h
blobc79e9e8fb8d24886c7ed71dca4eb5758bbbd4581
1 /*
2 * Copyright (C) 2010-2013 by CenterIM developers
4 * This file is part of CenterIM.
6 * CenterIM is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * CenterIM is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 /**
22 * @file
23 * TextEdit class
25 * @ingroup cppconsui
28 #ifndef __TEXTEDIT_H__
29 #define __TEXTEDIT_H__
31 #include "Widget.h"
33 #include <deque>
35 namespace CppConsUI
38 class TextEdit
39 : public Widget
41 public:
42 enum Flag {
43 FLAG_NUMERIC = 1 << 0,
44 FLAG_NOSPACE = 1 << 1
47 TextEdit(int w, int h, const char *text_ = NULL, int flags_ = 0,
48 bool single_line = false, bool accept_tabs_ = true,
49 bool masked_ = false);
50 virtual ~TextEdit();
52 // InputProcessor
53 virtual bool processInputText(const TermKeyKey &key);
55 // Widget
56 virtual void draw(Curses::ViewPort area);
58 /**
59 * Sets new text.
61 virtual void setText(const char *new_text);
62 /**
63 * Removes all text.
65 virtual void clear();
66 /**
67 * Returns inserted text.
69 virtual const char *getText() const;
71 virtual size_t getTextLength() const { return text_length; }
73 virtual void setFlags(int new_flags, bool revalidate = true);
74 virtual int getFlags() const { return flags; }
76 virtual void setSingleLineMode(bool new_single_line_mode);
77 virtual bool isSingleLineMode() const { return single_line_mode; }
79 virtual void setAcceptTabs(bool new_accept_tabs);
80 virtual bool doesAcceptTabs() const { return accept_tabs; }
82 virtual void setMasked(bool new_masked);
83 virtual bool isMasked() const { return masked; }
85 sigc::signal<void, TextEdit&> signal_text_change;
87 protected:
88 enum Direction {
89 DIR_BACK,
90 DIR_FORWARD
93 enum CursorMovement {
94 MOVE_LOGICAL_POSITIONS,
95 MOVE_VISUAL_POSITIONS,
96 MOVE_WORDS,
97 MOVE_DISPLAY_LINES,
98 MOVE_DISPLAY_LINE_ENDS,
99 MOVE_PARAGRAPHS,
100 MOVE_PARAGRAPH_ENDS,
101 MOVE_PAGES,
102 MOVE_BUFFER_ENDS,
103 MOVE_HORIZONTAL_PAGES
106 enum DeleteType {
107 DELETE_CHARS,
108 DELETE_WORD_ENDS
111 struct ScreenLine
114 * Pointer to the start of line (points into buffer).
116 const char *start;
118 * Pointer to the first byte that is not part of this line.
120 const char *end;
122 * Precalculated length.
124 size_t length;
126 ScreenLine(const char *start_, const char *end_, size_t length_)
127 : start(start_), end(end_), length(length_) {}
128 bool operator==(const ScreenLine& other) const;
131 struct CmpScreenLineEnd
133 bool operator()(ScreenLine& sline, const char *tag);
136 typedef std::deque<ScreenLine> ScreenLines;
138 ScreenLines screen_lines;
141 * Bitmask indicating which input is accepted.
143 int flags;
144 bool editable;
145 bool overwrite_mode;
146 bool single_line_mode;
147 bool accept_tabs;
148 bool masked;
151 * Character position from the start of buffer.
153 size_t current_pos;
155 * Cursor location in the buffer.
157 mutable char *point;
160 * Current cursor line (derived from current_pos and screen_lines).
162 size_t current_sc_line;
164 * Current cursor character number (in the current line).
166 size_t current_sc_linepos;
168 * Holds index into screen_lines that marks the first screen line.
170 size_t view_top;
173 * Start of text buffer.
175 char *buffer;
177 * First location outside buffer.
179 char *bufend;
181 * Start of gap.
183 mutable char *gapstart;
185 * First location after the gap end.
187 mutable char *gapend;
189 * Length in use, in chars.
191 size_t text_length;
193 mutable bool screen_lines_dirty;
195 // Widget
196 virtual void updateArea();
198 virtual void initBuffer(size_t size);
199 virtual size_t getGapSize() const;
200 virtual void expandGap(size_t size);
201 virtual void moveGapToCursor();
203 virtual char *getTextStart() const;
204 virtual char *prevChar(const char *p) const;
205 virtual char *nextChar(const char *p) const;
206 virtual int width(const char *start, size_t chars) const;
208 * Returns on-screen width of a given character in the same fashion as
209 * Curses::onscreen_width() does but handles the tab character and wide
210 * characters properly if the masked mode is active.
212 virtual int onScreenWidth(UTF8::UniChar uc, int w = 0) const;
214 virtual char *getScreenLine(const char *text, int max_width,
215 size_t *res_length) const;
217 * Recalculates all screen lines.
219 virtual void updateScreenLines();
221 * Recalculates necessary amout of screen lines.
223 virtual void updateScreenLines(const char *begin, const char *end);
224 virtual void assertUpdatedScreenLines();
227 * Recalculates screen cursor position based on current_pos and
228 * screen_lines, sets current_sc_line and current_sc_linepos and handles
229 * scrolling if necessary.
231 virtual void updateScreenCursor();
234 * Inserts given text at the current cursor position.
236 virtual void insertTextAtCursor(const char *new_text,
237 size_t new_text_bytes);
238 virtual void insertTextAtCursor(const char *new_text);
239 virtual void deleteFromCursor(DeleteType type, Direction dir);
240 virtual void moveCursor(CursorMovement step, Direction dir);
242 virtual void toggleOverwrite();
244 virtual size_t moveLogicallyFromCursor(Direction dir) const;
245 virtual size_t moveWordFromCursor(Direction dir, bool word_end) const;
247 private:
248 CONSUI_DISABLE_COPY(TextEdit);
250 void actionMoveCursor(CursorMovement step, Direction dir);
251 void actionDelete(DeleteType type, Direction dir);
252 void actionToggleOverwrite();
254 void declareBindables();
257 } // namespace CppConsUI
259 #endif // __TEXTEDIT_H__
261 /* vim: set tabstop=2 shiftwidth=2 textwidth=78 expandtab : */