The ZX Ecosystem v5.1.0;_GUI_v3.1.0
Loading...
Searching...
No Matches
ZXWidgetTextEditor.h
Go to the documentation of this file.
1
2/* **************************************************************************/
19#ifndef ZXWIDGET_TEXTEDITOR
20#define ZXWIDGET_TEXTEDITOR
21
22#include <vector>
24
25
26namespace zxeco
27{
28
29namespace gui
30{
31
32/*****************************************************************************
33*
34* Class: TextEditorWidget
35*
36*******************************************************************************/
37
39
41{
42 public:
43
44 TextEditorWidget(ID id, const std::string & name,
45 const CharArea & area,
46 const std::string & tooltiptext = "",
47 bool editable = true,
48 HorAlign hal = HorAlign::LEFT,
49 VertAlign val = VertAlign::TOP):Widget{id,name,
50 tooltiptext,
51 hal,val},
52 editable_{editable},
53 area_{area},
54 ltext_{editable},
55 firstline_{0} {}
57
62 const std::string & text(void) const { return(ltext_.linearText()); }
65 void setText(const std::string & txt);
67
69 void moveTop(void);
72 void moveDrawingZones(IntDist incx, IntDist incy) noexcept override
73 { editablerect_.move(incx,incy); Widget::moveDrawingZones(incx,incy); }
74 std::pair<bool,const CharRect *> hasFocusZone(void) const noexcept override
75 { return(std::make_pair(true,&editablerect_.get())); }
77 const char * className(void) const noexcept { return("TextEditortWidget"); }
78 const AreaFitting & calcFittingNeeds(const CharArea & area);
79 Widget * setDrawingZone(const CharRect & zone) override;
80
81
82 private:
83
84 class LinedText
85 {
86 public:
87
88 enum Direction { ADV_RIGHT, ADV_LEFT, ADV_UP, ADV_DOWN,
89 ADV_BEGINLINE, ADV_ENDLINE,
90 ADV_PGUP, ADV_PGDOWN,
91 ADV_BEGIN, ADV_END };
92
93 LinedText(bool edit): editable_{edit},
94 area_{0,0},cursor_{0,0,true},cursorshow_{false} {}
95
96 void setArea(const CharArea & area);
97
98 void set(const std::string & txt);
99 const std::string & linearText(void) const noexcept { return(text_); }
100 size_t numberOfLines(void) const noexcept { return(linestarts_.size());}
101
102 void print(Screen & scr, size_t firstline, bool enabled = true);
103
104 void setCursorShow(bool show) noexcept { cursorshow_ = show; }
105 void setCursor(size_t line, size_t column)
106 { setCursorAtCoords(line,column); }
107 void moveCursor(Direction dir = ADV_RIGHT);
108 size_t cursorLine(void) const noexcept { return(cursor_.line); }
109 size_t cursorColumn(void) const noexcept { return(cursor_.col); }
110 bool cursorAtEnd(void) const noexcept { return(cursor_.atend); }
111 bool cursorShown(void) const noexcept { return(cursorshow_); }
112
113 void insertText(const std::string & text);
114 void deletePrevChar(void);
115
116 std::string to_string(void) const;
117
118 private:
119
120 static constexpr bool isSpace(char c) noexcept { return(c == CHRSPC); }
121
122 const bool editable_;
123 CharArea area_;
124 std::string text_;
125 std::vector<size_t> linestarts_;
126 struct Cursor
127 {
128 size_t line;
129 size_t col;
130 bool atend;
131 } cursor_;
132 bool cursorshow_;
133
134 size_t lineLength(size_t line) const;
135 size_t cursorPos(void) const noexcept
136 { return(cursor_.atend ? text_.size() :
137 linestarts_[cursor_.line] + cursor_.col); }
138 void setCursorAtPos(size_t s);
139 void setCursorAtCoords(size_t line, size_t col);
140 void updateLining(size_t fromline = 0);
141 size_t actualLineEnd(size_t linestart, size_t lineend) const;
142 void printCursor(Screen & scr, size_t firstline, bool enabled);
143 };
144
145 static const std::vector<uint8_t> kUDGs;
146
147 const bool editable_;
148 const CharArea area_;
149 TypingZone editablerect_;
150 LinedText ltext_;
151 size_t firstline_;
152
153 bool findMouseClickInLinedText(const EventData & evdata,
154 size_t & line, size_t & column) const;
155 void adjustViewport(bool upordown, bool oneline);
156 void drawRaw(void);
157 int processEvent(const EventData & evdata, Widget * who) override;
158 std::string to_string(void) const;
159};
160
161
162
163
164
165} // end namespace gui
166} // end namespace zxeco
167
168#endif
169 // ZXWidgetTextEditor
171
172
173
const CharRect & get(void) const noexcept
Return the current typing zone (updated by derived classes).
Definition: ZXGUIGeneral.h:154
void move(IntDist xdiff, IntDist ydiff) noexcept
< Shift typing zone.
Definition: ZXGUIGeneral.h:157
virtual void moveDrawingZones(IntDist incx, IntDist incy) noexcept
Move the drawing and aligned zones corners by those increments.
HorAlign
Horizontal alignment of widgets within the areas of their containers.
VertAlign
Vertical alignment of widgets within the areas of their containers.
All widgets must inherit from this.
Definition: ZXWidgetsBase.h:56
int ID
A numeric ID associated to the widget.
Definition: ZXWidgetsBase.h:67
Information about the fitting of widgets drawings into rectangular areas.
std::pair< bool, const CharRect * > hasFocusZone(void) const noexcept override
< Must return TRUE and the zone if the widget contains a focus-able zone.
const char * className(void) const noexcept
Must return a text with the name of the class.
const AreaFitting & calcFittingNeeds(const CharArea &area)
Must calculate the widget fitting within the dimensions of AREA.
Widget * setDrawingZone(const CharRect &zone) override
Set the final drawing zone for the widget and the aligned one.
const std::string & text(void) const
Return a ref to consult the current edited text.
void moveDrawingZones(IntDist incx, IntDist incy) noexcept override
Move the drawing and aligned zones corners by those increments.
TextEditorWidget(ID id, const std::string &name, const CharArea &area, const std::string &tooltiptext="", bool editable=true, HorAlign hal=HorAlign::LEFT, VertAlign val=VertAlign::TOP)
Constructor (initially empty text).
void setText(const std::string &txt)
Change the current text by TXT, ignoring all ctrl chars.
void moveTop(void)
Move the content to start showing the first line at top.
A widget that shows an area for editing multi-line text with word-wrapping.
Area< CharDistT, CharDistT > CharArea
Shortcut for this kind of area.
Definition: ZXGraphics.h:605
int16_t IntDist
Positive and negative distances on screen.
Definition: ZXGraphics.h:99
The main namespace of the library, that spans across all the zx modules.
Definition: ZXChars.h:31
constexpr uint8_t CHRSPC
space
Definition: ZXChars.h:43