Added enter event to edit widget

Decided to add support for commit the content of a edit wiget
content by enter if a flag was set. Tried to add other flags
like auto complete but failed for now to find a good way.
Also more importantly added `zr_edit_box` to filter callback.
This commit is contained in:
vurtun 2016-01-30 11:43:38 +01:00
parent 96fe7f7775
commit c628115369
3 changed files with 155 additions and 65 deletions

View File

@ -369,6 +369,7 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme)
static size_t prog_value = 40;
static float property_float = 2;
static int property_int = 10;
static int property_neg = 10;
static float range_float_min = 0;
static float range_float_max = 100;
@ -401,6 +402,9 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme)
zr_property_float(ctx, "Float:", 0, &property_float, 64.0f, 0.1f, 0.2f);
zr_label(ctx, "Property int:", ZR_TEXT_LEFT);
zr_property_int(ctx, "Int:", 0, &property_int, 100.0f, 1, 1);
zr_label(ctx, "Property neg:", ZR_TEXT_LEFT);
zr_property_int(ctx, "Neg:", -10, &property_neg, 10, 1, 1);
zr_layout_row_dynamic(ctx, 25, 1);
zr_label(ctx, "Range:", ZR_TEXT_LEFT);
@ -409,9 +413,9 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme)
zr_property_float(ctx, "#float:", range_float_min, &range_float_value, range_float_max, 1.0f, 0.2f);
zr_property_float(ctx, "#max:", range_float_min, &range_float_max, 100, 1.0f, 0.2f);
zr_property_int(ctx, "#min:", INT_MIN, &range_int_min, range_int_value, 1, 10);
zr_property_int(ctx, "#int:", range_int_min, &range_int_value, range_int_max, 1, 10);
zr_property_int(ctx, "#max:", range_int_value, &range_int_max, INT_MAX, 1, 10);
zr_property_int(ctx, "#min:", INT_MIN, &range_int_min, range_int_max, 1, 10);
zr_property_int(ctx, "#neg:", range_int_min, &range_int_value, range_int_max, 1, 10);
zr_property_int(ctx, "#max:", range_int_min, &range_int_max, INT_MAX, 1, 10);
zr_layout_pop(ctx);
}
@ -574,14 +578,13 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme)
zr_edit_string(ctx, ZR_EDIT_FIELD, field_buffer, &field_len, 64, zr_filter_default);
zr_label(ctx, "Box:", ZR_TEXT_LEFT);
zr_layout_row_static(ctx, 75, 278, 1);
zr_layout_row_static(ctx, 300, 278, 1);
zr_edit_string(ctx, ZR_EDIT_BOX, box_buffer, &box_len, 512, zr_filter_default);
zr_layout_row(ctx, ZR_STATIC, 25, 2, ratio);
active = zr_edit_string(ctx, ZR_EDIT_FIELD, text[7], &text_len[7], 64, zr_filter_ascii);
if (zr_button_text(ctx, "Submit", ZR_BUTTON_DEFAULT)
|| ((active & ZR_EDIT_ACTIVE) &&
zr_input_is_key_pressed(&ctx->input, ZR_KEY_ENTER)))
active = zr_edit_string(ctx, ZR_EDIT_FIELD|ZR_EDIT_SIGCOMIT, text[7], &text_len[7], 64, zr_filter_ascii);
if (zr_button_text(ctx, "Submit", ZR_BUTTON_DEFAULT) ||
(active & ZR_EDIT_COMMITED))
{
text[7][text_len[7]] = '\n';
text_len[7]++;

106
zahnrad.c
View File

@ -416,6 +416,9 @@ zr_vec2(float x, float y)
*
* ===============================================================
*/
static int zr_str_match_here(const char *regexp, const char *text);
static int zr_str_match_star(int c, const char *regexp, const char *text);
static void*
zr_memcopy(void *dst0, const void *src0, zr_size length)
{
@ -591,6 +594,48 @@ zr_strtof(float *number, const char *buffer)
return 1;
}
static int
zr_str_match_here(const char *regexp, const char *text)
{
if (regexp[0] == '\0')
return 1;
if (regexp[1] == '*')
return zr_str_match_star(regexp[0], regexp+2, text);
if (regexp[0] == '$' && regexp[1] == '\0')
return *text == '\0';
if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text))
return zr_str_match_here(regexp+1, text+1);
return 0;
}
static int
zr_str_match_star(int c, const char *regexp, const char *text)
{
do {/* a '* matches zero or more instances */
if (zr_str_match_here(regexp, text))
return 1;
} while (*text != '\0' && (*text++ == c || c == '.'));
return 0;
}
static int
zr_strfilter(const char *text, const char *regexp)
{
/*
c matches any literal character c
. matches any single character
^ matches the beginning of the input string
$ matches the end of the input string
* matches zero or more occurrences of the previous character*/
if (regexp[0] == '^')
return zr_str_match_here(regexp+1, text);
do { /* must look even if string is empty */
if (zr_str_match_here(regexp, text))
return 1;
} while (*text++ != '\0');
return 0;
}
static float
zr_pow(float x, int n)
{
@ -2105,7 +2150,6 @@ zr_canvas_add_poly_line(struct zr_canvas *list, struct zr_vec2 *points,
int thick_line;
zr_draw_vertex_color col;
ZR_ASSERT(list);
if (!list) return;
if (!list || points_count < 2) return;
color.a *= list->global_alpha;
@ -3643,7 +3687,7 @@ zr_edit_box_buffer_input(struct zr_edit_box *box, const struct zr_input *i)
glyph_len = zr_utf_decode(i->keyboard.text, &unicode, i->keyboard.text_len);
while (glyph_len && ((text_len+glyph_len) <= i->keyboard.text_len)) {
/* filter to make sure the value is correct */
if (box->filter(unicode)) {
if (box->filter(box, unicode)) {
zr_edit_box_add(box, &i->keyboard.text[text_len], glyph_len);
text_len += glyph_len;
glyphs++;
@ -3706,6 +3750,30 @@ zr_edit_box_remove(struct zr_edit_box *box)
box->cursor--;
}
int
zr_edit_box_has_selection(const struct zr_edit_box *eb)
{
ZR_ASSERT(eb);
if (!eb) return 0;
return (int)(eb->sel.end - eb->sel.begin);
}
const char*
zr_edit_box_get_selection(zr_size *len, struct zr_edit_box *eb)
{
zr_size l;
const char *begin;
zr_rune unicode;
ZR_ASSERT(eb);
ZR_ASSERT(len);
if (!eb || !len || !(eb->sel.end - eb->sel.begin))
return 0;
begin = zr_edit_buffer_at(&eb->buffer, (int)eb->sel.begin, &unicode, &l);
*len = (eb->sel.end - eb->sel.begin) + 1;
return begin;
}
char*
zr_edit_box_get(struct zr_edit_box *eb)
{
@ -3715,7 +3783,7 @@ zr_edit_box_get(struct zr_edit_box *eb)
}
const char*
zr_edit_box_get_const(struct zr_edit_box *eb)
zr_edit_box_get_const(const struct zr_edit_box *eb)
{
ZR_ASSERT(eb);
if (!eb) return 0;
@ -5389,35 +5457,39 @@ zr_widget_edit_box(struct zr_command_buffer *out, struct zr_rect r,
}
}
int zr_filter_default(zr_rune unicode)
{(void)unicode;return zr_true;}
int zr_filter_default(const struct zr_edit_box *box, zr_rune unicode)
{(void)unicode;ZR_UNUSED(box);return zr_true;}
int
zr_filter_ascii(zr_rune unicode)
zr_filter_ascii(const struct zr_edit_box *box, zr_rune unicode)
{
ZR_UNUSED(box);
if (unicode > 128) return zr_false;
else return zr_true;
}
int
zr_filter_float(zr_rune unicode)
zr_filter_float(const struct zr_edit_box *box, zr_rune unicode)
{
ZR_UNUSED(box);
if ((unicode < '0' || unicode > '9') && unicode != '.' && unicode != '-')
return zr_false;
else return zr_true;
}
int
zr_filter_decimal(zr_rune unicode)
zr_filter_decimal(const struct zr_edit_box *box, zr_rune unicode)
{
ZR_UNUSED(box);
if (unicode < '0' || unicode > '9')
return zr_false;
else return zr_true;
}
int
zr_filter_hex(zr_rune unicode)
zr_filter_hex(const struct zr_edit_box *box, zr_rune unicode)
{
ZR_UNUSED(box);
if ((unicode < '0' || unicode > '9') &&
(unicode < 'a' || unicode > 'f') &&
(unicode < 'A' || unicode > 'F'))
@ -5426,17 +5498,19 @@ zr_filter_hex(zr_rune unicode)
}
int
zr_filter_oct(zr_rune unicode)
zr_filter_oct(const struct zr_edit_box *box, zr_rune unicode)
{
ZR_UNUSED(box);
if (unicode < '0' || unicode > '7')
return zr_false;
else return zr_true;
}
int
zr_filter_binary(zr_rune unicode)
zr_filter_binary(const struct zr_edit_box *box, zr_rune unicode)
{
if (unicode < '0' || unicode > '1')
ZR_UNUSED(box);
if (unicode != '0' && unicode != '1')
return zr_false;
else return zr_true;
}
@ -9378,8 +9452,14 @@ zr_edit_buffer(struct zr_context *ctx, zr_flags flags,
}
}
if (*active && (flags & ZR_EDIT_SIGCOMIT) &&
zr_input_is_key_pressed(i, ZR_KEY_ENTER)) {
ret_flags |= ZR_EDIT_SIGCOMIT;
*active = 0;
}
/* compress edit widget state and state changes into flags */
ret_flags = (*active) ? ZR_EDIT_ACTIVE: ZR_EDIT_INACTIVE;
ret_flags |= (*active) ? ZR_EDIT_ACTIVE: ZR_EDIT_INACTIVE;
if (old_flags == ZR_EDIT_INACTIVE && ret_flags & ZR_EDIT_ACTIVE)
ret_flags |= ZR_EDIT_ACTIVATED;
else if (old_flags == ZR_EDIT_ACTIVE && ret_flags & ZR_EDIT_INACTIVE)

View File

@ -118,6 +118,9 @@ struct zr_user_font_glyph;
/* ===============================================================
* UTILITY
* ===============================================================*/
#define ZR_UNDEFINED (-1.0f)
#define ZR_FLAG(x) (1 << (x))
enum {zr_false, zr_true};
struct zr_color {zr_byte r,g,b,a;};
struct zr_vec2 {float x,y;};
@ -939,26 +942,61 @@ struct zr_style {
/*===============================================================
* EDIT BOX
* ===============================================================*/
typedef int(*zr_filter)(zr_rune unicode);
typedef int(*zr_filter)(const struct zr_edit_box*, zr_rune unicode);
typedef void(*zr_paste_f)(zr_handle, struct zr_edit_box*);
typedef void(*zr_copy_f)(zr_handle, const char*, zr_size size);
/* filter function */
int zr_filter_default(zr_rune unicode);
int zr_filter_ascii(zr_rune unicode);
int zr_filter_float(zr_rune unicode);
int zr_filter_decimal(zr_rune unicode);
int zr_filter_hex(zr_rune unicode);
int zr_filter_oct(zr_rune unicode);
int zr_filter_binary(zr_rune unicode);
struct zr_edit_box;
int zr_filter_default(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_ascii(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_float(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_decimal(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_hex(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_oct(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_binary(const struct zr_edit_box*, zr_rune unicode);
enum zr_edit_flags {
ZR_EDIT_READ_ONLY = ZR_FLAG(0),
/* text inside the edit widget cannot be modified */
ZR_EDIT_CURSOR = ZR_FLAG(1),
/* edit widget will have a movable cursor */
ZR_EDIT_SELECTABLE = ZR_FLAG(2),
/* edit widget allows text selection */
ZR_EDIT_CLIPBOARD = ZR_FLAG(3),
/* edit widget tries to use the clipbard callback for copy & paste */
ZR_EDIT_SIGCOMIT = ZR_FLAG(4),
/* edit widget generateds ZR_EDIT_COMMITED event on enter */
ZR_EDIT_MULTILINE = ZR_FLAG(5)
/* edit widget with text wrapping text editing */
};
enum zr_edit_types {
ZR_EDIT_SIMPLE = 0,
ZR_EDIT_FIELD = (ZR_EDIT_CURSOR|ZR_EDIT_SELECTABLE|ZR_EDIT_CLIPBOARD),
ZR_EDIT_BOX = (ZR_EDIT_CURSOR|ZR_EDIT_SELECTABLE|
ZR_EDIT_CLIPBOARD|ZR_EDIT_MULTILINE)
};
enum zr_edit_events {
ZR_EDIT_ACTIVE = ZR_FLAG(0),
/* edit widget is currently being modified */
ZR_EDIT_INACTIVE = ZR_FLAG(1),
/* edit widget is not active and is not being modified */
ZR_EDIT_ACTIVATED = ZR_FLAG(2),
/* edit widget went from state inactive to state active */
ZR_EDIT_DEACTIVATED = ZR_FLAG(3),
/* edit widget went from state active to state inactive */
ZR_EDIT_COMMITED = ZR_FLAG(4)
/* edit widget has received an enter and lost focus */
};
/* editbox */
struct zr_edit_box;
void zr_edit_box_clear(struct zr_edit_box*);
void zr_edit_box_add(struct zr_edit_box*, const char*, zr_size);
void zr_edit_box_remove(struct zr_edit_box*);
char *zr_edit_box_get(struct zr_edit_box*);
const char *zr_edit_box_get_const(struct zr_edit_box*);
const char *zr_edit_box_get_const(const struct zr_edit_box*);
void zr_edit_box_at(struct zr_edit_box*, zr_size pos, zr_glyph, zr_size*);
void zr_edit_box_at_cursor(struct zr_edit_box*, zr_glyph, zr_size*);
char zr_edit_box_at_char(struct zr_edit_box*, zr_size pos);
@ -966,12 +1004,12 @@ void zr_edit_box_set_cursor(struct zr_edit_box*, zr_size pos);
zr_size zr_edit_box_get_cursor(struct zr_edit_box *eb);
zr_size zr_edit_box_len_char(struct zr_edit_box*);
zr_size zr_edit_box_len(struct zr_edit_box*);
int zr_edit_box_has_selection(const struct zr_edit_box*);
const char *zr_edit_box_get_selection(zr_size *len, struct zr_edit_box*);
/*==============================================================
* WINDOW
* =============================================================*/
#define ZR_UNDEFINED (-1.0f)
#define ZR_FLAG(x) (1 << (x))
enum zr_modify {
ZR_FIXED = zr_false,
@ -1035,37 +1073,6 @@ enum zr_button_behavior {
/* repeater behavior will trigger as long as button is down */
};
enum zr_edit_flags {
ZR_EDIT_READ_ONLY = ZR_FLAG(0),
/* text inside the edit widget cannot be modified */
ZR_EDIT_CURSOR = ZR_FLAG(1),
/* edit widget will have a movable cursor */
ZR_EDIT_SELECTABLE = ZR_FLAG(2),
/* edit widget allows text selection */
ZR_EDIT_CLIPBOARD = ZR_FLAG(3),
/* edit widget tries to use the clipbard callback for copy & paste */
ZR_EDIT_MULTILINE = ZR_FLAG(4)
/* edit widget with text wrapping text editing */
};
enum zr_edit_types {
ZR_EDIT_SIMPLE = 0,
ZR_EDIT_FIELD = (ZR_EDIT_CURSOR|ZR_EDIT_SELECTABLE|ZR_EDIT_CLIPBOARD),
ZR_EDIT_BOX = (ZR_EDIT_CURSOR|ZR_EDIT_SELECTABLE|
ZR_EDIT_CLIPBOARD|ZR_EDIT_MULTILINE)
};
enum zr_edit_return_flags {
ZR_EDIT_ACTIVE = ZR_FLAG(0),
/* edit widget is currently being modified */
ZR_EDIT_INACTIVE = ZR_FLAG(1),
/* edit widget is not active and is not being modified */
ZR_EDIT_ACTIVATED = ZR_FLAG(2),
/* edit widget went from state inactive to state active */
ZR_EDIT_DEACTIVATED = ZR_FLAG(3)
/* edit widget went from state active to state inactive */
};
enum zr_chart_type {
ZR_CHART_LINES,
/* Line chart with data points connected by lines */
@ -1516,7 +1523,7 @@ int zr_propertyi(struct zr_context *layout, const char *name, int min, int val,
/* text manipulation */
zr_flags zr_edit_string(struct zr_context*, zr_flags, char *buffer, zr_size *len,
zr_size max, zr_filter);
zr_size max, zr_filter);
zr_flags zr_edit_buffer(struct zr_context*, zr_flags, struct zr_buffer*, zr_filter);
/* simple chart */