giftcurs-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[giFTcurs-commits] giFTcurs/src ui.c ui.h ui_draw.c ui_main.c


From: Göran Weinholt
Subject: [giFTcurs-commits] giFTcurs/src ui.c ui.h ui_draw.c ui_main.c
Date: Mon, 13 Oct 2003 17:42:39 -0400

CVSROOT:        /cvsroot/giftcurs
Module name:    giFTcurs
Branch:         
Changes by:     Göran Weinholt <address@hidden> 03/10/13 17:42:38

Modified files:
        src            : ui.c ui.h ui_draw.c ui_main.c 

Log message:
        Fixes for input in utf8. ui_input_handler() should be OK now.

Patches:
Index: giFTcurs/src/ui.c
diff -u giFTcurs/src/ui.c:1.113 giFTcurs/src/ui.c:1.114
--- giFTcurs/src/ui.c:1.113     Mon Sep 15 17:28:19 2003
+++ giFTcurs/src/ui.c   Mon Oct 13 17:42:37 2003
@@ -18,7 +18,7 @@
  * along with giFTcurs; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,  USA.
  *
- * $Id: ui.c,v 1.113 2003/09/15 21:28:19 saturn Exp $
+ * $Id: ui.c,v 1.114 2003/10/13 21:42:37 weinholt Exp $
  */
 #include "giftcurs.h"
 
@@ -348,9 +348,9 @@
 
 /* This handles keys written into a inputbox. */
 /* FIXME: posp should take wide chars into account */
-int ui_input_handler(GString *foo, int *posp, int key)
+int ui_input_handler(GString *foo, int *posp, int *vposp, int key)
 {
-       int i, ret = 1, pos = *posp;
+       int ret = 1, pos = *posp;
 
        switch (key) {
        case 0:
@@ -371,24 +371,51 @@
                pos = 0;
                break;
        case XCTRL('w'):
+               /* FIXME: make this one recognize G_UNICODE_BREAK_SPACE or 
something? */
                remove_word(foo, &pos);
                break;
        case KEY_LEFT:
-               pos--;
+               if (utf8) {
+                       char *prevc = g_utf8_find_prev_char(foo->str, foo->str 
+ pos);
+                       
+                       pos = prevc - foo->str;
+               } else {
+                       pos--;
+               }
                break;
        case KEY_RIGHT:
-               pos++;
+               if (utf8) {
+                       char *nextc = g_utf8_find_next_char(foo->str + pos, 
NULL);
+                       
+                       pos = nextc - foo->str;
+               } else {
+                       pos++;
+               }
                break;
        case '\177':
        case '\b':
        case KEY_BACKSPACE:
        case KEY_DC:                            /* delete char */
-               i = foo->len;
-               if ((pos == 0 && key != KEY_DC) || (i == pos && key == KEY_DC))
+               if ((pos == 0 && key != KEY_DC) || (pos == foo->len && key == 
KEY_DC))
                        break;
-               if (key != KEY_DC)
-                       pos--;
-               g_string_erase(foo, pos, 1);
+               if (utf8) {
+                       if (key == KEY_DC) {
+                               char *nextc = g_utf8_find_next_char(foo->str + 
pos, NULL);
+                               int npos = nextc - foo->str;
+
+                               g_string_erase(foo, pos, npos - pos);
+                       } else {
+                               char *prevc = g_utf8_find_prev_char(foo->str, 
foo->str + pos);
+                               int npos = prevc - foo->str;
+
+                               g_string_erase(foo, npos, pos - npos);
+                               pos = npos;
+                       }
+               } else {
+                       if (key != KEY_DC)
+                               pos--;
+                       g_string_erase(foo, pos, 1);
+               }
                break;
        default:
                if (key > 255 || key < ' ') {
@@ -405,6 +432,14 @@
                pos = foo->len;
 
        *posp = pos;
+       if (utf8) {
+               char *p;
+
+               for (p = foo->str, *vposp = 0; *p && p < foo->str + pos; p = 
g_utf8_next_char(p))
+                       *vposp += g_unichar_iswide(g_utf8_get_char(p)) ? 2 : 1;
+       } else {
+               *vposp = pos;
+       }
 
        return ret;
 }
Index: giFTcurs/src/ui.h
diff -u giFTcurs/src/ui.h:1.44 giFTcurs/src/ui.h:1.45
--- giFTcurs/src/ui.h:1.44      Mon Sep 15 17:28:19 2003
+++ giFTcurs/src/ui.h   Mon Oct 13 17:42:37 2003
@@ -18,7 +18,7 @@
  * along with giFTcurs; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,  USA.
  *
- * $Id: ui.h,v 1.44 2003/09/15 21:28:19 saturn Exp $
+ * $Id: ui.h,v 1.45 2003/10/13 21:42:37 weinholt Exp $
  */
 #ifndef _UI_H
 #define _UI_H
@@ -57,7 +57,7 @@
 void ui_handler(int key);
 
 int ui_list_handler(list *foo, int key, int page_len);
-int ui_input_handler(GString *foo, int *pos, int key);
+int ui_input_handler(GString *foo, int *pos, int *vposp, int key);
 
 void ui_draw(void);
 
Index: giFTcurs/src/ui_draw.c
diff -u giFTcurs/src/ui_draw.c:1.109 giFTcurs/src/ui_draw.c:1.110
--- giFTcurs/src/ui_draw.c:1.109        Mon Oct 13 13:19:01 2003
+++ giFTcurs/src/ui_draw.c      Mon Oct 13 17:42:37 2003
@@ -18,7 +18,7 @@
  * along with giFTcurs; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,  USA.
  *
- * $Id: ui_draw.c,v 1.109 2003/10/13 17:19:01 weinholt Exp $
+ * $Id: ui_draw.c,v 1.110 2003/10/13 21:42:37 weinholt Exp $
  */
 #include "giftcurs.h"
 
@@ -75,29 +75,39 @@
 /* Returns the x-position of the end of the printed value */
 int draw_input(int x, int y, int w, const char *header, const char *value, int 
attr)
 {
-       int p = 3;
+       int p;
+       int print; /* This is how many columns from "value" we can print */
 
+       move(y, x);
        if (header) {
-               move(y, x);
                attrset(COLOR_PAIR(COLOR_HEADER) | A_BOLD);
                addstr(header);
                addstr(": ");
        }
 
+       /* Decoration */
        attrset(attr);
        addch('[');
+       getyx(stdscr, y, p);
+       attrset(COLOR_PAIR(COLOR_STAT_DATA));
+       hline('_', x + w - p - 1);  /* Trailing '_' */
+       move(y, x + w - 1);
+       attrset(attr);
+       addch(']');
 
        /* Value */
        attrset(COLOR_PAIR(COLOR_STAT_DATA));
-       if (header)
-               getyx(stdscr, y, p);
-       addnstr(value, w - p);
-       getyx(stdscr, y, p);
-       hline('_', x + w - p - 1);      /* Trailing '_' */
+       print = w - p;
+       if (utf8) {
+               const char *i = value;
+               int len;
 
-       attrset(attr);
-       move(y, x + w - 1);
-       addch(']');
+               for (len = 0; *i && len < print; i = g_utf8_next_char(i))
+                       len += g_unichar_iswide(g_utf8_get_char(i)) ? 2 : 1;
+
+               print = i - value;
+       }
+       mvaddnstr(y, p, value, print);
 
        return p;
 }
@@ -302,11 +312,17 @@
        erase();
 }
 
-/* Visual strlen(). Doesn't handle two chars wide chars yet. */
+/* Visual strlen(). */
 glong vstrlen(const char *str)
 {
-       if (!utf8)
+       if (utf8) {
+               const char *p;
+               int len;
+
+               for (p = str, len = 0; *p; p = g_utf8_next_char(p))
+                       len += g_unichar_iswide(g_utf8_get_char(p)) ? 2 : 1;
+               return len;
+       } else {
                return strlen(str);
-       else
-               return g_utf8_strlen(str, -1);
+       }
 }
Index: giFTcurs/src/ui_main.c
diff -u giFTcurs/src/ui_main.c:1.346 giFTcurs/src/ui_main.c:1.347
--- giFTcurs/src/ui_main.c:1.346        Mon Sep 15 17:28:19 2003
+++ giFTcurs/src/ui_main.c      Mon Oct 13 17:42:38 2003
@@ -18,7 +18,7 @@
  * along with giFTcurs; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,  USA.
  *
- * $Id: ui_main.c,v 1.346 2003/09/15 21:28:19 saturn Exp $
+ * $Id: ui_main.c,v 1.347 2003/10/13 21:42:38 weinholt Exp $
  */
 #include "giftcurs.h"
 
@@ -55,12 +55,12 @@
 
 static int active_field = FIELD_SEARCH_TERM;
 static GString search_term = { 0 };
-static unsigned int search_pos = 0;
+static unsigned int search_pos = 0, search_vpos = 0;
 static int curs_x, curs_y;
 static list search_realm = LIST_INITIALIZER;
 static GString slash_search_term = { 0 };
 static char *last_search_term = NULL;
-static int slash_search_pos;
+static int slash_search_pos, slash_search_vpos;
 static format_t hit_fmt;
 static format_t subhit_fmt;
 
@@ -399,7 +399,7 @@
                query *q;
 
        case FIELD_SEARCH_TERM:
-               if (!ui_input_handler(&search_term, &search_pos, key))
+               if (!ui_input_handler(&search_term, &search_pos, &search_vpos, 
key))
                        if (ui_list_handler(&queries, key, 5)) {
                                q = list_selected(&queries);
 
@@ -409,7 +409,7 @@
                                incoming_search_item();
                        }
                /* Check values */
-               ui_input_handler(&search_term, &search_pos, 0);
+               ui_input_handler(&search_term, &search_pos, &search_vpos, 0);
                if (key == KEY_ENTER && search_term.len) {
                        active_field = FIELD_SEARCH_BUTTON;
                        start_search_pressed();
@@ -497,7 +497,7 @@
                        g_free(last_search_term);
                        last_search_term = g_strdup(slash_search_term.str);
                        g_string_printf(&slash_search_term, "%c", key);
-                       slash_search_pos = 1;
+                       slash_search_pos = slash_search_vpos = 1;
                        main_screen_update_results();
                } else if (key == 'n' || key == 'N') {
                        slash_find_next(key == 'n' ? 1 : -1);
@@ -536,7 +536,7 @@
                refresh();
                return 1;
        case FIELD_STATUSBAR:
-               if (!ui_input_handler(&slash_search_term, &slash_search_pos, 
key)) {
+               if (!ui_input_handler(&slash_search_term, &slash_search_pos, 
&slash_search_vpos, key)) {
                        active_field = FIELD_RESULT_LIST;
                        if (!slash_search_term.str[0] || 
!slash_search_term.str[1])
                                g_string_assign(&slash_search_term, 
last_search_term);
@@ -628,7 +628,7 @@
        leaveok(stdscr, TRUE);
 
        draw_input_special(2, 1, SEARCH_BOX_W - 4, _("Query"),
-                                          search_term.str, FIELD_SEARCH_TERM, 
search_pos);
+                                          search_term.str, FIELD_SEARCH_TERM, 
search_vpos);
 
        draw_input_special(2, 2, SEARCH_BOX_W - 4, _("Realm"),
                                           list_selected(&search_realm), 
FIELD_SEARCH_REALM, -1);
@@ -825,7 +825,7 @@
        if (active_field == FIELD_STATUSBAR) {
                show_status(slash_search_term.str);
                curs_y = max_y - 1 - show_buttonbar;
-               curs_x = slash_search_pos;
+               curs_x = slash_search_vpos;
                curs_set(1);
                leaveok(stdscr, FALSE);
        } else if (active_field == FIELD_RESULT_LIST) {
@@ -927,7 +927,7 @@
 
        if (field == FIELD_SEARCH_TERM) {
                search_pos = rx - 1;
-               ui_input_handler(&search_term, &search_pos, 0);
+               ui_input_handler(&search_term, &search_pos, &search_vpos, 0);
        }
 
        active_field = field;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]