ref: e1a9def1933089b3c9f7d4f2a6f265fa03c17e42
parent: 9b81391d76eb3c2c4a14ec82b9b52b8f83875f0a
author: cancel <cancel@cancel.fm>
date: Mon Dec 17 18:17:31 EST 2018
Add start of forms use
--- a/field.c
+++ b/field.c
@@ -2,7 +2,7 @@
#include "gbuffer.h"
#include <ctype.h>
-void field_init(Field* f) {
+void gfield_init(Field* f) {
f->buffer = NULL;
f->height = 0;
f->width = 0;
--- a/field.h
+++ b/field.h
@@ -7,7 +7,7 @@
U16 width;
};
-void field_init(Field* field);
+void gfield_init(Field* field);
void field_init_fill(Field* field, Usz height, Usz width, Glyph fill_char);
void field_deinit(Field* field);
void field_resize_raw(Field* field, Usz height, Usz width);
--- a/term_util.c
+++ b/term_util.c
@@ -1,5 +1,6 @@
-#include "base.h"
#include "term_util.h"
+#include <form.h>
+#include <menu.h>
void term_util_init_colors() {
if (has_colors()) {
@@ -29,6 +30,22 @@
#define ORCA_CONTAINER_OF(ptr, type, member) \
((type*)((char*)(1 ? (ptr) : &((type*)0)->member) - offsetof(type, member)))
+struct Qmenu {
+ Qblock qblock;
+ MENU* ncurses_menu;
+ ITEM* ncurses_items[32];
+ Usz items_count;
+ int id;
+};
+
+struct Qform {
+ Qblock qblock;
+ FORM* ncurses_form;
+ FIELD* ncurses_fields[32];
+ Usz fields_count;
+ int id;
+};
+
Qnav_stack qnav_stack;
static struct { int unused; } qmenu_spacer_user_unique;
@@ -88,6 +105,25 @@
qnav_stack.blocks[qnav_stack.count] = NULL;
qnav_stack.stack_changed = true;
}
+
+void qmenu_free(Qmenu* qm);
+void qform_free(Qform* qf);
+
+void qnav_free_block(Qblock* qb) {
+ switch (qb->tag) {
+ case Qblock_type_qmsg: {
+ Qmsg* qm = qmsg_of(qb);
+ free(qm);
+ } break;
+ case Qblock_type_qmenu: {
+ qmenu_free(qmenu_of(qb));
+ } break;
+ case Qblock_type_qform: {
+ qform_free(qform_of(qb));
+ } break;
+ }
+}
+
void qblock_print_border(Qblock* qb, unsigned int attr) {
wborder(qb->outer_window, ACS_VLINE | attr, ACS_VLINE | attr,
ACS_HLINE | attr, ACS_HLINE | attr, ACS_ULCORNER | attr,
@@ -149,6 +185,10 @@
qm->id = id;
return qm;
}
+int qmenu_id(Qmenu const* qm) { return qm->id; }
+void qmenu_set_title(Qmenu* qm, char const* title) {
+ qblock_set_title(&qm->qblock, title);
+}
void qmenu_add_choice(Qmenu* qm, char const* text, int id) {
ITEM* item = new_item(text, NULL);
set_item_userptr(item, (void*)(intptr_t)(id));
@@ -186,19 +226,6 @@
free(qm);
}
-void qnav_free_block(Qblock* qb) {
- switch (qb->tag) {
- case Qblock_type_qmsg: {
- Qmsg* qm = qmsg_of(qb);
- free(qm);
- } break;
- case Qblock_type_qmenu: {
- Qmenu* qm = qmenu_of(qb);
- qmenu_free(qm);
- } break;
- }
-}
-
bool qmenu_drive(Qmenu* qm, int key, Qmenu_action* out_action) {
switch (key) {
case 27: {
@@ -253,4 +280,24 @@
return false;
Qmenu* qm = qmenu_of(qb);
return qm->id == id;
+}
+
+Qform* qform_create(int id) {
+ Qform* qf = (Qform*)malloc(sizeof(Qform));
+ qf->ncurses_form = NULL;
+ qf->ncurses_fields[0] = NULL;
+ qf->fields_count = 0;
+ qf->id = id;
+ return qf;
+}
+
+Qform* qform_of(Qblock* qb) { return ORCA_CONTAINER_OF(qb, Qform, qblock); }
+
+void qform_free(Qform* qf) {
+ unpost_form(qf->ncurses_form);
+ free_form(qf->ncurses_form);
+ for (Usz i = 0; i < qf->fields_count; ++i) {
+ free_field(qf->ncurses_fields[i]);
+ }
+ free(qf);
}
--- a/term_util.h
+++ b/term_util.h
@@ -1,5 +1,5 @@
#pragma once
-#include <menu.h>
+#include "base.h"
#include <ncurses.h>
#define CTRL_PLUS(c) ((c)&037)
@@ -42,6 +42,7 @@
typedef enum {
Qblock_type_qmsg,
Qblock_type_qmenu,
+ Qblock_type_qform,
} Qblock_type_tag;
typedef struct {
@@ -61,13 +62,7 @@
Qblock qblock;
} Qmsg;
-typedef struct {
- Qblock qblock;
- MENU* ncurses_menu;
- ITEM* ncurses_items[32];
- Usz items_count;
- int id;
-} Qmenu;
+typedef struct Qmenu Qmenu;
typedef enum {
Qmenu_action_type_canceled,
@@ -88,6 +83,8 @@
Qmenu_action_picked picked;
} Qmenu_action;
+typedef struct Qform Qform;
+
void qnav_init(void);
void qnav_deinit(void);
Qblock* qnav_top_block(void);
@@ -103,6 +100,8 @@
Qmsg* qmsg_of(Qblock* qb);
Qmenu* qmenu_create(int id);
+int qmenu_id(Qmenu const* qm);
+void qmenu_set_title(Qmenu* qm, char const* title);
void qmenu_add_choice(Qmenu* qm, char const* text, int id);
void qmenu_add_spacer(Qmenu* qm);
void qmenu_push_to_nav(Qmenu* qm);
@@ -109,5 +108,8 @@
bool qmenu_drive(Qmenu* qm, int key, Qmenu_action* out_action);
Qmenu* qmenu_of(Qblock* qb);
bool qmenu_top_is_menu(int id);
+
+Qform* qform_create(int id);
+Qform* qform_of(Qblock* qb);
extern Qnav_stack qnav_stack;
--- a/tool
+++ b/tool
@@ -277,7 +277,7 @@
add cc_flags -D_POSIX_C_SOURCE=200809L
;;
esac
- add libraries -lmenuw -lncursesw
+ add libraries -lmenuw -lformw -lncursesw
# If we wanted wide chars, use -lncursesw on Linux, and still just
# -lncurses on Mac.
;;
--- a/tui_main.c
+++ b/tui_main.c
@@ -341,7 +341,7 @@
} else {
new_node = malloc(sizeof(Undo_node));
++hist->count;
- field_init(&new_node->field);
+ gfield_init(&new_node->field);
}
field_copy(field, &new_node->field);
new_node->tick_num = tick_num;
@@ -670,9 +670,9 @@
} Ged;
void ged_init(Ged* a) {
- field_init(&a->field);
- field_init(&a->scratch_field);
- field_init(&a->clipboard_field);
+ gfield_init(&a->field);
+ gfield_init(&a->scratch_field);
+ gfield_init(&a->clipboard_field);
markmap_reusable_init(&a->markmap_r);
bank_init(&a->bank);
undo_history_init(&a->undo_hist);
@@ -1505,7 +1505,7 @@
qmenu_add_spacer(qm);
qmenu_add_choice(qm, "Quit", Main_menu_quit);
qmenu_push_to_nav(qm);
- qblock_set_title(&qm->qblock, "ORCA");
+ qmenu_set_title(qm, "ORCA");
}
void push_about_msg(void) {
@@ -1925,7 +1925,7 @@
Qmenu_action act;
// special case for main menu: pressing the key to open it will close
// it again.
- if (qm->id == Main_menu_id &&
+ if (qmenu_id(qm) == Main_menu_id &&
(key == CTRL_PLUS('d') || key == KEY_F(1))) {
qnav_stack_pop();
break;
@@ -1936,7 +1936,7 @@
qnav_stack_pop();
} break;
case Qmenu_action_type_picked: {
- if (qm->id == Main_menu_id) {
+ if (qmenu_id(qm) == Main_menu_id) {
switch (act.picked.id) {
case Main_menu_quit:
goto quit;
@@ -1960,6 +1960,8 @@
} break;
}
}
+ } break;
+ case Qblock_type_qform: {
} break;
}
goto next_getch;