Xinqi Bao's Git

continued with draw.c abstraction, also started util.{h,c} implementation, that will...
[dwm.git] / draw.c
1 /* See LICENSE file for copyright and license details. */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <X11/Xlib.h>
5
6 #include "draw.h"
7 #include "util.h"
8
9 Draw *
10 draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) {
11 Draw *draw = (Draw *)calloc(1, sizeof(Draw));
12 draw->dpy = dpy;
13 draw->screen = screen;
14 draw->win = win;
15 draw->w = w;
16 draw->h = h;
17 draw->drawable = XCreatePixmap(dpy, win, w, h, DefaultDepth(dpy, screen));
18 draw->gc = XCreateGC(dpy, win, 0, NULL);
19 XSetLineAttributes(dpy, draw->gc, 1, LineSolid, CapButt, JoinMiter);
20 return draw;
21 }
22
23 void
24 draw_resize(Draw *draw, unsigned int w, unsigned int h) {
25 if(!draw)
26 return;
27 draw->w = w;
28 draw->h = h;
29 XFreePixmap(draw->dpy, draw->drawable);
30 draw->drawable = XCreatePixmap(draw->dpy, draw->win, w, h, DefaultDepth(draw->dpy, draw->screen));
31 }
32
33 void
34 draw_free(Draw *draw) {
35 XFreePixmap(draw->dpy, draw->drawable);
36 XFreeGC(draw->dpy, draw->gc);
37 free(draw);
38 }
39
40 Fnt *
41 draw_font_create(Draw *draw, const char *fontname) {
42 Fnt *font;
43 char *def, **missing;
44 int n;
45
46 if(!draw)
47 return NULL;
48 font = (Fnt *)calloc(1, sizeof(Fnt));
49 font->set = XCreateFontSet(draw->dpy, fontname, &missing, &n, &def);
50 if(missing) {
51 while(n--)
52 fprintf(stderr, "draw: missing fontset: %s\n", missing[n]);
53 XFreeStringList(missing);
54 }
55 if(font->set) {
56 XFontStruct **xfonts;
57 char **font_names;
58
59 XExtentsOfFontSet(font->set);
60 n = XFontsOfFontSet(font->set, &xfonts, &font_names);
61 while(n--) {
62 font->ascent = MAX(font->ascent, (*xfonts)->ascent);
63 font->descent = MAX(font->descent,(*xfonts)->descent);
64 xfonts++;
65 }
66 }
67 else {
68 if(!(font->xfont = XLoadQueryFont(draw->dpy, fontname))
69 && !(font->xfont = XLoadQueryFont(draw->dpy, "fixed")))
70 die("error, cannot load font: '%s'\n", fontname);
71 font->ascent = font->xfont->ascent;
72 font->descent = font->xfont->descent;
73 }
74 font->h = font->ascent + font->descent;
75 return font;
76 }
77
78 void
79 draw_font_free(Draw *draw, Fnt *font) {
80 if(!draw || !font)
81 return;
82 if(font->set)
83 XFreeFontSet(draw->dpy, font->set);
84 else
85 XFreeFont(draw->dpy, font->xfont);
86 free(font);
87 }
88
89 Col *
90 draw_col_create(Draw *draw, const char *colname) {
91 Col *col = (Col *)calloc(1, sizeof(Col));
92 Colormap cmap = DefaultColormap(draw->dpy, draw->screen);
93 XColor color;
94
95 if(!XAllocNamedColor(draw->dpy, cmap, colname, &color, &color))
96 die("error, cannot allocate color '%s'\n", colname);
97 col->rgb = color.pixel;
98 return col;
99 }
100
101 void
102 draw_col_free(Draw *draw, Col *col) {
103 if(!col)
104 return;
105 free(col);
106 }
107
108 void
109 draw_setfont(Draw *draw, Fnt *font) {
110 if(!draw || !font)
111 return;
112 draw->font = font;
113 }
114
115 void
116 draw_setfg(Draw *draw, Col *col) {
117 if(!draw || !col)
118 return;
119 draw->fg = col;
120 }
121
122 void
123 draw_setbg(Draw *draw, Col *col) {
124 if(!draw || !col)
125 return;
126 draw->bg = col;
127 }
128
129 void
130 draw_rect(Draw *draw, int x, int y, unsigned int w, unsigned int h) {
131 if(!draw)
132 return;
133 /* TODO: draw the rectangle */
134 }
135
136 void
137 draw_text(Draw *draw, int x, int y, const char *text) {
138 if(!draw)
139 return;
140 /* TODO: draw the text */
141 }
142
143 void
144 draw_map(Draw *draw, int x, int y, unsigned int w, unsigned int h) {
145 if(!draw)
146 return;
147 /* TODO: map the draw contents in the region */
148 }
149
150 void
151 draw_getextents(Draw *draw, const char *text, TextExtents *extents) {
152 if(!draw || !extents)
153 return;
154 /* TODO: get extents */
155 }