src/menu.c
changeset 0 acc07f1f4b1e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/menu.c	Mon Jan 29 20:22:01 2007 +0000
     1.3 @@ -0,0 +1,176 @@
     1.4 +/*
     1.5 + * $Id: menu.c,v 1.5 2004/12/30 09:07:47 ali Exp $
     1.6 + * Copyright (C) 2007 J. Ali Harlow
     1.7 + * Min Hack may be freely redistributed.  See license for details. 
     1.8 + */
     1.9 +
    1.10 +#include <stdlib.h>
    1.11 +#include <stdio.h>
    1.12 +#include <string.h>
    1.13 +#include "minhack.h"
    1.14 +
    1.15 +void minhack_free_menu(int window)
    1.16 +{
    1.17 +    struct minhack_menu *menu=windows[window].menu;
    1.18 +    struct minhack_mitem *item,*next;
    1.19 +    if (menu)
    1.20 +    {
    1.21 +	if (menu->menu)
    1.22 +	{
    1.23 +#if 0
    1.24 +	    delwin(menu_win(menu->menu));
    1.25 +	    delwin(menu_sub(menu->menu));
    1.26 +#endif
    1.27 +	    free_menu(menu->menu);
    1.28 +	}
    1.29 +	for(item=menu->mitems;item;item=next)
    1.30 +	{
    1.31 +	    next=item->next;
    1.32 +	    free_item(item->item);
    1.33 +	    free(item->str);
    1.34 +	    free(item);
    1.35 +	}
    1.36 +	free(menu->items);
    1.37 +    }
    1.38 +    windows[window].menu=NULL;
    1.39 +}
    1.40 +
    1.41 +void minhack_start_menu(int window)
    1.42 +{
    1.43 +    minhack_free_menu(window);
    1.44 +    windows[window].menu=
    1.45 +      (struct minhack_menu *)calloc(1,sizeof(struct minhack_menu));
    1.46 +}
    1.47 +
    1.48 +void minhack_add_menu(int window, int glyph, int identifier, char accelerator,
    1.49 +  char groupacc, int attr, const char *str, nhproxy_bool_t preselected)
    1.50 +{
    1.51 +    struct minhack_mitem *item;
    1.52 +    item=(struct minhack_mitem *)malloc(sizeof(*item));
    1.53 +    item->next=windows[window].menu->mitems;
    1.54 +    item->identifier=identifier;
    1.55 +    if (accelerator)
    1.56 +	item->accelerator=accelerator;
    1.57 +    else if (!item->next)
    1.58 +	item->accelerator='a';
    1.59 +    else if (!identifier)
    1.60 +	item->accelerator=item->next->accelerator;
    1.61 +    else if (item->next->accelerator=='z')
    1.62 +	item->accelerator='A';
    1.63 +    else
    1.64 +	item->accelerator=item->next->accelerator+1;
    1.65 +    item->groupacc=groupacc;
    1.66 +    if (identifier)
    1.67 +    {
    1.68 +	item->str=malloc(strlen(str)+5);
    1.69 +	sprintf(item->str,"%c - %s",item->accelerator,str);
    1.70 +    }
    1.71 +    else if (*str)
    1.72 +	item->str=strdup(str);
    1.73 +    else
    1.74 +	item->str=strdup(" ");
    1.75 +    item->selected=preselected;
    1.76 +    item->item=new_item(item->str,item->str);
    1.77 +    if (!identifier)
    1.78 +	item_opts_off(item->item,O_SELECTABLE);
    1.79 +    windows[window].menu->mitems=item;
    1.80 +}
    1.81 +
    1.82 +static struct minhack_mitem *reverse(struct minhack_mitem *curr)
    1.83 +{
    1.84 +    struct minhack_mitem *next,*head=0;
    1.85 +    while (curr)
    1.86 +    {
    1.87 +	next=curr->next;
    1.88 +	curr->next=head;
    1.89 +	head=curr;
    1.90 +	curr=next;
    1.91 +    }
    1.92 +    return head;
    1.93 +}
    1.94 +
    1.95 +void minhack_end_menu(int window, const char *prompt)
    1.96 +{
    1.97 +    int i,rows,cols;
    1.98 +    struct minhack_mitem *item;
    1.99 +    WINDOW *win;
   1.100 +    windows[window].menu->mitems=reverse(windows[window].menu->mitems);
   1.101 +    if (*prompt)
   1.102 +    {
   1.103 +	minhack_add_menu(window,-1,0,0,0,0,"",FALSE);
   1.104 +	minhack_add_menu(window,-1,0,0,0,0,prompt,FALSE);
   1.105 +    }
   1.106 +    for(i=0,item=windows[window].menu->mitems;item;i++,item=item->next)
   1.107 +	;
   1.108 +    windows[window].menu->items=malloc((i+1)*sizeof(ITEM *));
   1.109 +    for(i=0,item=windows[window].menu->mitems;item;i++,item=item->next)
   1.110 +	windows[window].menu->items[i]=item->item;
   1.111 +    windows[window].menu->items[i]=NULL;
   1.112 +    windows[window].menu->menu=new_menu(windows[window].menu->items);
   1.113 +    scale_menu(windows[window].menu->menu,&rows,&cols);
   1.114 +    {
   1.115 +	char buf[100];
   1.116 +	sprintf(buf,"(%dx%d)",rows,cols);
   1.117 +	minhack_putstr(message_win,0,buf);
   1.118 +    }
   1.119 +#if 0
   1.120 +    win=newwin(rows,cols,0,0);
   1.121 +    set_menu_win(windows[window].menu->menu,win);
   1.122 +#endif
   1.123 +    win=newwin(rows,cols,0,0);
   1.124 +    set_menu_sub(windows[window].menu->menu,win);
   1.125 +    set_menu_win(windows[window].menu->menu,windows[map_win].win);
   1.126 +}
   1.127 +
   1.128 +int minhack_select_menu(int window, int how, struct nhproxy_mi **selected)
   1.129 +{
   1.130 +    int i,j,ch;
   1.131 +    struct minhack_mitem *item;
   1.132 +    if (how==MINHACK_PICK_ANY)
   1.133 +	menu_opts_off(windows[window].menu->menu,O_ONEVALUE);
   1.134 +    menu_opts_off(windows[window].menu->menu,O_IGNORECASE);
   1.135 +    menu_opts_off(windows[window].menu->menu,O_SHOWDESC);
   1.136 +    i=post_menu(windows[window].menu->menu);
   1.137 +    for(;;)
   1.138 +    {
   1.139 +	wrefresh(menu_win(windows[window].menu->menu));
   1.140 +#if 1
   1.141 +	wrefresh(menu_sub(windows[window].menu->menu));
   1.142 +#endif
   1.143 +	ch=getch();
   1.144 +	if (ch=='\033')
   1.145 +	{
   1.146 +	    i=how==MINHACK_PICK_NONE?0:-1;
   1.147 +	    break;
   1.148 +	}
   1.149 +	else if (ch>=' ' && ch<='~')
   1.150 +	    if (menu_driver(windows[window].menu->menu,ch)==E_OK)
   1.151 +	    {
   1.152 +		for(i=0,item=windows[window].menu->mitems;item;item=item->next)
   1.153 +		    if (item_value(item->item))
   1.154 +			i++;
   1.155 +		*selected=(struct nhproxy_mi *)calloc(i,
   1.156 +		  sizeof(struct nhproxy_mi));
   1.157 +		for(i=0,item=windows[window].menu->mitems;item;item=item->next)
   1.158 +		    if (item_value(item->item))
   1.159 +		    {
   1.160 +			(*selected)[i].item=item->identifier;
   1.161 +			(*selected)[i].count=-1;	/* FIXME */
   1.162 +		    }
   1.163 +		break;
   1.164 +	    }
   1.165 +    }
   1.166 +    unpost_menu(windows[window].menu->menu);
   1.167 +    redrawwin(menu_win(windows[window].menu->menu));
   1.168 +#if 0
   1.169 +    for(j=0;j<no_windows;j++)
   1.170 +	if (windows[j].win)
   1.171 +	    redrawwin(windows[j].win);
   1.172 +#endif
   1.173 +    return i;
   1.174 +}
   1.175 +
   1.176 +int minhack_message_menu(int let, int how, const char *mesg)
   1.177 +{
   1.178 +    return -1;
   1.179 +}