From 9a89e6cc1894c8a18da0854ac75f92b2ac5d0255 Mon Sep 17 00:00:00 2001 From: kojima Date: Sat, 15 Jul 2000 22:08:25 +0000 Subject: [PATCH] added string utils --- WINGs/ChangeLog | 1 + WINGs/Makefile.am | 2 + WINGs/WINGs.h | 2 + WINGs/WINGsP.h | 2 +- WINGs/WUtil.h | 14 ++++ WINGs/string.c | 181 ++++++++++++++++++++++++++++++++++++++++++++++ WINGs/wevent.c | 2 +- WINGs/wpanel.c | 83 +++++++++++++++++---- WINGs/wtext.c | 6 ++ WINGs/wview.c | 1 + WINGs/wwindow.c | 1 + 11 files changed, 279 insertions(+), 16 deletions(-) create mode 100644 WINGs/string.c diff --git a/WINGs/ChangeLog b/WINGs/ChangeLog index c351717b..2dbc1f2f 100644 --- a/WINGs/ChangeLog +++ b/WINGs/ChangeLog @@ -10,6 +10,7 @@ changes since wmaker 0.62.1: - added WMReparentWidget() - added WMCreateTabViewItem() - added W_CreateUnmanagedTopView() +- added wtokenize() changes since wmaker 0.62.0: ............................ diff --git a/WINGs/Makefile.am b/WINGs/Makefile.am index f1c1c26a..16f0b7ae 100644 --- a/WINGs/Makefile.am +++ b/WINGs/Makefile.am @@ -85,6 +85,7 @@ libWINGs_a_SOURCES = \ hashtable.c \ host.c \ memory.c \ + string.c \ usleep.c @@ -105,6 +106,7 @@ libWUtil_a_SOURCES = \ findfile.c \ hashtable.c \ memory.c \ + string.c \ usleep.c diff --git a/WINGs/WINGs.h b/WINGs/WINGs.h index 3404ee5e..f3b37b87 100644 --- a/WINGs/WINGs.h +++ b/WINGs/WINGs.h @@ -792,6 +792,8 @@ void WMUnmapWidget(WMWidget *w); void WMMapWidget(WMWidget *w); +void WMRaiseWidget(WMWidget *w); + void WMMoveWidget(WMWidget *w, int x, int y); void WMResizeWidget(WMWidget *w, unsigned int width, unsigned int height); diff --git a/WINGs/WINGsP.h b/WINGs/WINGsP.h index acc55dc9..3b35f5fa 100644 --- a/WINGs/WINGsP.h +++ b/WINGs/WINGsP.h @@ -298,7 +298,7 @@ typedef struct W_ViewDelegate { void (*willMove)(struct W_ViewDelegate*, WMView*, int*, int*); - void (*willResize)(struct W_ViewDelegate*, WMView*, + void (*willResize)(struct W_ViewDelegate*, WMView*, unsigned int*, unsigned int*); } W_ViewDelegate; diff --git a/WINGs/WUtil.h b/WINGs/WUtil.h index 10e25c45..368c554b 100644 --- a/WINGs/WUtil.h +++ b/WINGs/WUtil.h @@ -265,6 +265,20 @@ char *wstrdup(char *str); char *wstrappend(char *dst, char *src); + + + +void wtokensplit(char *command, char ***argv, int *argc); + +char *wtokenjoin(char **list, int count); + +void wtokenfree(char **tokens, int count); + +char *wtrimspace(char *s); + + + + char *wusergnusteppath(); char *wdefaultspathfordomain(char *domain); diff --git a/WINGs/string.c b/WINGs/string.c new file mode 100644 index 00000000..01ea275c --- /dev/null +++ b/WINGs/string.c @@ -0,0 +1,181 @@ + +#include +#include +#include + +#include "WUtil.h" + + + +#define PRC_ALPHA 0 +#define PRC_BLANK 1 +#define PRC_ESCAPE 2 +#define PRC_DQUOTE 3 +#define PRC_EOS 4 +#define PRC_SQUOTE 5 + +typedef struct { + short nstate; + short output; +} DFA; + + +static DFA mtable[9][6] = { + {{3,1},{0,0},{4,0},{1,0},{8,0},{6,0}}, + {{1,1},{1,1},{2,0},{3,0},{5,0},{1,1}}, + {{1,1},{1,1},{1,1},{1,1},{5,0},{1,1}}, + {{3,1},{5,0},{4,0},{1,0},{5,0},{6,0}}, + {{3,1},{3,1},{3,1},{3,1},{5,0},{3,1}}, + {{-1,-1},{0,0},{0,0},{0,0},{0,0},{0,0}}, /* final state */ + {{6,1},{6,1},{7,0},{6,1},{5,0},{3,0}}, + {{6,1},{6,1},{6,1},{6,1},{5,0},{6,1}}, + {{-1,-1},{0,0},{0,0},{0,0},{0,0},{0,0}}, /* final state */ +}; + +static char* +next_token(char *word, char **next) +{ + char *ptr; + char *ret, *t; + int state, ctype; + + t = ret = wmalloc(strlen(word)+1); + ptr = word; + + state = 0; + *t = 0; + while (1) { + if (*ptr==0) + ctype = PRC_EOS; + else if (*ptr=='\\') + ctype = PRC_ESCAPE; + else if (*ptr=='"') + ctype = PRC_DQUOTE; + else if (*ptr=='\'') + ctype = PRC_SQUOTE; + else if (*ptr==' ' || *ptr=='\t') + ctype = PRC_BLANK; + else + ctype = PRC_ALPHA; + + if (mtable[state][ctype].output) { + *t = *ptr; t++; + *t = 0; + } + state = mtable[state][ctype].nstate; + ptr++; + if (mtable[state][0].output<0) { + break; + } + } + + if (*ret==0) + t = NULL; + else + t = wstrdup(ret); + + free(ret); + + if (ctype==PRC_EOS) + *next = NULL; + else + *next = ptr; + + return t; +} + + +/* separate a string in tokens, taking " and ' into account */ +void +wtokensplit(char *command, char ***argv, int *argc) +{ + char *token, *line; + int count; + + count = 0; + line = command; + do { + token = next_token(line, &line); + if (token) { + if (count == 0) + *argv = wmalloc(sizeof(char**)); + else + *argv = wrealloc(*argv, (count+1)*sizeof(char**)); + (*argv)[count++] = token; + } + } while (token!=NULL && line!=NULL); + + *argc = count; +} + + + + + +char* +wtokenjoin(char **list, int count) +{ + int i, j; + char *flat_string, *wspace; + + j = 0; + for (i=0; i0) + strcat(flat_string, " "); + wspace = strpbrk(list[i], " \t"); + if (wspace) + strcat(flat_string, "\""); + strcat(flat_string, list[i]); + if (wspace) + strcat(flat_string, "\""); + } + } + + return flat_string; +} + + + +void +wtokenfree(char **tokens, int count) +{ + while (--count) free(tokens[count]); + free(tokens); +} + + + +char* +wtrimspace(char *s) +{ + char *t; + char *c; + + while (isspace(*s) && *s) s++; + t = s+strlen(s); + while (t > s && isspace(*t)) t--; + + c = wmalloc(t-s + 1); + memcpy(c, s, t-s); + c[t-s] = 0; + + return c; +} + + + diff --git a/WINGs/wevent.c b/WINGs/wevent.c index 2aacc52c..ab646eab 100644 --- a/WINGs/wevent.c +++ b/WINGs/wevent.c @@ -1,7 +1,7 @@ /* - * This event handling stuff was based on Tk. + * This event handling stuff was inspired on Tk. */ #include "WINGsP.h" diff --git a/WINGs/wpanel.c b/WINGs/wpanel.c index c7149a86..d421e6a9 100644 --- a/WINGs/wpanel.c +++ b/WINGs/wpanel.c @@ -52,6 +52,27 @@ WMRunAlertPanel(WMScreen *scrPtr, WMWindow *owner, panel = WMCreateAlertPanel(scrPtr, owner, title, msg, defaultButton, alternateButton, otherButton); + { + int px, py; + WMView *view = WMWidgetView(panel->win); + + if (owner) { + WMView *oview = WMWidgetView(owner); + WMPoint pt = WMGetViewScreenPosition(oview); + + px = (W_VIEW_WIDTH(oview)-W_VIEW_WIDTH(view))/2; + py = (W_VIEW_HEIGHT(oview)-W_VIEW_HEIGHT(view))/2; + + px += pt.x; + py += pt.y; + } else { + px = (W_VIEW_WIDTH(scrPtr->rootView)-W_VIEW_WIDTH(view))/2; + py = (W_VIEW_HEIGHT(scrPtr->rootView)-W_VIEW_HEIGHT(view))/2; + } + WMSetWindowInitialPosition(panel->win, px, py); + } + + scrPtr->modalView = W_VIEW(panel->win); WMMapWidget(panel->win); @@ -155,9 +176,9 @@ WMCreateAlertPanel(WMScreen *scrPtr, WMWindow *owner, /* create buttons */ if (otherButton) - ow = WMWidthOfString(scrPtr->normalFont, otherButton, + ow = WMWidthOfString(scrPtr->normalFont, otherButton, strlen(otherButton)); - + if (alternateButton) aw = WMWidthOfString(scrPtr->normalFont, alternateButton, strlen(alternateButton)); @@ -166,43 +187,56 @@ WMCreateAlertPanel(WMScreen *scrPtr, WMWindow *owner, dw = WMWidthOfString(scrPtr->normalFont, defaultButton, strlen(defaultButton)); - w = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0); - if (aw > w) - w = aw; - if (ow > w) - w = ow; + dw = dw + (scrPtr->buttonArrow ? scrPtr->buttonArrow->width : 0); + + aw += 30; + ow += 30; + dw += 30; + + w = WMAX(dw, WMAX(aw, ow)); + if ((w+10)*3 < 400) { + aw = w; + ow = w; + dw = w; + } else { + int t; + + t = 400 - 40 - aw - ow - dw; + aw += t/3; + ow += t/3; + dw += t/3; + } - w += 30; x = 400; if (defaultButton) { - x -= w + 10; + x -= dw + 10; panel->defBtn = WMCreateCommandButton(panel->win); WMSetButtonAction(panel->defBtn, alertPanelOnClick, panel); WMMoveWidget(panel->defBtn, x, 144); - WMResizeWidget(panel->defBtn, w, 24); + WMResizeWidget(panel->defBtn, dw, 24); WMSetButtonText(panel->defBtn, defaultButton); WMSetButtonImage(panel->defBtn, scrPtr->buttonArrow); WMSetButtonAltImage(panel->defBtn, scrPtr->pushedButtonArrow); WMSetButtonImagePosition(panel->defBtn, WIPRight); } if (alternateButton) { - x -= w + 10; + x -= aw + 10; panel->altBtn = WMCreateCommandButton(panel->win); WMMoveWidget(panel->altBtn, x, 144); - WMResizeWidget(panel->altBtn, w, 24); + WMResizeWidget(panel->altBtn, aw, 24); WMSetButtonAction(panel->altBtn, alertPanelOnClick, panel); WMSetButtonText(panel->altBtn, alternateButton); } if (otherButton) { - x -= w + 10; + x -= ow + 10; panel->othBtn = WMCreateCommandButton(panel->win); WMSetButtonAction(panel->othBtn, alertPanelOnClick, panel); WMMoveWidget(panel->othBtn, x, 144); - WMResizeWidget(panel->othBtn, w, 24); + WMResizeWidget(panel->othBtn, ow, 24); WMSetButtonText(panel->othBtn, otherButton); } @@ -267,6 +301,27 @@ WMRunInputPanel(WMScreen *scrPtr, WMWindow *owner, char *title, panel = WMCreateInputPanel(scrPtr, owner, title, msg, defaultText, okButton, cancelButton); + + { + int px, py; + WMView *view = WMWidgetView(panel->win); + + if (owner) { + WMView *oview = WMWidgetView(owner); + WMPoint pt = WMGetViewScreenPosition(oview); + + px = (W_VIEW_WIDTH(oview)-W_VIEW_WIDTH(view))/2; + py = (W_VIEW_HEIGHT(oview)-W_VIEW_HEIGHT(view))/2; + + px += pt.x; + py += pt.y; + } else { + px = (W_VIEW_WIDTH(scrPtr->rootView)-W_VIEW_WIDTH(view))/2; + py = (W_VIEW_HEIGHT(scrPtr->rootView)-W_VIEW_HEIGHT(view))/2; + } + WMSetWindowInitialPosition(panel->win, px, py); + } + WMMapWidget(panel->win); while (!panel->done || WMScreenPending(scrPtr)) { diff --git a/WINGs/wtext.c b/WINGs/wtext.c index faab1550..a00adfab 100644 --- a/WINGs/wtext.c +++ b/WINGs/wtext.c @@ -18,6 +18,12 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +/* README README README README README README README + * Nwanua: dont use // style comments please! + * It doesnt work in lots of compilers out there :/ + * -Alfredo + * README README README README README README README + */ #include "WINGsP.h" #include diff --git a/WINGs/wview.c b/WINGs/wview.c index 60af77c5..46c04379 100644 --- a/WINGs/wview.c +++ b/WINGs/wview.c @@ -692,6 +692,7 @@ WMSetViewNotifySizeChanges(WMView *view, Bool flag) view->flags.notifySizeChanged = flag; } + Window WMViewXID(WMView *view) { diff --git a/WINGs/wwindow.c b/WINGs/wwindow.c index 40e30243..a678a87b 100644 --- a/WINGs/wwindow.c +++ b/WINGs/wwindow.c @@ -478,6 +478,7 @@ WMSetWindowInitialPosition(WMWindow *win, int x, int y) + void WMSetWindowMinSize(WMWindow *win, unsigned width, unsigned height) {