aboutsummaryrefslogtreecommitdiffstats
path: root/draw.c
diff options
context:
space:
mode:
authorgotmor <gotmor@f2baff5b-bf2c-0410-a398-912abdc3d8b2>2007-07-19 12:50:58 +0000
committergotmor <gotmor@f2baff5b-bf2c-0410-a398-912abdc3d8b2>2007-07-19 12:50:58 +0000
commite4978330dc81571a625be90d0b88a60f63978f95 (patch)
tree6f116cc6fb53ea4f865ae28bc1f96de9b1ce5196 /draw.c
parent8a03a06c6555f2e754eeb0f814f0a6b1944d9dfc (diff)
downloaddzen-e4978330dc81571a625be90d0b88a60f63978f95.tar.gz
dzen-e4978330dc81571a625be90d0b88a60f63978f95.zip
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
Diffstat (limited to 'draw.c')
-rw-r--r--draw.c286
1 files changed, 157 insertions, 129 deletions
diff --git a/draw.c b/draw.c
index 547fab7..f9c2517 100644
--- a/draw.c
+++ b/draw.c
@@ -9,12 +9,13 @@
#include <stdio.h>
#include <string.h>
-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,