static newtComponent createButton(int left, int row, const char * text, int compact) {
newtComponent co;
struct button * bu;
+ int width = wstrlen(text,-1);
co = malloc(sizeof(*co));
bu = malloc(sizeof(struct button));
if (bu->compact) {
co->height = 1;
- co->width = strlen(text) + 3;
+ co->width = width + 3;
} else {
co->height = 4;
- co->width = strlen(text) + 5;
+ co->width = width + 5;
}
co->top = row;
co->callback = NULL;
co->height = 1;
- co->width = strlen(text) + 4;
+ co->width = wstrlen(text, -1) + 4;
co->top = top;
co->left = left;
co->takesFocus = 1;
int flags, int * indexes) {
struct items * curList, * newNode, * item;
struct items ** listPtr = NULL;
- int i, index, numIndexes;
+ int i, index, numIndexes, width;
struct CheckboxTree * ct = co->data;
numIndexes = 0;
item->depth = numIndexes - 1;
i = 4 + (3 * item->depth);
+ width = wstrlen(text, -1);
- if ((ct->userHasSetWidth == 0) && ((strlen(text) + i + ct->sbAdjust) > co->width)) {
- updateWidth(co, ct, strlen(text) + i);
+ if ((ct->userHasSetWidth == 0) && ((width + i + ct->sbAdjust) > co->width)) {
+ updateWidth(co, ct, width + i);
}
return 0;
{
struct CheckboxTree * ct;
struct items * item;
- int i;
+ int i, width;
if (!co) return;
ct = co->data;
i = 4 + (3 * item->depth);
- if ((ct->userHasSetWidth == 0) && ((strlen(text) + i + ct->sbAdjust) > co->width)) {
- updateWidth(co, ct, strlen(text) + i);
+ width = wstrlen(text, -1);
+ if ((ct->userHasSetWidth == 0) && ((width + i + ct->sbAdjust) > co->width)) {
+ updateWidth(co, ct, width + i);
}
ctDraw(co);
VERSION=$(awk '/^%define version/ {print $3}' $srcdir/newt.spec)
-VERSION=0.50.38
+VERSION=0.50.39
SONAME=0.50
AC_SUBST(VERSION)
AC_SUBST(SONAME)
#include "dialogboxes.h"
#include "newt.h"
+#include "newt_pr.h"
#include "popt.h"
/* globals -- ick */
} else
itemInfo[numItems].text = "";
- if (strlen(itemInfo[numItems].text) > (unsigned int)maxTextWidth)
- maxTextWidth = strlen(itemInfo[numItems].text);
- if (strlen(itemInfo[numItems].tag) > (unsigned int)maxTagWidth)
- maxTagWidth = strlen(itemInfo[numItems].tag);
+ if (wstrlen(itemInfo[numItems].text,-1) > (unsigned int)maxTextWidth)
+ maxTextWidth = wstrlen(itemInfo[numItems].text,-1);
+ if (wstrlen(itemInfo[numItems].tag,-1) > (unsigned int)maxTagWidth)
+ maxTagWidth = wstrlen(itemInfo[numItems].tag,-1);
numItems++;
}
else
cbStates[numBoxes] = ' ';
- if (strlen(cbInfo[numBoxes].tag) > (unsigned int)maxWidth)
- maxWidth = strlen(cbInfo[numBoxes].tag);
+ if (wstrlen(cbInfo[numBoxes].tag,-1) > (unsigned int)maxWidth)
+ maxWidth = wstrlen(cbInfo[numBoxes].tag,-1);
numBoxes++;
}
else
co->takesFocus = 0;
- if (initialValue && strlen(initialValue) > (unsigned int)width) {
- en->bufAlloced = strlen(initialValue) + 1;
+ if (initialValue && wstrlen(initialValue,-1) > (unsigned int)width) {
+ en->bufAlloced = wstrlen(initialValue,-1) + 1;
}
en->buf = malloc(en->bufAlloced);
en->resultPtr = resultPtr;
}
void newtGridWrappedWindow(newtGrid grid, char * title) {
- int width, height, offset = 0;
+ int w, width, height, offset = 0;
newtGridGetSize(grid, &width, &height);
- if (width < strlen(title) + 2) {
- offset = ((strlen(title) + 2) - width) / 2;
- width = strlen(title) + 2;
+ w = wstrlen(title,-1);
+ if (width < w + 2) {
+ offset = ((w + 2) - width) / 2;
+ width = w + 2;
}
newtCenteredWindow(width + 2, height + 2, title);
newtGridPlace(grid, 1 + offset, 1);
co->ops = &labelOps;
co->height = 1;
- co->width = strlen(text);
+ co->width = wstrlen(text, -1);
co->top = top;
co->left = left;
co->takesFocus = 0;
free(la->text);
la->text = strdup(text);
la->length = newLength;
- co->width = newLength;
+ co->width = wstrlen(text,-1);
}
labelDraw(co);
free(item->text);
item->text = strdup(text);
}
- if (li->userHasSetWidth == 0 && strlen(text) > li->curWidth) {
- updateWidth(co, li, strlen(text));
+ if (li->userHasSetWidth == 0 && wstrlen(text,-1) > li->curWidth) {
+ updateWidth(co, li, wstrlen(text,-1));
}
if (num >= li->startShowItem && num <= li->startShowItem + co->height)
item = li->boxItems = malloc(sizeof(struct items));
}
- if (!li->userHasSetWidth && text && (strlen(text) > li->curWidth))
- updateWidth(co, li, strlen(text));
+ if (!li->userHasSetWidth && text && (wstrlen(text,-1) > li->curWidth))
+ updateWidth(co, li, wstrlen(text,-1));
item->text = strdup(text); item->data = data; item->next = NULL;
item->isSelected = 0;
item->next = NULL;
}
- if (!li->userHasSetWidth && text && (strlen(text) > li->curWidth))
- updateWidth(co, li, strlen(text));
+ if (!li->userHasSetWidth && text && (wstrlen(text,-1) > li->curWidth))
+ updateWidth(co, li, wstrlen(text,-1));
item->text = strdup(text?text:"(null)"); item->data = data;
item->isSelected = 0;
if (!li->userHasSetWidth) {
widest = 0;
for (item = li->boxItems; item != NULL; item = item->next)
- if ((t = strlen(item->text)) > widest) widest = t;
+ if ((t = wstrlen(item->text,-1)) > widest) widest = t;
}
if (li->currItem >= num)
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
+#include <wchar.h>
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
return -1;
}
+int wstrlen(const char *str, int len) {
+ mbstate_t ps;
+ wchar_t tmp;
+ int nchars = 0;
+
+ if (!str) return 0;
+ if (!len) return 0;
+ if (len < 0) len = strlen(str);
+ memset(&ps,0,sizeof(mbstate_t));
+ while (len > 0) {
+ int x,y;
+
+ x = mbrtowc(&tmp,str,len,&ps);
+ if (x >0) {
+ len -= x;
+ y = wcwidth(tmp);
+ if (y>0)
+ nchars+=y;
+ } else break;
+ }
+ return nchars;
+}
+
void newtFlushInput(void) {
while (SLang_input_pending(0)) {
SLang_getkey();
SLsmg_draw_box(top - 1, left - 1, height + 2, width + 2);
if (currentWindow->title) {
- i = strlen(currentWindow->title) + 4;
+ i = wstrlen(currentWindow->title,-1) + 4;
i = ((width - i) / 2) + left;
SLsmg_gotorc(top - 1, i);
SLsmg_set_char_set(1);
struct eventResult newtDefaultEventHandler(newtComponent c,
struct event ev);
+int wstrlen(const char *str, int len);
+
#endif /* H_NEWT_PR */
+
#include <ctype.h>
#include <slang.h>
#include <stdlib.h>
#include <string.h>
+#include <wchar.h>
+#include <wctype.h>
#include "newt.h"
#include "newt_pr.h"
struct textbox {
char ** lines;
int numLines;
+ char *blankline;
int linesAlloced;
int doWrap;
newtComponent sb;
tb->lines = NULL;
tb->topLine = 0;
tb->textWidth = width;
+ tb->blankline = malloc(width+1);
+ memset(tb->blankline,' ',width);
+ tb->blankline[width] = '\0';
if (flags & NEWT_FLAG_SCROLL) {
co->width += 2;
return buf;
}
-#define iseuckanji(c) (0xa1 <= (unsigned char)(c&0xff) && (unsigned char)(c&0xff) <= 0xfe)
-
static void doReflow(const char * text, char ** resultPtr, int width,
int * badness, int * heightPtr) {
char * result = NULL;
int i;
int howbad = 0;
int height = 0;
- int kanji = 0;
+ wchar_t tmp;
+ mbstate_t ps;
if (resultPtr) {
/* XXX I think this will work */
result = malloc(strlen(text) + (strlen(text) / width) + 2);
*result = '\0';
}
-
+
+ memset(&ps,0,sizeof(mbstate_t));
while (*text) {
- kanji = 0;
end = strchr(text, '\n');
if (!end)
end = text + strlen(text);
while (*text && text <= end) {
- if (end - text < width) {
+ int len;
+
+ len = wstrlen(text, end - text);
+ if (len < width) {
if (result) {
strncat(result, text, end - text);
strcat(result, "\n");
height++;
}
- if (end - text < (width / 2))
- howbad += ((width / 2) - (end - text)) / 2;
+ if (len < (width / 2)) {
+#ifdef DEBUG_WRAP
+ fprintf(stderr,"adding %d\n",((width / 2) - (len)) / 2);
+#endif
+ howbad += ((width / 2) - (len)) / 2;
+ }
text = end;
if (*text) text++;
} else {
+ char *spcptr = NULL;
+ int spc =0,w2, x;
+
chptr = text;
- kanji = 0;
- for ( i = 0; i < width - 1; i++ ) {
- if ( !iseuckanji(*chptr)) {
- kanji = 0;
- } else if ( kanji == 1 ) {
- kanji = 2;
- } else {
- kanji = 1;
- }
- chptr++;
- }
- if (kanji == 0) {
- while (chptr > text && !isspace(*chptr)) chptr--;
- while (chptr > text && isspace(*chptr)) chptr--;
- chptr++;
+ w2 = 0;
+ for (i = 0; i < width - 1;) {
+ if ((x=mbrtowc(&tmp,chptr,end-chptr,&ps))<=0)
+ break;
+ if (spc && !iswspace(tmp))
+ spc = 0;
+ else if (!spc && iswspace(tmp)) {
+ spc = 1;
+ spcptr = chptr;
+ w2 = i;
+ }
+ chptr += x;
+ x = wcwidth(tmp);
+ if (x>0)
+ i+=x;
}
-
- if (chptr-text == 1 && !isspace(*chptr))
- chptr = text + width - 1;
-
- if (chptr > text)
- howbad += width - (chptr - text) + 1;
+ howbad += width - w2 + 1;
+#ifdef DEBUG_WRAP
+ fprintf(stderr,"adding %d\n",width - w2 + 1, chptr);
+#endif
+ if (spcptr) chptr = spcptr;
if (result) {
- if (kanji == 1) {
- strncat(result, text, chptr - text + 1 );
- chptr++;
- kanji = 0;
- } else {
strncat(result, text, chptr - text );
- }
strcat(result, "\n");
height++;
}
- if (isspace(*chptr))
- text = chptr + 1;
- else
- text = chptr;
- while (isspace(*text)) text++;
+ text = chptr;
+ while (1) {
+ if ((x=mbrtowc(&tmp,text,end-text,NULL))<=0)
+ break;
+ if (!iswspace(tmp)) break;
+ text += x;
+ }
}
}
}
if (badness) *badness = howbad;
if (resultPtr) *resultPtr = result;
if (heightPtr) *heightPtr = height;
+#ifdef DEBUG_WRAP
+ fprintf(stderr, "width %d, badness %d, height %d\n",width, howbad, height);
+#endif
}
char * newtReflowText(char * text, int width, int flexDown, int flexUp,
static void addLine(newtComponent co, const char * s, int len) {
struct textbox * tb = co->data;
- if (len > tb->textWidth) len = tb->textWidth;
-
- tb->lines[tb->numLines] = malloc(tb->textWidth + 1);
- memset(tb->lines[tb->numLines], ' ', tb->textWidth);
+ while (wstrlen(s,len) > tb->textWidth) {
+ len--;
+ }
+ tb->lines[tb->numLines] = malloc(len + 1);
memcpy(tb->lines[tb->numLines], s, len);
- tb->lines[tb->numLines++][tb->textWidth] = '\0';
+ tb->lines[tb->numLines++][len] = '\0';
}
static void textboxDraw(newtComponent c) {
SLsmg_set_color(NEWT_COLORSET_TEXTBOX);
for (i = 0; (i + tb->topLine) < tb->numLines && i < c->height; i++) {
+ newtGotorc(c->top + i, c->left);
+ SLsmg_write_string(tb->blankline);
newtGotorc(c->top + i, c->left);
SLsmg_write_string(tb->lines[i + tb->topLine]);
}
for (i = 0; i < tb->numLines; i++)
free(tb->lines[i]);
free(tb->lines);
+ free(tb->blankline);
free(tb);
free(co);
}