Popup menu: redraw as needed master
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 22 Feb 2009 13:56:39 +0000 (14:56 +0100)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 22 Feb 2009 18:33:18 +0000 (19:33 +0100)
Redraw the popup menu after a resize or an update during background
loading.

Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
tig.c

diff --git a/tig.c b/tig.c
index 9b9c34b..1d7dcce 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -2274,6 +2274,8 @@ resize_display(void)
        }
 }
 
        }
 }
 
+static void menu_redraw(void);
+
 static void
 redraw_display(bool clear)
 {
 static void
 redraw_display(bool clear)
 {
@@ -2286,6 +2288,8 @@ redraw_display(bool clear)
                redraw_view(view);
                update_view_title(view);
        }
                redraw_view(view);
                update_view_title(view);
        }
+
+       menu_redraw();
 }
 
 static void
 }
 
 static void
@@ -2952,6 +2956,10 @@ update_view(struct view *view)
        /* Update the title _after_ the redraw so that if the redraw picks up a
         * commit reference in view->ref it'll be available here. */
        update_view_title(view);
        /* Update the title _after_ the redraw so that if the redraw picks up a
         * commit reference in view->ref it'll be available here. */
        update_view_title(view);
+
+       /* Redraw menu if needed */
+       menu_redraw();
+
        return TRUE;
 }
 
        return TRUE;
 }
 
@@ -6549,6 +6557,9 @@ utf8_length(const char **start, size_t skip, int *width, size_t max_width, int *
 struct popup {
        WINDOW *win;
        int width;
 struct popup {
        WINDOW *win;
        int width;
+       int height;
+       const char *title;
+       const struct menu_item *items;
        bool hotkeys;
        int selected;
 };
        bool hotkeys;
        int selected;
 };
@@ -6556,6 +6567,32 @@ struct popup {
 static struct popup menu;
 
 
 static struct popup menu;
 
 
+static void
+menu_set_line_attr(int index, enum line_type type)
+{
+       WINDOW *win = menu.win;
+       int width = menu.width-2;
+       mvwchgat(win, index+1, 1, width,
+                       get_line_attr(type), type, NULL);
+}
+
+static void
+menu_putline(int index, const struct menu_item *item, bool selected)
+{
+       WINDOW *win = menu.win;
+       bool hotkeys = menu.hotkeys;
+       if (hotkeys) {
+               if (item->hotkey)
+                       mvwprintw(win, index+1, 2, "[%c] %s", (char) item->hotkey, item->text);
+               else
+                       mvwprintw(win, index+1, 6, "%s", item->text);
+       } else {
+               mvwprintw(win, index+1, 2, "%s", item->text);
+       }
+       if (selected)
+               menu_set_line_attr(index, LINE_CURSOR);
+}
+
 static void
 menu_popup(void)
 {
 static void
 menu_popup(void)
 {
@@ -6571,55 +6608,60 @@ menu_popdown(void)
 }
 
 static void
 }
 
 static void
-menu_create(int height, int width, const char* title, bool hotkeys)
+menu_redraw(void)
 {
 {
-       WINDOW *win;
-       int ymax, xmax, top, left;
-       enum line_type type = LINE_TITLE_FOCUS;
+       WINDOW *win = menu.win;
+       if (win)
+       {
+               int width = menu.width;
+               int height = menu.height;
+               const char *title = menu.title;
+               const struct menu_item *items = menu.items;
 
 
-       getmaxyx(stdscr, ymax, xmax);
-       top = (ymax - height)/2;
-       left = (xmax - width)/2;
-       if (top < 0)
-               top = 0;
-       if (left < 0)
-               left = 0;
+               int ymax, xmax, top, left;
+               enum line_type type = LINE_TITLE_FOCUS;
+               int i;
 
 
-       win = newwin(height, width, top, left);
-       wbkgd(win, COLOR_PAIR(type));
+               getmaxyx(stdscr, ymax, xmax);
+               top = (ymax - height)/2;
+               left = (xmax - width)/2;
+               if (top < 0)
+                       top = 0;
+               if (left < 0)
+                       left = 0;
 
 
-       box(win, 0, 0);
-       wattrset(win, get_line_attr(type));
-       mvwprintw(win, 0, (width-strlen(title))/2, " %s ", title);
+               mvwin(win, top, left);
+               wbkgd(win, COLOR_PAIR(type));
 
 
-       menu.win = win;
-       menu.width = width;
-       menu.hotkeys = hotkeys;
-       menu.selected = -1;
-}
+               box(win, 0, 0);
+               wattrset(win, get_line_attr(type));
+               mvwprintw(win, 0, (width-strlen(title))/2, " %s ", title);
 
 
-static void
-menu_set_line_attr(int index, enum line_type type)
-{
-       WINDOW *win = menu.win;
-       int width = menu.width-2;
-       mvwchgat(win, index+1, 1, width,
-                       get_line_attr(type), type, NULL);
+               for (i=0; items[i].text; ++i)
+                       menu_putline(i, &items[i], i==menu.selected);
+
+               menu_popup();
+       }
 }
 
 static void
 }
 
 static void
-menu_putline(int index, const struct menu_item *item)
+menu_create(int height, int width, const char *title, const struct menu_item *items)
 {
 {
-       WINDOW *win = menu.win;
-       bool hotkeys = menu.hotkeys;
-       if (hotkeys) {
-               if (item->hotkey)
-                       mvwprintw(win, index+1, 2, "[%c] %s", (char) item->hotkey, item->text);
-               else
-                       mvwprintw(win, index+1, 6, "%s", item->text);
-       } else {
-               mvwprintw(win, index+1, 2, "%s", item->text);
-       }
+       int i=0;
+       menu.win = newwin(height, width, 0, 0);
+       menu.width = width;
+       menu.height = height;
+       menu.title = title;
+       menu.items = items;
+       menu.hotkeys = false;
+       for(;items[i].text;++i)
+               if (items[i].hotkey) {
+                       menu.hotkeys=true;
+                       break;
+               }
+       menu.selected = -1;
+
+       menu_redraw();
 }
 
 static void
 }
 
 static void
@@ -6943,11 +6985,7 @@ static bool prompt_menu(const char *prompt, const struct menu_item *items, int *
        /* padding */
        width += hotkeys ? 8 : 4;
 
        /* padding */
        width += hotkeys ? 8 : 4;
 
-       menu_create(size+2, width, prompt, hotkeys);
-       for (i=0; i < size; ++i)
-               menu_putline(i, &items[i]);
-       menu_popup();
-
+       menu_create(size+2, width, prompt, items);
 
        while (status == INPUT_OK) {
                const struct menu_item *item = &items[*selected];
 
        while (status == INPUT_OK) {
                const struct menu_item *item = &items[*selected];