summaryrefslogtreecommitdiff
path: root/lib/libforms/forms.c
diff options
context:
space:
mode:
authorPaul Richards <paul@FreeBSD.org>1995-05-12 17:20:06 +0000
committerPaul Richards <paul@FreeBSD.org>1995-05-12 17:20:06 +0000
commit89e255f52c667a49e05cd8f4b135bb3f6c9b2df1 (patch)
tree6558dacc67a647a255afdab138d04182dc651b44 /lib/libforms/forms.c
parent97f3a7e6e7b2315a17055166bc138340d6f70017 (diff)
Notes
Diffstat (limited to 'lib/libforms/forms.c')
-rw-r--r--lib/libforms/forms.c400
1 files changed, 163 insertions, 237 deletions
diff --git a/lib/libforms/forms.c b/lib/libforms/forms.c
index f5a81a817801..575608fd08a0 100644
--- a/lib/libforms/forms.c
+++ b/lib/libforms/forms.c
@@ -33,309 +33,235 @@
*/
#include <strhash.h>
+#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <ncurses.h>
#include <forms.h>
#include <err.h>
-#include <ncurses.h>
#include "internal.h"
extern FILE *yyin;
-
-hash_table *global_bindings;
-
-unsigned int f_keymap[] = {
- KEY_UP, /* F_UP */
- KEY_DOWN, /* F_DOWN */
- 9, /* F_RIGHT */
- 8, /* F_LEFT */
- 10, /* F_NEXT */
- KEY_LEFT, /* F_CLEFT */
- KEY_RIGHT, /* F_CRIGHT */
- KEY_HOME, /* F_CHOME */
- KEY_END, /* F_CEND */
- 263, /* F_CBS */
- 330, /* F_CDEL */
- 10 /* F_ACCEPT */
+hash_table *root_table, *cbind;
+OBJECT *cur_obj;
+int done;
+
+/* Default attribute commands */
+struct attr_cmnd attr_cmnds[] = {
+ {"box", ATTR_BOX },
+ {"center", ATTR_CENTER },
+ {"right", ATTR_RIGHT }
};
-int
-form_load(const char *filename)
-{
- FILE *fd;
+/* Internal bindings */
- global_bindings = hash_create(0);
+struct intbind {
+ char *key;
+ void *addr;
+};
- if (!global_bindings)
- return (FS_ERROR);
+struct intbind internal_bindings[] = {
+ {"draw_box", &draw_box},
+ {"draw_shadow", &draw_shadow}
+};
- if (!(fd = fopen(filename, "r"))) {
- warn("Couldn't open forms file %s", filename);
- return (FS_ERROR);
- }
+/* Bind the internal function addresses */
- yyin = fd;
- yyparse();
+void
+bind_internals(hash_table *table)
+{
+ int i;
- if (fclose(fd)) {
- warn("Couldn't close forms file %s", filename);
- return (FS_ERROR);
- }
+ for (i=0; i < (sizeof internal_bindings)/(sizeof (struct intbind)); i++)
+ if (bind_tuple(table, internal_bindings[i].key,
+ TT_FUNC, internal_bindings[i].addr) == ST_ERROR)
+ errx(-1, "Failed to bind internal tuples");
+}
- hash_stats(global_bindings, 1);
+/*
+ * Find the default device and open a display on it.
+ */
+
+DISPLAY *
+default_open(DISPLAY *display)
+{
+ /* XXX -- not implemented, just calls ncurses */
+ return (ncurses_open(display));
- return (FS_OK);
}
int
-find_editable(char *key, void *data, void *arg)
+load_objects(const char *filename)
{
- struct Tuple *tuple = (struct Tuple *)data;
- struct Field *field;
+ FILE *fd;
- if (tuple->type != FT_FIELD_INST)
- return (1);
+ root_table = hash_create(0);
+ if (!root_table)
+ errx(-1, "Failed to allocate root bindings table");
- field = (struct Field *)tuple->addr;
+ cbind = root_table;
- if ((field->type == FF_INPUT) ||
- (field->type == FF_MENU) ||
- (field->type == FF_ACTION)) {
- arg = field;
- return (0);
- } else
- return (1);
-}
+ bind_internals(root_table);
-struct Form *
-form_start(char *formname)
-{
- struct Tuple *tuple;
- struct Form *form;
- struct Field *field = 0;
- struct Field *start = 0;
-
- tuple = form_get_tuple(global_bindings, formname, FT_FORM);
-
- if (!tuple) {
- warnx("No such form");
- return (0);
+ if (!(fd = fopen(filename, "r"))) {
+ warn("Couldn't open file %s", filename);
+ return (ST_ERROR);
}
- form = tuple->addr;
-
- /* Initialise form */
- if (!form->height)
- form->height = LINES;
- if (!form->width)
- form->width = COLS;
+ yyin = fd;
+ yyparse();
- form->window = newwin(form->height, form->width, form->y, form->x);
- if (!form->window) {
- warnx("Couldn't open window, closing form");
- return (0);
+ if (fclose(fd)) {
+ warn("Couldn't close file %s", filename);
+ return (ST_ERROR);
}
- /* Initialise the field instances */
-
- hash_traverse(form->bindings, init_field, field);
+ return (ST_OK);
+}
- tuple = form_get_tuple(form->bindings, form->startfield, FT_FIELD_INST);
+int
+start_object(char *objname)
+{
+ TUPLE *tuple;
+ OBJECT *object;
- if (!tuple) {
- warnx("No start field specified");
- /* Search for an editable field */
- hash_traverse(form->bindings, &find_editable, start);
- form->current_field = start;
- } else
- form->current_field = (struct Field *)tuple->addr;
+ tuple = get_tuple(root_table, objname, TT_OBJ_INST);
+ if (!tuple)
+ return (ST_NOBIND);
- if (!form->current_field)
- errx(1, "No suitable start field found, aborting");
+ object = (OBJECT *)tuple->addr;
+ cur_obj = object;
- form->prev_field = form->current_field;
+ set_display(object->display);
- form->status = FS_RUNNING;
+ cur_obj->status |= O_VISIBLE;
- return (form);
+ while (!done) {
+ hash_traverse(root_table, &display_tuples, root_table);
+ hash_traverse(root_table, &refresh_displays, root_table);
+ process_object(cur_obj);
+ }
+ return (ST_DONE);
}
int
-form_bind_tuple(hash_table *htable, char *name, TupleType type, void *addr)
+call_function(char *func, OBJECT *obj)
{
- struct Tuple *tuple;
+ TUPLE *tuple;
- tuple = malloc(sizeof (struct Tuple));
- if (!tuple) {
- warn("Couldn't allocate memory for new tuple");
- return (FS_ERROR);
- }
-
- tuple->name = name;
- tuple->type = type;
- tuple->addr = addr;
- tuple->next = 0;
-
- if (!htable)
- return (FS_ERROR);
- else {
- /* Check there isn't already a tuple of this type with this name */
- if (form_get_tuple(htable, name, type)) {
- warn("Duplicate tuple name, %s, skipping", name);
- return (FS_ERROR);
- } else
- hash_search(htable, tuple->name, tuple, NULL);
- }
+ tuple = tuple_search(obj, func, TT_FUNC);
+ if (!tuple)
+ return (0);
- return (0);
+ (*tuple->addr)(obj);
+ return (1);
}
-int
-tuple_match_any(char *key, struct Tuple *tuple, TupleType *type)
+set_display(DISPLAY *display)
{
- if (tuple->type != *type) {
- type = 0;
- return (1);
- } else {
- type = (TupleType *)tuple;
- return (0);
+ switch (display->type) {
+ case DT_NCURSES:
+ ncurses_set_display(display);
+ break;
+ default:
+ break;
}
}
-struct Tuple *
-form_get_tuple(hash_table *htable, char *key, TupleType type)
+void
+display_object(OBJECT *obj)
{
- void *arg = &type;
-
- /*
- * If a key is specified then search for that key,
- * otherwise, search the whole table for the first
- * tuple of the required type.
- */
-
- if (key)
- return(hash_search(htable, key, NULL, NULL));
- else {
- hash_traverse(htable, &tuple_match_any, arg);
- return (arg);
+ switch(obj->display->type) {
+ case DT_NCURSES:
+ ncurses_display_object(obj);
+ break;
+ default:
+ break;
}
}
-int
-show_field(char *key, void *data, void *arg)
+void
+process_object(OBJECT *obj)
{
- struct Tuple *tuple = (struct Tuple *)data;
- struct Field *field = (struct Field *)tuple->addr;
-
- display_field(arg, field);
+ TUPLE *tuple;
- return (1);
+ /* Call user routine, if there is one. */
+ if (obj->UserProcFunc)
+ if (call_function(obj->UserProcFunc, obj))
+ return;
+ /* Find the first non-compound object or a default override */
+ while (obj->type == OT_COMPOUND) {
+ tuple = tuple_search(obj, obj->object.compound->defobj, TT_OBJ_INST);
+ obj = (OBJECT *)tuple->addr;
+ }
+ cur_obj = obj;
+
+ switch(obj->display->type) {
+ case DT_NCURSES:
+ ncurses_process_object(obj);
+ break;
+ default:
+ break;
+ }
}
int
-form_show(char *formname)
+refresh_displays(char *key, void *data, void *arg)
{
- struct Tuple *tuple;
- struct Form *form;
- int x, y;
-
- tuple = form_get_tuple(global_bindings, formname, FT_FORM);
- if (!tuple)
- return (FS_NOBIND);
-
- form = tuple->addr;
-
- /* Clear form */
- wattrset(form->window, form->attr);
- for (y=0; y < form->height; y++)
- for (x=0; x < form->width; x++)
- mvwaddch(form->window, y, x, ' ');
+ TUPLE *tuple = (TUPLE *)data;
+ DISPLAY *display;
+
+ if (tuple->type == TT_DISPLAY)
+ display = (DISPLAY *)tuple->addr;
+ switch (display->type) {
+ case DT_NCURSES:
+ ncurses_refresh_display(display);
+ break;
+ default:
+ break;
+ }
- hash_traverse(form->bindings, show_field, form->window);
-
- return (FS_OK);
-}
-
-unsigned int
-do_key_bind(struct Form *form, unsigned int ch)
-{
- struct Field *field = form->current_field;
- struct Tuple *tuple=0;
-
- /* XXX -- check for keymappings here --- not yet done */
-
- if (ch == FK_UP) {
- if (field->fup) {
- tuple = form_get_tuple(form->bindings, field->fup, FT_FIELD_INST);
- if (!tuple)
- print_status("Field to move up to does not exist");
- } else
- print_status("Can't move up from this field");
- } else if (ch == FK_DOWN) {
- if (field->fdown) {
- tuple = form_get_tuple(form->bindings, field->fdown, FT_FIELD_INST);
- if (!tuple)
- print_status("Field to move down to does not exist");
- } else
- print_status("Can't move down from this field");
- } else if (ch == FK_LEFT) {
- if (field->fleft) {
- tuple = form_get_tuple(form->bindings, field->fleft, FT_FIELD_INST);
- if (!tuple)
- print_status("Field to move left to does not exist");
- } else
- print_status("Can't move left from this field");
- } else if (ch == FK_RIGHT) {
- if (field->fright) {
- tuple = form_get_tuple(form->bindings, field->fright, FT_FIELD_INST);
- if (!tuple)
- print_status("Field to move right to does not exist");
- } else
- print_status("Can't move right from this field");
- } else if (ch == FK_NEXT) {
- if (field->fnext) {
- tuple = form_get_tuple(form->bindings, field->fnext, FT_FIELD_INST);
- if (!tuple)
- print_status("Field to move to next does not exist");
- } else
- print_status("Can't move next from this field");
- } else
- /* No motion keys pressed */
- return (ch);
-
- if (tuple) {
- form->prev_field = form->current_field;
- form->current_field = tuple->addr;
- return (FS_OK);
- } else {
- beep();
- return (FS_ERROR);
- }
+ return (1);
}
-#ifdef DEBUG_NOT_YET
-void
-debug_dump_bindings(hash_table *htable)
+int
+display_tuples(char *key, void *data, void *arg)
{
- struct Tuple *binds;
-
- binds = form_get_tuple(htable, 0, FT_ANY);
- while (binds) {
- printf("%s, %d, %x\n", binds->name, binds->type, (int)binds->addr);
- binds = form_next_tuple(0, FT_ANY, binds->next);
+ TUPLE *tuple = (TUPLE *)data;
+ OBJECT *obj;
+ void (* fn)();
+
+ switch(tuple->type) {
+ case TT_OBJ_INST:
+ obj = (OBJECT *)tuple->addr;
+
+ /* Call user routine, if there is one. */
+ if (obj->UserDrawFunc) {
+ if (!call_function(obj->UserDrawFunc, obj))
+ display_object(obj);
+ } else
+ display_object(obj);
+
+ /* Display sub-objects */
+ if (obj->bind)
+ hash_traverse(obj->bind, &display_tuples, 0);
+ break;
+ default:
+ break;
}
+ return (1);
}
-void debug_dump_form(struct Form *form)
+AttrType
+parse_default_attributes(char *string)
{
- struct Field *field;
+ int i;
- field = form->fieldlist;
-
- for ( ; field; field = field->next) {
- printf("%s, %x, next = %x\n", field->defname, (int)field, (int)field->next);
- }
+ for (i=0; i < (sizeof attr_cmnds) / (sizeof (struct attr_cmnd)); i++)
+ if (!strcmp(string, attr_cmnds[i].attr_name))
+ return (attr_cmnds[i].attr_type);
+ return (ATTR_UNKNOWN);
}
-#endif