generously add tests for NULL in various menu related functions

This commit is contained in:
marschap
2005-02-28 20:21:40 +00:00
parent ed90f0f643
commit fcfd6b6b93
4 changed files with 572 additions and 436 deletions
+53 -14
View File
@@ -43,12 +43,15 @@ menu_create (char *id, MenuEventFunc(*event_func),
{ {
Menu *new_menu; Menu *new_menu;
debug (RPT_DEBUG, "%s( id=\"%s\", event_func=%p, text=\"%s\", association=%p )", __FUNCTION__, id, event_func, text, association); debug (RPT_DEBUG, "%s( id=\"%s\", event_func=%p, text=\"%s\", association=%p )",
__FUNCTION__, id, event_func, text, association);
new_menu = menuitem_create (MENUITEM_MENU, id, event_func, text); new_menu = menuitem_create (MENUITEM_MENU, id, event_func, text);
if (new_menu != NULL) {
new_menu->data.menu.contents = LL_new(); new_menu->data.menu.contents = LL_new();
new_menu->data.menu.association = association; new_menu->data.menu.association = association;
}
return new_menu; return new_menu;
} }
@@ -56,6 +59,9 @@ menu_create (char *id, MenuEventFunc(*event_func),
void void
menu_destroy (Menu *menu) menu_destroy (Menu *menu)
{ {
if (menu == NULL)
return;
debug (RPT_DEBUG, "%s( menu=[%s] )", __FUNCTION__, menu->id); debug (RPT_DEBUG, "%s( menu=[%s] )", __FUNCTION__, menu->id);
menu_destroy_all_items (menu); menu_destroy_all_items (menu);
@@ -68,9 +74,10 @@ menu_destroy (Menu *menu)
void void
menu_add_item (Menu *menu, MenuItem *item) menu_add_item (Menu *menu, MenuItem *item)
{ {
debug (RPT_DEBUG, "%s( menu=[%s], item=[%s] )", __FUNCTION__, menu->id, item->id); if ((menu == NULL) || (item == NULL))
return;
if (!menu) return; debug (RPT_DEBUG, "%s( menu=[%s], item=[%s] )", __FUNCTION__, menu->id, item->id);
/* Add the item to the menu */ /* Add the item to the menu */
LL_Push (menu->data.menu.contents, item); LL_Push (menu->data.menu.contents, item);
@@ -83,15 +90,18 @@ menu_remove_item (Menu *menu, MenuItem *item)
int i; int i;
MenuItem * item2; MenuItem * item2;
if ((menu == NULL) || (item == NULL))
return
debug (RPT_DEBUG, "%s( menu=[%s], item=[%s] )", __FUNCTION__, menu->id, item->id); debug (RPT_DEBUG, "%s( menu=[%s], item=[%s] )", __FUNCTION__, menu->id, item->id);
/* Find the item */ /* Find the item */
for (item2=LL_GetFirst(menu->data.menu.contents), i=0; for (item2 = LL_GetFirst(menu->data.menu.contents), i=0;
item2; item2 != NULL;
item2=LL_GetNext(menu->data.menu.contents), i++ ) { item2 = LL_GetNext(menu->data.menu.contents), i++ ) {
if (item==item2) { if (item == item2) {
LL_DeleteNode (menu->data.menu.contents); LL_DeleteNode (menu->data.menu.contents);
if (menu->data.menu.selector_pos>=i) { if (menu->data.menu.selector_pos >= i) {
menu->data.menu.selector_pos--; menu->data.menu.selector_pos--;
if (menu->data.menu.scroll > 0) if (menu->data.menu.scroll > 0)
menu->data.menu.scroll--; menu->data.menu.scroll--;
@@ -106,9 +116,12 @@ menu_destroy_all_items (Menu *menu)
{ {
MenuItem * item; MenuItem * item;
if (menu == NULL)
return;
debug (RPT_DEBUG, "%s( menu=[%s] )", __FUNCTION__, menu->id); debug (RPT_DEBUG, "%s( menu=[%s] )", __FUNCTION__, menu->id);
for( item = menu_getfirst_item(menu); item; item = menu_getfirst_item(menu) ) { for( item = menu_getfirst_item(menu); item != NULL; item = menu_getfirst_item(menu) ) {
menuitem_destroy (item); menuitem_destroy (item);
LL_Remove (menu->data.menu.contents, item); LL_Remove (menu->data.menu.contents, item);
} }
@@ -118,9 +131,12 @@ MenuItem *menu_find_item (Menu *menu, char *id, bool recursive)
{ {
MenuItem * item; MenuItem * item;
if ((menu == NULL) || (id == NULL))
return NULL;
debug (RPT_DEBUG, "%s( menu=[%s], id=\"%s\", recursive=%d )", __FUNCTION__, menu->id, id, recursive); debug (RPT_DEBUG, "%s( menu=[%s], id=\"%s\", recursive=%d )", __FUNCTION__, menu->id, id, recursive);
for( item = menu_getfirst_item(menu); item; item = menu_getnext_item(menu) ) { for( item = menu_getfirst_item(menu); item != NULL; item = menu_getnext_item(menu) ) {
if ( strcmp(item->id, id) == 0 ) { if ( strcmp(item->id, id) == 0 ) {
return item; return item;
} }
@@ -137,6 +153,9 @@ MenuItem *menu_find_item (Menu *menu, char *id, bool recursive)
void menu_reset (Menu *menu) void menu_reset (Menu *menu)
{ {
if (menu == NULL)
return;
debug (RPT_DEBUG, "%s( menu=[%s] )", __FUNCTION__, menu->id); debug (RPT_DEBUG, "%s( menu=[%s] )", __FUNCTION__, menu->id);
menu->data.menu.selector_pos = 0; menu->data.menu.selector_pos = 0;
@@ -149,6 +168,9 @@ void menu_build_screen (MenuItem *menu, Screen *s)
MenuItem * subitem; MenuItem * subitem;
int itemnr; int itemnr;
if ((menu == NULL) || (s == NULL))
return;
debug (RPT_DEBUG, "%s( menu=[%s], screen=[%s] )", __FUNCTION__, menu->id, s->id); debug (RPT_DEBUG, "%s( menu=[%s], screen=[%s] )", __FUNCTION__, menu->id, s->id);
/* TODO: Put menu in a frame to do easy scrolling */ /* TODO: Put menu in a frame to do easy scrolling */
@@ -156,13 +178,15 @@ void menu_build_screen (MenuItem *menu, Screen *s)
/* Create menu title widget */ /* Create menu title widget */
w = widget_create ("title", WID_TITLE, s); w = widget_create ("title", WID_TITLE, s);
if (w != NULL) {
screen_add_widget (s, w); screen_add_widget (s, w);
w->text = strdup(menu->text); w->text = strdup(menu->text);
w->x = 1; w->x = 1;
}
/* Create widgets for each subitem in the menu */ /* Create widgets for each subitem in the menu */
for (subitem = LL_GetFirst (menu->data.menu.contents), itemnr = 0; for (subitem = LL_GetFirst (menu->data.menu.contents), itemnr = 0;
subitem; subitem != NULL;
subitem = LL_GetNext (menu->data.menu.contents), itemnr ++ ) subitem = LL_GetNext (menu->data.menu.contents), itemnr ++ )
{ {
char buf[10]; char buf[10];
@@ -171,6 +195,7 @@ void menu_build_screen (MenuItem *menu, Screen *s)
buf[sizeof(buf)-1] = 0; buf[sizeof(buf)-1] = 0;
w = widget_create (buf, WID_STRING, s); w = widget_create (buf, WID_STRING, s);
/* (buf will be copied) */ /* (buf will be copied) */
if (w != NULL) {
screen_add_widget (s, w); screen_add_widget (s, w);
w->x = 2; w->x = 2;
@@ -202,7 +227,7 @@ void menu_build_screen (MenuItem *menu, Screen *s)
strcpy( w->text, subitem->text ); strcpy( w->text, subitem->text );
strcat( w->text, " >" ); strcat( w->text, " >" );
if (strlen(subitem->text) >= display_props->width-1) { if (strlen(subitem->text) >= display_props->width-1) {
(w->text)[display_props->width-1] = 0; (w->text)[display_props->width-1] = '\0';
} }
break; break;
case MENUITEM_ACTION: case MENUITEM_ACTION:
@@ -212,31 +237,38 @@ void menu_build_screen (MenuItem *menu, Screen *s)
/* Limit string length */ /* Limit string length */
w->text = strdup (subitem->text); w->text = strdup (subitem->text);
if (strlen(subitem->text) >= display_props->width-1) { if (strlen(subitem->text) >= display_props->width-1) {
(w->text)[display_props->width-1] = 0; (w->text)[display_props->width-1] = '\0';
} }
break; break;
} }
} }
}
/* Add arrow for selection on the left */ /* Add arrow for selection on the left */
w = widget_create ("selector", WID_ICON, s); w = widget_create ("selector", WID_ICON, s);
if (w != NULL) {
screen_add_widget (s, w); screen_add_widget (s, w);
w->length = ICON_SELECTOR_AT_LEFT; w->length = ICON_SELECTOR_AT_LEFT;
w->x = 1; w->x = 1;
}
/* Add scrollers on the right side on top and bottom */ /* Add scrollers on the right side on top and bottom */
/* TODO: when menu is in a frame, these can be removed */ /* TODO: when menu is in a frame, these can be removed */
w = widget_create ("upscroller", WID_ICON, s); w = widget_create ("upscroller", WID_ICON, s);
if (w != NULL) {
screen_add_widget (s, w); screen_add_widget (s, w);
w->length = ICON_ARROW_UP; w->length = ICON_ARROW_UP;
w->x = display_props->width; w->x = display_props->width;
w->y = 1; w->y = 1;
}
w = widget_create ("downscroller", WID_ICON, s); w = widget_create ("downscroller", WID_ICON, s);
if (w != NULL) {
screen_add_widget (s, w); screen_add_widget (s, w);
w->length = ICON_ARROW_DOWN; w->length = ICON_ARROW_DOWN;
w->x = display_props->width; w->x = display_props->width;
w->y = display_props->height; w->y = display_props->height;
}
} }
@@ -246,6 +278,9 @@ void menu_update_screen (MenuItem *menu, Screen *s)
MenuItem * subitem; MenuItem * subitem;
int itemnr; int itemnr;
if ((menu == NULL) || (s == NULL))
return;
debug (RPT_DEBUG, "%s( menu=[%s], screen=[%s] )", __FUNCTION__, menu->id, s->id); debug (RPT_DEBUG, "%s( menu=[%s], screen=[%s] )", __FUNCTION__, menu->id, s->id);
/* Update widgets for the title */ /* Update widgets for the title */
@@ -358,6 +393,9 @@ MenuResult menu_process_input (Menu *menu, MenuToken token, char * key)
{ {
MenuItem *subitem; MenuItem *subitem;
if (menu == NULL)
return MENURESULT_ERROR;
debug (RPT_DEBUG, "%s( menu=[%s], token=%d, key=\"%s\" )", __FUNCTION__, menu->id, token, key); debug (RPT_DEBUG, "%s( menu=[%s], token=%d, key=\"%s\" )", __FUNCTION__, menu->id, token, key);
switch (token) { switch (token) {
@@ -371,7 +409,8 @@ MenuResult menu_process_input (Menu *menu, MenuToken token, char * key)
case MENUITEM_ACTION: case MENUITEM_ACTION:
if (subitem->event_func) if (subitem->event_func)
subitem->event_func (subitem, MENUEVENT_SELECT); subitem->event_func (subitem, MENUEVENT_SELECT);
return subitem->data.action.menu_result; return MENURESULT_QUIT; return subitem->data.action.menu_result;
//return MENURESULT_QUIT;
case MENUITEM_CHECKBOX: case MENUITEM_CHECKBOX:
if (subitem->data.checkbox.allow_gray) { if (subitem->data.checkbox.allow_gray) {
subitem->data.checkbox.value = (subitem->data.checkbox.value + 1) % 3; subitem->data.checkbox.value = (subitem->data.checkbox.value + 1) % 3;
+14 -9
View File
@@ -64,7 +64,9 @@ static inline MenuItem *menu_getfirst_item (Menu *menu)
* Retrieves the first item from the list of items in the menu. * Retrieves the first item from the list of items in the menu.
*/ */
{ {
return (MenuItem*) LL_GetFirst( menu->data.menu.contents ); return (MenuItem*) ((menu != NULL)
? LL_GetFirst(menu->data.menu.contents)
: NULL);
} }
static inline MenuItem *menu_getnext_item (Menu *menu) static inline MenuItem *menu_getnext_item (Menu *menu)
@@ -74,15 +76,18 @@ static inline MenuItem *menu_getnext_item (Menu *menu)
* this function, to keep the list-cursor where it is. * this function, to keep the list-cursor where it is.
*/ */
{ {
return (MenuItem*) LL_GetNext( menu->data.menu.contents ); return (MenuItem*) ((menu != NULL)
? LL_GetNext(menu->data.menu.contents)
: NULL);
} }
static inline MenuItem *menu_get_current_item (Menu *menu) static inline MenuItem *menu_get_current_item (Menu *menu)
/* Retrieves the current item from the list of items in the menu. */ /* Retrieves the current item from the list of items in the menu. */
{ {
return LL_GetByIndex( return (MenuItem*) ((menu != NULL)
menu->data.menu.contents, ? LL_GetByIndex(menu->data.menu.contents,
menu->data.menu.selector_pos); menu->data.menu.selector_pos)
: NULL);
} }
MenuItem *menu_find_item (Menu *menu, char *id, bool recursive); MenuItem *menu_find_item (Menu *menu, char *id, bool recursive);
@@ -90,23 +95,23 @@ MenuItem *menu_find_item (Menu *menu, char *id, bool recursive);
void menu_reset (Menu *menu); void menu_reset (Menu *menu);
/* Resets it to initial state. /* Resets it to initial state.
* DO NOT CALL THIS FUNCTION, CALL menuitem_build_screen INSTEAD ! * DO NOT CALL THIS FUNCTION, CALL menuitem_reset_screen INSTEAD !
*/ */
void menu_build_screen (Menu *menu, Screen *s); void menu_build_screen (Menu *menu, Screen *s);
/* Builds the selected menuitem on screen using widgets. /* Builds the selected menuitem on screen using widgets.
* DO NOT CALL THIS FUNCTION, CALL menuitem_build_screen INSTEAD ! * DO NOT CALL THIS FUNCTION, CALL menuitem_rebuild_screen INSTEAD !
*/ */
void menu_update_screen (Menu *menu, Screen *s); void menu_update_screen (Menu *menu, Screen *s);
/* Updates the widgets of the selected menuitem /* Updates the widgets of the selected menuitem
* DO NOT CALL THIS FUNCTION, CALL menuitem_build_screen INSTEAD ! * DO NOT CALL THIS FUNCTION, CALL menuitem_update_screen INSTEAD !
*/ */
MenuResult menu_process_input (Menu *menu, MenuToken token, char * key); MenuResult menu_process_input (Menu *menu, MenuToken token, char * key);
/* Does something with the given input. /* Does something with the given input.
* key is only used if token is MENUTOKEN_OTHER. * key is only used if token is MENUTOKEN_OTHER.
* DO NOT CALL THIS FUNCTION, CALL menuitem_build_screen INSTEAD ! * DO NOT CALL THIS FUNCTION, CALL menuitem_process_input INSTEAD !
*/ */
#endif #endif
+140 -52
View File
@@ -115,6 +115,11 @@ MenuItem *menuitem_create (MenuItemType type, char *id, MenuEventFunc(*event_fun
debug (RPT_DEBUG, "%s( type=%d, id=\"%s\", event_func=%p, text=\"%s\" )", debug (RPT_DEBUG, "%s( type=%d, id=\"%s\", event_func=%p, text=\"%s\" )",
__FUNCTION__, type, id, event_func, text); __FUNCTION__, type, id, event_func, text);
if ((id == NULL) || (text == NULL)) {
// report (RPT_ERR, "%s: illegal id or text", __FUNCTION__);
return NULL;
}
/* Allocate space and fill struct */ /* Allocate space and fill struct */
new_item = malloc (sizeof(MenuItem)); new_item = malloc (sizeof(MenuItem));
if (!new_item) { if (!new_item) {
@@ -150,6 +155,7 @@ MenuItem *menuitem_create_action (char *id, MenuEventFunc(*event_func),
__FUNCTION__, id, event_func, text, menu_result); __FUNCTION__, id, event_func, text, menu_result);
new_item = menuitem_create (MENUITEM_ACTION, id, event_func, text); new_item = menuitem_create (MENUITEM_ACTION, id, event_func, text);
if (new_item != NULL)
new_item->data.action.menu_result = menu_result; new_item->data.action.menu_result = menu_result;
return new_item; return new_item;
@@ -164,8 +170,10 @@ MenuItem *menuitem_create_checkbox (char *id, MenuEventFunc(*event_func),
__FUNCTION__, id, event_func, text, allow_gray, value); __FUNCTION__, id, event_func, text, allow_gray, value);
new_item = menuitem_create (MENUITEM_CHECKBOX, id, event_func, text); new_item = menuitem_create (MENUITEM_CHECKBOX, id, event_func, text);
if (new_item != NULL) {
new_item->data.checkbox.allow_gray = allow_gray; new_item->data.checkbox.allow_gray = allow_gray;
new_item->data.checkbox.value = value; new_item->data.checkbox.value = value;
}
return new_item; return new_item;
} }
@@ -179,8 +187,10 @@ MenuItem *menuitem_create_ring (char *id, MenuEventFunc(*event_func),
__FUNCTION__, id, event_func, text, strings, value); __FUNCTION__, id, event_func, text, strings, value);
new_item = menuitem_create (MENUITEM_RING, id, event_func, text); new_item = menuitem_create (MENUITEM_RING, id, event_func, text);
if (new_item != NULL) {
new_item->data.ring.strings = tablist2linkedlist (strings); new_item->data.ring.strings = tablist2linkedlist (strings);
new_item->data.ring.value = value; new_item->data.ring.value = value;
}
return new_item; return new_item;
} }
@@ -195,12 +205,14 @@ MenuItem *menuitem_create_slider (char *id, MenuEventFunc(*event_func),
__FUNCTION__, id, event_func, text, mintext, maxtext, minvalue, maxvalue, stepsize, value); __FUNCTION__, id, event_func, text, mintext, maxtext, minvalue, maxvalue, stepsize, value);
new_item = menuitem_create (MENUITEM_SLIDER, id, event_func, text); new_item = menuitem_create (MENUITEM_SLIDER, id, event_func, text);
if (new_item != NULL) {
new_item->data.slider.mintext = strdup (mintext); new_item->data.slider.mintext = strdup (mintext);
new_item->data.slider.maxtext = strdup (maxtext); new_item->data.slider.maxtext = strdup (maxtext);
new_item->data.slider.minvalue = minvalue; new_item->data.slider.minvalue = minvalue;
new_item->data.slider.maxvalue = maxvalue; new_item->data.slider.maxvalue = maxvalue;
new_item->data.slider.stepsize = stepsize; new_item->data.slider.stepsize = stepsize;
new_item->data.slider.value = value; new_item->data.slider.value = value;
}
return new_item; return new_item;
} }
@@ -214,10 +226,12 @@ MenuItem *menuitem_create_numeric (char *id, MenuEventFunc(*event_func),
__FUNCTION__, id, event_func, text, minvalue, minvalue, value); __FUNCTION__, id, event_func, text, minvalue, minvalue, value);
new_item = menuitem_create (MENUITEM_NUMERIC, id, event_func, text); new_item = menuitem_create (MENUITEM_NUMERIC, id, event_func, text);
if (new_item != NULL) {
new_item->data.numeric.maxvalue = maxvalue; new_item->data.numeric.maxvalue = maxvalue;
new_item->data.numeric.minvalue = minvalue; new_item->data.numeric.minvalue = minvalue;
new_item->data.numeric.edit_str = malloc (MAX_NUMERIC_LEN); new_item->data.numeric.edit_str = malloc (MAX_NUMERIC_LEN);
new_item->data.numeric.value = value; new_item->data.numeric.value = value;
}
return new_item; return new_item;
} }
@@ -233,6 +247,7 @@ MenuItem *menuitem_create_alpha (char *id, MenuEventFunc(*event_func),
__FUNCTION__, id, event_func, text, password_char, maxlength, value); __FUNCTION__, id, event_func, text, password_char, maxlength, value);
new_item = menuitem_create (MENUITEM_ALPHA, id, event_func, text); new_item = menuitem_create (MENUITEM_ALPHA, id, event_func, text);
if (new_item != NULL) {
new_item->data.alpha.password_char = password_char; new_item->data.alpha.password_char = password_char;
new_item->data.alpha.minlength = minlength; new_item->data.alpha.minlength = minlength;
new_item->data.alpha.maxlength = maxlength; new_item->data.alpha.maxlength = maxlength;
@@ -247,6 +262,7 @@ MenuItem *menuitem_create_alpha (char *id, MenuEventFunc(*event_func),
new_item->data.alpha.value[maxlength] = 0; new_item->data.alpha.value[maxlength] = 0;
new_item->data.alpha.edit_str = malloc (maxlength + 1); new_item->data.alpha.edit_str = malloc (maxlength + 1);
}
return new_item; return new_item;
} }
@@ -254,9 +270,11 @@ MenuItem *menuitem_create_alpha (char *id, MenuEventFunc(*event_func),
void menuitem_destroy (MenuItem *item) void menuitem_destroy (MenuItem *item)
{ {
void (*destructor) (MenuItem *); debug (RPT_DEBUG, "%s( item=[%s] )",
__FUNCTION__, ((item != NULL) ? item->id : "(null)"));
debug (RPT_DEBUG, "%s( item=[%s] )", __FUNCTION__, item->id); if (item != NULL) {
void (*destructor) (MenuItem *);
/* First destroy type specific data */ /* First destroy type specific data */
destructor = destructor_table[item->type]; destructor = destructor_table[item->type];
@@ -269,83 +287,109 @@ void menuitem_destroy (MenuItem *item)
/* And finally...*/ /* And finally...*/
free (item); free (item);
}
} }
void menuitem_destroy_ring (MenuItem *item) void menuitem_destroy_ring (MenuItem *item)
{ {
debug (RPT_DEBUG, "%s( item=[%s] )",
__FUNCTION__, ((item != NULL) ? item->id : "(null)"));
if (item != NULL) {
char * s; char * s;
debug (RPT_DEBUG, "%s( item=[%s] )", __FUNCTION__, item->id);
/* deallocate the strings */ /* deallocate the strings */
for (s = LL_GetFirst(item->data.ring.strings); s; s = LL_GetNext(item->data.ring.strings)) { for (s = LL_GetFirst(item->data.ring.strings);
s != NULL;
s = LL_GetNext(item->data.ring.strings)) {
free (s); free (s);
} }
/* and the list */ /* and the list */
LL_Destroy (item->data.ring.strings); LL_Destroy (item->data.ring.strings);
}
} }
void menuitem_destroy_slider (MenuItem *item) void menuitem_destroy_slider (MenuItem *item)
{ {
debug (RPT_DEBUG, "%s( item=[%s] )", __FUNCTION__, item->id); debug (RPT_DEBUG, "%s( item=[%s] )",
__FUNCTION__, ((item != NULL) ? item->id : "(null)"));
if (item != NULL) {
/* These strings should always be allocated */ /* These strings should always be allocated */
free (item->data.slider.mintext); free (item->data.slider.mintext);
free (item->data.slider.maxtext); free (item->data.slider.maxtext);
}
} }
void menuitem_destroy_numeric (MenuItem *item) void menuitem_destroy_numeric (MenuItem *item)
{ {
debug (RPT_DEBUG, "%s( item=[%s] )", __FUNCTION__, item->id); debug (RPT_DEBUG, "%s( item=[%s] )",
__FUNCTION__, ((item != NULL) ? item->id : "(null)"));
if (item != NULL) {
/* This string should always be allocated */ /* This string should always be allocated */
free (item->data.alpha.edit_str); free (item->data.numeric.edit_str);
}
} }
void menuitem_destroy_alpha (MenuItem *item) void menuitem_destroy_alpha (MenuItem *item)
{ {
debug (RPT_DEBUG, "%s( item=[%s] )", __FUNCTION__, item->id); debug (RPT_DEBUG, "%s( item=[%s] )",
__FUNCTION__, ((item != NULL) ? item->id : "(null)"));
if (item != NULL) {
/* These strings should always be allocated */ /* These strings should always be allocated */
free (item->data.alpha.allowed_extra); free (item->data.alpha.allowed_extra);
free (item->data.alpha.value); free (item->data.alpha.value);
free (item->data.alpha.edit_str); free (item->data.alpha.edit_str);
}
} }
/******** MENU ITEM RESET FUNCTIONS ********/ /******** MENU ITEM RESET FUNCTIONS ********/
void menuitem_reset (MenuItem *item) void menuitem_reset (MenuItem *item)
{ {
void (*func) (MenuItem *); debug (RPT_DEBUG, "%s( item=[%s] )",
__FUNCTION__, ((item != NULL) ? item->id : "(null)"));
debug (RPT_DEBUG, "%s( item=[%s] )", __FUNCTION__, item->id); if (item != NULL) {
void (*func) (MenuItem *);
/* First destroy type specific data */ /* First destroy type specific data */
func = reset_table[item->type]; func = reset_table[item->type];
if (func) if (func)
func (item); func (item);
}
} }
void menuitem_reset_numeric (MenuItem *item) void menuitem_reset_numeric (MenuItem *item)
{ {
debug (RPT_DEBUG, "%s( item=[%s] )", __FUNCTION__, item->id); debug (RPT_DEBUG, "%s( item=[%s] )",
__FUNCTION__, ((item != NULL) ? item->id : "(null)"));
if (item != NULL) {
item->data.numeric.edit_pos = 0; item->data.numeric.edit_pos = 0;
memset ( item->data.numeric.edit_str, 0, MAX_NUMERIC_LEN); memset ( item->data.numeric.edit_str, 0, MAX_NUMERIC_LEN);
if (item->data.numeric.minvalue < 0) { if (item->data.numeric.minvalue < 0) {
snprintf (item->data.numeric.edit_str, MAX_NUMERIC_LEN, "%+d", item->data.numeric.value); snprintf (item->data.numeric.edit_str, MAX_NUMERIC_LEN,
"%+d", item->data.numeric.value);
} else { } else {
snprintf (item->data.numeric.edit_str, MAX_NUMERIC_LEN, "%d", item->data.numeric.value); snprintf (item->data.numeric.edit_str, MAX_NUMERIC_LEN,
"%d", item->data.numeric.value);
}
} }
} }
void menuitem_reset_alpha (MenuItem *item) void menuitem_reset_alpha (MenuItem *item)
{ {
debug (RPT_DEBUG, "%s( item=[%s] )", __FUNCTION__, item->id); debug (RPT_DEBUG, "%s( item=[%s] )",
__FUNCTION__, ((item != NULL) ? item->id : "(null)"));
if (item != NULL) {
item->data.alpha.edit_pos = 0; item->data.alpha.edit_pos = 0;
memset (item->data.alpha.edit_str, 0, item->data.alpha.maxlength+1); memset (item->data.alpha.edit_str, 0, item->data.alpha.maxlength+1);
strcpy (item->data.alpha.edit_str, item->data.alpha.value); strcpy (item->data.alpha.edit_str, item->data.alpha.value);
}
} }
@@ -356,7 +400,9 @@ void menuitem_rebuild_screen (MenuItem *item, Screen *s)
Widget * w; Widget * w;
void (*build_screen) (MenuItem *item, Screen *s); void (*build_screen) (MenuItem *item, Screen *s);
debug (RPT_DEBUG, "%s( item=[%s], screen=[%s] )", __FUNCTION__, item->id, s->id); debug (RPT_DEBUG, "%s( item=[%s], screen=[%s] )", __FUNCTION__,
((item != NULL) ? item->id : "(null)"),
((s != NULL) ? s->id : "(null)"));
if (!display_props) { if (!display_props) {
/* Nothing to build if no display size is known */ /* Nothing to build if no display size is known */
@@ -364,8 +410,9 @@ void menuitem_rebuild_screen (MenuItem *item, Screen *s)
return; return;
} }
if (s != NULL) {
/* First remove all widgets from the screen */ /* First remove all widgets from the screen */
while ( (w = screen_getfirst_widget(s)) ) { while ( (w = screen_getfirst_widget(s)) != NULL) {
/* We know these widgets don't have subwidgets, so we can /* We know these widgets don't have subwidgets, so we can
* easily remove them * easily remove them
*/ */
@@ -373,6 +420,7 @@ void menuitem_rebuild_screen (MenuItem *item, Screen *s)
widget_destroy (w); widget_destroy (w);
} }
if (item != NULL) {
/* Call type specific screen building function */ /* Call type specific screen building function */
build_screen = build_screen_table [item->type]; build_screen = build_screen_table [item->type];
if (build_screen) { if (build_screen) {
@@ -384,13 +432,17 @@ void menuitem_rebuild_screen (MenuItem *item, Screen *s)
/* Also always call update_screen */ /* Also always call update_screen */
menuitem_update_screen (item, s); menuitem_update_screen (item, s);
}
}
} }
void menuitem_rebuild_screen_slider (MenuItem *item, Screen *s) void menuitem_rebuild_screen_slider (MenuItem *item, Screen *s)
{ {
Widget * w; Widget * w;
debug (RPT_DEBUG, "%s( item=[%s], screen=[%s] )", __FUNCTION__, item->id, s->id); debug (RPT_DEBUG, "%s( item=[%s], screen=[%s] )", __FUNCTION__,
((item != NULL) ? item->id : "(null)"),
((s != NULL) ? s->id : "(null)"));
if (display_props->height >= 2 ) { if (display_props->height >= 2 ) {
/* Only add a title if enough space... */ /* Only add a title if enough space... */
@@ -438,7 +490,9 @@ void menuitem_rebuild_screen_numeric (MenuItem *item, Screen *s)
{ {
Widget * w; Widget * w;
debug (RPT_DEBUG, "%s( item=[%s], screen=[%s] )", __FUNCTION__, item->id, s->id); debug (RPT_DEBUG, "%s( item=[%s], screen=[%s] )", __FUNCTION__,
((item != NULL) ? item->id : "(null)"),
((s != NULL) ? s->id : "(null)"));
if (display_props->height >= 2 ) { if (display_props->height >= 2 ) {
/* Only add a title if enough space... */ /* Only add a title if enough space... */
@@ -469,7 +523,9 @@ void menuitem_rebuild_screen_alpha (MenuItem *item, Screen *s)
{ {
Widget * w; Widget * w;
debug (RPT_DEBUG, "%s( item=[%s], screen=[%s] )", __FUNCTION__, item->id, s->id); debug (RPT_DEBUG, "%s( item=[%s], screen=[%s] )", __FUNCTION__,
((item != NULL) ? item->id : "(null)"),
((s != NULL) ? s->id : "(null)"));
if (display_props->height >= 2 ) { if (display_props->height >= 2 ) {
/* Only add a title if enough space... */ /* Only add a title if enough space... */
@@ -502,7 +558,12 @@ void menuitem_update_screen (MenuItem *item, Screen *s)
{ {
void (*update_screen) (MenuItem *item, Screen *s); void (*update_screen) (MenuItem *item, Screen *s);
debug (RPT_DEBUG, "%s( item=[%s], screen=[%s] )", __FUNCTION__, item->id, s->id); debug (RPT_DEBUG, "%s( item=[%s], screen=[%s] )", __FUNCTION__,
((item != NULL) ? item->id : "(null)"),
((s != NULL) ? s->id : "(null)"));
if ((item == NULL) || (s == NULL))
return;
/* Disable the cursor by default */ /* Disable the cursor by default */
s->cursor = CURSOR_OFF; s->cursor = CURSOR_OFF;
@@ -522,7 +583,12 @@ void menuitem_update_screen_slider (MenuItem *item, Screen *s)
Widget * w; Widget * w;
int min_len, max_len; int min_len, max_len;
debug (RPT_DEBUG, "%s( item=[%s], screen=[%s] )", __FUNCTION__, item->id, s->id); debug (RPT_DEBUG, "%s( item=[%s], screen=[%s] )", __FUNCTION__,
((item != NULL) ? item->id : "(null)"),
((s != NULL) ? s->id : "(null)"));
if ((item == NULL) || (s == NULL))
return;
/* Calculate the bar position and length by filling buffers */ /* Calculate the bar position and length by filling buffers */
min_len = strlen (item->data.slider.mintext); min_len = strlen (item->data.slider.mintext);
@@ -559,7 +625,12 @@ void menuitem_update_screen_numeric (MenuItem *item, Screen *s)
{ {
Widget * w; Widget * w;
debug (RPT_DEBUG, "%s( item=[%s], screen=[%s] )", __FUNCTION__, item->id, s->id); debug (RPT_DEBUG, "%s( item=[%s], screen=[%s] )", __FUNCTION__,
((item != NULL) ? item->id : "(null)"),
((s != NULL) ? s->id : "(null)"));
if ((item == NULL) || (s == NULL))
return;
w = screen_find_widget (s, "value"); w = screen_find_widget (s, "value");
strcpy (w->text, item->data.numeric.edit_str); strcpy (w->text, item->data.numeric.edit_str);
@@ -580,7 +651,12 @@ void menuitem_update_screen_alpha (MenuItem *item, Screen *s)
{ {
Widget * w; Widget * w;
debug (RPT_DEBUG, "%s( item=[%s], screen=[%s] )", __FUNCTION__, item->id, s->id); debug (RPT_DEBUG, "%s( item=[%s], screen=[%s] )", __FUNCTION__,
((item != NULL) ? item->id : "(null)"),
((s != NULL) ? s->id : "(null)"));
if ((item == NULL) || (s == NULL))
return;
w = screen_find_widget (s, "value"); w = screen_find_widget (s, "value");
if (item->data.alpha.password_char == 0) { if (item->data.alpha.password_char == 0) {
@@ -608,7 +684,11 @@ MenuResult menuitem_process_input (MenuItem *item, MenuToken token, char * key)
{ {
MenuResult (*process_input) (MenuItem *item, MenuToken token, char * key); MenuResult (*process_input) (MenuItem *item, MenuToken token, char * key);
debug (RPT_DEBUG, "%s( item=[%s], token=%d, key=\"%s\" )", __FUNCTION__, item->id, token, key); debug (RPT_DEBUG, "%s( item=[%s], token=%d, key=\"%s\" )", __FUNCTION__,
((item != NULL) ? item->id : "(null)"), token, key);
if (item == NULL)
return MENURESULT_ERROR;
/* Call type specific screen building function */ /* Call type specific screen building function */
process_input = process_input_table [item->type]; process_input = process_input_table [item->type];
@@ -622,7 +702,11 @@ MenuResult menuitem_process_input (MenuItem *item, MenuToken token, char * key)
MenuResult menuitem_process_input_slider (MenuItem *item, MenuToken token, char * key) MenuResult menuitem_process_input_slider (MenuItem *item, MenuToken token, char * key)
{ {
debug (RPT_DEBUG, "%s( item=[%s], token=%d, key=\"%s\" )", __FUNCTION__, item->id, token, key); debug (RPT_DEBUG, "%s( item=[%s], token=%d, key=\"%s\" )", __FUNCTION__,
((item != NULL) ? item->id : "(null)"), token, key);
if (item == NULL)
return MENURESULT_ERROR;
switch (token) { switch (token) {
case MENUTOKEN_MENU: case MENUTOKEN_MENU:
@@ -653,21 +737,18 @@ MenuResult menuitem_process_input_numeric (MenuItem *item, MenuToken token, char
char buf1[MAX_NUMERIC_LEN]; char buf1[MAX_NUMERIC_LEN];
char buf2[MAX_NUMERIC_LEN]; char buf2[MAX_NUMERIC_LEN];
char *format_str;
int max_len; int max_len;
debug (RPT_DEBUG, "%s( item=[%s], token=%d, key=\"%s\" )", __FUNCTION__,
((item != NULL) ? item->id : "(null)"), token, key);
if (item != NULL) {
/* To make life easy... */ /* To make life easy... */
char *str = item->data.numeric.edit_str; char *str = item->data.numeric.edit_str;
int pos = item->data.numeric.edit_pos; int pos = item->data.numeric.edit_pos;
int allow_signed = (item->data.numeric.minvalue < 0); int allow_signed = (item->data.numeric.minvalue < 0);
char *format_str = (allow_signed) ? "%+d" : "%d";
debug (RPT_DEBUG, "%s( item=[%s], token=%d, key=\"%s\" )", __FUNCTION__, item->id, token, key);
if (allow_signed) {
format_str = "%+d";
} else {
format_str = "%d";
}
snprintf (buf1, MAX_NUMERIC_LEN, format_str, item->data.numeric.minvalue); snprintf (buf1, MAX_NUMERIC_LEN, format_str, item->data.numeric.minvalue);
snprintf (buf2, MAX_NUMERIC_LEN, format_str, item->data.numeric.maxvalue); snprintf (buf2, MAX_NUMERIC_LEN, format_str, item->data.numeric.maxvalue);
@@ -689,7 +770,7 @@ MenuResult menuitem_process_input_numeric (MenuItem *item, MenuToken token, char
} }
return MENURESULT_NONE; return MENURESULT_NONE;
case MENUTOKEN_ENTER: case MENUTOKEN_ENTER:
if (str[pos] == 0) { if (str[pos] == '\0') {
int value; int value;
/* The user completed his input */ /* The user completed his input */
@@ -773,13 +854,14 @@ MenuResult menuitem_process_input_numeric (MenuItem *item, MenuToken token, char
item->data.numeric.edit_pos = 0; item->data.numeric.edit_pos = 0;
return MENURESULT_NONE; return MENURESULT_NONE;
} }
/* proces numeric keys */ /* process numeric keys */
if ( strlen(key) == 1 && key[0] >= '0' && key[0] <= '9') { if ( strlen(key) == 1 && key[0] >= '0' && key[0] <= '9') {
str[pos] = key[0]; str[pos] = key[0];
item->data.numeric.edit_pos ++; item->data.numeric.edit_pos ++;
} }
return MENURESULT_NONE; return MENURESULT_NONE;
} }
}
return MENURESULT_ERROR; return MENURESULT_ERROR;
} }
@@ -788,12 +870,14 @@ MenuResult menuitem_process_input_alpha (MenuItem *item, MenuToken token, char *
char * p; char * p;
static char * chars = NULL; static char * chars = NULL;
debug (RPT_DEBUG, "%s( item=[%s], token=%d, key=\"%s\" )", __FUNCTION__,
((item != NULL) ? item->id : "(null)"), token, key);
if (item != NULL) {
/* To make life easy... */ /* To make life easy... */
char *str = item->data.alpha.edit_str; char *str = item->data.alpha.edit_str;
int pos = item->data.alpha.edit_pos; int pos = item->data.alpha.edit_pos;
debug (RPT_DEBUG, "%s( item=[%s], token=%d, key=\"%s\" )", __FUNCTION__, item->id, token, key);
/* Create list of allowed chars */ /* Create list of allowed chars */
chars = realloc (chars, 26 + 26 + 10 + strlen(item->data.alpha.allowed_extra) + 1); chars = realloc (chars, 26 + 26 + 10 + strlen(item->data.alpha.allowed_extra) + 1);
chars[0] = 0; /* clear string */ chars[0] = 0; /* clear string */
@@ -905,18 +989,21 @@ MenuResult menuitem_process_input_alpha (MenuItem *item, MenuToken token, char *
} }
return MENURESULT_NONE; return MENURESULT_NONE;
} }
}
return MENURESULT_ERROR; return MENURESULT_ERROR;
} }
LinkedList * tablist2linkedlist (char *strings) LinkedList * tablist2linkedlist (char *strings)
{ {
char *tabptr, *p, *new_s;
LinkedList * list; LinkedList * list;
list = LL_new(); list = LL_new();
/* Parse strings */ /* Parse strings */
p = strings; if (strings != NULL) {
char *p = strings;
char *tabptr, *new_s;
while ((tabptr = strchr(p, '\t')) != NULL) { while ((tabptr = strchr(p, '\t')) != NULL) {
int len = (int)(tabptr - p); int len = (int)(tabptr - p);
@@ -933,49 +1020,50 @@ LinkedList * tablist2linkedlist (char *strings)
/* Add last string */ /* Add last string */
new_s = strdup (p); new_s = strdup (p);
LL_Push (list, new_s); LL_Push (list, new_s);
}
return list; return list;
} }
MenuItemType menuitem_typename_to_type (char *name) MenuItemType menuitem_typename_to_type (char *name)
{ {
if (name != NULL) {
MenuItemType type; MenuItemType type;
for (type = 0; type < NUM_ITEMTYPES; type ++) { for (type = 0; type < NUM_ITEMTYPES; type ++) {
if (strcmp (menuitemtypenames[type], name) == 0) { if (strcmp (menuitemtypenames[type], name) == 0) {
return type; return type;
} }
} }
}
return -1; return -1;
} }
char *menuitem_type_to_typename (MenuItemType type) char *menuitem_type_to_typename (MenuItemType type)
{ {
if (type >= 0 && type < NUM_ITEMTYPES) { return ((type >= 0 && type < NUM_ITEMTYPES)
return menuitemtypenames[type]; ? menuitemtypenames[type]
} : NULL);
else {
return NULL;
}
} }
MenuEventType menuitem_eventtypename_to_eventtype (char *name) MenuEventType menuitem_eventtypename_to_eventtype (char *name)
{ {
if (name != NULL) {
MenuEventType type; MenuEventType type;
for (type = 0; type < NUM_EVENTTYPES; type ++) { for (type = 0; type < NUM_EVENTTYPES; type ++) {
if (strcmp (menueventtypenames[type], name) == 0) { if (strcmp (menueventtypenames[type], name) == 0) {
return type; return type;
} }
} }
}
return -1; return -1;
} }
char *menuitem_eventtype_to_eventtypename (MenuEventType type) char *menuitem_eventtype_to_eventtypename (MenuEventType type)
{ {
if (type >= 0 && type < NUM_EVENTTYPES) { return ((type >= 0 && type < NUM_EVENTTYPES)
return menueventtypenames[type]; ? menueventtypenames[type]
} : NULL);
else {
return NULL;
}
} }
+6 -2
View File
@@ -65,12 +65,16 @@ int screen_remove_widget (Screen * s, Widget * w);
/* List functions */ /* List functions */
static inline Widget * screen_getfirst_widget (Screen * s) static inline Widget * screen_getfirst_widget (Screen * s)
{ {
return LL_GetFirst(s->widgetlist); return (Widget *) ((s != NULL)
? LL_GetFirst(s->widgetlist)
: NULL);
} }
static inline Widget * screen_getnext_widget (Screen * s) static inline Widget * screen_getnext_widget (Screen * s)
{ {
return LL_GetNext(s->widgetlist); return (Widget *) ((s != NULL)
? LL_GetNext(s->widgetlist)
: NULL);
} }