summaryrefslogtreecommitdiff
path: root/code/fe310/eos/eve/widget/strw.c
blob: 82e86b78b466c80f85427ef8e3aa47bb0b0c6262 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "eve.h"
#include "eve_kbd.h"

#include "screen/screen.h"
#include "screen/window.h"
#include "screen/page.h"
#include "screen/font.h"

#include "label.h"
#include "widget.h"
#include "strw.h"

#define CH_BS                   0x08
#define CH_DEL                  0x7f
#define CH_EOF                  0x1a

#define CH_CTRLX                0x18
#define CH_CTRLC                0x03
#define CH_CTRLV                0x16

#define STRW_TMODE_CURSOR       1
#define STRW_TMODE_SCROLL_X     2
#define STRW_TMODE_SCROLL_Y     3

#define CHAR_VALID_INPUT(c)     ((c >= 0x20) && (c < 0x7f))

void eve_strw_init(EVEStrWidget *widget, EVERect *g, EVEFont *font, char *str, uint16_t str_size) {
    EVEWidget *_widget = &widget->w;

    memset(widget, 0, sizeof(EVEStrWidget));
    eve_widget_init(_widget, EVE_WIDGET_TYPE_STR, g, eve_strw_touch, eve_strw_draw, eve_strw_putc);
    widget->font = font;
    widget->str = str;
    widget->str_size = str_size;
    widget->str_len = strlen(str);
    widget->str_g.w = eve_font_str_w(font, str);
    if (_widget->g.h == 0) _widget->g.h = eve_font_h(font);
}

static void set_focus(EVEStrWidget *widget, EVEPage *page) {
    EVERect focus;
    EVEWidget *_widget = &widget->w;

    focus.x = _widget->g.x;
    focus.y = _widget->g.y;
    focus.w = _widget->g.w;
    focus.h = 2 * widget->font->h;
    eve_page_set_focus(page, _widget, &focus);
}

static EVEStrCursor *cursor_prox(EVEStrWidget *widget, EVEStrCursor *cursor, EVEPage *page, EVETouch *t, short *dx) {
    EVEWidget *_widget = &widget->w;
    int x = eve_page_x(page, t->x0) - _widget->g.x + widget->str_g.x;
    int _dx;

    *dx = cursor->x - x;
    _dx = *dx < 0 ? -(*dx) : *dx;

    if (_dx <= widget->font->w) return cursor;
    return NULL;
}

int eve_strw_touch(EVEWidget *_widget, EVEPage *page, uint8_t tag0, int touch_idx) {
    EVEStrWidget *widget = (EVEStrWidget *)_widget;
    EVETouch *t;
    uint16_t evt;

    if (touch_idx > 0) return 0;

    t = eve_touch_evt(tag0, touch_idx, widget->tag, 1, &evt);
    if (t && evt) {
        EVEStrCursor *t_cursor = NULL;
        short dx;

        if (evt & (EVE_TOUCH_ETYPE_LPRESS | EVE_TOUCH_ETYPE_TRACK_START)) {
            if (widget->cursor2.on) {
                t_cursor = cursor_prox(widget, &widget->cursor2, page, t, &dx);
            }
            if ((t_cursor == NULL) && widget->cursor1.on) {
                t_cursor = cursor_prox(widget, &widget->cursor1, page, t, &dx);
            }
            if (evt & EVE_TOUCH_ETYPE_TRACK_START) {
                widget->track.cursor = t_cursor;
                if (t_cursor) {
                    widget->track.mode = STRW_TMODE_CURSOR;
                    widget->track.dx = dx;
                } else if (t->eevt & EVE_TOUCH_EETYPE_TRACK_X) {
                    widget->track.mode = STRW_TMODE_SCROLL_X;
                } else if (t->eevt & EVE_TOUCH_EETYPE_TRACK_Y) {
                    widget->track.mode = STRW_TMODE_SCROLL_Y;
                }
            }
        }

        switch (widget->track.mode) {
            case STRW_TMODE_SCROLL_Y:
                if (page->handle_evt) page->handle_evt(page, _widget, t, evt, tag0, touch_idx);
                break;

            case STRW_TMODE_SCROLL_X:
                if (evt & EVE_TOUCH_ETYPE_TRACK_START) {
                    widget->str_g.x0 = widget->str_g.x;
                }
                if (evt & EVE_TOUCH_ETYPE_TRACK) {
                    int x = widget->str_g.x0 + t->x0 - t->x;
                    int w1 = widget->w.g.w - widget->font->w;

                    if (x > widget->str_g.w - w1) x = widget->str_g.w - w1;
                    if (x < 0) x = 0;
                    widget->str_g.x = x;
                }
                break;

            case STRW_TMODE_CURSOR:
                if (evt & EVE_TOUCH_ETYPE_TRACK) eve_strw_cursor_set(widget, widget->track.cursor, eve_page_x(page, t->x) + widget->track.dx);
                break;

            default:
                if (evt & EVE_TOUCH_ETYPE_LPRESS) {
                    if (widget->cursor2.on) {
                        // copy
                    } else if (widget->cursor1.on) {
                        if (t_cursor) {
                            // paste
                        } else {
                            eve_strw_cursor_set(widget, &widget->cursor2, eve_page_x(page, t->x));
                        }
                    } else {
                        // select
                    }
                }
                if ((evt & EVE_TOUCH_ETYPE_POINT_UP) && !(t->eevt & EVE_TOUCH_EETYPE_LPRESS)) {
                    eve_strw_cursor_set(widget, &widget->cursor1, eve_page_x(page, t->x0));
                    if (widget->cursor2.on) eve_strw_cursor_clear(widget, &widget->cursor2);
                    set_focus(widget, page);
                }
                break;
        }

        if (evt & EVE_TOUCH_ETYPE_TRACK_STOP) {
            widget->track.mode = 0;
            widget->track.cursor = NULL;
            widget->track.dx = 0;
        }

        return 1;
    }

    return 0;
}

static void _draw_str(EVEStrWidget *widget, EVEWindow *window, uint16_t ch, uint16_t len, uint16_t x1, uint16_t x2, char s) {
    int16_t x;
    EVEWidget *_widget = &widget->w;

    x = _widget->g.x - widget->str_g.x;
    if (x1 != x2) {
        eve_cmd_dl(BEGIN(EVE_RECTS));
        if (!s) eve_cmd_dl(COLOR_MASK(0 ,0 ,0 ,0));
        eve_cmd_dl(VERTEX2F(x + x1, _widget->g.y));
        eve_cmd_dl(VERTEX2F(x + x2, _widget->g.y + widget->font->h));
        if (!s) eve_cmd_dl(COLOR_MASK(1 ,1 ,1 ,1));
        eve_cmd_dl(END());
        if (len) {
            if (s) eve_cmd_dl(COLOR_RGBC(window->color_bg));
            eve_cmd(CMD_TEXT, "hhhhpb", x + x1, _widget->g.y, widget->font->id, 0, widget->str + ch, len, 0);
            if (s) eve_cmd_dl(COLOR_RGBC(window->color_fg));
        }
    }
}

static void _draw_cursor(EVEStrWidget *widget, EVEStrCursor *cursor) {
    uint16_t x, y;
    EVEWidget *_widget = &widget->w;

    x = _widget->g.x - widget->str_g.x + cursor->x;
    y = _widget->g.y;
    eve_cmd_dl(BEGIN(EVE_LINES));
    eve_cmd_dl(VERTEX2F(x, y));
    eve_cmd_dl(VERTEX2F(x, y + widget->font->h));
    eve_cmd_dl(END());
}

uint8_t eve_strw_draw(EVEWidget *_widget, EVEPage *page, uint8_t tag0) {
    EVEStrWidget *widget = (EVEStrWidget *)_widget;
    char cut = widget->str_g.x || (widget->str_g.w > _widget->g.w);

    widget->tag = tag0;
    if (tag0 != EVE_TAG_NOTAG) {
        eve_cmd_dl(TAG(tag0));
        tag0++;
    }

    if (cut) {
        EVEWindow *window = page->v.window;
        EVEScreen *screen = window->screen;
        int16_t x = eve_page_scr_x(page, _widget->g.x);
        int16_t y = eve_page_scr_y(page, _widget->g.y);
        uint16_t w = _widget->g.w;
        uint16_t h = _widget->g.h;
        int16_t win_x1 = window->g.x;
        int16_t win_y1 = window->g.y;
        int16_t win_x2 = win_x1 + window->g.w;
        int16_t win_y2 = win_y1 + window->g.h;

        if (win_x1 < 0) win_x1 = 0;
        if (win_y1 < 0) win_y1 = 0;
        if (win_x2 > screen->w) win_x2 = screen->w;
        if (win_y2 > screen->h) win_y2 = screen->h;
        if (x < win_x1) {
            w += x - win_x1;
            x = win_x1;
        }
        if (y < win_y1) {
            h += y - win_y1;
            y = win_y1;
        }
        if (x + w > win_x2) w = win_x2 - x;
        if (y + h > win_y2) h = win_y2 - y;

        eve_cmd_dl(SAVE_CONTEXT());
        eve_cmd_dl(SCISSOR_XY(x, y));
        eve_cmd_dl(SCISSOR_SIZE(w, h));
    }

    if (widget->cursor2.on) {
        EVEStrCursor *c1, *c2;
        int l1, l2, l3;

        if (widget->cursor1.ch <= widget->cursor2.ch) {
            c1 = &widget->cursor1;
            c2 = &widget->cursor2;
        } else {
            c1 = &widget->cursor2;
            c2 = &widget->cursor1;
        }

        l1 = c1->ch;
        l2 = c2->ch - c1->ch;
        l3 = strlen(widget->str) - c2->ch;
        _draw_str(widget, page->v.window, 0, l1, 0, c1->x, 0);
        _draw_str(widget, page->v.window, c1->ch, l2, c1->x, c2->x, 1);
        _draw_str(widget, page->v.window, c2->ch, l3, c2->x, widget->str_g.x + _widget->g.w, 0);
    } else {
        if (widget->cursor1.on) _draw_cursor(widget, &widget->cursor1);
        _draw_str(widget, page->v.window, 0, strlen(widget->str), 0, widget->str_g.x + _widget->g.w, 0);
    }

    if (cut) {
        eve_cmd_dl(RESTORE_CONTEXT());
    }

    return tag0;
}

void eve_strw_putc(void *_page, int c) {
    EVEPage *page = _page;
    EVEStrWidget *widget = (EVEStrWidget *)eve_page_get_focus(page);
    EVEStrCursor *cursor1 = &widget->cursor1;
    EVEStrCursor *cursor2 = &widget->cursor2;
    char *str;
    int w0 = widget->font->w;
    int w1 = widget->w.g.w - widget->font->w;

    if (c == CH_EOF) {
        if (cursor1->on) eve_strw_cursor_clear(widget, cursor1);
        if (cursor2->on) eve_strw_cursor_clear(widget, cursor2);
        return;
    }

    if (!cursor1->on) return;

    if (cursor2->on) {
        EVEStrCursor *c1;
        EVEStrCursor *c2;
        char *clipb = NULL;
        int ins_c = 0, del_c = 0;
        int ins_ch_w = 0, del_ch_w = 0;

        if (cursor1->ch <= cursor2->ch) {
            c1 = cursor1;
            c2 = cursor2;
        } else {
            c1 = cursor2;
            c2 = cursor1;
        }

        str = widget->str + c1->ch;
        del_c = c2->ch - c1->ch;
        del_ch_w = eve_font_buf_w(widget->font, str, del_c);
        if ((c == CH_CTRLX) || (c == CH_CTRLC)) {
            // eve_clipb_push(str, del_c);
            if (c == CH_CTRLC) return;
        }
        if (CHAR_VALID_INPUT(c) && (widget->str_len < widget->str_size + del_c - 1)) {
            ins_c = 1;
            ins_ch_w = widget->font->w_ch[c];
        } else if (c == CH_CTRLV) {
            // clipb = eve_clipb_pop();
            ins_c = clipb ? strlen(clipb) : 0;
            if (ins_c) {
                if (widget->str_len >= widget->str_size - (ins_c - del_c)) ins_c = widget->str_size - widget->str_len + del_c - 1;
                ins_ch_w = eve_font_buf_w(widget->font, clipb, ins_c);
            }
        }
        if (ins_c != del_c) memmove(str + ins_c, str + del_c, widget->str_len - c2->ch + 1);
        if (ins_c) {
            if (c == CH_CTRLV) {
                memcpy(str, clipb, ins_c);
            } else {
                *str = c;
            }
            c1->ch += ins_c;
            c1->x += ins_ch_w;
        }
        widget->str_len += ins_c - del_c;
        widget->str_g.w += ins_ch_w - del_ch_w;
        if (c1 == cursor2) widget->cursor1 = widget->cursor2;
        eve_strw_cursor_clear(widget, cursor2);
    } else {
        uint8_t ch_w;

        str = widget->str + cursor1->ch;
        switch (c) {
            case CH_BS:
                if (cursor1->ch > 0) {
                    ch_w = widget->font->w_ch[*(str - 1)];
                    memmove(str - 1, str, widget->str_len - cursor1->ch + 1);
                    widget->str_len--;
                    widget->str_g.w -= ch_w;
                    cursor1->ch--;
                    cursor1->x -= ch_w;
                }
                break;

            case CH_DEL:
                if (cursor1->ch < widget->str_len) {
                    ch_w = widget->font->w_ch[*str];
                    memmove(str, str + 1, widget->str_len - cursor1->ch);
                    widget->str_len--;
                    widget->str_g.w -= ch_w;
                }
                break;

            default:
                if (CHAR_VALID_INPUT(c) && (widget->str_len < widget->str_size - 1)) {
                    ch_w = widget->font->w_ch[c];
                    memmove(str + 1, str, widget->str_len - cursor1->ch + 1);
                    *str = c;
                    widget->str_len++;
                    widget->str_g.w += ch_w;
                    cursor1->ch++;
                    cursor1->x += ch_w;
                }
                break;
        }
        if (((c == CH_BS) || (c == CH_DEL)) && (widget->str_g.w - widget->str_g.x < w1)) {
            widget->str_g.x -= ch_w;
            if (widget->str_g.x < 0) widget->str_g.x = 0;
        }
    }

    if (cursor1->x - widget->str_g.x < w0) widget->str_g.x = cursor1->x > w0 ? cursor1->x - w0 : 0;
    if (cursor1->x - widget->str_g.x > w1) widget->str_g.x = cursor1->x - w1;
}

void eve_strw_cursor_set(EVEStrWidget *widget, EVEStrCursor *cursor, int16_t x) {
    int i;
    int16_t _x, _d;
    EVEWidget *_widget = &widget->w;

    x = x - _widget->g.x + widget->str_g.x;

    _x = 0;
    _d = x;
    for (i=0; i<widget->str_len; i++) {
        _x += widget->font->w_ch[widget->str[i]];
        if (_x >= x) {
            if (_x - x < _d) {
                i++;
            } else {
                _x -= widget->font->w_ch[widget->str[i]];
            }
            break;
        } else {
            _d = x - _x;
        }
    }
    cursor->x = _x;
    cursor->ch = i;
    cursor->on = 1;
}

void eve_strw_cursor_clear(EVEStrWidget *widget, EVEStrCursor *cursor) {
    cursor->on = 0;
}