]> granicus.if.org Git - nethack/commitdiff
Curses: Select menu items via mouse
authorPasi Kallinen <paxed@alt.org>
Thu, 21 Jan 2021 10:53:02 +0000 (12:53 +0200)
committerPasi Kallinen <paxed@alt.org>
Thu, 21 Jan 2021 10:53:02 +0000 (12:53 +0200)
doc/fixes37.0
win/curses/cursdial.c

index 1116e31edd8dd2acc13d55f5ab3e8c1cb8d53ac8..5ecc66eb8546eacb2ec98ffd8b8f5f9248628038 100644 (file)
@@ -763,6 +763,7 @@ user_sounds: provide an experimental mechanism for terminal-side sounds similar
        terminal-side code external to NetHack to recognize the sequence and
        act on it)
 curses: implement 'selectsaved', restore via menu of saved games
+curses: implement selecting menu items via mouse
 Qt: the "paper doll" inventory subset can be controlled via the "Qt Settings"
        dialog box ("Preferences..." on OSX)
 Qt: draw a border around each tile in the paper doll inventory; when BUC is
index d4c388349572e813033321386813dd213f04e691..5d4d959282fdbf787282d6c331e7b90ce41f57b1 100644 (file)
@@ -1107,6 +1107,62 @@ menu_win_size(nhmenu *menu)
     menu->height = max(maxheight, 5);
 }
 
+#ifdef NCURSES_MOUSE_VERSION
+nhmenu_item *
+get_menuitem_y(nhmenu *menu, WINDOW * win, int page_num, int liney)
+{
+    nhmenu_item *menu_item_ptr;
+    int count, num_lines, entry_cols;
+    char *tmpstr;
+
+    menu_item_ptr = menu->entries;
+
+    while (menu_item_ptr != NULL) {
+        if (menu_item_ptr->page_num == page_num) {
+            break;
+        }
+        menu_item_ptr = menu_item_ptr->next_item;
+    }
+
+    if (menu_item_ptr == NULL) {        /* Page not found */
+        impossible("get_menuitem_y: attempt to display nonexistent page");
+        return NULL;
+    }
+
+    if (menu->prompt && *menu->prompt) {
+        num_lines = curses_num_lines(menu->prompt, menu->width);
+
+        for (count = 0; count < num_lines; count++) {
+            tmpstr = curses_break_str(menu->prompt, menu->width, count + 1);
+            free(tmpstr);
+        }
+    }
+
+    while (menu_item_ptr != NULL) {
+        if (menu_item_ptr->page_num != page_num) {
+            break;
+        }
+        if (menu_item_ptr->identifier.a_void != NULL) {
+            if (menu_item_ptr->line_num + 1 == liney)
+                return menu_item_ptr;
+        }
+
+        num_lines = curses_num_lines(menu_item_ptr->str, entry_cols);
+        for (count = 0; count < num_lines; count++) {
+            if (menu_item_ptr->str && *menu_item_ptr->str) {
+                tmpstr = curses_break_str(menu_item_ptr->str,
+                                          entry_cols, count + 1);
+                free(tmpstr);
+            }
+        }
+
+        menu_item_ptr = menu_item_ptr->next_item;
+    }
+
+    return NULL;
+
+}
+#endif /*NCURSES_MOUSE_VERSION*/
 
 /* Displays menu selections in the given window */
 
@@ -1356,6 +1412,33 @@ menu_get_selections(WINDOW * win, nhmenu *menu, int how)
             case '\r':
                 dismiss = TRUE;
                 break;
+#ifdef NCURSES_MOUSE_VERSION
+            case KEY_MOUSE: {
+                MEVENT mev;
+
+                if (getmouse(&mev) == OK && how != PICK_NONE) {
+                    if (wmouse_trafo(win, &mev.y, &mev.x, FALSE)) {
+                        int y = mev.y;
+
+                        menu_item_ptr = get_menuitem_y(menu, win, curpage, y);
+
+                        if (menu_item_ptr) {
+                            if (how == PICK_ONE) {
+                                menu_clear_selections(menu);
+                                menu_select_deselect(win, menu_item_ptr,
+                                                     SELECT, curpage);
+                                num_selected = 1;
+                                dismiss = TRUE;
+                            } else {
+                                menu_select_deselect(win, menu_item_ptr,
+                                                     INVERT, curpage);
+                            }
+                        }
+                    }
+                }
+            }
+                break;
+#endif /*NCURSES_MOUSE_VERSION*/
             case KEY_RIGHT:
             case KEY_NPAGE:
             case MENU_NEXT_PAGE: