From e4978330dc81571a625be90d0b88a60f63978f95 Mon Sep 17 00:00:00 2001 From: gotmor Date: Thu, 19 Jul 2007 12:50:58 +0000 Subject: changed a_menuexec and a_menuprint to use the new parser ready for release 0.6.0 git-svn-id: http://dzen.googlecode.com/svn/trunk@131 f2baff5b-bf2c-0410-a398-912abdc3d8b2 --- README | 31 +++---- README.dzen | 39 +++++---- action.c | 13 ++- config.mk | 2 +- draw.c | 286 +++++++++++++++++++++++++++++++++--------------------------- dzen.h | 5 ++ 6 files changed, 208 insertions(+), 168 deletions(-) diff --git a/README b/README index 8f33a15..52a9253 100644 --- a/README +++ b/README @@ -374,34 +374,27 @@ of lines to the slave window as defined by the parameter to '-l'. This feature allows to dynamically (at runtime) set foreground and background colors for dzen's input (colored text) working in all -modes (title only, slave window and menu mode). You can specify the -colors by using their symbolic names or '#rrggbb' values. +modes (title only, slave window and menu mode). -The input must be in the following format: +Syntax: - * Setting only the foreground color: +Foreground color: ^fg(color) +Background color: ^bg(color) - ^fg(red)I'm red text ^fg(blue)I am blue - Escaped version, if you view this file with dzen: - ^^fg(red)I'm red text ^^fg(blue)I am blue +These commands can appear anywhere and in any combination in dzen's +input. The color can be specified either as symbolic name (e.g. red, +darkgreen, etc.) or as #rrggbb hex-value (e.g. #ffffaa). +Doubling the '^' character removes the special meaning from it. - * Setting foreground and/or background colors: +Some examples: - ^bg(#ffaaaa)The ^fg(yellow)text to ^bg(blue)^fg(orange)colorize + ^fg(red)I'm red text ^fg(blue)I am blue - Escaped version, if you view this file with dzen: - ^^bg(#ffaaaa)The ^^fg(yellow)text to ^^bg(blue)^^fg(orange)colorize + ^bg(#ffaaaa)The ^fg(yellow)text to ^bg(blue)^fg(orange)colorize - - * If you need to print '^^' characters double them in order to escape them - from being interpreted - - ^fg(grey70)Some text containing ^^ characters - - Escaped version, if you view this file with dzen: - ^^fg(grey70)Some text containing ^^^^ characters + ^fg(grey70)Some text containing ^^ characters diff --git a/README.dzen b/README.dzen index 4fe689b..8d279e2 100644 --- a/README.dzen +++ b/README.dzen @@ -374,34 +374,41 @@ of lines to the slave window as defined by the parameter to '-l'. This feature allows to dynamically (at runtime) set foreground and background colors for dzen's input (colored text) working in all -modes (title only, slave window and menu mode). You can specify the -colors by using their symbolic names or '#rrggbb' values. +modes (title only, slave window and menu mode). -The input must be in the following format: +Syntax: - * Setting only the foreground color: +Foreground color: ^^fg(color) +Background color: ^^bg(color) - ^fg(red)I'm red text ^fg(blue)I am blue - Escaped version, if you view this file with dzen: - ^^fg(red)I'm red text ^^fg(blue)I am blue +These commands can appear anywhere and in any combination in dzen's +input. The color can be specified either as symbolic name (e.g. red, +darkgreen, etc.) or as #rrggbb hex-value (e.g. #ffffaa). +Doubling the '^^' character removes the special meaning from it. - * Setting foreground and/or background colors: +Some examples: - ^bg(#ffaaaa)The ^fg(yellow)text to ^bg(blue)^fg(orange)colorize + Input: + ^^fg(red)I'm red text ^^fg(blue)I am blue - Escaped version, if you view this file with dzen: - ^^bg(#ffaaaa)The ^^fg(yellow)text to ^^bg(blue)^^fg(orange)colorize + Resulting in: + ^fg(red)I'm red text ^fg(blue)I am blue - * If you need to print '^^' characters double them in order to escape them - from being interpreted + Input: + ^^bg(#ffaaaa)The ^^fg(yellow)text to ^^bg(blue)^^fg(orange)colorize - ^fg(grey70)Some text containing ^^ characters + Resulting in: + ^bg(#ffaaaa)The ^fg(yellow)text to ^bg(blue)^fg(orange)colorize - Escaped version, if you view this file with dzen: - ^^fg(grey70)Some text containing ^^^^ characters + + Input: + ^^fg(grey70)Some text containing ^^^^ characters + + Resulting in: + ^fg(grey70)Some text containing ^^ characters diff --git a/action.c b/action.c index ea66889..fac5a44 100644 --- a/action.c +++ b/action.c @@ -439,23 +439,30 @@ a_print(char * opt[]) { int a_menuprint(char * opt[]) { + char *text; (void)opt; + if(dzen.slave_win.ismenu && dzen.slave_win.sel_line != -1 && (dzen.slave_win.sel_line + dzen.slave_win.first_line_vis) < dzen.slave_win.tcnt) { - puts(dzen.slave_win.tbuf[dzen.slave_win.sel_line + dzen.slave_win.first_line_vis].text); + text = parse_line(NULL, dzen.slave_win.sel_line, 0, 0, 1); + puts(text); fflush(stdout); dzen.slave_win.sel_line = -1; - fflush(stdout); + free(text); } return 0; } int a_menuexec(char * opt[]) { + char *text; (void)opt; + if(dzen.slave_win.ismenu && dzen.slave_win.sel_line != -1 && (dzen.slave_win.sel_line + dzen.slave_win.first_line_vis) < dzen.slave_win.tcnt) { - spawn(dzen.slave_win.tbuf[dzen.slave_win.sel_line + dzen.slave_win.first_line_vis].text); + text = parse_line(NULL, dzen.slave_win.sel_line, 0, 0, 1); + spawn(text); dzen.slave_win.sel_line = -1; + free(text); } return 0; } diff --git a/config.mk b/config.mk index 6686193..03d1178 100644 --- a/config.mk +++ b/config.mk @@ -1,5 +1,5 @@ # dzen version -VERSION = 0.5.0 +VERSION = 0.6.0 # Customize below to fit your system diff --git a/draw.c b/draw.c index 547fab7..f9c2517 100644 --- a/draw.c +++ b/draw.c @@ -9,12 +9,13 @@ #include #include -void parse_line(const char *, int, int, int); -int get_tokval(const char*, char**); -int get_token(const char *, int *, char **); - +/* command types for the in text parser */ enum ctype {bg, fg, icon}; +int get_tokval(const char* line, char **retdata); +int get_token(const char* line, int * t, char **tval); +void set_opts(int type, char * value, int reverse); + static unsigned int textnw(const char *text, unsigned int len) { XRectangle r; @@ -44,9 +45,9 @@ drawtext(const char *text, int reverse, int line, int align) { } if(dzen.font.set) - parse_line(text, line, align, reverse); + parse_line(text, line, align, reverse, 0); else - parse_line(text, line, align, reverse); + parse_line(text, line, align, reverse, 0); } long @@ -149,179 +150,206 @@ get_token(const char *line, int * t, char **tval) { return next_pos+off; } -void -parse_line(const char *line, int lnr, int align, int reverse) { - int i, next_pos=0, j=0, px=0, py=0, xorig, h, tw, ow; - char lbuf[MAX_LINE_LEN]; +void +set_opts(int type, char * value, int reverse) { + switch(type) { + case fg: + reverse ? + XSetBackground(dzen.dpy, dzen.tgc, getcolor(value)) : + XSetForeground(dzen.dpy, dzen.tgc, getcolor(value)); + break; + case bg: + reverse ? + XSetForeground(dzen.dpy, dzen.tgc, getcolor(value)) : + XSetBackground(dzen.dpy, dzen.tgc, getcolor(value)); + break; + } +} + +char * +parse_line(const char *line, int lnr, int align, int reverse, int nodraw) { + int i, next_pos=0, j=0, px=0, py=0, xorig, h=0, tw, ow; + char lbuf[MAX_LINE_LEN], *rbuf = NULL; int t=-1; char *tval=NULL; Drawable pm; XRectangle r = { dzen.x, dzen.y, dzen.w, dzen.h}; - h = dzen.font.ascent + dzen.font.descent; - xorig = 0; py = dzen.font.ascent + (dzen.line_height - h) / 2;; + /* parse line and return the text without control commands*/ + if(nodraw) { + rbuf = emalloc(MAX_LINE_LEN); + rbuf[0] = '\0'; + if( (lnr + dzen.slave_win.first_line_vis) >= dzen.slave_win.tcnt) + line = NULL; + else + line = dzen.slave_win.tbuf[dzen.slave_win.first_line_vis+lnr].text; - if(lnr != -1) { - pm = XCreatePixmap(dzen.dpy, RootWindow(dzen.dpy, DefaultScreen(dzen.dpy)), dzen.slave_win.width, - dzen.line_height, DefaultDepth(dzen.dpy, dzen.screen)); } + /* parse line and render text */ else { - pm = XCreatePixmap(dzen.dpy, RootWindow(dzen.dpy, DefaultScreen(dzen.dpy)), dzen.title_win.width, - dzen.line_height, DefaultDepth(dzen.dpy, dzen.screen)); - } + h = dzen.font.ascent + dzen.font.descent; + xorig = 0; py = dzen.font.ascent + (dzen.line_height - h) / 2;; - if(!reverse) { - XSetBackground(dzen.dpy, dzen.tgc, dzen.norm[ColBG]); - XSetForeground(dzen.dpy, dzen.tgc, dzen.norm[ColBG]); - } - else { - XSetBackground(dzen.dpy, dzen.tgc, dzen.norm[ColFG]); - XSetForeground(dzen.dpy, dzen.tgc, dzen.norm[ColFG]); - } - XFillRectangles(dzen.dpy, pm, dzen.tgc, &r, 1); + if(lnr != -1) { + pm = XCreatePixmap(dzen.dpy, RootWindow(dzen.dpy, DefaultScreen(dzen.dpy)), dzen.slave_win.width, + dzen.line_height, DefaultDepth(dzen.dpy, dzen.screen)); + } + else { + pm = XCreatePixmap(dzen.dpy, RootWindow(dzen.dpy, DefaultScreen(dzen.dpy)), dzen.title_win.width, + dzen.line_height, DefaultDepth(dzen.dpy, dzen.screen)); + } - if(!reverse) { - XSetForeground(dzen.dpy, dzen.tgc, dzen.norm[ColFG]); - } - else { - XSetForeground(dzen.dpy, dzen.tgc, dzen.norm[ColBG]); - } + if(!reverse) { + XSetBackground(dzen.dpy, dzen.tgc, dzen.norm[ColBG]); + XSetForeground(dzen.dpy, dzen.tgc, dzen.norm[ColBG]); + } + else { + XSetBackground(dzen.dpy, dzen.tgc, dzen.norm[ColFG]); + XSetForeground(dzen.dpy, dzen.tgc, dzen.norm[ColFG]); + } + XFillRectangles(dzen.dpy, pm, dzen.tgc, &r, 1); + + if(!reverse) { + XSetForeground(dzen.dpy, dzen.tgc, dzen.norm[ColFG]); + } + else { + XSetForeground(dzen.dpy, dzen.tgc, dzen.norm[ColBG]); + } + + if( (lnr + dzen.slave_win.first_line_vis) >= dzen.slave_win.tcnt) { + if(dzen.font.set) + XmbDrawImageString(dzen.dpy, pm, dzen.font.set, + dzen.tgc, px, py, "", 0); + else + XDrawImageString(dzen.dpy, pm, dzen.tgc, px, py, "", 0); - if( (lnr + dzen.slave_win.first_line_vis) >= dzen.slave_win.tcnt) { - XmbDrawImageString(dzen.dpy, pm, dzen.font.set, - dzen.tgc, px, py, "", 0); XCopyArea(dzen.dpy, pm, dzen.slave_win.drawable[lnr], dzen.gc, 0, 0, px, dzen.line_height, xorig, 0); XFreePixmap(dzen.dpy, pm); - return; + return NULL; + } } - - for(i=0; i < strlen(line); i++) { + for(i=0; (unsigned)i < strlen(line); i++) { if(*(line+i) == '^') { lbuf[j] = '\0'; - if(t != -1 && tval) { - switch(t) { - case fg: - reverse ? - XSetBackground(dzen.dpy, dzen.tgc, getcolor(tval)) : - XSetForeground(dzen.dpy, dzen.tgc, getcolor(tval)); - break; - case bg: - reverse ? - XSetForeground(dzen.dpy, dzen.tgc, getcolor(tval)) : - XSetBackground(dzen.dpy, dzen.tgc, getcolor(tval)); - break; - } - } - ow = j; - tw = textnw(lbuf, strlen(lbuf)); - while( (tw + px) > (dzen.w - h)) { - lbuf[--j] = '\0'; - tw = textnw(lbuf, strlen(lbuf)); - } - if(j < ow) { - if(j > 1) - lbuf[j - 1] = '.'; - if(j > 2) - lbuf[j - 2] = '.'; - if(j > 3) - lbuf[j - 3] = '.'; + + if(nodraw) { + strcat(rbuf, lbuf); } + else { + if(t != -1 && tval) + set_opts(t, tval, reverse); + + /* check if text is longer than window's width */ + ow = j; tw = textnw(lbuf, strlen(lbuf)); + while((tw + px) > (dzen.w - h)) { + lbuf[--j] = '\0'; + tw = textnw(lbuf, strlen(lbuf)); + } + if(j < ow) { + if(j > 1) + lbuf[j - 1] = '.'; + if(j > 2) + lbuf[j - 2] = '.'; + if(j > 3) + lbuf[j - 3] = '.'; + } - - XmbDrawImageString(dzen.dpy, pm, dzen.font.set, - dzen.tgc, px, py, lbuf, tw); - px += tw; + + if(dzen.font.set) + XmbDrawImageString(dzen.dpy, pm, dzen.font.set, + dzen.tgc, px, py, lbuf, tw); + else + XDrawImageString(dzen.dpy, pm, dzen.tgc, px, py, lbuf, tw); + px += tw; + } j=0; t=-1; tval=NULL; - /* get values */ next_pos = get_token(line+i, &t, &tval); i += next_pos; /* ^^ escapes */ - if(next_pos == 0) { - lbuf[j++] = line[i]; - i++; - } + if(next_pos == 0) + lbuf[j++] = line[i++]; } - else { + else lbuf[j++] = line[i]; - } } lbuf[j] = '\0'; - if(t != -1 && tval) { - switch(t) { - case fg: - reverse ? - XSetBackground(dzen.dpy, dzen.tgc, getcolor(tval)) : - XSetForeground(dzen.dpy, dzen.tgc, getcolor(tval)); - break; - case bg: - reverse ? - XSetForeground(dzen.dpy, dzen.tgc, getcolor(tval)) : - XSetBackground(dzen.dpy, dzen.tgc, getcolor(tval)); - break; - } - } - ow = j; - tw = textnw(lbuf, strlen(lbuf)); - while( (tw + px) > (dzen.w - h)) { - lbuf[--j] = '\0'; - tw = textnw(lbuf, strlen(lbuf)); - } - if(j < ow) { - if(j > 1) - lbuf[j - 1] = '.'; - if(j > 2) - lbuf[j - 2] = '.'; - if(j > 3) - lbuf[j - 3] = '.'; + if(nodraw) { + strcat(rbuf, lbuf); } + else { + if(t != -1 && tval) + set_opts(t, tval, reverse); - XmbDrawImageString(dzen.dpy, pm, dzen.font.set, - dzen.tgc, px, py, lbuf, tw); - px += tw; + /* check if text is longer than window's width */ + ow = j; tw = textnw(lbuf, strlen(lbuf)); + while((tw + px) > (dzen.w - h)) { + lbuf[--j] = '\0'; + tw = textnw(lbuf, strlen(lbuf)); + } + if(j < ow) { + if(j > 1) + lbuf[j - 1] = '.'; + if(j > 2) + lbuf[j - 2] = '.'; + if(j > 3) + lbuf[j - 3] = '.'; + } - if(align == ALIGNLEFT) - xorig = h/2; - if(align == ALIGNCENTER) { - xorig = (lnr != -1) ? - (dzen.slave_win.width - px)/2 : - (dzen.title_win.width - px)/2; - } - else if(align == ALIGNRIGHT) { - xorig = (lnr != -1) ? - (dzen.slave_win.width - px - h/2) : - (dzen.title_win.width - px - h/2); - } - if(lnr != -1) { - XCopyArea(dzen.dpy, pm, dzen.slave_win.drawable[lnr], dzen.gc, - 0, 0, px, dzen.line_height, xorig, 0); - } - else { - XCopyArea(dzen.dpy, pm, dzen.title_win.drawable, dzen.gc, - 0, 0, px, dzen.line_height, xorig, 0); + if(dzen.font.set) + XmbDrawImageString(dzen.dpy, pm, dzen.font.set, + dzen.tgc, px, py, lbuf, tw); + else + XDrawImageString(dzen.dpy, pm, dzen.tgc, px, py, lbuf, tw); + px += tw; + + + if(align == ALIGNLEFT) + xorig = h/2; + if(align == ALIGNCENTER) { + xorig = (lnr != -1) ? + (dzen.slave_win.width - px)/2 : + (dzen.title_win.width - px)/2; + } + else if(align == ALIGNRIGHT) { + xorig = (lnr != -1) ? + (dzen.slave_win.width - px - h/2) : + (dzen.title_win.width - px - h/2); + } + + if(lnr != -1) { + XCopyArea(dzen.dpy, pm, dzen.slave_win.drawable[lnr], dzen.gc, + 0, 0, px, dzen.line_height, xorig, 0); + } + else { + XCopyArea(dzen.dpy, pm, dzen.title_win.drawable, dzen.gc, + 0, 0, px, dzen.line_height, xorig, 0); + } + XFreePixmap(dzen.dpy, pm); } - XFreePixmap(dzen.dpy, pm); + + return nodraw ? rbuf : NULL; } void drawheader(const char * text) { + XRectangle r = { dzen.x, dzen.y, dzen.w, dzen.h}; dzen.x = 0; dzen.y = 0; dzen.w = dzen.title_win.width; dzen.h = dzen.line_height; - XRectangle r = { dzen.x, dzen.y, dzen.w, dzen.h}; if(text) { XSetForeground(dzen.dpy, dzen.gc, dzen.norm[ColBG]); XFillRectangles(dzen.dpy, dzen.title_win.drawable, dzen.gc, &r, 1); XSetForeground(dzen.dpy, dzen.gc, dzen.norm[ColFG]); - parse_line(text, -1, dzen.title_win.alignment, 0); + parse_line(text, -1, dzen.title_win.alignment, 0, 0); } XCopyArea(dzen.dpy, dzen.title_win.drawable, dzen.title_win.win, diff --git a/dzen.h b/dzen.h index 3ea113a..911dcab 100644 --- a/dzen.h +++ b/dzen.h @@ -124,6 +124,11 @@ extern void drawtext(const char *text, int reverse, int line, int aligne); +extern char * parse_line(const char * text, + int linenr, + int align, + int reverse, + int nodraw); extern long getcolor(const char *colstr); /* returns color of colstr */ extern void setfont(const char *fontstr); /* sets global font */ extern unsigned int textw(const char *text); /* returns width of text in px */ -- cgit v1.2.3-54-g00ecf