FabGL
ESP32 Display Controller and Graphics Library
inputbox.h
Go to the documentation of this file.
1/*
2 Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3 Copyright (c) 2019-2022 Fabrizio Di Vittorio.
4 All rights reserved.
5
6
7* Please contact fdivitto2013@gmail.com if you need a commercial license.
8
9
10* This library and related software is available under GPL v3.
11
12 FabGL is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 FabGL is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26
27#pragma once
28
29
38#include <stdint.h>
39#include <stddef.h>
40
41#include "fabglconf.h"
42#include "fabui.h"
43#include "fabutils.h"
46
47
48
49namespace fabgl {
50
51
52
56enum class InputResult {
57 None = 0,
58 ButtonExt0 = 1,
59 ButtonExt1 = 2,
60 ButtonExt2 = 3,
61 ButtonExt3 = 4,
62 Cancel = 5,
63 ButtonLeft = 5,
64 Enter = 6,
65 ButtonRight = 6,
66};
67
68
69
71// InputForm
72
73
74class InputBox;
75
76
77struct InputForm {
78 InputForm(InputBox * inputBox_)
79 : inputBox(inputBox_)
80 {
81 }
82
83 void init(uiApp * app_, bool modalDialog_);
84
85 virtual void addControls() = 0;
86 virtual void calcRequiredSize() = 0;
87 virtual void finalize() { }
88 virtual void show() { }
89
90 void doExit(int value);
91 void defaultEnterHandler(uiKeyEventInfo const & key);
92 void defaultEscapeHandler(uiKeyEventInfo const & key);
93
94
95 static constexpr int BUTTONS = 6;
96
97 InputBox * inputBox;
98
99 uiApp * app;
100
101 char const * titleText;
102 int autoOK;
103
104 FontInfo const * font;
105 int requiredWidth;
106 int requiredHeight;
107
108 uiFrame * mainFrame;
109 uiPanel * panel;
110 uiLabel * autoOKLabel;
111
112 InputResult retval;
113 int buttonSubItem; // in case of button with subitems, specifies the selected subitem
114
115 uiWindow * controlToFocus;
116
117 bool modalDialog;
118};
119
120
121
123// InputApp
124
125
126struct InputApp : public uiApp {
127 InputApp(InputForm * form_) { form = form_; }
128 virtual void init() { form->init(this, false); }
129
130 InputForm * form;
131};
132
133
134
135
137// TextInputForm
138
139
140struct TextInputForm : public InputForm {
141 TextInputForm(InputBox * inputBox_)
142 : InputForm(inputBox_)
143 {
144 }
145
146 void addControls();
147 void calcRequiredSize();
148 void finalize();
149
150 char const * labelText;
151 char * inOutString;
152 int maxLength;
153 bool passwordMode;
154
155 int editExtent;
156 int labelExtent;
157
158 uiTextEdit * edit;
159};
160
161
162
164// MessageForm
165
166
167struct MessageForm : public InputForm {
168 MessageForm(InputBox * inputBox_)
169 : InputForm(inputBox_)
170 {
171 }
172
173 void addControls();
174 void calcRequiredSize();
175 void finalize();
176
177 char const * messageText;
178
179 int messageExtent;
180};
181
182
183
185// SelectForm
186
187
188struct SelectForm : public InputForm {
189 SelectForm(InputBox * inputBox_)
190 : InputForm(inputBox_)
191 {
192 }
193
194 void addControls();
195 void calcRequiredSize();
196 void finalize();
197
198 int countItems(size_t * maxLength);
199
200 char const * messageText;
201 char const * items; // "separator" separated items (zero ends the list)
202 char separator;
203 StringList * itemsList;
204 bool menuMode;
205
206 int listBoxHeight;
207 int outSelected;
208
209 uiListBox * listBox;
210};
211
212
213
215// ProgressForm
216
217
218struct ProgressForm : public InputForm {
219 ProgressForm(InputBox * inputBox_)
220 : InputForm(inputBox_)
221 {
222 }
223
224 void addControls();
225 void calcRequiredSize();
226 void show();
227
228 bool update(int percentage, char const * format, ...);
229
230 static const int progressBarHeight = 16;
231
232 bool hasProgressBar;
233 Delegate<ProgressForm*> execFunc;
234 int width;
235
236 uiLabel * label;
237 uiProgressBar * progressBar;
238};
239
240
241
243// FileBrowserForm
244
245
246struct FileBrowserForm : public InputForm {
247 static constexpr int SIDE_BUTTONS_WIDTH = 65;
248 static constexpr int SIDE_BUTTONS_HEIGHT = 18;
249 static constexpr int CTRLS_DIST = 4;
250 static constexpr int BROWSER_WIDTH = 150;
251 static constexpr int BROWSER_HEIGHT = 242;
252 static constexpr int MAXNAME = 32;
253
254 FileBrowserForm(InputBox * inputBox_)
255 : InputForm(inputBox_)
256 {
257 }
258 ~FileBrowserForm() {
259 free(srcDirectory);
260 free(srcFilename);
261 }
262 void doCopy();
263 void doPaste();
264
265 void addControls();
266 void calcRequiredSize();
267 void finalize();
268
269 char const * directory;
270
271 char * srcDirectory = nullptr;
272 char * srcFilename = nullptr;
273
274 uiFileBrowser * fileBrowser;
275 uiButton * newFolderButton;
276 uiButton * renameButton;
277 uiButton * deleteButton;
278 uiButton * copyButton;
279 uiButton * pasteButton;
280};
281
282
283
285// FileSelectorForm
286
287struct FileSelectorForm : public InputForm {
288 static constexpr int CTRLS_DIST = 4;
289 static constexpr int BROWSER_WIDTH = 180;
290 static constexpr int BROWSER_HEIGHT = 150;
291 static constexpr int MINIMUM_EDIT_WIDTH = 64;
292
293 FileSelectorForm(InputBox * inputBox_)
294 : InputForm(inputBox_)
295 {
296 }
297
298 void addControls();
299 void calcRequiredSize();
300 void finalize();
301
302 char const * labelText;
303 char * inOutDirectory;
304 int maxDirectoryLength;
305 char * inOutFilename;
306 int maxFilenameLength;
307
308 int editExtent;
309 int labelExtent;
310
311 uiTextEdit * edit;
312 uiFileBrowser * fileBrowser;
313};
314
315
316
318// InputBox
319
320
322class InputBox {
323
324public:
325
331 InputBox(uiApp * app = nullptr);
332
333 ~InputBox();
334
343 void begin(char const * modeline = nullptr, int viewPortWidth = -1, int viewPortHeight = -1, int displayColors = 16);
344
350 void begin(BitmappedDisplayController * displayController);
351
358
362 void end();
363
369 void setBackgroundColor(RGB888 const & value) { m_backgroundColor = value; }
370
371 RGB888 backgroundColor() { return m_backgroundColor; }
372
380 void setAutoOK(int timeout) { m_autoOK = timeout; }
381
392 void setupButton(int index, char const * text, char const * subItems = nullptr, int subItemsHeight = 80);
393
399 void setMinButtonsWidth(int value) { m_minButtonsWidth = value; }
400
401 int minButtonsWidth() { return m_minButtonsWidth; }
402
403 char const * buttonText(int index) { return m_buttonText[index]; }
404
405 char const * buttonSubItems(int index) { return m_buttonSubItems[index]; }
406
407 int buttonsSubItemsHeight(int index) { return m_buttonSubItemsHeight[index]; }
408
414 InputResult getLastResult() { return m_lastResult; }
415
421 int selectedSubItem() { return m_buttonSubItem; }
422
448 InputResult textInput(char const * titleText, char const * labelText, char * inOutString, int maxLength, char const * buttonCancelText = "Cancel", char const * buttonOKText = "OK", bool passwordMode = false);
449
472 InputResult message(char const * titleText, char const * messageText, char const * buttonCancelText = nullptr, char const * buttonOKText = "OK");
473
494 InputResult messageFmt(char const * titleText, char const * buttonCancelText, char const * buttonOKText, const char *format, ...);
495
516 int select(char const * titleText, char const * messageText, char const * itemsText, char separator = ';', char const * buttonCancelText = "Cancel", char const * buttonOKText = "OK");
517
546 InputResult select(char const * titleText, char const * messageText, StringList * items, char const * buttonCancelText = "Cancel", char const * buttonOKText = "OK");
547
566 int menu(char const * titleText, char const * messageText, char const * itemsText, char separator = ';');
567
590 int menu(char const * titleText, char const * messageText, StringList * items);
591
618 template <typename Func>
619 InputResult progressBox(char const * titleText, char const * buttonCancelText, bool hasProgressBar, int width, Func execFunc) {
620 ProgressForm form(this);
621 form.execFunc = execFunc;
622 return progressBoxImpl(form, titleText, buttonCancelText, hasProgressBar, width);
623 }
624
642 InputResult folderBrowser(char const * titleText, char const * directory = "/", char const * buttonOKText = "Close");
643
670 InputResult fileSelector(char const * titleText, char const * messageText, char * inOutDirectory, int maxDirectoryLength, char * inOutFilename, int maxFilenameLength, char const * buttonCancelText = "Cancel", char const * buttonOKText = "OK");
671
672
673 // delegates
674
678 Delegate<Canvas *> onPaint;
679
680
681
682private:
683
684 InputResult progressBoxImpl(ProgressForm & form, char const * titleText, char const * buttonCancelText, bool hasProgressBar, int width);
685
686 void exec(InputForm * form);
687 void resetButtons();
688
689 BitmappedDisplayController * m_dispCtrl;
690 VGAPalettedController * m_vgaCtrl;
691 RGB888 m_backgroundColor;
692 uiApp * m_existingApp; // uiApp in case of running on existing app
693 uint16_t m_autoOK; // auto ok in seconds
694 int16_t m_buttonSubItem; // in case of button with subitems, specifies the selected subitem //
695 char const * m_buttonText[InputForm::BUTTONS] = { };
696 char const * m_buttonSubItems[InputForm::BUTTONS] = { }; // ext button is uiButton is nullptr, uiSplitButton otherwise
697 uint16_t m_buttonSubItemsHeight[InputForm::BUTTONS] = { };
698 InputResult m_lastResult = InputResult::None;
699 int16_t m_minButtonsWidth;
700};
701
702
703
704} // namespace fabgl
Represents the base abstract class for bitmapped display controllers.
InputResult getLastResult()
Gets last dialog result.
Definition: inputbox.h:414
BitmappedDisplayController * getDisplayController()
Gets created or assigned display controller.
Definition: inputbox.h:357
InputResult textInput(char const *titleText, char const *labelText, char *inOutString, int maxLength, char const *buttonCancelText="Cancel", char const *buttonOKText="OK", bool passwordMode=false)
Shows a dialog with a label and a text edit box.
Definition: inputbox.cpp:146
InputResult message(char const *titleText, char const *messageText, char const *buttonCancelText=nullptr, char const *buttonOKText="OK")
Shows a dialog with just a label.
Definition: inputbox.cpp:164
void setMinButtonsWidth(int value)
Sets minimum buttons size.
Definition: inputbox.h:399
InputResult progressBox(char const *titleText, char const *buttonCancelText, bool hasProgressBar, int width, Func execFunc)
Shows a dialog with a label and a progress bar, updated dynamically by a user function.
Definition: inputbox.h:619
void setupButton(int index, char const *text, char const *subItems=nullptr, int subItemsHeight=80)
Setups extended button or split-button.
Definition: inputbox.cpp:112
InputResult fileSelector(char const *titleText, char const *messageText, char *inOutDirectory, int maxDirectoryLength, char *inOutFilename, int maxFilenameLength, char const *buttonCancelText="Cancel", char const *buttonOKText="OK")
Selects a file and directory starting from the specified path.
Definition: inputbox.cpp:295
int selectedSubItem()
Gets the selected item on a multichoice button.
Definition: inputbox.h:421
int menu(char const *titleText, char const *messageText, char const *itemsText, char separator=';')
Shows a dialog with a label and a list box. The dialog exits when an item is selected,...
Definition: inputbox.cpp:235
InputResult messageFmt(char const *titleText, char const *buttonCancelText, char const *buttonOKText, const char *format,...)
Shows a dialog with a just a label. Allows printf like formatted text.
Definition: inputbox.cpp:179
void end()
Cleanup resources and eventually disable VGA output.
Definition: inputbox.cpp:102
int select(char const *titleText, char const *messageText, char const *itemsText, char separator=';', char const *buttonCancelText="Cancel", char const *buttonOKText="OK")
Shows a dialog with a label and a list box.
Definition: inputbox.cpp:197
void begin(char const *modeline=nullptr, int viewPortWidth=-1, int viewPortHeight=-1, int displayColors=16)
Initializes InputBox from VGA modeline, using a VGA16Controller.
Definition: inputbox.cpp:73
void setAutoOK(int timeout)
Specifies a timeout for the dialog.
Definition: inputbox.h:380
InputBox(uiApp *app=nullptr)
Creates a new InputBox instance.
Definition: inputbox.cpp:57
InputResult folderBrowser(char const *titleText, char const *directory="/", char const *buttonOKText="Close")
Shows a dialog with files and folders and buttons to create new folders, delete and rename folders an...
Definition: inputbox.cpp:281
Delegate< Canvas * > onPaint
Paint event delegate.
Definition: inputbox.h:678
void setBackgroundColor(RGB888 const &value)
Sets the background color.
Definition: inputbox.h:369
InputBox is an helper class which allows to create simple UI interfaces, like wizards or simple input...
Definition: inputbox.h:322
Represents the base class for paletted bitmapped controllers like VGA16Controller,...
Represents the whole application base class.
Definition: fabui.h:3105
Represents a button control. A button can have text and optionally a bitmap.
Definition: fabui.h:1257
Shows and navigates Virtual Filesystem content.
Definition: fabui.h:2183
A frame is a window with a title bar, maximize/minimize/close buttons and that is resizeable or movea...
Definition: fabui.h:820
A label is a static text UI element.
Definition: fabui.h:1560
Shows a list of selectable string items.
Definition: fabui.h:2139
A panel is used to contain and to group some controls.
Definition: fabui.h:1813
A progress bar shows progress percentage using a colored bar.
Definition: fabui.h:2860
Represents a text edit control.
Definition: fabui.h:1418
uint8_t width
This file contains FabGL library configuration settings, like number of supported colors,...
This file contains all classes related to FabGL Graphical User Interface.
This file contains some utility classes and functions.
InputResult
Result of InputBox dialogs helper class.
Definition: inputbox.h:56
This file contains fabgl::PS2Controller definition.
Represents a 24 bit RGB color.
Contains details about the key event.
Definition: fabui.h:157
This file contains fabgl::VGAPalettedController definition.