Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libmenu Added new menu style O_RADIO which implements a ...



details:   https://anonhg.NetBSD.org/src/rev/b6404403bf25
branches:  trunk
changeset: 534553:b6404403bf25
user:      blymn <blymn%NetBSD.org@localhost>
date:      Mon Jul 29 13:03:51 2002 +0000

description:
Added new menu style O_RADIO which implements a radio button style
menu selection.  Thanks go to rtr(at)wasabisystems.com for doing
most of the work on this.

diffstat:

 lib/libmenu/driver.c          |  36 +++++++++++++++++++---
 lib/libmenu/internals.c       |   4 +-
 lib/libmenu/item.c            |  35 +++++++++++++++++++++-
 lib/libmenu/menu.c            |  68 +++++++++++++++++++++++++++++++++++++++++-
 lib/libmenu/menu.h            |   4 +-
 lib/libmenu/menu_item_value.3 |  33 +++++++++++++++++++-
 lib/libmenu/menus.3           |  10 +++++-
 lib/libmenu/post.c            |  10 +++--
 lib/libmenu/shlib_version     |   4 +-
 9 files changed, 183 insertions(+), 21 deletions(-)

diffs (truncated from 416 to 300 lines):

diff -r 904a06e3bfd0 -r b6404403bf25 lib/libmenu/driver.c
--- a/lib/libmenu/driver.c      Mon Jul 29 12:58:10 2002 +0000
+++ b/lib/libmenu/driver.c      Mon Jul 29 13:03:51 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: driver.c,v 1.7 2001/06/13 10:45:59 wiz Exp $   */
+/*     $NetBSD: driver.c,v 1.8 2002/07/29 13:03:51 blymn Exp $ */
 
 /*-
  * Copyright (c) 1998-1999 Brett Lymn (blymn%baea.com.au@localhost, brett_lymn%yahoo.com.au@localhost)
@@ -42,8 +42,10 @@
 int
 menu_driver(MENU *menu, int c)
 {
-       int drv_top_row, drv_scroll, it, status = E_OK;
+       int drv_top_row, drv_scroll, i, it, status = E_OK;
        ITEM *drv_new_item;
+
+       i = 0;
        
        if (menu == NULL)
                return E_BAD_ARGUMENT;
@@ -167,8 +169,32 @@
                          }
                          break;
                  case REQ_TOGGLE_ITEM:
-                         if ((menu->opts & O_ONEVALUE) == O_ONEVALUE) {
-                                 return E_REQUEST_DENIED;
+                         if ((menu->opts & (O_RADIO | O_ONEVALUE)) != 0) {
+                             if ((menu->opts & O_RADIO) == O_RADIO) {
+                                 if ((drv_new_item->opts & O_SELECTABLE)
+                                                       != O_SELECTABLE)
+                                         return E_NOT_SELECTABLE;
+
+                                   /* don't deselect selected item */
+                                 if (drv_new_item->selected == 1)
+                                         return E_REQUEST_DENIED;
+                                 
+                                 /* deselect all items */
+                                 for (i = 0; i < menu->item_count; i++) {
+                                     if ((menu->items[i]->selected) &&
+                                         (drv_new_item->index != i)) {
+                                         menu->items[i]->selected ^= 1;
+                                         _menui_draw_item(menu,
+                                               menu->items[i]->index);
+                                     }
+                                 }
+
+                                   /* turn on selected item */
+                                 drv_new_item->selected ^= 1;
+                                 _menui_draw_item(menu, drv_new_item->index);
+                             } else {
+                                 return E_REQUEST_DENIED;
+                             }
                          } else {
                                  if ((drv_new_item->opts
                                       & O_SELECTABLE) == O_SELECTABLE) {
@@ -176,7 +202,7 @@
                                          drv_new_item->selected ^= 1;
                                            /* update item in menu */
                                          _menui_draw_item(menu,
-                                                           drv_new_item->index);
+                                               drv_new_item->index);
                                  } else {
                                          return E_NOT_SELECTABLE;
                                  }
diff -r 904a06e3bfd0 -r b6404403bf25 lib/libmenu/internals.c
--- a/lib/libmenu/internals.c   Mon Jul 29 12:58:10 2002 +0000
+++ b/lib/libmenu/internals.c   Mon Jul 29 13:03:51 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: internals.c,v 1.9 2002/02/04 13:02:05 blymn Exp $      */
+/*     $NetBSD: internals.c,v 1.10 2002/07/29 13:03:51 blymn Exp $     */
 
 /*-
  * Copyright (c) 1998-1999 Brett Lymn (blymn%baea.com.au@localhost, brett_lymn%yahoo.com.au@localhost)
@@ -388,7 +388,7 @@
              menu->items[item]->row - menu->top_row,
              menu->items[item]->col * menu->col_width);
 
-       if ((menu->cur_item == item) || (menu->items[item]->selected == 1))
+       if (menu->cur_item == item)
                wattrset(menu->scrwin, menu->fore);
        if ((menu->items[item]->opts & O_SELECTABLE) != O_SELECTABLE)
                wattron(menu->scrwin, menu->grey);
diff -r 904a06e3bfd0 -r b6404403bf25 lib/libmenu/item.c
--- a/lib/libmenu/item.c        Mon Jul 29 12:58:10 2002 +0000
+++ b/lib/libmenu/item.c        Mon Jul 29 13:03:51 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: item.c,v 1.7 2001/06/13 10:45:59 wiz Exp $     */
+/*     $NetBSD: item.c,v 1.8 2002/07/29 13:03:51 blymn Exp $   */
 
 /*-
  * Copyright (c) 1998-1999 Brett Lymn (blymn%baea.com.au@localhost, brett_lymn%yahoo.com.au@localhost)
@@ -143,6 +143,39 @@
 }
 
 /*
+ * Returns the number of items that are selected.
+ * The index numbers of the items are placed in the dynamically allocated
+ * int array *sel.
+ */
+int
+item_selected(MENU *menu, int **sel)
+{
+       int i, j;
+
+       if (menu == NULL)
+               return E_BAD_ARGUMENT;
+
+       /* count selected */
+       for (i = 0, j = 0; i < menu->item_count; i++)
+               if (menu->items[i]->selected)
+                       j++;
+
+       if (j == 0) {
+               *sel = NULL;
+               return 0;
+       }
+       
+       if ( (*sel = malloc(sizeof(int) * j)) == NULL)
+               return E_SYSTEM_ERROR;
+
+       for (i = 0, j = 0; i < menu->item_count; i++)
+               if (menu->items[i]->selected)
+                       (*sel)[j++] = i;
+
+       return j;
+}
+
+/*
  * Set the item options.  We keep a global copy of the current item options
  * as subsequent new_item calls will use the updated options as their
  * defaults.
diff -r 904a06e3bfd0 -r b6404403bf25 lib/libmenu/menu.c
--- a/lib/libmenu/menu.c        Mon Jul 29 12:58:10 2002 +0000
+++ b/lib/libmenu/menu.c        Mon Jul 29 13:03:51 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: menu.c,v 1.11 2002/02/04 13:02:05 blymn Exp $  */
+/*     $NetBSD: menu.c,v 1.12 2002/07/29 13:03:51 blymn Exp $  */
 
 /*-
  * Copyright (c) 1998-1999 Brett Lymn (blymn%baea.com.au@localhost, brett_lymn%yahoo.com.au@localhost)
@@ -306,11 +306,34 @@
 int
 set_menu_opts(MENU *param_menu, OPTIONS opts)
 {
+       int i, seen;
        MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
        OPTIONS old_opts = menu->opts;
        
         menu->opts = opts;
 
+         /*
+          * If the radio option is selected then make sure only one
+          * item is actually selected in the items.
+          */
+       if (((opts & O_RADIO) == O_RADIO) && (menu->items != NULL) &&
+           (menu->items[0] != NULL)) {
+               seen = 0;
+               for (i = 0; i < menu->item_count; i++) {
+                       if (menu->items[i]->selected == 1) {
+                               if (seen == 0) {
+                                       seen = 1;
+                               } else {
+                                       menu->items[i]->selected = 0;
+                               }
+                       }
+               }
+
+                 /* if none selected, select the first item */
+               if (seen == 0)
+                       menu->items[0]->selected = 1;
+       }
+
        if ((menu->opts & O_ROWMAJOR) != (old_opts &  O_ROWMAJOR))
                  /* changed menu layout - need to recalc neighbours */
                _menui_stitch_items(menu);
@@ -324,11 +347,33 @@
 int
 menu_opts_on(MENU *param_menu, OPTIONS opts)
 {
+       int i, seen;
        MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
        OPTIONS old_opts = menu->opts;
 
         menu->opts |= opts;
 
+         /*
+          * If the radio option is selected then make sure only one
+          * item is actually selected in the items.
+          */
+       if (((opts & O_RADIO) == O_RADIO) && (menu->items != NULL) &&
+           (menu->items[0] != NULL)) {
+               seen = 0;
+               for (i = 0; i < menu->item_count; i++) {
+                       if (menu->items[i]->selected == 1) {
+                               if (seen == 0) {
+                                       seen = 1;
+                               } else {
+                                       menu->items[i]->selected = 0;
+                               }
+                       }
+               }
+                 /* if none selected then select the top item */
+               if (seen == 0)
+                       menu->items[0]->selected = 1;
+       }
+
        if ((menu->items != NULL) &&
            (menu->opts & O_ROWMAJOR) != (old_opts &  O_ROWMAJOR))
                  /* changed menu layout - need to recalc neighbours */
@@ -485,7 +530,7 @@
 set_menu_items(MENU *param_menu, ITEM **items)
 {
        MENU *menu = (param_menu != NULL) ? param_menu : &_menui_default_menu;
-       int i, new_count = 0;
+       int i, new_count = 0, sel_count = 0;
        
          /* don't change if menu is posted */
        if (menu->posted == 1)
@@ -496,10 +541,19 @@
                if ((items[new_count]->parent != NULL) &&
                    (items[new_count]->parent != menu))
                        return E_CONNECTED;
+               if (items[new_count]->selected == 1)
+                       sel_count++;
                new_count++;
        }
+
+         /*
+          * don't allow multiple selected items if menu is radio
+          * button style.
+          */
+       if (((menu->opts & O_RADIO) == O_RADIO) &&
+           (sel_count > 1))
+               return E_BAD_ARGUMENT;
        
-
          /* if there were items connected then disconnect them. */
        if (menu->items != NULL) {
                for (i = 0; i < menu->item_count; i++) {
@@ -525,6 +579,14 @@
                menu->match_len = 0;
        }
        
+         /*
+          * make sure at least one item is selected on a radio
+          * button style menu.
+          */
+       if (((menu->opts & O_RADIO) == O_RADIO) && (sel_count == 0))
+               menu->items[0]->selected = 1;
+       
+       
        _menui_stitch_items(menu); /* recalculate the item neighbours */
        
        return E_OK;
diff -r 904a06e3bfd0 -r b6404403bf25 lib/libmenu/menu.h
--- a/lib/libmenu/menu.h        Mon Jul 29 12:58:10 2002 +0000
+++ b/lib/libmenu/menu.h        Mon Jul 29 13:03:51 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: menu.h,v 1.11 2002/02/04 13:02:06 blymn Exp $  */
+/*     $NetBSD: menu.h,v 1.12 2002/07/29 13:03:51 blymn Exp $  */
 
 /*-
  * Copyright (c) 1998-1999 Brett Lymn (blymn%baea.com.au@localhost, brett_lymn%yahoo.com.au@localhost)
@@ -67,6 +67,7 @@
 #define O_SHOWMATCH  (0x10)
 #define O_NONCYCLIC  (0x20)
 #define O_SELECTABLE (0x40)
+#define O_RADIO      (0x80)
 
 typedef struct __menu_str {
         char *string;
@@ -190,6 +191,7 @@
 OPTIONS item_opts(ITEM *);
 int item_opts_off(ITEM *, OPTIONS);
 int item_opts_on(ITEM *, OPTIONS);
+int item_selected(MENU *, int **); /* return the item index of selected */
 Menu_Hook item_term(MENU *);
 char *item_userptr(ITEM *);
 int item_value(ITEM *);
diff -r 904a06e3bfd0 -r b6404403bf25 lib/libmenu/menu_item_value.3
--- a/lib/libmenu/menu_item_value.3     Mon Jul 29 12:58:10 2002 +0000
+++ b/lib/libmenu/menu_item_value.3     Mon Jul 29 13:03:51 2002 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: menu_item_value.3,v 1.6 2002/02/20 11:47:56 wiz Exp $
+.\"    $NetBSD: menu_item_value.3,v 1.7 2002/07/29 13:03:51 blymn Exp $
 .\"
 .\" Copyright (c) 1999
 .\"    Brett Lymn - blymn%baea.com.au@localhost, brett_lymn%yahoo.com.au@localhost
@@ -34,7 +34,8 @@
 .Os
 .Sh NAME
 .Nm item_value ,
-.Nm set_item_value
+.Nm set_item_value ,
+.Nm item_selected
 .Nd get or set value for an item
 .Sh LIBRARY
 .Lb libmenu
@@ -44,6 +45,8 @@



Home | Main Index | Thread Index | Old Index