Skip to content

Commit 9dcf891

Browse files
committed
scroll_vbox: Add horizontal scrollbar
Signed-off-by: Alexey Gladkov <legion@kernel.org>
1 parent 2b5619b commit 9dcf891

3 files changed

Lines changed: 55 additions & 28 deletions

File tree

src/widget_scroll_vbox.c

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,37 @@
1010
struct widget_svbox {
1111
struct widget *pad;
1212
struct widget *vscroll;
13+
struct widget *hscroll;
1314
};
1415

1516
static void scroll_vbox_sync(struct widget *sv)
1617
{
1718
struct widget_svbox *st = sv->state.svbox;
1819

19-
if (!st || !st->pad || !st->vscroll)
20+
if (!st || !st->pad)
2021
return;
2122

22-
int scroll_y, content_h;
23+
if (st->vscroll) {
24+
int scroll_y, content_h;
25+
26+
widget_get(st->pad, PROP_SCROLL_Y, &scroll_y);
27+
widget_get(st->pad, PROP_SCROLL_CONTENT_H, &content_h);
28+
29+
widget_set(st->vscroll, PROP_SCROLL_Y, &scroll_y);
30+
widget_set(st->vscroll, PROP_SCROLL_CONTENT_H, &content_h);
31+
widget_set(st->vscroll, PROP_SCROLL_VIEW_H, &st->pad->h);
32+
}
33+
34+
if (st->hscroll) {
35+
int scroll_x, content_w;
2336

24-
widget_get(st->pad, PROP_SCROLL_Y, &scroll_y);
25-
widget_get(st->pad, PROP_SCROLL_CONTENT_H, &content_h);
37+
widget_get(st->pad, PROP_SCROLL_X, &scroll_x);
38+
widget_get(st->pad, PROP_SCROLL_CONTENT_W, &content_w);
2639

27-
widget_set(st->vscroll, PROP_SCROLL_Y, &scroll_y);
28-
widget_set(st->vscroll, PROP_SCROLL_CONTENT_H, &content_h);
29-
widget_set(st->vscroll, PROP_SCROLL_VIEW_H, &st->pad->h);
40+
widget_set(st->hscroll, PROP_SCROLL_X, &scroll_x);
41+
widget_set(st->hscroll, PROP_SCROLL_CONTENT_W, &content_w);
42+
widget_set(st->hscroll, PROP_SCROLL_VIEW_W, &st->pad->w);
43+
}
3044
}
3145

3246
static void scroll_vbox_measure(struct widget *w)
@@ -39,27 +53,28 @@ static void scroll_vbox_measure(struct widget *w)
3953

4054
pad->measure(pad);
4155

42-
w->min_h = pad->min_h;
43-
w->pref_h = pad->pref_h;
56+
w->min_h = 1;
57+
w->pref_h = pad->pref_h + 1;
4458

45-
w->min_w = pad->min_w + 1;
59+
w->min_w = 1;
4660
w->pref_w = pad->pref_w + 1;
4761
}
4862

4963
static void scroll_vbox_layout(struct widget *w)
5064
{
51-
struct widget *hbox = TAILQ_FIRST(&w->children);
65+
struct widget *vbox = TAILQ_FIRST(&w->children);
5266

53-
if (!hbox)
67+
if (!vbox)
5468
return;
5569

56-
widget_layout_tree(hbox, 0, 0, w->w, w->h);
70+
widget_layout_tree(vbox, 0, 0, w->w, w->h);
5771
}
5872

5973
static void scroll_vbox_render(struct widget *w)
6074
{
61-
//struct widget *hbox = TAILQ_FIRST(&w->children);
62-
//widget_render_tree(hbox);
75+
struct widget *hbox = TAILQ_FIRST(&w->children);
76+
if (hbox)
77+
widget_render_tree(hbox);
6378
scroll_vbox_sync(w);
6479
}
6580

@@ -89,18 +104,25 @@ static int scroll_vbox_input(const struct widget *w, wchar_t key)
89104
{
90105
struct widget_svbox *st = w->state.svbox;
91106

92-
int delta = 0;
107+
int delta_y = 0;
108+
int delta_x = 0;
93109

94110
switch (key) {
95-
case KEY_UP: delta = -1; break;
96-
case KEY_DOWN: delta = +1; break;
97-
case KEY_PPAGE: delta = -w->h; break;
98-
case KEY_NPAGE: delta = +w->h; break;
111+
case KEY_UP: delta_y = -1; break;
112+
case KEY_DOWN: delta_y = +1; break;
113+
case KEY_PPAGE: delta_y = -w->h; break;
114+
case KEY_NPAGE: delta_y = +w->h; break;
115+
case KEY_LEFT: delta_x = -1; break;
116+
case KEY_RIGHT: delta_x = +1; break;
99117
default:
100118
return 0;
101119
}
102120

103-
widget_set(st->pad, PROP_SCROLL_INC_Y, &delta);
121+
if (delta_y)
122+
widget_set(st->pad, PROP_SCROLL_INC_Y, &delta_y);
123+
124+
if (delta_x)
125+
widget_set(st->pad, PROP_SCROLL_INC_X, &delta_x);
104126

105127
return 1;
106128
}
@@ -120,11 +142,13 @@ struct widget *make_scroll_vbox(void)
120142
if (!root)
121143
return NULL;
122144

145+
struct widget *vbox = make_vbox();
123146
struct widget *hbox = make_hbox();
124147
struct widget *pad = make_pad_box();
125148
struct widget *vs = make_vscroll();
149+
struct widget *hs = make_hscroll();
126150

127-
if (!hbox || !pad || !vs) {
151+
if (!hbox || !pad || !vs || !hs) {
128152
widget_free(root);
129153
return NULL;
130154
}
@@ -138,12 +162,15 @@ struct widget *make_scroll_vbox(void)
138162

139163
st->pad = pad;
140164
st->vscroll = vs;
165+
st->hscroll = hs;
141166

142167
root->state.svbox = st;
143168

144-
widget_add(root, hbox);
169+
widget_add(root, vbox);
170+
widget_add(vbox, hbox);
145171
widget_add(hbox, pad);
146172
widget_add(hbox, vs);
173+
widget_add(vbox, hs);
147174

148175
root->add_child = scroll_vbox_add_child;
149176
root->measure = scroll_vbox_measure;

tests/e2e-msg-scrollbar.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ draw_testcase()
1111
"$topdir"/plainmouth plugin=msgbox action=create id=w1 \
1212
height=30 width=50 border=true \
1313
text="00 When I find my code in tons of trouble
14-
01 Friends and colleagues come to me
14+
01 Friends and colleagues come to me (qwerty asdfgh zxcvbn 1234567890)
1515
02 Speaking words of wisdom
1616
03 Write in C
1717
04

tests/expected-e2e-msg-scrollbar

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
+--------------------------------------------------+
22
|┌────────────────────────────────────────────────┐|
33
|│00 When I find my code in tons of trouble ^│|
4-
|│01 Friends and colleagues come to me │|
4+
|│01 Friends and colleagues come to me (qwerty as │|
55
|│02 Speaking words of wisdom │|
66
|│03 Write in C │|
77
|│04 │|
@@ -10,8 +10,8 @@
1010
|│07 Somewhere someone whispers │|
1111
|│08 Write in C │|
1212
|│09 │|
13-
|│10 Write in C, write in C │|
14-
|│11 Write in C, write in C v│|
13+
|│10 Write in C, write in C v│|
14+
|│11 Write in C, write in C #│|
1515
|│12 LISP is dead and buried #│|
1616
|│13 Write in C #│|
1717
|│14 #│|
@@ -26,7 +26,7 @@
2626
|│23 Write in C #│|
2727
|│24 #│|
2828
|│25 Write in C, write in C #│|
29-
|│26 Write in C, yeah, write in C #│|
29+
|│< >################│|
3030
|│[OK][Cancel] │|
3131
|└────────────────────────────────────────────────┘|
3232
+--------------------------------------------------+

0 commit comments

Comments
 (0)