pane

Termbox2 based terminal UI library
git clone git://git.dimitrijedobrota.com/pane.git
Log | Files | Refs

commit beac70e094573a3313fbdadef2f7738c3e3eca87
parent 3e73558cfd28618457cb6fc98a35a6f627381964
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 31 Aug 2022 17:12:07 +0200

Unsplit pane, preserve movement and activity

Diffstat:
Minclude/pane.h | 1+
Msrc/main.c | 81+++++++++++++++++++++++++++++++++++++++++++------------------------------------
Msrc/pane.c | 57+++++++++++++++++++++++++++++++++++++++++++--------------
3 files changed, 88 insertions(+), 51 deletions(-)

diff --git a/include/pane.h b/include/pane.h @@ -12,6 +12,7 @@ T pane_child(T self, int index); T *pane_split(T self, size_t count, ...); T *pane_vsplit(T self, size_t count, ...); +void pane_unsplit(T self); void pane_setTitle(T self, char *title); diff --git a/src/main.c b/src/main.c @@ -9,7 +9,7 @@ extern Pane_T MAIN, ACTIVE; -void load(char *pass, int index) +void quit(char *pass, int index) { pane_stop(); UNIMPLEMENTED; @@ -23,13 +23,48 @@ struct menu_T mainMenu = { .spacing = 2, .items = { - { load, "Start" }, - { load, "Help" }, - { load, "Exit" }, + { quit, "Start" }, + { quit, "Help" }, + { quit, "Exit" }, }, .items_size = 3 }; +void movement(void) +{ + struct tb_event ev; + + while (1) { + tb_poll_event(&ev); + switch (ev.type) { + case TB_EVENT_RESIZE: + HANDLE_RESIZE; + break; + case TB_EVENT_KEY: + switch (ev.ch) { + case 'q': + return; + } + switch (ev.key) { + case TB_KEY_ARROW_UP: + setActive(0); + break; + case TB_KEY_ARROW_RIGHT: + setActive(1); + break; + case TB_KEY_ARROW_DOWN: + setActive(2); + break; + case TB_KEY_ARROW_LEFT: + setActive(3); + break; + case TB_KEY_ESC: + quit(0, 0); + } + } + } +} + int main(int argc, char **argv) { struct tb_event ev; @@ -40,44 +75,16 @@ int main(int argc, char **argv) Pane_T *sections, *mids, *bottoms; sections = pane_vsplit(MAIN, 3, 1, 1, 1); - mids = pane_split(sections[1], 2, 1, 2); bottoms = pane_split(sections[2], 2, 1, 1); - pane_vsplit(mids[0], 2, 1, 1); - mainMenu.pane = mids[1]; - /* pane_menu(&mainMenu); */ - - Pane_T active = MAIN; - tb_present(); while (1) { - tb_poll_event(&ev); - switch (ev.type) { - case TB_EVENT_RESIZE: - HANDLE_RESIZE; - break; - case TB_EVENT_KEY: - switch (ev.ch) { - case 'q': - goto end; - } - switch (ev.key) { - case TB_KEY_ARROW_UP: - setActive(0); - break; - case TB_KEY_ARROW_RIGHT: - setActive(1); - break; - case TB_KEY_ARROW_DOWN: - setActive(2); - break; - case TB_KEY_ARROW_LEFT: - setActive(3); - break; - } - } + mids = pane_split(sections[1], 2, 1, 2); + pane_vsplit(mids[0], 2, 1, 1); + movement(); + pane_unsplit(sections[1]); + movement(); } } -end: pane_stop(); printDiagram(MAIN); return 0; diff --git a/src/pane.c b/src/pane.c @@ -47,6 +47,8 @@ void draw_title(T self) { if (self->title) tb_printf(self->x + 1, self->y, TB_RED, 0, " %s ", self->title); + else + tb_printf(self->x + 1, self->y, TB_RED, 0, " %d ", self->index); } void draw_border(T self) @@ -72,11 +74,24 @@ void draw_border(T self) tb_print(self->x, y_max, color, 0, BORDER_CORNER_3); tb_print(x_max, y_max, color, 0, BORDER_CORNER_4); - tb_printf(self->x + 1, self->y + 1, TB_RED, 0, "%d", self->index); + /* tb_printf(self->x + 1, self->y + 1, TB_RED, 0, "%d", self->index); */ draw_title(self); } +void pane_clear(T self, int clear_border) +{ + size_t i; + /* int border = self->children_num != 0; */ + int border = !clear_border; + int width = self->width - 3 * border; + char *line = malloc(width * sizeof(char)); + memset(line, ' ', width); + + for (i = self->y + border; i < self->y + self->height - border; i++) + tb_print(self->x + border, i, TB_BLACK, 0, line); +} + #define SPLIT_FORMULA(ac, ad, sc, sd) \ { \ int total = 0, part, next; \ @@ -237,6 +252,33 @@ void split(T self, size_t count, va_list ap) pane_resize(self); } +void pane_unsplit(T self) +{ + size_t i; + int active = 0; + + if (self->children_num) { + for (i = 0; i < self->children_num; i++) { + pane_unsplit(self->children[i]); + if (self->children[i]->active) + active = 1; + free(self->children[i]); + } + free(self->children); + free(self->rules); + } else { + active = self->active; + } + self->children_num = 0; + self->active = active; + if (active) { + ACTIVE = self; + } + pane_clear(self, 1); + draw_border(self); + tb_present(); +} + T *pane_split(T self, size_t count, ...) { size_t i; @@ -284,19 +326,6 @@ int centerHorisontal(T self, int len) return (self->width - len - 2) / 2 + 1; } -void pane_clear(T self, int clear_border) -{ - size_t i; - /* int border = self->children_num != 0; */ - int border = !clear_border; - int width = self->width - 3 * border; - char *line = malloc(width * sizeof(char)); - memset(line, ' ', width); - - for (i = self->y + border; i < self->y + self->height - border; i++) - tb_print(self->x + border, i, TB_BLACK, 0, line); -} - void pane_menu(menu_T menu) { struct tb_event ev;