1
0
mirror of https://github.com/gryf/wmaker.git synced 2025-12-19 12:28:22 +01:00

added string utils

This commit is contained in:
kojima
2000-07-15 22:08:25 +00:00
parent dcd44b17b1
commit 9a89e6cc18
11 changed files with 279 additions and 16 deletions

View File

@@ -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:
............................

View File

@@ -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

View File

@@ -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);

View File

@@ -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;

View File

@@ -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);

181
WINGs/string.c Normal file
View File

@@ -0,0 +1,181 @@
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#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; i<count; i++) {
if (list[i]!=NULL && list[i][0]!=0) {
j += strlen(list[i]);
if (strpbrk(list[i], " \t"))
j += 2;
}
}
flat_string = malloc(j+count+1);
if (!flat_string) {
return NULL;
}
*flat_string = 0;
for (i=0; i<count; i++) {
if (list[i]!=NULL && list[i][0]!=0) {
if (i>0)
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;
}

View File

@@ -1,7 +1,7 @@
/*
* This event handling stuff was based on Tk.
* This event handling stuff was inspired on Tk.
*/
#include "WINGsP.h"

View File

@@ -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)) {

View File

@@ -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 <X11/keysym.h>

View File

@@ -692,6 +692,7 @@ WMSetViewNotifySizeChanges(WMView *view, Bool flag)
view->flags.notifySizeChanged = flag;
}
Window
WMViewXID(WMView *view)
{

View File

@@ -478,6 +478,7 @@ WMSetWindowInitialPosition(WMWindow *win, int x, int y)
void
WMSetWindowMinSize(WMWindow *win, unsigned width, unsigned height)
{