Home | History | Annotate | Line # | Download | only in libmenu
driver.c revision 1.2
      1 /*      $NetBSD: driver.c,v 1.2 1999/11/24 12:43:15 kleink Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998-1999 Brett Lymn (blymn (at) baea.com.au, brett_lymn (at) yahoo.com)
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. The name of the author may not be used to endorse or promote products
     13  *    derived from this software withough specific prior written permission
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  *
     26  *
     27  */
     28 
     29 #include <menu.h>
     30 #include <ctype.h>
     31 #include <stdlib.h>
     32 #include "internals.h"
     33 
     34 /*
     35  * The guts of the menu library.  This function processes the character
     36  * in c and performs actions based on the value of the character.  If the
     37  * character is a normal one then the driver attempts to match the character
     38  * against the items.  If the character is a recognised request then the
     39  * request is processed by the driver, if the character is not a recognised
     40  * request and is not printable then it assumed to be a user defined command.
     41  */
     42 int
     43 menu_driver(menu, c)
     44 	MENU *menu;
     45 	int c;
     46 {
     47 	int top_row, scroll, it, status = E_OK;
     48 	ITEM *new_item;
     49 
     50 	if (menu == NULL)
     51 		return E_BAD_ARGUMENT;
     52 	if (menu->posted == 0)
     53 		return E_NOT_POSTED;
     54 	if (menu->items == NULL)
     55 		return E_NOT_CONNECTED;
     56 	if (*menu->items == NULL)
     57 		return E_NOT_CONNECTED;
     58 	if (menu->in_init == 1)
     59 		return E_BAD_STATE;
     60 
     61 	  /* this one should never happen but just in case.... */
     62 	if (menu->items[menu->cur_item] == NULL) return E_SYSTEM_ERROR;
     63 
     64 	new_item = menu->items[menu->cur_item];
     65 	it = menu->cur_item;
     66 	top_row = menu->top_row;
     67 
     68 	if ((c > REQ_BASE_NUM) && (c <= MAX_COMMAND)) {
     69 		  /* is a known driver request  - first check if the pattern
     70 		   * buffer needs to be cleared, we do this on non-search
     71 		   * type requests.
     72 		   */
     73 		if (! ((c == REQ_BACK_PATTERN) || (c == REQ_NEXT_MATCH) ||
     74 		       (c == REQ_PREV_MATCH))) {
     75 			if ((c == REQ_CLEAR_PATTERN)
     76 			    && (menu->pattern == NULL))
     77 				return E_REQUEST_DENIED;
     78 			free(menu->pattern);
     79 			menu->pattern = NULL;
     80 			menu->plen = 0;
     81 			menu->match_len = 0;
     82 		}
     83 
     84 		switch (c) {
     85 		  case REQ_LEFT_ITEM:
     86 			  new_item = new_item->left;
     87 			  break;
     88 		  case REQ_RIGHT_ITEM:
     89 			  new_item = new_item->right;
     90 			  break;
     91 		  case REQ_UP_ITEM:
     92 			  new_item = new_item->up;
     93 			  break;
     94 		  case REQ_DOWN_ITEM:
     95 			  new_item = new_item->down;
     96 			  break;
     97 		  case REQ_SCR_ULINE:
     98 			  if (top_row == 0)
     99 				  return E_REQUEST_DENIED;
    100 			  top_row--;
    101 			  new_item = new_item->up;
    102 			  break;
    103 		  case REQ_SCR_DLINE:
    104 			  top_row++;
    105 			  if ((top_row + menu->rows - 1)> menu->item_rows)
    106 				  return E_REQUEST_DENIED;
    107 			  new_item = new_item->down;
    108 			  break;
    109 		  case REQ_SCR_DPAGE:
    110 			  scroll = menu->item_rows - menu->rows
    111 				  - menu->top_row;
    112 			  if (scroll > menu->rows) {
    113 				  scroll = menu->rows;
    114 			  }
    115 
    116 			  if (scroll <= 0) {
    117 				  return E_REQUEST_DENIED;
    118 			  } else {
    119 				  top_row += scroll;
    120 				  while (scroll-- > 0)
    121 					  new_item = new_item->down;
    122 			  }
    123 			  break;
    124 		  case REQ_SCR_UPAGE:
    125 			  if (menu->rows < menu->top_row) {
    126 				  scroll = menu->rows;
    127 			  } else {
    128 				  scroll = menu->top_row;
    129 			  }
    130 			  if (scroll == 0)
    131 				  return E_REQUEST_DENIED;
    132 
    133 			  top_row -= scroll;
    134 			  while (scroll-- > 0)
    135 				  new_item = new_item->up;
    136 			  break;
    137 		  case REQ_FIRST_ITEM:
    138 			  new_item = menu->items[0];
    139 			  break;
    140 		  case REQ_LAST_ITEM:
    141 			  new_item = menu->items[menu->item_count - 1];
    142 			  break;
    143 		  case REQ_NEXT_ITEM:
    144 			  if ((menu->cur_item + 1) >= menu->item_count) {
    145 				  if ((menu->opts & O_NONCYCLIC)
    146 				      == O_NONCYCLIC) {
    147 					  return E_REQUEST_DENIED;
    148 				  } else {
    149 					  new_item = menu->items[0];
    150 				  }
    151 			  } else {
    152 				  new_item = menu->items[menu->cur_item + 1];
    153 			  }
    154 			  break;
    155 		  case REQ_PREV_ITEM:
    156 			  if (menu->cur_item == 0) {
    157 				  if ((menu->opts & O_NONCYCLIC)
    158 				      == O_NONCYCLIC) {
    159 					  return E_REQUEST_DENIED;
    160 				  } else {
    161 					  new_item = menu->items[
    162 						  menu->item_count - 1];
    163 				  }
    164 			  } else {
    165 				  new_item = menu->items[menu->cur_item - 1];
    166 			  }
    167 			  break;
    168 		  case REQ_TOGGLE_ITEM:
    169 			  if ((menu->opts & O_ONEVALUE) == O_ONEVALUE) {
    170 				  return E_REQUEST_DENIED;
    171 			  } else {
    172 				  if ((new_item->opts
    173 				       & O_SELECTABLE) == O_SELECTABLE) {
    174 					    /* toggle select flag */
    175 					  new_item->selected ^= 1;
    176 					    /* update item in menu */
    177 					  __menui_draw_item(menu,
    178 							    new_item->index);
    179 				  } else {
    180 					  return E_NOT_SELECTABLE;
    181 				  }
    182 			  }
    183 			  break;
    184 		  case REQ_CLEAR_PATTERN:
    185 			    /* this action is taken before the
    186 			       case statement */
    187 			  break;
    188 		  case REQ_BACK_PATTERN:
    189 			  if (menu->pattern == NULL)
    190 				  return E_REQUEST_DENIED;
    191 
    192 			  if (menu->plen == 0)
    193 				  return E_REQUEST_DENIED;
    194 			  menu->pattern[menu->plen--] = '\0';
    195 			  break;
    196 		  case REQ_NEXT_MATCH:
    197 			  if (menu->pattern == NULL)
    198 				  return E_REQUEST_DENIED;
    199 
    200 			  status = __menui_match_pattern(menu, 0,
    201 							 MATCH_NEXT_FORWARD,
    202 							 &it);
    203 			  new_item = menu->items[it];
    204 			  break;
    205 		  case REQ_PREV_MATCH:
    206 			  if (menu->pattern == NULL)
    207 				  return E_REQUEST_DENIED;
    208 
    209 			  status = __menui_match_pattern(menu, 0,
    210 							 MATCH_NEXT_REVERSE,
    211 							 &it);
    212 			  new_item = menu->items[it];
    213 			  break;
    214 		}
    215 	} else if (c > MAX_COMMAND) {
    216 		  /* must be a user command */
    217 		return E_UNKNOWN_COMMAND;
    218 	} else if (isprint((char) c)) {
    219 		  /* otherwise search items for the character. */
    220 		status = __menui_match_pattern(menu, c, MATCH_FORWARD,
    221 					       &it);
    222 		new_item = menu->items[it];
    223 
    224 		  /* update the position of the cursor if we are doing
    225 		   * show match and the current item has not changed.  If
    226 		   * we don't do this here it won't get done since the
    227 		   * display will not be updated due to the current item
    228 		   * not changing.
    229 		   */
    230 		if ((new_item->index == menu->cur_item)
    231 		    && ((menu->opts & O_SHOWMATCH) == O_SHOWMATCH)) {
    232 			pos_menu_cursor(menu);
    233 		}
    234 
    235 
    236 	} else {
    237 		  /* bad character */
    238 		return E_BAD_ARGUMENT;
    239 	}
    240 
    241 	if (new_item == NULL) return E_REQUEST_DENIED;
    242 
    243 	if (new_item->row < top_row) top_row = new_item->row;
    244 	if (new_item->row >= (top_row + menu->rows))
    245 		top_row = new_item->row - menu->rows + 1;
    246 
    247 	if ((new_item->index != menu->cur_item) || (top_row != menu->top_row))
    248 		__menui_goto_item(menu, new_item, top_row);
    249 
    250 	return status;
    251 }
    252 
    253 
    254 
    255 
    256 
    257