From 28f812bef8091b54452b9a6e6124b447a6bd68cf Mon Sep 17 00:00:00 2001 From: marschap Date: Tue, 5 Apr 2005 07:28:35 +0000 Subject: [PATCH] - implemented menuevents enter+leave - new method menuitem_get_client() - more maintainable way to specify count of enums - corrected some typos (VB) --- server/commands/menu_commands.c | 23 ++++++++++++++++++----- server/menuitem.c | 18 +++++++++++++++++- server/menuitem.h | 21 +++++++++++---------- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/server/commands/menu_commands.c b/server/commands/menu_commands.c index 2acecdc..b287e99 100644 --- a/server/commands/menu_commands.c +++ b/server/commands/menu_commands.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "shared/report.h" #include "shared/sockets.h" @@ -163,6 +164,8 @@ menu_add_item_func (Client * c, int argc, char **argv) item = menuitem_create_ip (item_id, menu_commands_handler, text, 0, "192.168.1.245"); break; + default: + assert(!"unexpected menuitem type"); } menu_add_item (menu, item); menuscreen_inform_item_modified (menu); @@ -659,6 +662,8 @@ menu_goto_func (Client * c, int argc, char **argv) char * menu_id; Menu * menu; + debug (RPT_DEBUG, "%s( Client [%d], %s )", + __FUNCTION__, c->sock, (argc > 1 ? argv[1] : "") ); if (!c->ack) return 1; @@ -676,12 +681,14 @@ menu_goto_func (Client * c, int argc, char **argv) /* A specified menu */ menu = menu_find_item (c->menu, menu_id, true); } + if (!menu) { sock_send_string (c->sock, "huh? Cannot find menu id\n"); return 0; } menuscreen_goto (menu); /* Failure is not returned */ + /* why not? (Volker) */ sock_send_string(c->sock, "success\n"); return 0; } @@ -695,12 +702,11 @@ MenuEventFunc (menu_commands_handler) { char buf[80] = ""; Client * c; - MenuItem * i; /* Compose message */ if( event == MENUEVENT_UPDATE || event == MENUEVENT_MINUS - || event == MENUEVENT_PLUS ) { + || event == MENUEVENT_PLUS) { switch( item->type ) { case MENUITEM_CHECKBOX: snprintf (buf, sizeof(buf)-1, "menuevent %s %.40s %s\n", @@ -738,6 +744,13 @@ MenuEventFunc (menu_commands_handler) item->id); } } + else if (event == MENUEVENT_ENTER + || event == MENUEVENT_LEAVE) + { + snprintf (buf, sizeof(buf)-1, "menuevent %s %.40s\n", + menuitem_eventtype_to_eventtypename(event), + item->id); + } else { snprintf (buf, sizeof(buf)-1, "menuevent %s %.40s\n", menuitem_eventtype_to_eventtypename(event), @@ -747,10 +760,10 @@ MenuEventFunc (menu_commands_handler) buf[sizeof(buf)-1] = 0; /* Where should the message go to ? */ - for( i = item; i && i->parent != main_menu; i = i->parent ); - c = (Client *) i->data.menu.association; + c = menuitem_get_client(item); if( !c ) { - report( RPT_ERR, "%s: Could not find client of item \"%s\"", __FUNCTION__, item->id ); + report( RPT_ERR, "%s: Could not find client of item \"%s\"", + __FUNCTION__, item->id ); return -1; } diff --git a/server/menuitem.c b/server/menuitem.c index a543fbf..84dc5fd 100644 --- a/server/menuitem.c +++ b/server/menuitem.c @@ -16,10 +16,12 @@ #include #include +#include #include "shared/report.h" #include "menuitem.h" +#include "menuscreens.h" #include "menu.h" #include "drivers.h" @@ -30,7 +32,7 @@ char *error_strs[] = {"", "Out of range", "Too long", "Too short", "Invalid Address"}; char *menuitemtypenames[] = {"menu", "action", "checkbox", "ring", "slider", "numeric", "alpha","ip"}; -char *menueventtypenames[] = {"select", "update", "plus", "minus"}; +char *menueventtypenames[] = {"select", "update", "plus", "minus", "enter", "leave"}; void menuitem_destroy_action (MenuItem *item); void menuitem_destroy_checkbox (MenuItem *item); @@ -1291,6 +1293,20 @@ MenuResult menuitem_process_input_ip (MenuItem *item, MenuToken token, char * ke return MENURESULT_ERROR; } +/** + * get the Client that owns item. extracted from menu_commands_handler(). + */ +Client * menuitem_get_client(MenuItem * item) +{ + MenuItem * i; + + /* not fool prove... */ + assert(item != NULL); + for (i = item; i && i->parent != main_menu; i = i->parent) + ; + return (Client *) i->data.menu.association; +} + LinkedList * tablist2linkedlist (char *strings) { LinkedList * list; diff --git a/server/menuitem.h b/server/menuitem.h index 0f227f3..3ce3ae6 100644 --- a/server/menuitem.h +++ b/server/menuitem.h @@ -41,7 +41,6 @@ * Data definitions of the menustuff */ -#define NUM_ITEMTYPES 8 typedef enum MenuItemType { /* These values are used in the */ MENUITEM_MENU = 0, /* function tables in menuitem.c ! */ MENUITEM_ACTION = 1, @@ -51,6 +50,7 @@ typedef enum MenuItemType { /* These values are used in the */ MENUITEM_NUMERIC = 5, MENUITEM_ALPHA = 6, MENUITEM_IP = 7, + NUM_ITEMTYPES = 8 } MenuItemType; typedef enum CheckboxValue { @@ -80,16 +80,18 @@ typedef enum MenuResult { } MenuResult; /* Events caused by a menuitem */ -#define NUM_EVENTTYPES 4 typedef enum MenuEventType { MENUEVENT_SELECT = 0, /* Item has been selected (action chosen) */ - MENUEVENT_UPDATE, /* Item has been modified + MENUEVENT_UPDATE = 1, /* Item has been modified (checkbox, numeric, alphanumeric) */ - MENUEVENT_PLUS, /* Item has been modified in positive direction + MENUEVENT_PLUS = 2, /* Item has been modified in positive direction (slider moved) */ - MENUEVENT_MINUS, /* Item has been modified in negative direction + MENUEVENT_MINUS = 3, /* Item has been modified in negative direction (slider moved) */ + MENUEVENT_ENTER = 4, /* Menu has been entered */ + MENUEVENT_LEAVE = 5, /* Menu has been left */ + NUM_EVENTTYPES = 6 } MenuEventType; #define MenuEventFunc(f) int (f) (struct MenuItem *item, MenuEventType event) @@ -204,7 +206,7 @@ MenuItem *menuitem_create (MenuItemType type, char *id, MenuItem *menuitem_create_action (char *id, MenuEventFunc(*event_func), char *text, MenuResult menu_result); /* Creates a an action item (a string only). - * Generated events: MENUEVENT_ENTER when user selects the item. + * Generated events: MENUEVENT_SELECT when user selects the item. */ MenuItem *menuitem_create_checkbox (char *id, MenuEventFunc(*event_func), @@ -229,7 +231,6 @@ MenuItem *menuitem_create_slider (char *id, MenuEventFunc(*event_func), * at the end positions of the slider. * You can set the step size. Make it 0 to disable the automatic value chaning, * and update the value yourself. - * Generated events: MENUEVENT_ENTER upon entering this item, * MENUEVENT_PLUS, MENUEVENT_MINUS when slider is moved (immediately). */ @@ -237,7 +238,6 @@ MenuItem *menuitem_create_numeric (char *id, MenuEventFunc(*event_func), char *text, int minvalue, int maxvalue, int value); /* Creates a numeric value box. * Value can range from minvalue to maxvalue. - * Generated events: MENUEVENT_ENTER upon entering this item, * MENUEVENT_UPDATE when user finishes the value (no immediate update). */ @@ -250,14 +250,12 @@ MenuItem *menuitem_create_alpha (char *id, MenuEventFunc(*event_func), * caps, non-caps and numbers are allowed. Also you can alow other characters. * If password char is non-zero, you will only see this char, not the actual * input. - * Generated events: MENUEVENT_ENTER upon entering this item, * MENUEVENT_UPDATE when user finishes the value (no immediate update). */ MenuItem *menuitem_create_ip (char *id, MenuEventFunc(*event_func), char *text, bool v6, char *value); /* Creates an ip value box. can be either v4 or v6 - * Generated events: MENUEVENT_ENTER upon entering this item, * MENUEVENT_UPDATE when user finishes the value (no immediate update). */ @@ -296,6 +294,9 @@ MenuResult menuitem_process_input (MenuItem *item, MenuToken token, char * key, * key is only used if token is MENUTOKEN_OTHER. */ +Client * menuitem_get_client(MenuItem * item); +/* returns the Client that owns the MenuItem. item must not be null */ + LinkedList * tablist2linkedlist (char * strings); /* Converts a tab-separated list to a LinkedList. */