1 /* Skippy - Seduces Kids Into Perversion
3 * Copyright (C) 2004 Hyriand <hyriand@thegraveyard.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 tooltip_destroy(Tooltip *tt)
28 XftFontClose(tt->mainwin->dpy, tt->font);
30 XftDrawDestroy(tt->draw);
31 if(tt->color.pixel != None)
32 XftColorFree(tt->mainwin->dpy,
34 tt->mainwin->colormap,
36 if(tt->background.pixel != None)
37 XftColorFree(tt->mainwin->dpy,
39 tt->mainwin->colormap,
41 if(tt->border.pixel != None)
42 XftColorFree(tt->mainwin->dpy,
44 tt->mainwin->colormap,
46 if(tt->shadow.pixel != None)
47 XftColorFree(tt->mainwin->dpy,
49 tt->mainwin->colormap,
51 if(tt->window != None)
52 XDestroyWindow(tt->mainwin->dpy, tt->window);
58 tooltip_create(MainWin *mw, dlist *config)
61 XSetWindowAttributes attr;
65 tt = (Tooltip *)malloc(sizeof(Tooltip));
74 tt->color.pixel = tt->background.pixel = tt->border.pixel = tt->shadow.pixel = None;
76 attr.override_redirect = True;
77 attr.border_pixel = None;
78 attr.background_pixel = None;
79 attr.event_mask = ExposureMask;
80 attr.colormap = mw->colormap;
82 tt->window = XCreateWindow(mw->dpy, mw->root,
84 mw->depth, InputOutput, mw->visual,
85 CWBorderPixel|CWBackPixel|CWOverrideRedirect|CWEventMask|CWColormap,
87 if(tt->window == None)
89 fprintf(stderr, "WARNING: Couldn't create tooltip window.\n");
94 tmp = config_get(config, "tooltip", "border", "#e0e0e0");
95 if(! XftColorAllocName(mw->dpy, mw->visual, mw->colormap, tmp, &tt->border))
97 fprintf(stderr, "WARNING: Invalid color '%s'.\n", tmp);
102 tmp = config_get(config, "tooltip", "background", "#404040");
103 if(! XftColorAllocName(mw->dpy, mw->visual, mw->colormap, tmp, &tt->background))
105 fprintf(stderr, "WARNING: Invalid color '%s'.\n", tmp);
110 tmp = config_get(config, "tooltip", "opacity", "128");
111 tmp_l = MIN(MAX(0, strtol(tmp, 0, 0) * 256), 65535);
112 tt->background.color.alpha = tmp_l;
113 tt->border.color.alpha = tmp_l;
115 tmp = config_get(config, "tooltip", "text", "#e0e0e0");
116 if(! XftColorAllocName(mw->dpy, mw->visual, mw->colormap, tmp, &tt->color))
118 fprintf(stderr, "WARNING: Couldn't allocate color '%s'.\n", tmp);
123 tmp = config_get(config, "tooltip", "textShadow", "black");
124 if(strcasecmp(tmp, "none") != 0)
126 if(! XftColorAllocName(mw->dpy, mw->visual, mw->colormap, tmp, &tt->shadow))
128 fprintf(stderr, "WARNING: Couldn't allocate color '%s'.\n", tmp);
134 tt->draw = XftDrawCreate(mw->dpy, tt->window, mw->visual, mw->colormap);
137 fprintf(stderr, "WARNING: Couldn't create Xft draw surface.\n");
142 tt->font = XftFontOpenName(mw->dpy, mw->screen, config_get(config, "tooltip", "font", "fixed-11:weight=bold"));
145 fprintf(stderr, "WARNING: Couldn't open Xft font.\n");
150 tt->font_height = tt->font->ascent + tt->font->descent;
156 tooltip_map(Tooltip *tt, int x, int y, const FcChar8 *text, int len)
158 XUnmapWindow(tt->mainwin->dpy, tt->window);
160 XftTextExtents8(tt->mainwin->dpy, tt->font, text, len, &tt->extents);
162 tt->width = tt->extents.width + 8;
163 tt->height = tt->font_height + 5 + (tt->shadow.pixel ? 2 : 0);
164 XResizeWindow(tt->mainwin->dpy, tt->window, tt->width, tt->height);
165 tooltip_move(tt, x, y);
170 tt->text = (FcChar8 *)malloc(len);
171 memcpy(tt->text, text, len);
175 XMapWindow(tt->mainwin->dpy, tt->window);
176 XRaiseWindow(tt->mainwin->dpy, tt->window);
180 tooltip_move(Tooltip *tt, int x, int y)
182 if(x + tt->extents.width + 9 > tt->mainwin->x + tt->mainwin->width)
183 x = tt->mainwin->x + tt->mainwin->width - tt->extents.width - 9;
186 if(y + tt->extents.height + 8 > tt->mainwin->y + tt->mainwin->height)
187 y = tt->mainwin->height + tt->mainwin->y - tt->extents.height - 8;
190 XMoveWindow(tt->mainwin->dpy, tt->window, x, y);
194 tooltip_unmap(Tooltip *tt)
196 XUnmapWindow(tt->mainwin->dpy, tt->window);
204 tooltip_handle(Tooltip *tt, XEvent *ev)
209 if(ev->type == Expose && ev->xexpose.count == 0)
211 XftDrawRect(tt->draw, &tt->border, 0, 0, tt->width, 1);
212 XftDrawRect(tt->draw, &tt->border, 0, 1, 1, tt->height - 2);
213 XftDrawRect(tt->draw, &tt->border, 0, tt->height - 1, tt->width, 1);
214 XftDrawRect(tt->draw, &tt->border, tt->width - 1, 1, 1, tt->height - 2);
215 XftDrawRect(tt->draw, &tt->background, 1, 1, tt->width - 2, tt->height - 2);
217 XftDrawString8(tt->draw, &tt->shadow, tt->font, 6, 3 + tt->extents.y + (tt->font_height - tt->extents.y) / 2, tt->text, tt->text_len);
218 XftDrawString8(tt->draw, &tt->color, tt->font, 4, 1 + tt->extents.y + (tt->font_height - tt->extents.y) / 2, tt->text, tt->text_len);