]> icculus.org git repositories - mikachu/rspanel.git/blob - gui.c
better geometry calculations and stuff
[mikachu/rspanel.git] / gui.c
1 #include "gui.h"
2 #include "dims.h"
3 #include "xprop.h"
4 #include "icon.h"
5 #include "rspanel.h"
6
7 #include <openbox/theme.h>
8
9 static int pager_size;
10
11 static RrAppearance *background;
12 static RrAppearance *focused_task;
13 static RrAppearance *iconified_task;
14 static RrAppearance *unfocused_task;
15 static RrAppearance *normal_text;
16 static RrAppearance *iconified_text;
17 static RrAppearance *shaded_text;
18 static RrAppearance *focused_text;
19 static RrAppearance *a_icon;
20 static RrColor      *normal_text_color;
21 static RrColor      *focused_text_color;
22
23 /* you can edit these */
24 #define MAX_TASK_WIDTH 500
25 #define HEIGHT (24 + sc->theme->obwidth)
26 #define PADDING 2
27 #define ICON_SIZE (HEIGHT - sc->theme->obwidth - PADDING*2)
28 #define ICON_SPACE (ICON_SIZE + PADDING*2)
29
30 static void set_icon_geometry(screen *sc, taskbar *tb, task *tk);
31
32 #define SURF(x) with->surface.x
33 #define TEXT(x) with->texture[0].data.text.x
34 #define USE(x) (with = x)
35 #define SETTEXT(x, y, z) with->texture[0].type = RR_TEXTURE_TEXT; \
36                          with->texture[0].data.text.font = x; \
37                          with->texture[0].data.text.justify = y; \
38                          with->texture[0].data.text.ellipsize = z;
39 #define SETSHADOW(y, z, u, v) with->texture[0].data.text.shadow_offset_x = y; \
40                               with->texture[0].data.text.shadow_offset_y = z; \
41                               with->texture[0].data.text.shadow_alpha = u; \
42                               with->texture[0].data.text.shadow_color = v;
43
44 void gui_init(screen *sc)
45 {
46     XGCValues gcv;
47     
48     gcv.graphics_exposures = False;
49     sc->fore_gc = XCreateGC(sc->dd, sc->root, GCGraphicsExposures, &gcv);
50
51     gui_load_theme(sc);
52 }
53
54 void gui_load_theme(screen *sc)
55 {
56     RrFont *font;
57     RrAppearance *with;
58     char *name;
59
60     printf("loading theme\n");
61
62     RrAppearanceFree(background);
63     RrAppearanceFree(focused_task);
64     RrAppearanceFree(iconified_task);
65     RrAppearanceFree(unfocused_task);
66     RrAppearanceFree(normal_text);
67     RrAppearanceFree(iconified_text);
68     RrAppearanceFree(shaded_text);
69     RrAppearanceFree(focused_text);
70     RrAppearanceFree(a_icon);
71     RrColorFree(normal_text_color);
72     RrColorFree(focused_text_color);
73
74     name = xprop_get_utf8(sc, sc->root, _OB_THEME);
75     RrThemeFree(sc->theme);
76     sc->theme = RrThemeNew(sc->rr, name, TRUE, NULL, NULL, NULL, NULL, NULL);
77
78     /* We don't allow different fonts for various window states... */
79     font = RrFontOpen(sc->rr, "Candara, sans", 10,
80                       RR_FONTWEIGHT_NORMAL, RR_FONTSLANT_NORMAL);
81
82     /* this appearance will be used to draw icons */
83     a_icon = RrAppearanceNew(sc->rr, 1);
84     USE(a_icon);
85     SURF(grad) = RR_SURFACE_PARENTREL;
86     SURF(parentx) = PADDING;
87     SURF(parenty) = PADDING;
88     a_icon->texture[0].type = RR_TEXTURE_RGBA;
89
90     /* this is the appearance for the background of the panel */
91     background = RrAppearanceCopy(sc->theme->osd_hilite_bg);
92
93     /* this is the appearance for unfocused tasks,
94      * text needs to be separate appearances so we can align it correctly */
95     unfocused_task = RrAppearanceNew(sc->rr, 0);
96     USE(unfocused_task);
97     SURF(parent) = background;
98     SURF(grad) = RR_SURFACE_PARENTREL;
99
100     /* ... for iconified tasks, also used for shaded ones currently */
101     iconified_task = RrAppearanceCopy(unfocused_task);
102     USE(iconified_task);
103     SURF(parent) = background; /* RrAppearanceCopy doesn't copy .parent */
104
105     /* ... for focused tasks */
106     focused_task = RrAppearanceCopy(sc->theme->a_hover_focused_max);
107     USE(focused_task);
108     SURF(parent) = background;
109
110     /* this is the text used for all normal unfocused tasks */
111     /* we don't set .parent here, but in draw_task, since we
112      * want to combine _task and _text semirandomly.
113      * XXX plz not when themes are here */
114     normal_text = RrAppearanceCopy(sc->theme->osd_hilite_label);
115     normal_text_color = RrColorNew(sc->rr,
116                                    RrColorRed(sc->theme->osd_color),
117                                    RrColorGreen(sc->theme->osd_color),
118                                    RrColorBlue(sc->theme->osd_color));
119     USE(normal_text);
120     TEXT(color) = normal_text_color;
121     TEXT(ellipsize) = RR_ELLIPSIZE_END;
122
123     /* ... for iconified tasks */
124     iconified_text = RrAppearanceCopy(normal_text);
125     shaded_text = RrAppearanceCopy(normal_text);
126
127     focused_text = RrAppearanceCopy(normal_text);
128     focused_text_color =
129         RrColorNew(sc->rr,
130                    RrColorRed(sc->theme->titlebut_hover_focused_color),
131                    RrColorGreen(sc->theme->titlebut_hover_focused_color),
132                    RrColorBlue(sc->theme->titlebut_hover_focused_color));
133     USE(focused_text);
134     TEXT(color) = focused_text_color;
135     TEXT(ellipsize) = RR_ELLIPSIZE_END;
136
137     XFree(name);
138 }
139
140 void gui_create_taskbar(screen *sc, taskbar *tb)
141 {
142     XSizeHints size_hints;
143     XSetWindowAttributes att;
144     XClassHint xclhints;
145
146     att.event_mask = ButtonPressMask;
147
148     /* XXX make this stuff conigurable */
149     tb->w = sc->width * 0.7;
150     tb->h = HEIGHT;
151
152     tb->x = (sc->width - tb->w) / 2;
153     tb->y = sc->height - tb->h;
154
155     tb->win = XCreateWindow(/* display  */ sc->dd,
156                             /* parent   */ sc->root,
157                             /* x        */ tb->x,
158                             /* y        */ tb->y,
159                             /* width    */ tb->w,
160                             /* height   */ tb->h,
161                             /* border   */ 0,
162                             /* depth    */ CopyFromParent,
163                             /* class    */ InputOutput,
164                             /* visual   */ CopyFromParent,
165                             /*value mask*/ CWEventMask,
166                             /* attribs  */ &att);
167     tb->bgwin = XCreateWindow(/* display  */ sc->dd,
168                               /* parent   */ tb->win,
169                               /* x        */ 0,
170                               /* y        */ 0,
171                               /* width    */ tb->w,
172                               /* height   */ tb->h,
173                               /* border   */ 0,
174                               /* depth    */ CopyFromParent,
175                               /* class    */ InputOutput,
176                               /* visual   */ CopyFromParent,
177                               /*value mask*/ 0,
178                               /* attribs  */ 0);
179     tb->bg = None;
180
181     /* reside on ALL desktops */
182     xprop_set_num(sc, tb->win, _NET_WM_DESKTOP, 0xFFFFFFFF);
183     xprop_set_atom(sc, tb->win, _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK);
184     xprop_set_atom(sc, tb->win, _NET_WM_STATE, _NET_WM_STATE_BELOW);
185     xprop_set_string(sc, tb->win, WM_NAME, "rspanel");
186
187     /* make sure the WM obays our window position */
188     size_hints.flags = PPosition;
189     size_hints.x = tb->x;
190     size_hints.y = tb->y;
191     /*XSetWMNormalHints (sc->dd, tb->win, &size_hints); */
192     XChangeProperty(sc->dd, tb->win, XA_WM_NORMAL_HINTS, XA_WM_SIZE_HINTS, 32,
193                     PropModeReplace, (unsigned char *)&size_hints,
194                     sizeof(XSizeHints) / 4);
195
196     xclhints.res_name = "rspanel";
197     xclhints.res_class = "RSPanel";
198     XSetClassHint(sc->dd, tb->win, &xclhints);
199
200     XMapWindow(sc->dd, tb->win);
201     XMapWindow(sc->dd, tb->bgwin);
202 }
203
204 void gui_draw_task(screen *sc, taskbar *tb, task *tk, int redraw)
205 {
206     RrAppearance *a;
207     RrAppearance *b;
208     const icon *i;
209     int icon_space = 0;
210     char *name;
211
212     if (tk->iconified)
213         b = iconified_task;
214     else if (tk->focused)
215         b = focused_task;
216     else if (tk->shaded)
217         b = iconified_task;
218     else
219         b = unfocused_task;
220
221     if (tk->iconified)
222         a = iconified_text;
223     else if (tk->shaded)
224         a = shaded_text;
225     else if (tk->focused)
226         a = focused_text;
227     else
228         a = normal_text;
229
230     if (tk->iconified)
231         name = g_strconcat("(", tk->name, ")", NULL);
232     else
233         name = g_strdup(tk->name);
234
235     i = icon_get_best(tk->icons, tk->nicons, ICON_SIZE, ICON_SIZE);
236
237     if (i) {
238         RrTextureRGBA *d = &a_icon->texture[0].data.rgba;
239         icon_space = ICON_SPACE;
240         a_icon->surface.parent = b;
241         a_icon->surface.parentx = (icon_space - ICON_SIZE) / 2;
242         a_icon->surface.parenty = (tb->drawh - ICON_SIZE) / 2;
243         d->width = i->width;
244         d->height = i->height;
245         d->alpha = tk->iconified ? 0x70 : tk->shaded ? 0xB0 : 0xff;
246         d->data = i->data;
247     }
248
249     a->surface.parent = b;
250     a->surface.parentx = icon_space;
251     a->texture[0].data.text.string = name;
252     b->surface.parentx = tk->pos_x;
253
254     RrPaintPixmap(b, tk->width, tb->drawh);
255     RrPaintPixmap(a, tk->width-icon_space, tb->drawh);
256
257     if (i) {
258         RrPaintPixmap(a_icon, ICON_SIZE, ICON_SIZE);
259         XCopyArea(sc->dd, a_icon->pixmap, b->pixmap, sc->fore_gc, 0, 0,
260                   ICON_SIZE, ICON_SIZE,
261                   (icon_space - ICON_SIZE) / 2,
262                   (tb->drawh - ICON_SIZE) / 2);
263     }
264
265     XCopyArea(sc->dd, a->pixmap, b->pixmap, sc->fore_gc, 0, 0,
266               tk->width-icon_space, tb->drawh,
267               icon_space, 0);
268     XCopyArea(sc->dd, b->pixmap, tb->bg, sc->fore_gc, 0, 0,
269               tk->width, tb->drawh, tk->pos_x, 0);
270
271     XFreePixmap(sc->dd, a->pixmap);
272     XFreePixmap(sc->dd, b->pixmap);
273     XFreePixmap(sc->dd, a_icon->pixmap);
274     if (redraw) {
275         XSetWindowBackgroundPixmap(sc->dd, tb->bgwin, tb->bg);
276         XClearWindow(sc->dd, tb->bgwin);
277     }
278
279     g_free(name);
280 }
281
282 void gui_draw_taskbar(screen *sc, taskbar *tb)
283 {
284     task *tk;
285     int x, width, taskw;
286     int num_tasks = 0;
287
288 #ifdef PAGER
289     pager_draw();
290 #else
291     pager_size = TEXTPAD;
292 #endif
293
294     tb->draww = tb->w - sc->theme->obwidth * 2;
295     tb->drawh = tb->h - sc->theme->obwidth;
296     XMoveResizeWindow(sc->dd, tb->bgwin, 0,
297                       (tb->at_top ? -sc->theme->obwidth : 0),
298                       tb->draww, tb->drawh);
299
300     x = pager_size + 2;
301     width = tb->draww - (pager_size + GRILL_WIDTH);
302 #warning only rerender if width changed!
303     if (tb->bg) XFreePixmap(sc->dd, tb->bg);
304     tb->bg = XCreatePixmap(sc->dd, sc->root, tb->draww, tb->drawh,
305                            RrDepth(sc->rr));
306
307     XFreePixmap(sc->dd, RrPaintPixmap(background, tb->draww, tb->drawh));
308     XCopyArea(sc->dd, background->pixmap, tb->bg, sc->fore_gc, 0, 0,
309               tb->draww, tb->drawh, 0, 0);
310
311     /* find the number of visible tasks */
312     for (tk = tb->task_list; tk; tk = tk->next) {
313         if (!task_shown(tk))
314             continue;
315         num_tasks++;
316     }
317
318     if (num_tasks == 0)
319         goto clear;
320
321     taskw = width / num_tasks;
322     if (taskw > MAX_TASK_WIDTH)
323         taskw = MAX_TASK_WIDTH;
324
325     for (tk = tb->task_list; tk; tk = tk->next) {
326         if (!task_shown(tk))
327             continue;
328         tk->pos_x = x;
329         tk->width = taskw - 1;
330         gui_draw_task(sc, tb, tk, FALSE);
331         set_icon_geometry(sc, tb, tk);
332         x += taskw;
333     }
334
335 clear:
336     XSetWindowBackgroundPixmap(sc->dd, tb->bgwin, tb->bg);
337     XClearWindow(sc->dd, tb->bgwin);
338     XSetWindowBorderWidth(sc->dd, tb->bgwin, sc->theme->obwidth);
339     XSetWindowBorder(sc->dd, tb->bgwin,
340                      RrColorPixel(sc->theme->osd_border_color));
341 }
342
343 void gui_move_taskbar(screen *sc, taskbar *tb)
344 {
345     tb->x = tb->y = 0;
346
347     if (tb->hidden)
348         tb->x = TEXTPAD - tb->w;
349
350     if (!tb->at_top)
351         tb->y = sc->height - tb->h;
352
353     XMoveWindow(sc->dd, tb->win, tb->x, tb->y);
354
355     gui_draw_taskbar(sc, tb);
356 }
357
358 static void set_icon_geometry(screen *sc, taskbar *tb, task *tk)
359 {
360     long coords[4];
361
362     coords[0] = tb->x + tk->pos_x;
363     coords[1] = tb->y;
364     coords[2] = MAX(tk->width, 1);
365     coords[3] = tb->h;
366
367     xprop_set_array(sc, tk->win, _NET_WM_ICON_GEOMETRY, coords, 4);
368 }