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