Rocco Rutte:
[apps/madmutt.git] / menu.c
diff --git a/menu.c b/menu.c
index 5e46eed..a097c48 100644 (file)
--- a/menu.c
+++ b/menu.c
  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  */
 
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include "mutt.h"
 #include "mutt_curses.h"
 #include "mutt_menu.h"
@@ -35,21 +39,18 @@ extern size_t UngetCount;
 
 static void print_enriched_string (int attr, unsigned char *s, int do_color)
 {
+  wchar_t wc;
   size_t k;
   size_t n = mutt_strlen ((char *)s);
-  short f1, f2, b1, b2;
+  mbstate_t mbstate;
 
+  memset (&mbstate, 0, sizeof (mbstate));
   while (*s)
   {
     if (*s < M_TREE_MAX)
     {
       if (do_color)
-      {
-        pair_content(PAIR_NUMBER(ColorDefs[MT_COLOR_TREE]), &f1, &b1);
-        pair_content(PAIR_NUMBER(attr), &f2, &b2);
-        if (b1 == b2)
-          SETCOLOR (MT_COLOR_TREE);
-      }
+        SETCOLOR (MT_COLOR_TREE);
       while (*s && *s < M_TREE_MAX)
       {
         switch (*s)
@@ -133,10 +134,10 @@ static void print_enriched_string (int attr, unsigned char *s, int do_color)
       }
       if (do_color) attrset(attr);
     }
-    else if (*s > 0)
+    else if ((k = mbrtowc (&wc, (char *)s, n, &mbstate)) > 0)
     {
-      addnstr ((char *)s, 1);
-      s += 1, n-= 1;
+      addnstr ((char *)s, k);
+      s += k, n-= k;
     }
     else
       break;
@@ -158,9 +159,12 @@ void menu_pad_string (char *s, size_t n)
 {
   int shift = option (OPTARROWCURSOR) ? 3 : 0;
   int cols = COLS - shift - SidebarWidth;
+  char* tmpbuf = safe_malloc (n);
 
-  mutt_format_string (s, n, cols, cols, 0, ' ', s, strlen (s), 1);
-  s[n - 1] = 0;
+  mutt_format_string (tmpbuf, n, cols, cols, 0, ' ', s, strlen (s), 1);
+  tmpbuf[n - 1] = 0;
+  snprintf(s,n,"%s",tmpbuf); /* overkill */
+  FREE(&tmpbuf);
 }
 
 void menu_redraw_full (MUTTMENU *menu)
@@ -372,32 +376,37 @@ void menu_redraw_prompt (MUTTMENU *menu)
 
 void menu_check_recenter (MUTTMENU *menu)
 {
-  if (menu->max <= menu->pagelen && menu->top != 0)
+  int c = MIN (MenuContext, menu->pagelen / 2);
+  int old_top = menu->top;
+
+  if (!option (OPTMENUMOVEOFF) && menu->max <= menu->pagelen) /* less entries than lines */
   {
-    menu->top = 0;
-    set_option (OPTNEEDREDRAW);
-    menu->redraw |= REDRAW_INDEX;
+    if (menu->top != 0) {
+      menu->top = 0;
+      set_option (OPTNEEDREDRAW);
+    }
   }
-  else if (menu->current >= menu->top + menu->pagelen)
+  else if (menu->current >= menu->top + menu->pagelen - c) /* indicator below bottom threshold */
   {
     if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
-      menu->top = menu->current - menu->pagelen + 1;
+      menu->top = menu->current - menu->pagelen + c + 1;
     else
-      menu->top += menu->pagelen * ((menu->current - menu->top) / menu->pagelen);
-    menu->redraw |= REDRAW_INDEX;
+      menu->top += (menu->pagelen - c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
   }
-  else if (menu->current < menu->top)
+  else if (menu->current < menu->top + c) /* indicator above top threshold */
   {
     if (option (OPTMENUSCROLL) || (menu->pagelen <= 0))
-      menu->top = menu->current;
+      menu->top = menu->current - c;
     else
-    {
-      menu->top -= menu->pagelen * ((menu->top + menu->pagelen - 1 - menu->current) / menu->pagelen);
-      if (menu->top < 0)
-        menu->top = 0;
-    }
-    menu->redraw |= REDRAW_INDEX;
+      menu->top -= (menu->pagelen - c) * ((menu->top + menu->pagelen - 1 - menu->current) / (menu->pagelen - c)) - c;
   }
+
+  if (!option (OPTMENUMOVEOFF)) /* make entries stick to bottom */
+    menu->top = MIN (menu->top, menu->max - menu->pagelen);
+  menu->top = MAX (menu->top, 0);
+
+  if (menu->top != old_top)
+    menu->redraw |= REDRAW_INDEX;
 }
 
 void menu_jump (MUTTMENU *menu)
@@ -429,10 +438,13 @@ void menu_next_line (MUTTMENU *menu)
 {
   if (menu->max)
   {
-    if (menu->top + 1 < menu->max)
+    int c = MIN (MenuContext, menu->pagelen / 2);
+
+    if (menu->top + 1 < menu->max - c
+      && (option(OPTMENUMOVEOFF) || (menu->max > menu->pagelen && menu->top < menu->max - menu->pagelen)))
     {
       menu->top++;
-      if (menu->current < menu->top)
+      if (menu->current < menu->top + c && menu->current < menu->max - 1)
         menu->current++;
       menu->redraw = REDRAW_INDEX;
     }
@@ -447,8 +459,10 @@ void menu_prev_line (MUTTMENU *menu)
 {
   if (menu->top > 0)
   {
+    int c = MIN (MenuContext, menu->pagelen / 2);
+
     menu->top--;
-    if (menu->current >= menu->top + menu->pagelen)
+    if (menu->current >= menu->top + menu->pagelen - c && menu->current > 1)
       menu->current--;
     menu->redraw = REDRAW_INDEX;
   }
@@ -481,7 +495,9 @@ void menu_next_page (MUTTMENU *menu)
 
 void menu_prev_page (MUTTMENU *menu)
 {
-  if (menu->top > 0)
+  int c = MIN (MenuContext, menu->pagelen / 2);
+
+  if (menu->top > c)
   {
     if ((menu->top -= menu->pagelen) < 0)
       menu->top = 0;
@@ -697,7 +713,7 @@ void mutt_menuDestroy (MUTTMENU **p)
     for (i=0; i < (*p)->max; i++)
       FREE (&(*p)->dialog[i]);
 
-    FREE ((*p)->dialog);
+    FREE ((*p)->dialog);
   }
 
   FREE (p);