factorize mouse button press event handling for status bar
[awesome.git] / draw.c
blob450bc5891808c26394ae5ff719fd3212a7305105
1 /*
2 * draw.c - draw functions
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <cairo.h>
23 #include <cairo-ft.h>
24 #include <cairo-xlib.h>
25 #include <math.h>
26 #include "layout.h"
27 #include "util.h"
28 #include "draw.h"
30 void
31 drawtext(Display *disp, int screen, int x, int y, int w, int h, Drawable drawable, int dw, int dh, XftFont *font, const char *text, XColor color[ColLast])
33 int nw = 0;
34 static char buf[256];
35 size_t len, olen;
36 cairo_font_face_t *font_face;
37 cairo_surface_t *surface;
38 cairo_t *cr;
40 drawrectangle(disp, screen, x, y, w, h, drawable, dw, dh, True, color[ColBG]);
41 if(!a_strlen(text))
42 return;
44 surface = cairo_xlib_surface_create(disp, drawable, DefaultVisual(disp, screen), dw, dh);
45 cr = cairo_create(surface);
46 font_face = cairo_ft_font_face_create_for_pattern(font->pattern);
47 cairo_set_font_face(cr, font_face);
48 cairo_set_font_size(cr, font->height);
49 cairo_set_source_rgb(cr, color[ColFG].red / 65535.0, color[ColFG].green / 65535.0, color[ColFG].blue / 65535.0);
51 olen = len = a_strlen(text);
52 if(len >= sizeof(buf))
53 len = sizeof(buf) - 1;
54 memcpy(buf, text, len);
55 buf[len] = 0;
56 while(len && (nw = textwidth(disp, font, buf)) > w)
57 buf[--len] = 0;
58 if(nw > w)
59 return; /* too long */
60 if(len < olen)
62 if(len > 1)
63 buf[len - 1] = '.';
64 if(len > 2)
65 buf[len - 2] = '.';
66 if(len > 3)
67 buf[len - 3] = '.';
70 cairo_move_to(cr, x + font->height / 2, y + font->ascent + (dh - font->height) / 2);
71 cairo_show_text(cr, buf);
73 cairo_font_face_destroy(font_face);
74 cairo_destroy(cr);
75 cairo_surface_destroy(surface);
78 void
79 drawrectangle(Display *disp, int screen, int x, int y, int w, int h, Drawable drawable, int dw, int dh, Bool filled, XColor color)
81 cairo_surface_t *surface;
82 cairo_t *cr;
84 surface = cairo_xlib_surface_create(disp, drawable, DefaultVisual(disp, screen), dw, dh);
85 cr = cairo_create (surface);
87 cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
88 cairo_set_line_width(cr, 1.0);
89 cairo_set_source_rgb(cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
90 if(filled)
92 cairo_rectangle(cr, x, y, w + 1, h + 1);
93 cairo_fill(cr);
95 else
96 cairo_rectangle(cr, x + 1, y, w, h);
98 cairo_stroke(cr);
100 cairo_destroy(cr);
101 cairo_surface_destroy(surface);
104 void
105 drawcircle(Display *disp, int screen, int x, int y, int r, Drawable drawable, int dw, int dh, Bool filled, XColor color)
107 cairo_surface_t *surface;
108 cairo_t *cr;
110 surface = cairo_xlib_surface_create(disp, drawable, DefaultVisual(disp, screen), dw, dh);
111 cr = cairo_create (surface);
112 cairo_set_line_width(cr, 1.0);
113 cairo_set_source_rgb(cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
114 if(filled)
116 cairo_arc (cr, x + r, y + r, r, 0, 2 * M_PI);
117 cairo_fill(cr);
119 else
120 cairo_arc (cr, x + r, y + r, r - 1, 0, 2 * M_PI);
122 cairo_stroke(cr);
124 cairo_destroy(cr);
125 cairo_surface_destroy(surface);
128 unsigned short
129 textwidth(Display *disp, XftFont *font, char *text)
131 cairo_surface_t *surface;
132 cairo_t *cr;
133 cairo_font_face_t *font_face;
134 cairo_text_extents_t te;
136 surface = cairo_xlib_surface_create(disp, DefaultScreen(disp),
137 DefaultVisual(disp, DefaultScreen(disp)),
138 DisplayWidth(disp, DefaultScreen(disp)),
139 DisplayHeight(disp, DefaultScreen(disp)));
140 cr = cairo_create(surface);
141 font_face = cairo_ft_font_face_create_for_pattern(font->pattern);
142 cairo_set_font_face(cr, font_face);
143 cairo_set_font_size(cr, font->height);
144 cairo_text_extents(cr, text, &te);
145 cairo_destroy(cr);
146 cairo_surface_destroy(surface);
147 cairo_font_face_destroy(font_face);
149 return MAX(te.x_advance, te.width) + font->height;
152 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99