The ZX Ecosystem v5.1.0;_GUI_v3.1.0
Loading...
Searching...
No Matches
ZXWidgetsSimple.h
Go to the documentation of this file.
1
2/* **************************************************************************/
20#ifndef ZXWIDGETS_SIMPLE
21#define ZXWIDGETS_SIMPLE
22
24
25
26namespace zxeco
27{
28
29namespace gui
30{
31
32/*****************************************************************************
33*
34* Class: LabelWidget
35*
36*******************************************************************************/
37
39
44class LabelWidget: public Widget
45{
46 public:
47
48 LabelWidget(ID id, const std::string & name,
49 const std::string & initlabel,
50 const std::string & tooltiptext = "",
51 HorAlign hal = HorAlign::LEFT,
52 VertAlign val = VertAlign::TOP): Widget{id,name,tooltiptext,
53 hal,val},
54 label_{initlabel} {}
56
60 const std::string & getLabel(void) noexcept { return(label_.get()); }
63 void setLabel(const std::string & newlab);
65
67 const char * className(void) const noexcept { return("LabelWidget"); }
68 const AreaFitting & calcFittingNeeds(const CharArea & area);
69
70
71 private:
72
73 Label label_;
74
75 void drawRaw(void);
76};
77
78
79/*****************************************************************************
80*
81* Class: ButtonWidget
82*
83*******************************************************************************/
84
86class ButtonWidget: public Widget
87{
88 public:
89
90
91 ButtonWidget(ID id, const std::string & name,
92 const std::string & initlabel,
93 bool withborder = true,
94 const std::string & tooltiptext = "",
95 HorAlign hal = HorAlign::LEFT,
96 VertAlign val = VertAlign::TOP): Widget{id,name,
97 tooltiptext,
98 hal,val},
99 withborder_{withborder},
100 isimage_{false},
101 image_{{0,0}},
102 label_{initlabel},
103 pressed_{false}
105
107 { if (label_.get().empty())
108 RUNTIMEEXCEP("Cannot create empty label button"); }
109
110 ButtonWidget(ID id, const std::string & name,
111 const DoubleGraphic & img, const CharRect & region,
112 bool withborder = false,
113 const std::string & tooltiptext = "",
114 HorAlign hal = HorAlign::LEFT,
115 VertAlign val = VertAlign::TOP):
116 Widget{id,name,
117 tooltiptext,
118 hal,val},
119 withborder_{withborder},
120 isimage_{true},
121 image_{const_cast<DoubleGraphic &>(img),region},
122 pressed_{false}
124
128 { if (image_.attrmap.dims.empty())
129 RUNTIMEEXCEP("Cannot create image button with empty image"); }
130
131 const std::string & getLabel(void) noexcept { return(label_.get()); }
133
134 void setLabel(const std::string & newlab);
136
139
140 void setImage(const DoubleGraphic & img, const CharRect & region);
142
143
144 const char * className(void) const noexcept { return("ButtonWidget"); }
145 const AreaFitting & calcFittingNeeds(const CharArea & area);
146
147
148 private:
149
150 static const CharArea kBordersMargin;
151
152 const bool withborder_;
153 const bool isimage_;
154
155 DoubleGraphic image_;
156 Label label_;
157 bool pressed_;
158
159 PixRect borderRect(bool pressed) const;
160 void drawRaw(void);
161 int processEvent(const EventData & evdata, Widget * who) override;
162};
163
164
165/*****************************************************************************
166*
167* Class: SwitchWidget
168*
169*******************************************************************************/
170
172class SwitchWidget: public Widget
173{
174 public:
175
176 SwitchWidget(ID id, const std::string & name,
177 const std::string & labelleft, const std::string & labelright,
178 const std::string & tooltiptext = "",
179 HorAlign hal = HorAlign::LEFT,
180 VertAlign val = VertAlign::TOP);
182
190 bool state(void) const noexcept { return(pressed_); }
192
193 void changeState(bool st);
195
198 const char * className(void) const noexcept { return("SwitchWidget"); }
199 const AreaFitting & calcFittingNeeds(const CharArea & area);
200
201
202 private:
203
204 static const uint8_t kUDGs[2*8];
205
206 std::string labelleft_,labelright_;
207 bool pressed_;
208
209 void drawRaw(void);
210 int processEvent(const EventData & evdata, Widget * who) override;
211};
212
213
214/*****************************************************************************
215*
216* Class: LineInputWidget
217*
218*******************************************************************************/
219
221
224{
225 public:
226
227 LineInputWidget(ID id, const std::string & name,
228 CharDist inputlen = 0, bool editable = true,
229 bool enterendsedition = true,
230 CharDist markedlen = 0, const uint8_t * printfont = nullptr,
231 const std::string & tooltiptext = "",
232 HorAlign hal = HorAlign::LEFT,
233 VertAlign val = VertAlign::TOP);
235
250 virtual ~LineInputWidget(void);
252
253
254 const std::string & text(void) const noexcept { return(typedtxt_); }
256
257 void setText(const std::string & txt);
259
264 void putCursorAtExtreme(char which);
266
270 bool hasLostFocusOrEnter(const Widget::CallbackParms & cbps) noexcept
271 { return(cbps.isAfterWithData() &&
272 (cbps.pevd->isLostFocus() || cbps.hasPressedEnter())); }
273
274 void moveDrawingZones(IntDist incx, IntDist incy) noexcept override
275 { typing_zone_.move(incx,incy); Widget::moveDrawingZones(incx,incy); }
276 std::pair<bool,const CharRect *> hasFocusZone(void) const noexcept override
277 { return(std::make_pair(true,&typing_zone_.get())); }
278
279 const char * className(void) const noexcept { return("LineInputWidget"); }
280 const AreaFitting & calcFittingNeeds(const CharArea & area);
281
282
283 private:
284
285 const size_t marklen_;
286 const CharDist typlen_;
287 const bool editable_;
288 const bool enterendsedition_;
289 const uint8_t * origfont_;
290
291 uint8_t * printfont_;
292 TypingZone typing_zone_;
293 std::string typedtxt_,prevtypedtxt_;
294 CharCoord editcol_;
295 std::string::size_type firstshown_;
296 std::string::size_type curposintxt_;
297 bool pendingextreme_;
298
299 void drawTextAndCursor(void);
300 void drawRaw(void);
301 bool checkMouseClickInTyping(const EventData & evdata);
302 int processEvent(const EventData & evdata, Widget * who) override;
303 std::string to_string(void) const;
304};
305
306
307/*******************************************************************************
308*
309* Class: ChoiceWidget
310*
311*******************************************************************************/
312
314
315class ChoiceWidget: public Widget
316{
317 public:
318
319 ChoiceWidget(ID id, const std::string & name,
320 const Label::Vector & labels, char kindofselection,
321 bool horizorvert = false,
322 const std::string & tooltiptext = "",
323 HorAlign hal = HorAlign::LEFT,
324 VertAlign val = VertAlign::TOP);
326
332 size_t numberOfChoices(void) const noexcept { return(labels_.size()); }
334
335 bool isSelected(size_t who) const { return(labels_.marked(who)); }
337
338 int firstSelected(void) const noexcept;
340
341 void setSelect(size_t who, bool select = true)
343 { bool res = select ? labels_.mark(who) : labels_.unmark(who);
344 if (res && drawn()) drawSelects(); }
345
346 void toggleSelect(size_t who)
348 { labels_.toggle(who); if (drawn()) drawSelects(); }
349
350 void setAll(bool select = true)
352
353 { bool res = select ? labels_.markAll() : labels_.unmarkAll();
354 if (res && drawn()) drawSelects(); }
355
356 void toggleAll(void) { labels_.toggleAll(); if (drawn()) drawSelects(); }
358
360 const LabelList & labels(void) const noexcept { return(labels_); }
363 size_t whoCursor(const PointingStatus & coords) const noexcept;
366 const char * className(void) const noexcept { return("ChoiceWidget"); }
367 const AreaFitting & calcFittingNeeds(const CharArea & area);
368
369 private:
370
371 static const uint8_t kUDGs[4*8];
372
373 const bool horizorvert_;
374 CharDist charsbetweensels_;
375
376 LabelList labels_;
377 CharArea neededspace_;
378
379 void drawSelects(void);
380 void drawRaw(void);
381 int processEvent(const EventData & evdata, Widget * who) override;
382};
383
384
385/*****************************************************************************
386*
387* Class: SeparatorWidget
388*
389*******************************************************************************/
390
393{
394 public:
395
396 SeparatorWidget(ID id, const std::string & name,
397 CharDist separation = 1):Widget{id,name,"",
398 HorAlign::LEFT,
399 VertAlign::TOP},
400 sep_{separation}
402 { if (separation <= 0) RUNTIMEEXCEP("Invalid separation"); }
403
404
405 const char * className(void) const noexcept { return("SeparatorWidget"); }
406 const AreaFitting & calcFittingNeeds(const CharArea & area);
407
408 private:
409
410 const CharDist sep_;
411};
412
413
414/*****************************************************************************
415*
416* Class: ImageWidget
417*
418*******************************************************************************/
419
421
422class ImageWidget: public Widget
423{
424 public:
425
426 ImageWidget(ID id, const std::string & name,
427 const DoubleGraphic & img, const CharRect & region,
428 const std::string & tooltip = "",
429 HorAlign hal = HorAlign::LEFT,
430 VertAlign val = VertAlign::TOP):
431 Widget{id,name,tooltip,hal,val},
432 image_{const_cast<DoubleGraphic &>(img),region}
433 { setImage(img,region); }
435
438 const DoubleGraphic & getImage(void) const noexcept { return(image_); }
441 void setImage(const DoubleGraphic & img, const CharRect & region);
443
445 const char * className(void) const noexcept { return("ImageWidget"); }
446 const AreaFitting & calcFittingNeeds(const CharArea & area);
447
448
449 private:
450
451 DoubleGraphic image_;
452
453 void drawRaw(void);
454};
455
456
457
458} // end namespace gui
459} // end namespace zxeco
460
461#endif
462 // ZXWidgetsSimple
464
465
466
#define RUNTIMEEXCEP(txt)
Raise a runtime exception with the given std::string TXT + additional info.
Definition: CppAddons.h:93
const std::string & get(void) const noexcept
Get the current label text.
Definition: ZXGUIGeneral.h:203
std::vector< Label > Vector
A vector of labels.
Definition: ZXGUIGeneral.h:183
A non-empty label for a widget.
Definition: ZXGUIGeneral.h:179
A list of labels to be displayed vertically, with possible marks on them.
Definition: ZXGUIGeneral.h:231
A zone of a widget where the user can type.
Definition: ZXGUIGeneral.h:145
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.
A class to contain the parameters received by a callback routine.
Data passed to a widget when an event occurs.
size_t numberOfChoices(void) const noexcept
Return the number of choices. *‍/.
const std::string & getLabel(void) noexcept
Return the label text.
ButtonWidget(ID id, const std::string &name, const std::string &initlabel, bool withborder=true, const std::string &tooltiptext="", HorAlign hal=HorAlign::LEFT, VertAlign val=VertAlign::TOP)
void setText(const std::string &txt)
Change the current text by TXT, ignoring all ctrl chars.
std::pair< bool, const CharRect * > hasFocusZone(void) const noexcept override
< Must return TRUE and the zone if the widget contains a focus-able zone.
bool hasLostFocusOrEnter(const Widget::CallbackParms &cbps) noexcept
Return TRUE if the focus is being lost or ENTER has been pressed.
void setImage(const DoubleGraphic &img, const CharRect &region)
Change the image and redraw the widget or throw if not an image button.
const AreaFitting & calcFittingNeeds(const CharArea &area)
Must calculate the widget fitting within the dimensions of AREA.
void changeState(bool st)
Change the state to ST if different from current one.
const char * className(void) const noexcept
Must return a text with the name of the class.
ButtonWidget(ID id, const std::string &name, const DoubleGraphic &img, const CharRect &region, bool withborder=false, const std::string &tooltiptext="", HorAlign hal=HorAlign::LEFT, VertAlign val=VertAlign::TOP)
void toggleAll(void)
Toggle the selection of all options.
const AreaFitting & calcFittingNeeds(const CharArea &area)
Must calculate the widget fitting within the dimensions of AREA.
const AreaFitting & calcFittingNeeds(const CharArea &area)
Must calculate the widget fitting within the dimensions of AREA.
ImageWidget(ID id, const std::string &name, const DoubleGraphic &img, const CharRect &region, const std::string &tooltip="", HorAlign hal=HorAlign::LEFT, VertAlign val=VertAlign::TOP)
Constructor for an image. The image is copied internally.
const char * className(void) const noexcept
Must return a text with the name of the class.
int firstSelected(void) const noexcept
Return the index of the first selected option, or -1 if none.
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.
void setAll(bool select=true)
bool isSelected(size_t who) const
Return TRUE if the given option is currently selected.
void setLabel(const std::string &newlab)
Change the label and redraw the widget.
void moveDrawingZones(IntDist incx, IntDist incy) noexcept override
Move the drawing and aligned zones corners by those increments.
void setLabel(const std::string &newlab)
Change the label and redraw the widget or throw if it is not text buttn.
const AreaFitting & calcFittingNeeds(const CharArea &area)
Must calculate the widget fitting within the dimensions of AREA.
virtual ~LineInputWidget(void)
Destructor.
LabelWidget(ID id, const std::string &name, const std::string &initlabel, const std::string &tooltiptext="", HorAlign hal=HorAlign::LEFT, VertAlign val=VertAlign::TOP)
Constructor. The initial label is copied internally.
const std::string & text(void) const noexcept
Return the text currently in the line.
LineInputWidget(ID id, const std::string &name, CharDist inputlen=0, bool editable=true, bool enterendsedition=true, CharDist markedlen=0, const uint8_t *printfont=nullptr, const std::string &tooltiptext="", HorAlign hal=HorAlign::LEFT, VertAlign val=VertAlign::TOP)
Constructor.
DoubleGraphic & getImage(void)
Return a reference to the image or throw if it is not an image button.
void setSelect(size_t who, bool select=true)
void toggleSelect(size_t who)
const char * className(void) const noexcept
Must return a text with the name of the class.
const char * className(void) const noexcept
Must return a text with the name of the class.
const std::string & getLabel(void) noexcept
Return the current label text.
void putCursorAtExtreme(char which)
Put the cursor to the end ('E') or to the beginning ('B') in the text.
SwitchWidget(ID id, const std::string &name, const std::string &labelleft, const std::string &labelright, const std::string &tooltiptext="", HorAlign hal=HorAlign::LEFT, VertAlign val=VertAlign::TOP)
Constructor for a text-label button.
bool state(void) const noexcept
Return TRUE if the switch is ON.
ChoiceWidget(ID id, const std::string &name, const Label::Vector &labels, char kindofselection, bool horizorvert=false, const std::string &tooltiptext="", HorAlign hal=HorAlign::LEFT, VertAlign val=VertAlign::TOP)
Constructor. Labels are copied internally. They can contain ctrl color.
A widget that is a button, either text or image.
A widget consisting of a number of options, for single or multiple selection.
A widget consisting in just one image.
A widget consisting only in a single-line label, with possibly control codes.
A widget that shows a line for user typing.
A separator between adjacent widgets.
A widget that is a button with two positions that are held over time.
uint16_t CharCoord
Represent both x and y screen char coordinates.
Definition: ZXGraphics.h:94
int16_t IntDist
Positive and negative distances on screen.
Definition: ZXGraphics.h:99
uint16_t CharDist
A distance meadured in screen chars.
Definition: ZXGraphics.h:97
A bitmap plus an attrmap, possibly synchronized.
Definition: ZXGraphics.h:1250
Status of the pointing device.
Definition: ZXPointing.h:39
The main namespace of the library, that spans across all the zx modules.
Definition: ZXChars.h:31