Home | History | Annotate | Line # | Download | only in libmenu
internals.c revision 1.1
      1 /*      $Id: internals.c,v 1.1 1999/11/23 11:12:36 blymn 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 <string.h>
     33 #include "internals.h"
     34 
     35 /* internal function prototypes */
     36 static void
     37 __menui_calc_neighbours(MENU *, int, int, int, int, ITEM **, ITEM **,
     38 			ITEM **, ITEM **);
     39 static void __menui_redraw_menu __P((MENU *, int, int));
     40 
     41   /*
     42    * Link all the menu items together to speed up navigation.  We need
     43    * to calculate the widest item entry, then work out how many columns
     44    * of items the window will accomodate and then how many rows there will
     45    * be.  Once the layout is determined the neighbours of each item is
     46    * calculated and the item structures updated.
     47    */
     48 int
     49 __menui_stitch_items(menu)
     50 	MENU *menu;
     51 {
     52 	int i, cycle, row_major;
     53 
     54 	cycle = ((menu->opts & O_NONCYCLIC) != O_NONCYCLIC);
     55 	row_major = ((menu->opts & O_ROWMAJOR) == O_ROWMAJOR);
     56 
     57 	if (menu->posted == 1)
     58 		return E_POSTED;
     59 	if (menu->items == NULL)
     60 		return E_BAD_ARGUMENT;
     61 
     62 	if (row_major) {
     63 		menu->item_rows = menu->item_count / menu->cols;
     64 		menu->item_cols = menu->cols;
     65 		if (menu->item_count > (menu->item_rows * menu->item_cols))
     66 			menu->item_rows += 1;
     67 	} else {
     68 		menu->item_cols = menu->item_count / menu->rows;
     69 		menu->item_rows = menu->rows;
     70 		if (menu->item_count > (menu->item_rows * menu->item_cols))
     71 			menu->item_cols += 1;
     72 	}
     73 
     74 
     75 	__menui_max_item_size(menu);
     76 
     77 	for (i = 0; i < menu->item_count; i++) {
     78 		  /* Calculate the neighbours.  The ugliness here deals with
     79 		   * the differing menu layout styles.  The layout affects
     80 		   * the neighbour calculation so we change the arguments
     81 		   * around depending on the layout style.
     82 		   */
     83 		__menui_calc_neighbours(menu, i, cycle,
     84 					(row_major) ? menu->item_rows
     85 					: menu->item_cols,
     86 					(row_major) ? menu->item_cols
     87 					: menu->item_rows,
     88 					(row_major) ? &menu->items[i]->right
     89 					: &menu->items[i]->down,
     90 					(row_major) ? &menu->items[i]->left
     91 					: &menu->items[i]->up,
     92 					(row_major) ? &menu->items[i]->down
     93 					: &menu->items[i]->right,
     94 					(row_major) ? &menu->items[i]->up
     95 					: &menu->items[i]->left);
     96 
     97 		  /* fill in the row and column value of the item */
     98 		if (row_major) {
     99 			menu->items[i]->row = i / menu->item_cols;
    100 			menu->items[i]->col = i % menu->item_cols;
    101 		} else {
    102 			menu->items[i]->row = i % menu->item_rows;
    103 			menu->items[i]->col = i / menu->item_rows;
    104 		}
    105 	}
    106 
    107 	return E_OK;
    108 }
    109 
    110   /*
    111    * Calculate the neighbours for an item in menu.  This routine deliberately
    112    * does not refer to up/down/left/right as these concepts depend on the menu
    113    * layout style (row major or not).  By arranging the arguments in the right
    114    * order the caller can generate the the neighbours for either menu layout
    115    * style.
    116    */
    117 static void
    118 __menui_calc_neighbours(menu, index, cycle, item_rows, item_cols, next, prev,
    119 			major_next, major_prev)
    120 	MENU *menu;
    121 	int index;
    122 	int cycle;
    123 	int item_rows;
    124 	int item_cols;
    125 	ITEM **next;
    126 	ITEM **prev;
    127 	ITEM **major_next;
    128 	ITEM **major_prev;
    129 {
    130 	int neighbour;
    131 
    132 	if (item_rows < 2) {
    133 		if (cycle) {
    134 			*major_next = menu->items[index];
    135 			*major_prev = menu->items[index];
    136 		} else {
    137 			*major_next = NULL;
    138 			*major_prev = NULL;
    139 		}
    140 	} else {
    141 		neighbour = index + item_cols;
    142 		if (neighbour >= menu->item_count) {
    143 			if (cycle) {
    144 				if (item_rows == 2) {
    145 					neighbour = index - item_cols;
    146 					if (neighbour < 0)
    147 						neighbour = index;
    148 					*major_next = menu->items[neighbour];
    149 				} else {
    150 					*major_next =
    151 						menu->items[index % item_cols];
    152 				}
    153 			} else
    154 				*major_next = NULL;
    155 		} else
    156 			*major_next = menu->items[neighbour];
    157 
    158 
    159 		neighbour = index - item_cols;
    160 		if (neighbour < 0) {
    161 			if (cycle) {
    162 				if (item_rows == 2) {
    163 					neighbour = index + item_cols;
    164 					if (neighbour >= menu->item_count)
    165 						neighbour = index;
    166 					*major_prev = menu->items[neighbour];
    167 				} else {
    168 					neighbour = index +
    169 						(item_rows - 1) * item_cols;
    170 
    171 					if (neighbour >= menu->item_count)
    172 						neighbour = index +
    173 							(item_rows - 2)
    174 							* item_cols;
    175 
    176 					*major_prev = menu->items[neighbour];
    177 				}
    178 			} else
    179 				*major_prev = NULL;
    180 		} else
    181 			*major_prev = menu->items[neighbour];
    182 	}
    183 
    184 	if ((index % item_cols) == 0) {
    185 		if (cycle) {
    186 			if (item_cols  < 2) {
    187 				*prev = menu->items[index];
    188 			} else {
    189 				neighbour = index + item_cols - 1;
    190 				if (neighbour >= menu->item_count) {
    191 					if (item_cols == 2) {
    192 						*prev = menu->items[index];
    193 					} else {
    194 						*prev = menu->items[menu->item_count - 1];
    195 					}
    196 				} else
    197 					*prev = menu->items[neighbour];
    198 			}
    199 		} else
    200 			*prev = NULL;
    201 	} else
    202 		*prev = menu->items[index - 1];
    203 
    204 	if ((index % item_cols) == (item_cols - 1)) {
    205 		if (cycle) {
    206 			if (item_cols  < 2) {
    207 				*next = menu->items[index];
    208 			} else {
    209 				neighbour = index - item_cols + 1;
    210 				if (neighbour >= menu->item_count) {
    211 					if (item_cols == 2) {
    212 						*next = menu->items[index];
    213 					} else {
    214 						neighbour = item_cols * index / item_cols;
    215 
    216 						*next = menu->items[neighbour];
    217 					}
    218 				} else
    219 					*next = menu->items[neighbour];
    220 			}
    221 		} else
    222 			*next = NULL;
    223 	} else {
    224 		neighbour = index + 1;
    225 		if (neighbour >= menu->item_count) {
    226 			if (cycle) {
    227 				neighbour = item_cols * (item_rows - 1);
    228 				*next = menu->items[neighbour];
    229 			} else
    230 				*next = NULL;
    231 		} else
    232 			*next = menu->items[neighbour];
    233 	}
    234 }
    235 
    236 /*
    237  * Goto the item pointed to by item and adjust the menu structure
    238  * accordingly.  Call the term and init functions if required.
    239  */
    240 int
    241 __menui_goto_item(menu, item, top_row)
    242 	MENU *menu;
    243 	ITEM *item;
    244 	int top_row;
    245 {
    246 	int old_top_row = menu->top_row, old_cur_item = menu->cur_item;
    247 
    248 	  /* If we get a null then the menu is not cyclic so deny request */
    249 	if (item == NULL)
    250 		return E_REQUEST_DENIED;
    251 
    252 	menu->in_init = 1;
    253 	if (menu->top_row != top_row) {
    254 		if ((menu->posted == 1) && (menu->menu_term != NULL))
    255 			menu->menu_term(menu);
    256 		menu->top_row = top_row;
    257 
    258 		if ((menu->posted == 1) && (menu->menu_init != NULL))
    259 			menu->menu_init(menu);
    260 	}
    261 
    262 	  /* this looks like wasted effort but it can happen.... */
    263 	if (menu->cur_item != item->index) {
    264 
    265 		if ((menu->posted == 1) && (menu->item_term != NULL))
    266 			menu->item_term(menu);
    267 
    268 		menu->cur_item = item->index;
    269 		menu->cur_row = item->row;
    270 		menu->cur_col = item->col;
    271 
    272 		if (menu->posted == 1)
    273 			__menui_redraw_menu(menu, old_top_row, old_cur_item);
    274 
    275 		if ((menu->posted == 1) && (menu->item_init != NULL))
    276 			menu->item_init(menu);
    277 
    278 	}
    279 
    280 	menu->in_init = 0;
    281 	return E_OK;
    282 }
    283 
    284 /*
    285  * Attempt to match items with the pattern buffer in the direction given
    286  * by iterating over the menu items.  If a match is found return E_OK
    287  * otherwise return E_NO_MATCH
    288  */
    289 int
    290 __menui_match_items(menu, direction, item_matched)
    291 	MENU *menu;
    292 	int direction;
    293 	int *item_matched;
    294 {
    295 	int i, caseless;
    296 
    297 	caseless = ((menu->opts & O_IGNORECASE) == O_IGNORECASE);
    298 
    299 	i = menu->cur_item;
    300 	if (direction == MATCH_NEXT_FORWARD) {
    301 		if (++i >= menu->item_count) i = 0;
    302 	} else if (direction == MATCH_NEXT_REVERSE) {
    303 		if (--i < 0) i = menu->item_count - 1;
    304 	}
    305 
    306 
    307 	do {
    308 		if (menu->items[i]->name.length >= menu->plen) {
    309 			  /* no chance if pattern is longer */
    310 			if (caseless) {
    311 				if (strncasecmp(menu->items[i]->name.string,
    312 						menu->pattern,
    313 						menu->plen) == 0) {
    314 					*item_matched = i;
    315 					menu->match_len = menu->plen;
    316 					return E_OK;
    317 				}
    318 			} else {
    319 				if (strncmp(menu->items[i]->name.string,
    320 					    menu->pattern, menu->plen) == 0) {
    321 					*item_matched = i;
    322 					menu->match_len = menu->plen;
    323 					return E_OK;
    324 				}
    325 			}
    326 		}
    327 
    328 		if ((direction == MATCH_FORWARD) ||
    329 		    (direction == MATCH_NEXT_FORWARD)) {
    330 			if (++i >= menu->item_count) i = 0;
    331 		} else {
    332 			if (--i <= 0) i = menu->item_count - 1;
    333 		}
    334 	} while (i != menu->cur_item);
    335 
    336 	menu->match_len = 0; /* match did not succeed - kill the match len. */
    337 	return E_NO_MATCH;
    338 }
    339 
    340 /*
    341  * Attempt to match the pattern buffer against the items.  If c is a
    342  * printable character then add it to the pattern buffer prior to
    343  * performing the match.  Direction determines the direction of matching.
    344  * If the match is successful update the item_matched variable with the
    345  * index of the item that matched the pattern.
    346  */
    347 int
    348 __menui_match_pattern(menu, c, direction, item_matched)
    349 	MENU *menu;
    350 	char c;
    351 	int direction;
    352 	int *item_matched;
    353 {
    354 	if (menu == NULL)
    355 		return E_BAD_ARGUMENT;
    356 	if (menu->items == NULL)
    357 		return E_BAD_ARGUMENT;
    358 	if (*menu->items == NULL)
    359 		return E_BAD_ARGUMENT;
    360 
    361 	if (isprint(c)) {
    362 		  /* add char to buffer - first allocate room for it */
    363 		if ((menu->pattern = (char *)
    364 		     realloc(menu->pattern,
    365 			     menu->plen + sizeof(char) +
    366 			     ((menu->plen > 0)? 0 : 1)))
    367 		    == NULL)
    368 			return E_SYSTEM_ERROR;
    369 		menu->pattern[menu->plen] = c;
    370 		menu->pattern[++menu->plen] = '\0';
    371 
    372 		  /* there is no chance of a match if pattern is longer
    373 		     than all the items */
    374 		if (menu->plen >= menu->max_item_width) {
    375 			menu->pattern[--menu->plen] = '\0';
    376 			return E_NO_MATCH;
    377 		}
    378 
    379 		if (__menui_match_items(menu, direction,
    380 					item_matched) == E_NO_MATCH) {
    381 			menu->pattern[--menu->plen] = '\0';
    382 			return E_NO_MATCH;
    383 		} else
    384 			return E_OK;
    385 	} else {
    386 		if (__menui_match_items(menu, direction,
    387 					item_matched) == E_OK) {
    388 			return E_OK;
    389 		} else {
    390 			return E_NO_MATCH;
    391 		}
    392 	}
    393 }
    394 
    395 /*
    396  * Draw an item in the subwindow complete with appropriate highlighting.
    397  */
    398 void
    399 __menui_draw_item(menu, item)
    400 	MENU *menu;
    401 	int item;
    402 {
    403 	int j, pad_len, mark_len;
    404 
    405 	mark_len = max(menu->mark.length, menu->unmark.length);
    406 
    407 	wmove(menu->menu_subwin,
    408 	      menu->items[item]->row - menu->top_row,
    409 	      menu->items[item]->col * (menu->col_width + 1));
    410 
    411 	if ((menu->cur_item == item) || (menu->items[item]->selected == 1))
    412 		wattron(menu->menu_subwin, menu->fore);
    413 	if ((menu->items[item]->opts & O_SELECTABLE) != O_SELECTABLE)
    414 		wattron(menu->menu_subwin, menu->grey);
    415 
    416 	  /* deal with the menu mark, if  one is set.
    417 	   * We mark the selected items and write blanks for
    418 	   * all others unless the menu unmark string is set in which
    419 	   * case the unmark string is written.
    420 	   */
    421 	if (menu->items[item]->selected == 1) {
    422 		if (menu->mark.string != NULL) {
    423 			for (j = 0; j < menu->mark.length; j++) {
    424 				waddch(menu->menu_subwin,
    425 				       menu->mark.string[j]);
    426 			}
    427 		}
    428 		  /* blank any length difference between mark & unmark */
    429 		for (j = menu->mark.length; j < mark_len; j++)
    430 			waddch(menu->menu_subwin, ' ');
    431 	} else {
    432 		if (menu->unmark.string != NULL) {
    433 			for (j = 0; j < menu->unmark.length; j++) {
    434 				waddch(menu->menu_subwin,
    435 				       menu->unmark.string[j]);
    436 			}
    437 		}
    438 		  /* blank any length difference between mark & unmark */
    439 		for (j = menu->unmark.length; j < mark_len; j++)
    440 			waddch(menu->menu_subwin, ' ');
    441 	}
    442 
    443 	  /* add the menu name */
    444 	for (j=0; j < menu->items[item]->name.length; j++)
    445 		waddch(menu->menu_subwin,
    446 		       menu->items[item]->name.string[j]);
    447 
    448 	pad_len = menu->col_width - menu->items[item]->name.length
    449 		- mark_len - 1;
    450 	if ((menu->opts & O_SHOWDESC) == O_SHOWDESC) {
    451 		pad_len -= menu->items[item]->description.length - 1;
    452 		for (j = 0; j < pad_len; j++)
    453 			waddch(menu->menu_subwin, menu->pad);
    454 		for (j = 0; j < menu->items[item]->description.length; j++) {
    455 			waddch(menu->menu_subwin,
    456 			       menu->items[item]->description.string[j]);
    457 		}
    458 	} else {
    459 		for (j = 0; j < pad_len; j++)
    460 			waddch(menu->menu_subwin, ' ');
    461 	}
    462 	menu->items[item]->visible = 1;
    463 	  /* kill any special attributes... */
    464 	wattrset(menu->menu_subwin, menu->back);
    465 
    466 	  /* and position the cursor nicely */
    467 	pos_menu_cursor(menu);
    468 }
    469 
    470 /*
    471  * Draw the menu in the subwindow provided.
    472  */
    473 int
    474 __menui_draw_menu(menu)
    475 	MENU *menu;
    476 {
    477 	int rowmajor, i, j, max_items, last_item, row = -1, col = -1;
    478 
    479 	rowmajor = ((menu->opts & O_ROWMAJOR) == O_ROWMAJOR);
    480 
    481 	for (i = 0;  i < menu->item_count; i++) {
    482 		if (menu->items[i]->row == menu->top_row)
    483 			break;
    484 		menu->items[i]->visible = 0;
    485 	}
    486 
    487 	wmove(menu->menu_subwin, 0, 0);
    488 
    489 	menu->col_width = getmaxx(menu->menu_subwin) / menu->cols;
    490 
    491 	  /*if ((menu->opts & O_SHOWDESC) == O_SHOWDESC)
    492 	    menu->col_width++;*/
    493 
    494 	max_items = menu->rows * menu->cols;
    495 	last_item = ((max_items + i) > menu->item_count) ? menu->item_count :
    496 		(max_items + i);
    497 
    498 	for (; i < last_item; i++) {
    499 		if (i > menu->item_count) {
    500 			  /* no more items to draw, write background blanks */
    501 			wattron(menu->menu_subwin, menu->back);
    502 			if (row < 0) {
    503 				row = menu->items[menu->item_count - 1]->row;
    504 				col = menu->items[menu->item_count - 1]->col;
    505 			}
    506 
    507 			if (rowmajor) {
    508 				col++;
    509 				if (col > menu->cols) {
    510 					col = 0;
    511 					row++;
    512 				}
    513 			} else {
    514 				row++;
    515 				if (row > menu->rows) {
    516 					row = 0;
    517 					col++;
    518 				}
    519 			}
    520 			wmove(menu->menu_subwin, row,
    521 			      col * (menu->col_width + 1));
    522 			for (j = 0; j < menu->col_width; j++)
    523 				waddch(menu->menu_subwin, ' ');
    524 		} else {
    525 			__menui_draw_item(menu, i);
    526 
    527 		}
    528 
    529 	}
    530 
    531 	if (last_item < menu->item_count) {
    532 		for (j = last_item; j < menu->item_count; j++)
    533 			menu->items[j]->visible = 0;
    534 	}
    535 
    536 	return E_OK;
    537 }
    538 
    539 
    540 /*
    541  * Calculate the widest menu item and stash it in the menu struct.
    542  *
    543  */
    544 void
    545 __menui_max_item_size(menu)
    546 	MENU *menu;
    547 {
    548 	int i, with_desc, width;
    549 
    550 	with_desc = ((menu->opts & O_SHOWDESC) == O_SHOWDESC);
    551 
    552 	for (i = 0; i < menu->item_count; i++) {
    553 		width = menu->items[i]->name.length
    554 			+ max(menu->mark.length, menu->unmark.length);
    555 		if (with_desc)
    556 			width += menu->items[i]->description.length + 1;
    557 
    558 		menu->max_item_width = max(menu->max_item_width, width);
    559 	}
    560 }
    561 
    562 
    563 /*
    564  * Redraw the menu on the screen.  If the current item has changed then
    565  * unhighlight the old item and highlight the new one.
    566  */
    567 static void
    568 __menui_redraw_menu(menu, old_top_row, old_cur_item)
    569 	MENU *menu;
    570 	int old_top_row;
    571 	int old_cur_item;
    572 {
    573 
    574 	if (menu->top_row != old_top_row) {
    575 		  /* top row changed - redo the whole menu
    576 		   * XXXX this could be improved if we had wscrl implemented.
    577 
    578 		   * XXXX we could scroll the window and just fill in the
    579 		   * XXXX changed lines.
    580 		   */
    581 		wclear(menu->menu_subwin);
    582 		__menui_draw_menu(menu);
    583 	} else {
    584 		if (menu->cur_item != old_cur_item) {
    585 			  /* redo the old item as a normal one. */
    586 			__menui_draw_item(menu, old_cur_item);
    587 		}
    588 		  /* and then redraw the current item */
    589 		__menui_draw_item(menu, menu->cur_item);
    590 	}
    591 }
    592