aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Hanspeter Portner <dev@open-music-kontrollers.ch>2019-07-30 09:31:09 +0200
committerGravatar Hanspeter Portner <dev@open-music-kontrollers.ch>2019-07-30 09:31:09 +0200
commit15c59a14f297f7bc446959b2ffa2fc165ece8fec (patch)
tree1a9cf7aef188bce8037b4a0984581200dc7a9391
parent1860aa65bc20407914a4c6ec5f6282eb79a17916 (diff)
downloadd2tk-15c59a14f297f7bc446959b2ffa2fc165ece8fec.zip
d2tk-15c59a14f297f7bc446959b2ffa2fc165ece8fec.tar.gz
d2tk-15c59a14f297f7bc446959b2ffa2fc165ece8fec.tar.bz2
d2tk-15c59a14f297f7bc446959b2ffa2fc165ece8fec.tar.xz
base: support label alignment in buttons/toggles.
-rw-r--r--VERSION2
-rw-r--r--d2tk/base.h7
-rw-r--r--example/example.c32
-rw-r--r--src/base.c31
-rw-r--r--test/base.c6
5 files changed, 46 insertions, 32 deletions
diff --git a/VERSION b/VERSION
index 8bb0d09..d958a5f 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.1.747
+0.1.749
diff --git a/d2tk/base.h b/d2tk/base.h
index 5a213f1..3ba8d40 100644
--- a/d2tk/base.h
+++ b/d2tk/base.h
@@ -331,14 +331,15 @@ d2tk_base_cursor(d2tk_base_t *base, const d2tk_rect_t *rect);
D2TK_API d2tk_state_t
d2tk_base_button_label_image(d2tk_base_t *base, d2tk_id_t id, ssize_t lbl_len,
- const char *lbl, ssize_t path_len, const char *path, const d2tk_rect_t *rect);
+ const char *lbl, d2tk_align_t align, ssize_t path_len, const char *path,
+ const d2tk_rect_t *rect);
#define d2tk_base_button_label_image_is_changed(...) \
d2tk_state_is_changed(d2tk_base_button_label_image(__VA_ARGS__))
D2TK_API d2tk_state_t
d2tk_base_button_label(d2tk_base_t *base, d2tk_id_t id, ssize_t lbl_len,
- const char *lbl, const d2tk_rect_t *rect);
+ const char *lbl, d2tk_align_t align, const d2tk_rect_t *rect);
#define d2tk_base_button_label_is_changed(...) \
d2tk_state_is_changed(d2tk_base_button_label(__VA_ARGS__))
@@ -370,7 +371,7 @@ d2tk_base_button(d2tk_base_t *base, d2tk_id_t id, const d2tk_rect_t *rect);
D2TK_API d2tk_state_t
d2tk_base_toggle_label(d2tk_base_t *base, d2tk_id_t id, ssize_t lbl_len,
- const char *lbl, const d2tk_rect_t *rect, bool *value);
+ const char *lbl, d2tk_align_t align, const d2tk_rect_t *rect, bool *value);
#define d2tk_base_toggle_label_is_changed(...) \
d2tk_state_is_changed(d2tk_base_toggle_label(__VA_ARGS__))
diff --git a/example/example.c b/example/example.c
index 2247646..9cce286 100644
--- a/example/example.c
+++ b/example/example.c
@@ -108,7 +108,7 @@ _render_c_mix(d2tk_base_t *base, const d2tk_rect_t *rect)
const size_t lbl_len = snprintf(lbl, sizeof(lbl), "%03X", k);
if(d2tk_base_toggle_label_is_changed(
- base, id, lbl_len, lbl, bnd, &val->b))
+ base, id, lbl_len, lbl, D2TK_ALIGN_CENTERED, bnd, &val->b))
{
fprintf(stdout, "toggle %016"PRIx64" %s\n", id, val->b ? "ON" : "OFF");
};
@@ -119,7 +119,7 @@ _render_c_mix(d2tk_base_t *base, const d2tk_rect_t *rect)
const size_t lbl_len = snprintf(lbl, sizeof(lbl), "%03X", k);
if(d2tk_base_button_label_is_changed(
- base, id, lbl_len, lbl, bnd))
+ base, id, lbl_len, lbl, D2TK_ALIGN_CENTERED, bnd))
{
fprintf(stdout, "button %016"PRIx64" DOWN\n", id);
}
@@ -421,6 +421,7 @@ _render_c_scroll(d2tk_base_t *base, const d2tk_rect_t *rect)
char lbl [32];
const size_t lbl_len = snprintf(lbl, sizeof(lbl), "%u-%03u", j, k);
if(d2tk_base_button_label_image_is_changed( base, id, lbl_len, lbl,
+ D2TK_ALIGN_MIDDLE | D2TK_ALIGN_LEFT,
-1, "libre-arrow-circle-right.png", bnd))
{
fprintf(stdout, "button %016"PRIx64" DOWN\n", id);
@@ -448,7 +449,8 @@ _render_c_pane(d2tk_base_t *base, const d2tk_rect_t *rect)
// 1st
if(x == 0)
{
- if(d2tk_base_button_label_is_changed(base, D2TK_ID, -1, "1st", hrect))
+ if(d2tk_base_button_label_is_changed(base, D2TK_ID, -1, "1st",
+ D2TK_ALIGN_CENTERED, hrect))
{
fprintf(stdout, "button 1st DOWN\n");
}
@@ -466,7 +468,8 @@ _render_c_pane(d2tk_base_t *base, const d2tk_rect_t *rect)
// 1st
if(y == 0)
{
- if(d2tk_base_button_label_is_changed(base, D2TK_ID, -1, "2nd", vrect))
+ if(d2tk_base_button_label_is_changed(base, D2TK_ID, -1, "2nd",
+ D2TK_ALIGN_CENTERED, vrect))
{
fprintf(stdout, "button 2nd DOWN\n");
}
@@ -475,7 +478,8 @@ _render_c_pane(d2tk_base_t *base, const d2tk_rect_t *rect)
}
// 2nd
- if(d2tk_base_button_label_is_changed(base, D2TK_ID, -1, "3rd", vrect))
+ if(d2tk_base_button_label_is_changed(base, D2TK_ID, -1, "3rd",
+ D2TK_ALIGN_CENTERED, vrect))
{
fprintf(stdout, "button 3rd DOWN\n");
}
@@ -507,7 +511,7 @@ _render_c_layout(d2tk_base_t *base, const d2tk_rect_t *rect)
const ssize_t lbl_len = snprintf(lbl, sizeof(lbl), "%"PRIu32, vfrac[y]);
if(d2tk_base_button_label_is_changed(base, D2TK_ID_IDX(x*N + y),
- lbl_len, lbl, vrect))
+ lbl_len, lbl, D2TK_ALIGN_CENTERED, vrect))
{
fprintf(stdout, "button DOWN\n");
}
@@ -520,7 +524,7 @@ _render_c_layout(d2tk_base_t *base, const d2tk_rect_t *rect)
const ssize_t lbl_len = snprintf(lbl, sizeof(lbl), "%"PRIu32, hfrac[x]);
if(d2tk_base_button_label_is_changed(base, D2TK_ID_IDX(x*N + 0),
- lbl_len, lbl, hrect))
+ lbl_len, lbl, D2TK_ALIGN_CENTERED, hrect))
{
fprintf(stdout, "button DOWN\n");
}
@@ -611,7 +615,8 @@ _render_c_flowmatrix(d2tk_base_t *base, const d2tk_rect_t *rect)
const d2tk_id_t id = D2TK_ID_IDX(i);
bool *val = &toggle[i];
- state = d2tk_base_toggle_label(base, id, lbl_len, lbl, bnd, val);
+ state = d2tk_base_toggle_label(base, id, lbl_len, lbl,
+ D2TK_ALIGN_CENTERED, bnd, val);
if(d2tk_state_is_active(state))
{
d2tk_flowmatrix_set_src(flowm, id, &pos_nodes[i]);
@@ -886,7 +891,8 @@ _render_c_browser(d2tk_base_t *base, const d2tk_rect_t *rect)
const char *icon = "libre-gui-folder.png";
if(d2tk_base_button_label_image_is_changed(base,
- D2TK_ID_IDX(k), nxt - ptr, ptr, -1, icon, bnd))
+ D2TK_ID_IDX(k), nxt - ptr, ptr,
+ D2TK_ALIGN_MIDDLE | D2TK_ALIGN_LEFT, -1, icon, bnd))
{
*nxt = '\0';
}
@@ -930,7 +936,8 @@ _render_c_browser(d2tk_base_t *base, const d2tk_rect_t *rect)
: "libre-gui-file.png";
if(d2tk_base_button_label_image_is_changed(base,
- D2TK_ID_IDX(k), -1, itm->d_name, -1, icon, bnd))
+ D2TK_ID_IDX(k), -1, itm->d_name,
+ D2TK_ALIGN_MIDDLE | D2TK_ALIGN_LEFT, -1, icon, bnd))
{
if(is_dir)
{
@@ -1434,7 +1441,7 @@ _render_c_keyboard(d2tk_base_t *base, const d2tk_rect_t *rect)
: keybtn->name;
const d2tk_state_t state = d2tk_base_button_label(base,
- D2TK_ID_IDX(keybtn-keybtns), -1, lbl, &bnd);
+ D2TK_ID_IDX(keybtn-keybtns), -1, lbl, D2TK_ALIGN_CENTERED, &bnd);
if(d2tk_state_is_down(state))
{
@@ -1517,7 +1524,8 @@ d2tk_example_run(d2tk_base_t *base, d2tk_coord_t w, d2tk_coord_t h)
const unsigned b = d2tk_table_get_index(tab);
bool val = (b == bar);
- d2tk_base_toggle_label(base, D2TK_ID_IDX(b), -1, bar_lbl[b], hrect, &val);
+ d2tk_base_toggle_label(base, D2TK_ID_IDX(b), -1, bar_lbl[b],
+ D2TK_ALIGN_CENTERED, hrect, &val);
if(val)
{
bar = b;
diff --git a/src/base.c b/src/base.c
index 6abe25b..26ea845 100644
--- a/src/base.c
+++ b/src/base.c
@@ -1657,7 +1657,7 @@ d2tk_base_cursor(d2tk_base_t *base, const d2tk_rect_t *rect)
static inline void
_d2tk_base_draw_button(d2tk_core_t *core, ssize_t lbl_len, const char *lbl,
- ssize_t path_len, const char *path,
+ d2tk_align_t align, ssize_t path_len, const char *path,
const d2tk_rect_t *rect, d2tk_triple_t triple, const d2tk_style_t *style)
{
const bool has_lbl = lbl_len && lbl;
@@ -1676,6 +1676,7 @@ _d2tk_base_draw_button(d2tk_core_t *core, ssize_t lbl_len, const char *lbl,
const uint64_t hash = d2tk_hash_foreach(&triple, sizeof(d2tk_triple_t),
rect, sizeof(d2tk_rect_t),
style, sizeof(d2tk_style_t),
+ &align, sizeof(d2tk_align_t),
(lbl ? lbl : path), (lbl ? lbl_len : path_len),
(path ? path : NULL), (path ? path_len : 0),
NULL);
@@ -1708,8 +1709,7 @@ _d2tk_base_draw_button(d2tk_core_t *core, ssize_t lbl_len, const char *lbl,
if(has_lbl)
{
const d2tk_coord_t h_2 = rect->h / 2;
- const d2tk_align_t lbl_align = D2TK_ALIGN_MIDDLE
- | (has_img ? D2TK_ALIGN_LEFT : D2TK_ALIGN_CENTER);
+ const d2tk_align_t lbl_align = align;
const size_t ref = d2tk_core_bbox_push(core, true, rect);
@@ -1740,7 +1740,8 @@ _d2tk_base_draw_button(d2tk_core_t *core, ssize_t lbl_len, const char *lbl,
D2TK_API d2tk_state_t
d2tk_base_button_label_image(d2tk_base_t *base, d2tk_id_t id, ssize_t lbl_len,
- const char *lbl, ssize_t path_len, const char *path, const d2tk_rect_t *rect)
+ const char *lbl, d2tk_align_t align, ssize_t path_len, const char *path,
+ const d2tk_rect_t *rect)
{
d2tk_state_t state = d2tk_base_is_active_hot(base, id, rect, D2TK_FLAG_NONE);
@@ -1766,35 +1767,38 @@ d2tk_base_button_label_image(d2tk_base_t *base, d2tk_id_t id, ssize_t lbl_len,
triple |= D2TK_TRIPLE_FOCUS;
}
- _d2tk_base_draw_button(base->core, lbl_len, lbl, path_len, path, rect, triple,
- d2tk_base_get_style(base));
+ _d2tk_base_draw_button(base->core, lbl_len, lbl, align, path_len, path, rect,
+ triple, d2tk_base_get_style(base));
return state;
}
D2TK_API d2tk_state_t
d2tk_base_button_label(d2tk_base_t *base, d2tk_id_t id, ssize_t lbl_len,
- const char *lbl, const d2tk_rect_t *rect)
+ const char *lbl, d2tk_align_t align, const d2tk_rect_t *rect)
{
- return d2tk_base_button_label_image(base, id, lbl_len, lbl, 0, NULL, rect);
+ return d2tk_base_button_label_image(base, id, lbl_len, lbl,
+ align, 0, NULL, rect);
}
D2TK_API d2tk_state_t
d2tk_base_button_image(d2tk_base_t *base, d2tk_id_t id, ssize_t path_len,
const char *path, const d2tk_rect_t *rect)
{
- return d2tk_base_button_label_image(base, id, 0, NULL, path_len, path, rect);
+ return d2tk_base_button_label_image(base, id, 0, NULL,
+ D2TK_ALIGN_NONE, path_len, path, rect);
}
D2TK_API d2tk_state_t
d2tk_base_button(d2tk_base_t *base, d2tk_id_t id, const d2tk_rect_t *rect)
{
- return d2tk_base_button_label_image(base, id, 0, NULL, 0, NULL, rect);
+ return d2tk_base_button_label_image(base, id, 0, NULL,
+ D2TK_ALIGN_NONE, 0, NULL, rect);
}
D2TK_API d2tk_state_t
d2tk_base_toggle_label(d2tk_base_t *base, d2tk_id_t id, ssize_t lbl_len,
- const char *lbl, const d2tk_rect_t *rect, bool *value)
+ const char *lbl, d2tk_align_t align, const d2tk_rect_t *rect, bool *value)
{
d2tk_state_t state = d2tk_base_is_active_hot(base, id, rect, D2TK_FLAG_NONE);
@@ -1821,7 +1825,7 @@ d2tk_base_toggle_label(d2tk_base_t *base, d2tk_id_t id, ssize_t lbl_len,
triple |= D2TK_TRIPLE_FOCUS;
}
- _d2tk_base_draw_button(base->core, lbl_len, lbl, 0, NULL, rect, triple,
+ _d2tk_base_draw_button(base->core, lbl_len, lbl, align, 0, NULL, rect, triple,
d2tk_base_get_style(base));
return state;
@@ -1831,7 +1835,8 @@ D2TK_API d2tk_state_t
d2tk_base_toggle(d2tk_base_t *base, d2tk_id_t id, const d2tk_rect_t *rect,
bool *value)
{
- return d2tk_base_toggle_label(base, id, 0, NULL, rect, value);
+ return d2tk_base_toggle_label(base, id, 0, NULL,
+ D2TK_ALIGN_NONE, rect, value);
}
D2TK_API void
diff --git a/test/base.c b/test/base.c
index 9580977..87b57fe 100644
--- a/test/base.c
+++ b/test/base.c
@@ -1282,7 +1282,7 @@ _test_button_label_image()
assert(base);
const d2tk_state_t state = d2tk_base_button_label_image(base, D2TK_ID,
- -1, "label", -1, "image.png", &rect);
+ -1, "label", D2TK_ALIGN_CENTERED, -1, "image.png", &rect);
assert(state == (D2TK_STATE_HOT | D2TK_STATE_FOCUS | D2TK_STATE_FOCUS_IN
|D2TK_STATE_OVER) );
@@ -1301,7 +1301,7 @@ _test_button_label()
assert(base);
const d2tk_state_t state = d2tk_base_button_label(base, D2TK_ID,
- -1, "label", &rect);
+ -1, "label", D2TK_ALIGN_CENTERED, &rect);
assert(state == (D2TK_STATE_HOT | D2TK_STATE_FOCUS | D2TK_STATE_FOCUS_IN
| D2TK_STATE_OVER) );
@@ -1358,7 +1358,7 @@ _test_toggle_label()
bool val = false;
const d2tk_state_t state = d2tk_base_toggle_label(base, D2TK_ID,
- -1, "label", &rect, &val);
+ -1, "label", D2TK_ALIGN_CENTERED, &rect, &val);
assert(state == (D2TK_STATE_HOT | D2TK_STATE_FOCUS | D2TK_STATE_FOCUS_IN
| D2TK_STATE_OVER) );
assert(val == false);