src/menu.c
author ali@juiblex.co.uk
Mon Jan 29 20:22:01 2007 +0000 (2007-01-29)
changeset 0 acc07f1f4b1e
permissions -rw-r--r--
Initial check-in. Mostly sort of works. Ish.
     1 /*
     2  * $Id: menu.c,v 1.5 2004/12/30 09:07:47 ali Exp $
     3  * Copyright (C) 2007 J. Ali Harlow
     4  * Min Hack may be freely redistributed.  See license for details. 
     5  */
     6 
     7 #include <stdlib.h>
     8 #include <stdio.h>
     9 #include <string.h>
    10 #include "minhack.h"
    11 
    12 void minhack_free_menu(int window)
    13 {
    14     struct minhack_menu *menu=windows[window].menu;
    15     struct minhack_mitem *item,*next;
    16     if (menu)
    17     {
    18 	if (menu->menu)
    19 	{
    20 #if 0
    21 	    delwin(menu_win(menu->menu));
    22 	    delwin(menu_sub(menu->menu));
    23 #endif
    24 	    free_menu(menu->menu);
    25 	}
    26 	for(item=menu->mitems;item;item=next)
    27 	{
    28 	    next=item->next;
    29 	    free_item(item->item);
    30 	    free(item->str);
    31 	    free(item);
    32 	}
    33 	free(menu->items);
    34     }
    35     windows[window].menu=NULL;
    36 }
    37 
    38 void minhack_start_menu(int window)
    39 {
    40     minhack_free_menu(window);
    41     windows[window].menu=
    42       (struct minhack_menu *)calloc(1,sizeof(struct minhack_menu));
    43 }
    44 
    45 void minhack_add_menu(int window, int glyph, int identifier, char accelerator,
    46   char groupacc, int attr, const char *str, nhproxy_bool_t preselected)
    47 {
    48     struct minhack_mitem *item;
    49     item=(struct minhack_mitem *)malloc(sizeof(*item));
    50     item->next=windows[window].menu->mitems;
    51     item->identifier=identifier;
    52     if (accelerator)
    53 	item->accelerator=accelerator;
    54     else if (!item->next)
    55 	item->accelerator='a';
    56     else if (!identifier)
    57 	item->accelerator=item->next->accelerator;
    58     else if (item->next->accelerator=='z')
    59 	item->accelerator='A';
    60     else
    61 	item->accelerator=item->next->accelerator+1;
    62     item->groupacc=groupacc;
    63     if (identifier)
    64     {
    65 	item->str=malloc(strlen(str)+5);
    66 	sprintf(item->str,"%c - %s",item->accelerator,str);
    67     }
    68     else if (*str)
    69 	item->str=strdup(str);
    70     else
    71 	item->str=strdup(" ");
    72     item->selected=preselected;
    73     item->item=new_item(item->str,item->str);
    74     if (!identifier)
    75 	item_opts_off(item->item,O_SELECTABLE);
    76     windows[window].menu->mitems=item;
    77 }
    78 
    79 static struct minhack_mitem *reverse(struct minhack_mitem *curr)
    80 {
    81     struct minhack_mitem *next,*head=0;
    82     while (curr)
    83     {
    84 	next=curr->next;
    85 	curr->next=head;
    86 	head=curr;
    87 	curr=next;
    88     }
    89     return head;
    90 }
    91 
    92 void minhack_end_menu(int window, const char *prompt)
    93 {
    94     int i,rows,cols;
    95     struct minhack_mitem *item;
    96     WINDOW *win;
    97     windows[window].menu->mitems=reverse(windows[window].menu->mitems);
    98     if (*prompt)
    99     {
   100 	minhack_add_menu(window,-1,0,0,0,0,"",FALSE);
   101 	minhack_add_menu(window,-1,0,0,0,0,prompt,FALSE);
   102     }
   103     for(i=0,item=windows[window].menu->mitems;item;i++,item=item->next)
   104 	;
   105     windows[window].menu->items=malloc((i+1)*sizeof(ITEM *));
   106     for(i=0,item=windows[window].menu->mitems;item;i++,item=item->next)
   107 	windows[window].menu->items[i]=item->item;
   108     windows[window].menu->items[i]=NULL;
   109     windows[window].menu->menu=new_menu(windows[window].menu->items);
   110     scale_menu(windows[window].menu->menu,&rows,&cols);
   111     {
   112 	char buf[100];
   113 	sprintf(buf,"(%dx%d)",rows,cols);
   114 	minhack_putstr(message_win,0,buf);
   115     }
   116 #if 0
   117     win=newwin(rows,cols,0,0);
   118     set_menu_win(windows[window].menu->menu,win);
   119 #endif
   120     win=newwin(rows,cols,0,0);
   121     set_menu_sub(windows[window].menu->menu,win);
   122     set_menu_win(windows[window].menu->menu,windows[map_win].win);
   123 }
   124 
   125 int minhack_select_menu(int window, int how, struct nhproxy_mi **selected)
   126 {
   127     int i,j,ch;
   128     struct minhack_mitem *item;
   129     if (how==MINHACK_PICK_ANY)
   130 	menu_opts_off(windows[window].menu->menu,O_ONEVALUE);
   131     menu_opts_off(windows[window].menu->menu,O_IGNORECASE);
   132     menu_opts_off(windows[window].menu->menu,O_SHOWDESC);
   133     i=post_menu(windows[window].menu->menu);
   134     for(;;)
   135     {
   136 	wrefresh(menu_win(windows[window].menu->menu));
   137 #if 1
   138 	wrefresh(menu_sub(windows[window].menu->menu));
   139 #endif
   140 	ch=getch();
   141 	if (ch=='\033')
   142 	{
   143 	    i=how==MINHACK_PICK_NONE?0:-1;
   144 	    break;
   145 	}
   146 	else if (ch>=' ' && ch<='~')
   147 	    if (menu_driver(windows[window].menu->menu,ch)==E_OK)
   148 	    {
   149 		for(i=0,item=windows[window].menu->mitems;item;item=item->next)
   150 		    if (item_value(item->item))
   151 			i++;
   152 		*selected=(struct nhproxy_mi *)calloc(i,
   153 		  sizeof(struct nhproxy_mi));
   154 		for(i=0,item=windows[window].menu->mitems;item;item=item->next)
   155 		    if (item_value(item->item))
   156 		    {
   157 			(*selected)[i].item=item->identifier;
   158 			(*selected)[i].count=-1;	/* FIXME */
   159 		    }
   160 		break;
   161 	    }
   162     }
   163     unpost_menu(windows[window].menu->menu);
   164     redrawwin(menu_win(windows[window].menu->menu));
   165 #if 0
   166     for(j=0;j<no_windows;j++)
   167 	if (windows[j].win)
   168 	    redrawwin(windows[j].win);
   169 #endif
   170     return i;
   171 }
   172 
   173 int minhack_message_menu(int let, int how, const char *mesg)
   174 {
   175     return -1;
   176 }