1// This file is part of SmallBASIC
2//
3// Copyright(C) 2001-2014 Chris Warren-Smith.
4//
5// This program is distributed under the terms of the GPL v2.0 or later
6// Download the GNU Public License (GPL) from www.gnu.org
7//
8
9#ifndef INPUTS_H
10#define INPUTS_H
11
12#include <config.h>
13
14#include "common/var.h"
15#include "lib/maapi.h"
16#include "ui/strlib.h"
17#include "ui/shape.h"
18#include "ui/image.h"
19#include "ui/utils.h"
20
21const uint32_t colors[] = {
22 0x000000, // 0 black
23 0x000080, // 1 blue
24 0x008000, // 2 green
25 0x008080, // 3 cyan
26 0x800000, // 4 red
27 0x800080, // 5 magenta
28 0x808000, // 6 yellow
29 0xC0C0C0, // 7 white
30 0x808080, // 8 gray
31 0x0000FF, // 9 light blue
32 0x00FF00, // 10 light green
33 0x00FFFF, // 11 light cyan
34 0xFF0000, // 12 light red
35 0xFF00FF, // 13 light magenta
36 0xFFFF00, // 14 light yellow
37 0xFFFFFF // 15 bright white
38};
39
40using namespace strlib;
41
42#define BN_H 12
43#define BN_W 16
44#define LN_H 2
45#define CHOICE_BN_W 6
46#define GRAY_BG_COL 0x4e4c46
47#define LABEL_TEXT_COL 0xdfdbd2
48#define FOCUS_COLOR 0x444999
49
50#define FORM_VALUE "value"
51#define FORM_INPUTS "inputs"
52#define FORM_FOCUS "focus"
53#define FORM_INPUT_ID "ID"
54#define FORM_INPUT_X "x"
55#define FORM_INPUT_Y "y"
56#define FORM_INPUT_W "width"
57#define FORM_INPUT_H "height"
58#define FORM_INPUT_VALUE "value"
59#define FORM_INPUT_LABEL "label"
60#define FORM_INPUT_NAME "name"
61#define FORM_INPUT_TYPE "type"
62#define FORM_INPUT_HELP "help"
63#define FORM_INPUT_BG "backgroundColor"
64#define FORM_INPUT_FG "color"
65#define FORM_INPUT_IS_EXIT "isExit"
66#define FORM_INPUT_IS_EXTERNAL "isExternal"
67#define FORM_INPUT_RESIZABLE "resizable"
68#define FORM_INPUT_VISIBLE "visible"
69#define FORM_INPUT_INDEX "selectedIndex"
70#define FORM_INPUT_LENGTH "length"
71#define FORM_INPUT_NO_FOCUS "noFocus"
72#define FORM_INPUT_ONCLICK "onclick"
73
74namespace form_ui {
75 bool optionSelected(int index);
76};
77
78void set_input_defaults(int fg, int bg);
79int get_color(var_p_t value, int def);
80
81struct IFormWidgetListModel {
82 virtual ~IFormWidgetListModel() {}
83 virtual const char *getTextAt(int index) = 0;
84 virtual int getIndex(const char *label) = 0;
85 virtual int rows() const = 0;
86 virtual int selected() const = 0;
87 virtual void selected(int index) = 0;
88};
89
90struct ListModel : IFormWidgetListModel {
91 ListModel(int selectedIndex, var_t *v);
92 virtual ~ListModel() { clear(); }
93
94 void clear();
95 void create(var_t *v);
96 const char *getTextAt(int index);
97 int getIndex(const char *t);
98 int rows() const { return _list.size(); }
99 int selected() const { return _selectedIndex; }
100 void selected(int index) { _selectedIndex = index; }
101
102private:
103 void fromArray(var_t *v);
104 StringList _list;
105 int _selectedIndex;
106};
107
108struct FormInput : public Shape {
109 FormInput(int x, int y, int w, int h);
110 virtual ~FormInput();
111
112 virtual bool selected(MAPoint2d pt, int scrollX, int scrollY, bool &redraw);
113 virtual const char *getText() const { return NULL; }
114 virtual void setText(const char *text) {}
115 virtual bool edit(int key, int screenWidth, int charWidth);
116 virtual void updateField(var_p_t form) {};
117 virtual void updateForm(var_p_t form);
118 virtual bool updateUI(var_p_t form, var_p_t field);
119 virtual int padding(bool vert) const { return vert ? BN_H : BN_W; }
120 virtual void clicked(int x, int y, bool pressed);
121 virtual bool isDrawTop() { return false; }
122 virtual bool hasHover() { return false; }
123 virtual void setFocus(bool focus);
124 virtual void layout(int w, int h) {}
125
126 void construct(var_p_t form, var_p_t field, int id);
127 void drawButton(const char *caption, int x, int y, int w, int h, bool pressed);
128 void drawHover(int dx, int dy, bool selected);
129 void drawLink(const char *caption, int x, int y, int sw, int chw);
130 void drawText(const char *text, int x, int y, int sw, int chw);
131 void draw(int x, int y, int w, int h, int chw);
132 const char *getValue();
133 bool overlaps(MAPoint2d pt, int scrollX, int scrollY);
134 bool hasFocus() const;
135 void hide() { _visible = false; }
136 int getId() { return _id; }
137 var_p_t getField(var_p_t form);
138 bool isNoFocus() { return _noFocus; }
139 bool isResizable() { return _resizable; }
140 bool isVisible() { return _visible; }
141 void setColor(int bg, int fg) { _bg = bg; _fg = fg; }
142 void setTextColor();
143 void setHelpTextColor();
144 void selected();
145 void show() { _visible = true; }
146 bool _pressed;
147
148protected:
149 bool __padding[3];
150 int _id;
151 bool _exit;
152 bool _visible;
153 bool _noFocus;
154 bool _resizable;
155 int _bg;
156 int _fg;
157 bcip_t _onclick;
158};
159
160struct FormButton : public FormInput {
161 FormButton(const char *caption, int x, int y, int w, int h);
162 virtual ~FormButton() {}
163
164 const char *getText() const { return _label.c_str(); }
165 void draw(int x, int y, int w, int h, int chw) {
166 drawButton(_label.c_str(), x, y, _width, _height, _pressed);
167 }
168 void setText(const char *text) { _label = text; }
169
170protected:
171 String _label;
172};
173
174struct FormLabel : public FormInput {
175 FormLabel(const char *caption, int x, int y, int w, int h);
176 virtual ~FormLabel() {}
177
178 bool updateUI(var_p_t form, var_p_t field);
179 const char *getText() const { return _label.c_str(); }
180 void draw(int x, int y, int w, int h, int chw);
181 void setText(const char *text) { _label = text; }
182 int padding(bool vert) const { return 0; }
183 void clicked(int x, int y, bool pressed) {}
184 bool selected(MAPoint2d pt, int scrollX, int scrollY, bool &redraw) { return false; }
185
186private:
187 String _label;
188};
189
190struct FormLink : public FormInput {
191 FormLink(const char *link, bool external, int x, int y, int w, int h);
192 virtual ~FormLink() {}
193
194 void clicked(int x, int y, bool pressed);
195 const char *getText() const { return _link.c_str(); }
196 bool hasHover() { return true; }
197
198 void draw(int x, int y, int w, int h, int chw);
199 int padding(bool vert) const { return vert ? LN_H : 0; }
200
201protected:
202 String _link;
203 bool _external;
204};
205
206struct FormEditInput : public FormInput {
207 FormEditInput(int x, int y, int w, int h);
208 virtual ~FormEditInput();
209
210 virtual char *copy(bool cut) = 0;
211 virtual void paste(const char *text) = 0;
212 virtual void selectAll() = 0;
213 virtual const char *completeKeyword(int index) = 0;
214 virtual int getCompletions(StringList *list, int max) = 0;
215
216 void clicked(int x, int y, bool pressed);
217 void setFocus(bool focus);
218 int getControlKey(int key);
219 bool getControlMode() const { return _controlMode; }
220 void setControlMode(bool cursorMode) { _controlMode = cursorMode; }
221
222protected:
223 bool _controlMode;
224};
225
226struct FormLineInput : public FormEditInput {
227 FormLineInput(const char *text, const char *help,
228 int maxSize, bool grow, int x, int y, int w, int h);
229 virtual ~FormLineInput();
230
231 void close();
232 void draw(int x, int y, int w, int h, int chw);
233 bool edit(int key, int screenWidth, int charWidth);
234 const char *getText() const { return _buffer; }
235 void setText(const char *text) {}
236 void clicked(int x, int y, bool pressed);
237 void updateField(var_p_t form);
238 bool updateUI(var_p_t form, var_p_t field);
239 bool selected(MAPoint2d pt, int scrollX, int scrollY, bool &redraw);
240 int padding(bool) const { return 0; }
241 char *copy(bool cut);
242 void paste(const char *text);
243 void cut();
244 void selectAll();
245 const char *completeKeyword(int index) { return NULL; }
246 int getCompletions(StringList *list, int max) { return 0; }
247
248private:
249 String _help;
250 char *_buffer;
251 int _size;
252 int _scroll;
253 int _mark;
254 int _point;
255 bool _grow;
256};
257
258struct FormList : public FormInput {
259 FormList(ListModel *model, int x, int y, int w, int h);
260 virtual ~FormList();
261
262 bool edit(int key, int screenWidth, int charWidth);
263 const char *getText() const { return _model->getTextAt(_model->selected()); }
264 void optionSelected(int index);
265 void updateForm(var_p_t form);
266 bool updateUI(var_p_t form, var_p_t field);
267 virtual int getListHeight() const { return _height; }
268
269protected:
270 void selectIndex(int index);
271
272 ListModel *_model;
273 // scroll offset
274 int _topIndex;
275 // zero is first display item
276 int _activeIndex;
277};
278
279struct FormDropList : public FormList {
280 FormDropList(ListModel *model, int x, int y, int w, int h);
281 virtual ~FormDropList() {}
282
283 void clicked(int x, int y, bool pressed);
284 void draw(int dx, int dy, int w, int h, int chw);
285 bool selected(MAPoint2d pt, int scrollX, int scrollY, bool &redraw);
286 void updateForm(var_p_t form);
287 bool isDrawTop() { return _listActive; }
288 int getListHeight() const { return _listHeight; }
289
290private:
291 void drawList(int dx, int dy, int sh);
292 int _listWidth;
293 int _listHeight;
294 int _visibleRows;
295 bool _listActive;
296};
297
298struct FormListBox : public FormList {
299 FormListBox(ListModel *model, const char *help, int x, int y, int w, int h);
300 void clicked(int x, int y, bool pressed);
301 void draw(int x, int y, int w, int h, int chw);
302 bool selected(MAPoint2d pt, int scrollX, int scrollY, bool &redraw);
303 virtual ~FormListBox() {}
304private:
305 String _help;
306};
307
308struct FormImage : public FormInput {
309 FormImage(ImageDisplay *image, int x, int y);
310 virtual ~FormImage();
311 void draw(int x, int y, int w, int h, int chw);
312
313private:
314 ImageDisplay *_image;
315};
316
317struct MenuButton : public FormButton {
318 MenuButton(int index, int &selectedIndex,
319 const char *caption, int x, int y, int w, int h);
320 void clicked(int x, int y, bool pressed);
321 void draw(int x, int y, int w, int h, int chw);
322
323 int &_selectedIndex;
324 int _index;
325};
326
327FormEditInput *get_focus_edit();
328
329#endif
330