Esempio n. 1
0
int curses_read_char()
{
    int ch, tmpch;

    ch = getch();
    tmpch = ch;
    ch = curses_convert_keys(ch);

    if (ch == 0)
    {
        ch = '\033'; /* map NUL to ESC since nethack doesn't expect NUL */
    }

#if defined(ALT_0) && defined(ALT_9)    /* PDCurses, maybe others */
    if ((ch >= ALT_0) && (ch <= ALT_9))
    {
        tmpch = (ch - ALT_0) + '0';
        ch = M(tmpch);
    }
#endif

#if defined(ALT_A) && defined(ALT_Z)    /* PDCurses, maybe others */
    if ((ch >= ALT_A) && (ch <= ALT_Z))
    {
        tmpch = (ch - ALT_A) + 'a';
        ch = M(tmpch);
    }
#endif

#ifdef KEY_RESIZE
    /* Handle resize events via get_nh_event, not this code */
    if (ch == KEY_RESIZE)
    {
        ch = '\033'; /* NetHack doesn't know what to do with KEY_RESIZE */
    }
#endif

    if (counting && !isdigit(ch)) /* Dismiss count window if necissary */
    {
        curses_count_window(NULL);
        curses_refresh_nethack_windows();
    }

    return ch;
}
Esempio n. 2
0
int curses_character_input_dialog(const char *prompt, const char *choices, char def)
{
    WINDOW *askwin = NULL;
    int answer, count, maxwidth, map_height, map_width;
    char *linestr;
    char askstr[BUFSZ + QBUFSZ];
    char choicestr[QBUFSZ];
    int prompt_width = strlen(prompt);
    int prompt_height = 1;
    boolean any_choice = FALSE;
    boolean accept_count = FALSE;

    if (invent || (moves > 1))
    {
        curses_get_window_size(MAP_WIN, &map_height, &map_width);
    }
    else
    {
        map_height = term_rows;
        map_width = term_cols;
    }
    
    maxwidth = map_width - 2;
    
    if (choices != NULL)
    {
        for (count = 0; choices[count] != '\0'; count++)
        {
            if (choices[count] == '#')   /* Accept a count */
            {
                accept_count = TRUE;
            }
        }
        choicestr[0] = ' ';
        choicestr[1] = '[';
        for (count = 0; choices[count] != '\0'; count++)
        {
            if (choices[count] == '\033')   /* Escape */
            {
                break;
            }
            choicestr[count + 2] = choices[count];
        }
        choicestr[count + 2] = ']';
        if (((def >= 'A') && (def <= 'Z')) || ((def >= 'a') && (def <= 'z')))
        {
            choicestr[count + 3] = ' ';
            choicestr[count + 4] = '(';
            choicestr[count + 5] = def;
            choicestr[count + 6] = ')';
            choicestr[count + 7] = '\0';
        }
        else    /* No usable default choice */
        {
            choicestr[count + 3] = '\0';
            def = '\0'; /* Mark as no default */
        }
        strcpy(askstr, prompt);
        strcat(askstr, choicestr);
    }
    else
    {
        strcpy(askstr, prompt);
        any_choice = TRUE;
    }
    
    prompt_width = strlen(askstr);
    
    if ((prompt_width + 2) > maxwidth)
    {
        prompt_height = curses_num_lines(askstr, maxwidth);
        prompt_width = map_width - 2;
    }

    if (iflags.wc_popup_dialog)
    {
        askwin = curses_create_window(prompt_width, prompt_height, UP);
        for (count = 0; count < prompt_height; count++)
        {
            linestr = curses_break_str(askstr, maxwidth, count + 1);
            mvwaddstr(askwin, count + 1, 1, linestr);
            free(linestr);
        }
    
        wrefresh(askwin);
    }
    else
    {
        linestr = curses_copy_of(askstr);
        pline("%s", linestr);
        free(linestr);
        curs_set(1);
    }

    while (1)
    {
        answer = getch();
        answer = curses_convert_keys(answer);

        if (answer==KEY_ESC)
        {
            if (choices == NULL)
            {
                break;
            }
            answer = def;
            for (count = 0; choices[count] != '\0'; count++)
            {
                if (choices[count] == 'q') /* q is preferred over n */
                {
                    answer = 'q';
                }
                else if ((choices[count] == 'n') && answer != 'q')
                {
                    answer = 'n';
                }
            }
            break;
        }
        else if ((answer == '\n') || (answer == '\r') ||
         (answer == ' '))
        {
            if ((choices != NULL) && (def != '\0'))
            {
                answer = def;
            }
            break;
        }
        
        if (digit(answer))
        {
            if (accept_count)
            {
                if (answer != '0')
                {
                    yn_number = curses_get_count(answer - '0');
                    touchwin(askwin);
                    refresh();
                }
                
                answer = '#';
                break;
            }
        }
        
        if (any_choice)
        {
            break;
        }
        
        if (choices != NULL)
        {
            for (count = 0; count < strlen(choices); count++)
            {
                if (choices[count] == answer)
                {
                    break;
                }
            }
            if (choices[count] == answer)
            {
                break;
            }
        }
    }

    if (iflags.wc_popup_dialog)
    {
        /* Kludge to make prompt visible after window is dismissed
        when inputting a number */
        if (digit(answer))
        {
            linestr = curses_copy_of(askstr);
            pline("%s", linestr);
            free(linestr);
            curs_set(1);
        }

        curses_destroy_win(askwin);
    }
    else
    {
        curses_clear_unhighlight_message_window();
        curs_set(0);
    }

    return answer;
}
Esempio n. 3
0
static int menu_get_selections(WINDOW *win, nhmenu *menu, int how)
{
    int curletter;
    int count = -1;
    int count_letter = '\0';
    int curpage = 1;
    int num_selected = 0;
    boolean dismiss = FALSE;
    char search_key[BUFSZ];
    nhmenu_item *menu_item_ptr = menu->entries;

    menu_display_page(menu, win, 1);

    while (!dismiss)
    {
        curletter = getch();
        
        if (curletter == '\033')
        {
            curletter = curses_convert_keys(curletter);
        }
        
        if (isdigit(curletter) && (how != PICK_NONE))
        {
            count = curses_get_count(curletter - '0');
            touchwin(win);
            refresh();
            curletter = getch();
            
            if (count > 0)
            {
                count_letter = curletter;
            }
        }
        

        if ((how == PICK_NONE) && (menu->num_pages == 1))
        {
            if (how==PICK_NONE)
            {    
                if (curletter == KEY_ESC)
                {
                    num_selected = -1;
                }
                else
                {
                    num_selected = 0;

                }
                dismiss = TRUE;
                break;
            }                
        }
        
        switch (curletter)
        {
            case KEY_ESC:
            {
                num_selected = -1;
                dismiss = TRUE;
                break;
            }
            case '\n':
            case '\r':
            {
                dismiss = TRUE;
                break;
            }
            case KEY_RIGHT:
            case KEY_NPAGE:
            case MENU_NEXT_PAGE:
            case ' ':
            {
                if (curpage < menu->num_pages)
                {
                    curpage++;
                    menu_display_page(menu, win, curpage);
                }
                else if (curletter == ' ')
                {
                    dismiss = TRUE;
                    break;
                }
                break;
            }
            case KEY_LEFT:
            case KEY_PPAGE:
            case MENU_PREVIOUS_PAGE:
            {
                if (curpage > 1)
                {
                    curpage--;
                    menu_display_page(menu, win, curpage);
                }
                break;
            }
            case KEY_END:
            case MENU_LAST_PAGE:
            {
                if (curpage != menu->num_pages)
                {
                    curpage = menu->num_pages;
                    menu_display_page(menu, win, curpage);
                }
                break;
            }
            case KEY_HOME:
            case MENU_FIRST_PAGE:
            {
                if (curpage != 1)
                {
                    curpage = 1;
                    menu_display_page(menu, win, curpage);
                }
                break;
            }
            case MENU_SEARCH:
            {
                curses_line_input_dialog("Search for:", search_key,
                 BUFSZ);
        		
                refresh();
                touchwin(win);
                wrefresh(win);
                
        		if (strlen(search_key) == 0)
        		{
        		    break;
        		}
        		
                menu_item_ptr = menu->entries;

                while (menu_item_ptr != NULL)
                {
                    if ((menu_item_ptr->identifier.a_void != NULL) &&
                     (strstri(menu_item_ptr->str, search_key)))
                    {
                        if (how == PICK_ONE)
                        {
                            menu_clear_selections(menu);
                            menu_select_deselect(win, menu_item_ptr,
                             SELECT);
                            num_selected = 1;
                            dismiss = TRUE;
                            break;
                        }
                        else
                        {
                            menu_select_deselect(win, menu_item_ptr,
                             INVERT);
                        }
                    }

                    menu_item_ptr = menu_item_ptr->next_item;
                }

                menu_item_ptr = menu->entries;
                break;
            }
            default:
            {
                if (how==PICK_NONE)
                {    
                    num_selected = 0;
                    dismiss = TRUE;
                    break;
                }                
            }
        }
        if (how == PICK_ANY)
        {
            switch (curletter)
                {
                case MENU_SELECT_PAGE:
                {
                    (void) menu_operation(win, menu, SELECT, curpage);
                    break;
                }
                case MENU_SELECT_ALL:
                {
                    curpage = menu_operation(win, menu, SELECT, 0);
                    break;
                }
                case MENU_UNSELECT_PAGE:
                {
                    (void) menu_operation(win, menu, DESELECT, curpage);
                    break;
                }
                case MENU_UNSELECT_ALL:
                {
                    curpage = menu_operation(win, menu, DESELECT, 0);
                    break;
                }
                case MENU_INVERT_PAGE:
                {
                    (void) menu_operation(win, menu, INVERT, curpage);
                    break;
                }
                case MENU_INVERT_ALL:
                {
                    curpage = menu_operation(win, menu, INVERT, 0);
                    break;
                }
            }
        }
        
        menu_item_ptr = menu->entries;
        
        while (menu_item_ptr != NULL)
        {
            if (menu_item_ptr->identifier.a_void != NULL)
            {
                if (((curletter == menu_item_ptr->accelerator) &&
                 ((curpage == menu_item_ptr->page_num) ||
                 (!menu->reuse_accels))) || ((menu_item_ptr->group_accel)
                 && (curletter == menu_item_ptr->group_accel)))
                {
                    if (curpage != menu_item_ptr->page_num)
                    {
                        curpage = menu_item_ptr->page_num;
                        menu_display_page(menu, win, curpage);
                    }

                    if (how == PICK_ONE)
                    {
                        menu_clear_selections(menu);
                        menu_select_deselect(win, menu_item_ptr, SELECT);
                        num_selected = 1;
                        dismiss = TRUE;
                        break;
                    }
                    else
                    {
                        menu_select_deselect(win, menu_item_ptr, INVERT);
                    }
                }
            }
            menu_item_ptr = menu_item_ptr->next_item;
        }
    }
    
    if ((how == PICK_ANY) && (num_selected != -1))
    {
        num_selected = 0;
        menu_item_ptr = menu->entries;
        
        while (menu_item_ptr != NULL)
        {
            if (menu_item_ptr->identifier.a_void != NULL)
            {
                if (menu_item_ptr->selected)
                {
                    num_selected++;
                    
                    if (menu_item_ptr->accelerator == count_letter)
                    {
                        menu_item_ptr->count = count;
                        count = 0;
                        count_letter = '\0';
                    }
                }
            }
            menu_item_ptr = menu_item_ptr->next_item;
        }
    }
    
    return num_selected;
}