changeset: 3:b3aa07ccca94
tag: tip
user: Connor Lane Smith <cls_AT_lubutu.com>
date: Fri Jul 02 05:50:18 2010 +0100
files: Makefile commitdraw.c draw.h drawcursor.c drawsquare.c drawtext.c setupdraw.c
description:
extended, simpler invert
diff -r 11c543acbeac -r b3aa07ccca94 Makefile
--- a/Makefile Fri Jul 02 03:44:03 2010 +0100
+++ b/Makefile Fri Jul 02 05:50:18 2010 +0100
@@ -3,8 +3,8 @@
include config.mk
-SRC = cleanupdraw.c drawsquare.c drawtext.c eprint.c getcolor.c initfont.c \
- setupdraw.c textnw.c textw.c
+SRC = cleanupdraw.c commitdraw.c drawcursor.c drawsquare.c drawtext.c eprint.c \
+ getcolor.c initfont.c setupdraw.c textnw.c textw.c
OBJ = ${SRC:.c=.o}
all: options libdraw.a
@@ -22,6 +22,7 @@
libdraw.a: ${OBJ}
@echo AR $@
+ @rm -f $@
@ar cr $@ $+
clean:
diff -r 11c543acbeac -r b3aa07ccca94 commitdraw.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/commitdraw.c Fri Jul 02 05:50:18 2010 +0100
@@ -0,0 +1,12 @@
+#include <X11/Xlib.h>
+#include <draw.h>
+
+void
+commitdraw(DC *dc, Window w)
+{
+ XWindowAttributes wa;
+
+ if(!XGetWindowAttributes(dc->dpy, w, &wa))
+ eprint("cannot get window attributes\n");
+ XCopyArea(dc->dpy, dc->drawable, w, dc->gc, 0, 0, wa.width, wa.height, 0, 0);
+}
diff -r 11c543acbeac -r b3aa07ccca94 draw.h
--- a/draw.h Fri Jul 02 03:44:03 2010 +0100
+++ b/draw.h Fri Jul 02 05:50:18 2010 +0100
@@ -4,14 +4,18 @@
#include <X11/Xlib.h>
+#define FG(dc, col) ((col)[(dc)->invert ? ColBG : ColFG])
+#define BG(dc, col) ((col)[(dc)->invert ? ColFG : ColBG])
+
/* enums */
enum { ColBorder, ColFG, ColBG, ColLast };
/* typedefs */
typedef struct {
int x, y, w, h;
+ Bool invert;
+ Display *dpy;
Drawable drawable;
- Display *dpy;
GC gc;
struct {
XFontStruct *xfont;
@@ -24,8 +28,10 @@
/* forward declarations */
void cleanupdraw(DC *dc);
-void drawsquare(DC *dc, Bool filled, unsigned long col[ColLast], Bool invert);
-void drawtext(DC *dc, const char *text, unsigned long col[ColLast], Bool invert);
+void commitdraw(DC *dc, Window w);
+void drawcursor(DC *dc, const char *text, size_t pos, unsigned long col[ColLast]);
+void drawsquare(DC *dc, Bool filled, unsigned long col[ColLast]);
+void drawtext(DC *dc, const char *text, unsigned long col[ColLast]);
void eprint(const char *fmt, ...);
unsigned long getcolor(DC *dc, const char *colstr);
void initfont(DC *dc, const char *fontstr);
diff -r 11c543acbeac -r b3aa07ccca94 drawcursor.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/drawcursor.c Fri Jul 02 05:50:18 2010 +0100
@@ -0,0 +1,11 @@
+#include <X11/Xlib.h>
+#include <draw.h>
+
+void
+drawcursor(DC *dc, const char *text, size_t pos, unsigned long col[ColLast]) {
+ XRectangle r = { dc->x, dc->y + 2, 1, dc->font.height - 2 };
+
+ r.x += textnw(dc, text, pos) + dc->font.height / 2;
+ XSetForeground(dc->dpy, dc->gc, FG(dc, col));
+ XFillRectangles(dc->dpy, dc->drawable, dc->gc, &r, 1);
+}
diff -r 11c543acbeac -r b3aa07ccca94 drawsquare.c
--- a/drawsquare.c Fri Jul 02 03:44:03 2010 +0100
+++ b/drawsquare.c Fri Jul 02 05:50:18 2010 +0100
@@ -3,11 +3,11 @@
#include <draw.h>
void
-drawsquare(DC *dc, Bool filled, unsigned long col[ColLast], Bool invert) {
+drawsquare(DC *dc, Bool filled, unsigned long col[ColLast]) {
int n;
XRectangle r = { dc->x, dc->y, dc->w, dc->h };
- XSetForeground(dc->dpy, dc->gc, col[invert ? ColBG : ColFG]);
+ XSetForeground(dc->dpy, dc->gc, FG(dc, col));
n = ((dc->font.ascent + dc->font.descent + 2) / 4) + (filled ? 1 : 0);
r.width = r.height = n;
r.x = dc->x + 1;
diff -r 11c543acbeac -r b3aa07ccca94 drawtext.c
--- a/drawtext.c Fri Jul 02 03:44:03 2010 +0100
+++ b/drawtext.c Fri Jul 02 05:50:18 2010 +0100
@@ -6,14 +6,14 @@
#define MIN(a, b) ((a) < (b) ? (a) : (b))
void
-drawtext(DC *dc, const char *text, unsigned long col[ColLast], Bool invert) {
+drawtext(DC *dc, const char *text, unsigned long col[ColLast]) {
char buf[256];
int i, x, y, h, len, olen;
XRectangle r = { dc->x, dc->y, dc->w, dc->h };
- XSetForeground(dc->dpy, dc->gc, col[invert ? ColFG : ColBG]);
+ XSetForeground(dc->dpy, dc->gc, BG(dc, col));
XFillRectangles(dc->dpy, dc->drawable, dc->gc, &r, 1);
- if(!text)
+ if(!text || !*text)
return;
olen = strlen(text);
h = dc->font.height;
@@ -26,9 +26,11 @@
memcpy(buf, text, len);
if(len < olen)
for(i = len; i && i > len - 3; buf[--i] = '.');
- XSetForeground(dc->dpy, dc->gc, col[invert ? ColBG : ColFG]);
+ XSetForeground(dc->dpy, dc->gc, FG(dc, col));
if(dc->font.set)
XmbDrawString(dc->dpy, dc->drawable, dc->font.set, dc->gc, x, y, buf, len);
- else
+ else {
+ XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid);
XDrawString(dc->dpy, dc->drawable, dc->gc, x, y, buf, len);
+ }
}
diff -r 11c543acbeac -r b3aa07ccca94 setupdraw.c
--- a/setupdraw.c Fri Jul 02 03:44:03 2010 +0100
+++ b/setupdraw.c Fri Jul 02 05:50:18 2010 +0100
@@ -6,11 +6,11 @@
setupdraw(DC *dc, Window w) {
XWindowAttributes wa;
- XGetWindowAttributes(dc->dpy, w, &wa);
+ if(!XGetWindowAttributes(dc->dpy, w, &wa))
+ eprint("cannot get window attributes");
dc->drawable = XCreatePixmap(dc->dpy, w, wa.width, wa.height,
DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
dc->gc = XCreateGC(dc->dpy, w, 0, NULL);
XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter);
- if(!dc->font.set)
- XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid);
+ dc->invert = False;
}
Received on Fri Jul 02 2010 - 06:50:27 CEST
This archive was generated by hypermail 2.2.0 : Fri Jul 02 2010 - 07:00:08 CEST