b83ec3159ee5d757986943bad8410b81f3e5448e
[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
21 /* you can edit these */
22 #define MAX_TASK_WIDTH 500
23 #define PADDING 2
24 #define ICON_SIZE ((tb->h)-(2*PADDING))
25
26 static void set_icon_geometry(screen *sc, taskbar *tb, task *tk);
27
28 #define SURF(x) with->surface.x
29 #define TEXT(x) with->texture[0].data.text.x
30 #define USE(x) (with = x)
31 #define SETTEXT(x, y, z) with->texture[0].type = RR_TEXTURE_TEXT; \
32                          with->texture[0].data.text.font = x; \
33                          with->texture[0].data.text.justify = y; \
34                          with->texture[0].data.text.ellipsize = z;
35 #define SETSHADOW(y, z, u, v) with->texture[0].data.text.shadow_offset_x = y; \
36                               with->texture[0].data.text.shadow_offset_y = z; \
37                               with->texture[0].data.text.shadow_alpha = u; \
38                               with->texture[0].data.text.shadow_color = v;
39
40 void gui_init(screen *sc)
41 {
42     XGCValues gcv;
43     RrFont *font;
44     RrAppearance *with;
45     
46     gcv.graphics_exposures = False;
47     sc->fore_gc = XCreateGC(sc->dd, sc->root, GCGraphicsExposures, &gcv);
48
49     /* We don't allow different fonts for various window states... */
50     font = RrFontOpen(sc->rr, "Candara, sans", 10,
51                       RR_FONTWEIGHT_NORMAL, RR_FONTSLANT_NORMAL);
52
53     /* this appearance will be used to draw icons */
54     a_icon = RrAppearanceNew(sc->rr, 1);
55     USE(a_icon);
56     SURF(grad) = RR_SURFACE_PARENTREL;
57     SURF(parentx) = PADDING;
58     SURF(parenty) = PADDING;
59     a_icon->texture[0].type = RR_TEXTURE_RGBA;
60
61     /* this is the appearance for the background of the panel */
62     background = RrAppearanceNew(sc->rr, 0);
63     USE(background);
64     SURF(grad) = RR_SURFACE_DIAGONAL;
65     SURF(primary) = RrColorNew(sc->rr, 170, 170, 190);
66     SURF(secondary) = RrColorNew(sc->rr, 100, 100, 160);
67
68     /* this is the appearance for unfocused tasks,
69      * text needs to be separate appearances so we can align it correctly */
70     unfocused_task = RrAppearanceNew(sc->rr, 0);
71     USE(unfocused_task);
72     SURF(parent) = background;
73     SURF(grad) = RR_SURFACE_PARENTREL;
74     SURF(border) = TRUE;
75     SURF(border_color) = RrColorNew(sc->rr, 0, 0, 80);
76
77     /* ... for iconified tasks, also used for shaded ones currently */
78     iconified_task = RrAppearanceCopy(unfocused_task);
79     USE(iconified_task);
80     SURF(relief) = RR_RELIEF_SUNKEN;
81     SURF(border) = FALSE;
82     SURF(parent) = background; /* RrAppearanceCopy doesn't copy .parent */
83
84     /* ... for focused tasks */
85     focused_task = RrAppearanceNew(sc->rr, 0);
86     USE(focused_task);
87     SURF(grad) = RR_SURFACE_CROSS_DIAGONAL;
88     SURF(secondary) = RrColorNew(sc->rr, 70, 80, 110);
89     SURF(primary) = RrColorNew(sc->rr, 130, 160, 250);
90     SURF(relief) = RR_RELIEF_RAISED;
91
92     /* this is the text used for all normal unfocused tasks */
93     /* we don't set .parent here, but in draw_task, since we
94      * want to combine _task and _text semirandomly.
95      * XXX plz not when themes are here */
96     normal_text = RrAppearanceNew(sc->rr, 1);
97     USE(normal_text);
98     SURF(grad) = RR_SURFACE_PARENTREL;
99     SETTEXT(font, RR_JUSTIFY_LEFT, RR_ELLIPSIZE_END);
100
101     /* ... for iconified tasks */
102     iconified_text = RrAppearanceCopy(normal_text);
103     /* ... and for focused tasks, we copy this here (ie not 5 lines down)
104      * so the color isn't copied i actually don't know if that would
105      * hurt so XXX on that */
106     focused_text = RrAppearanceCopy(normal_text);
107     USE(focused_text);
108     TEXT(color) = RrColorNew(sc->rr, 230, 230, 255);
109
110     USE(normal_text);
111     TEXT(color) = RrColorNew(sc->rr, 20, 20, 40);
112
113     USE(iconified_text);
114     SETSHADOW(2, 2, 100, RrColorNew(sc->rr, 0, 0, 0));
115     TEXT(color) = RrColorNew(sc->rr, 200, 200, 200);
116
117     shaded_text = RrAppearanceCopy(normal_text);
118     USE(shaded_text);
119     TEXT(color) = RrColorNew(sc->rr, 50, 60, 90);
120 }
121
122 void gui_create_taskbar(screen *sc, taskbar *tb)
123 {
124     XSizeHints size_hints;
125     XSetWindowAttributes att;
126     XClassHint xclhints;
127
128     att.event_mask = ButtonPressMask;
129
130     /* XXX make the height conigurable */
131     tb->x = 0;
132     tb->y = sc->height - 24;
133     tb->w = sc->width;
134     tb->h = 24;
135
136     tb->win = XCreateWindow(/* display  */ sc->dd,
137                             /* parent   */ sc->root,
138                             /* x        */ tb->x,
139                             /* y        */ tb->y,
140 /* XXX Maybe just use scr_width here? */
141                             /* width    */ tb->w,
142                             /* height   */ tb->h,
143                             /* border   */ 0,
144                             /* depth    */ CopyFromParent,
145                             /* class    */ InputOutput,
146                             /* visual   */ CopyFromParent,
147                             /*value mask*/ CWEventMask,
148                             /* attribs  */ &att);
149     tb->bg = None;
150
151     /* reside on ALL desktops */
152     xprop_set_num(sc, tb->win, _NET_WM_DESKTOP, 0xFFFFFFFF);
153     xprop_set_atom(sc, tb->win, _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK);
154     xprop_set_atom(sc, tb->win, _NET_WM_STATE, _NET_WM_STATE_BELOW);
155     xprop_set_string(sc, tb->win, WM_NAME, "rspanel");
156
157     /* make sure the WM obays our window position */
158     size_hints.flags = PPosition;
159     /*XSetWMNormalHints (sc->dd, tb->win, &size_hints); */
160     XChangeProperty(sc->dd, tb->win, XA_WM_NORMAL_HINTS, XA_WM_SIZE_HINTS, 32,
161                     PropModeReplace, (unsigned char *)&size_hints,
162                     sizeof(XSizeHints) / 4);
163
164     xclhints.res_name = "rspanel";
165     xclhints.res_class = "RSPanel";
166     XSetClassHint(sc->dd, tb->win, &xclhints);
167
168     XMapWindow(sc->dd, tb->win);
169 }
170
171 void gui_draw_task(screen *sc, taskbar *tb, task *tk, int redraw)
172 {
173     RrAppearance *a;
174     RrAppearance *b;
175     const icon *i;
176     int icon_size = 0;
177
178     if (tk->iconified)
179         b = iconified_task;
180     else if (tk->focused)
181         b = focused_task;
182     else if (tk->shaded)
183         b = iconified_task;
184     else
185         b = unfocused_task;
186
187     if (tk->iconified)
188         a = iconified_text;
189     else if (tk->shaded)
190         a = shaded_text;
191     else if (tk->focused)
192         a = focused_text;
193     else
194         a = normal_text;
195
196     i = icon_get_best(tk->icons, tk->nicons, ICON_SIZE, ICON_SIZE);
197
198     if (i) {
199         RrTextureRGBA *d = &a_icon->texture[0].data.rgba;
200         a_icon->surface.parent = b;
201         d->width = i->width;
202         d->height = i->height;
203         d->alpha = tk->iconified ? 0x80 : tk->shaded ? 0xB0 : 0xff;
204         d->data = i->data;
205         icon_size = ICON_SIZE + PADDING;
206     }
207
208     a->surface.parent = b;
209     a->surface.parentx = icon_size + PADDING;
210     a->texture[0].data.text.string = tk->name;
211     b->surface.parentx = tk->pos_x;
212
213     RrPaintPixmap(b, tk->width, tb->h);
214     RrPaintPixmap(a, tk->width-(2*PADDING+icon_size), tb->h);
215
216     if (i) {
217         RrPaintPixmap(a_icon, ICON_SIZE, ICON_SIZE);
218         XCopyArea(sc->dd, a_icon->pixmap, b->pixmap, sc->fore_gc, 0, 0,
219                   ICON_SIZE, ICON_SIZE, PADDING, PADDING);
220     }
221
222     XCopyArea(sc->dd, a->pixmap, b->pixmap, sc->fore_gc, 0, 0,
223               tk->width-(2*PADDING+icon_size), tb->h, icon_size + PADDING, 0);
224     XCopyArea(sc->dd, b->pixmap, tb->bg, sc->fore_gc, 0, 0,
225               tk->width, tb->h, tk->pos_x, 0);
226
227     XFreePixmap(sc->dd, a->pixmap);
228     XFreePixmap(sc->dd, b->pixmap);
229     XFreePixmap(sc->dd, a_icon->pixmap);
230     if (redraw) {
231         XSetWindowBackgroundPixmap(sc->dd, tb->win, tb->bg);
232         XClearWindow(sc->dd, tb->win);
233     }
234 }
235
236 void gui_draw_taskbar(screen *sc, taskbar *tb)
237 {
238     task *tk;
239     int x, width, taskw;
240     int num_tasks = 0;
241
242 #ifdef PAGER
243     pager_draw();
244 #else
245     pager_size = TEXTPAD;
246 #endif
247
248     x = pager_size + 2;
249     width = tb->w - (pager_size + GRILL_WIDTH);
250 #warning only rerender if width changed!
251     if (tb->bg) XFreePixmap(sc->dd, tb->bg);
252     tb->bg = XCreatePixmap(sc->dd, sc->root, tb->w, tb->h, RrDepth(sc->rr));
253
254     XFreePixmap(sc->dd, RrPaintPixmap(background, tb->w, tb->h));
255     XCopyArea(sc->dd, background->pixmap, tb->bg, sc->fore_gc, 0, 0,
256               tb->w, tb->h, 0, 0);
257
258     /* find the number of visible tasks */
259     for (tk = tb->task_list; tk; tk = tk->next) {
260         if (!task_shown(tk))
261             continue;
262         num_tasks++;
263     }
264
265     if (num_tasks == 0)
266         goto clear;
267
268     taskw = width / num_tasks;
269     if (taskw > MAX_TASK_WIDTH)
270         taskw = MAX_TASK_WIDTH;
271
272     for (tk = tb->task_list; tk; tk = tk->next) {
273         if (!task_shown(tk))
274             continue;
275         tk->pos_x = x;
276         tk->width = taskw - 1;
277         gui_draw_task(sc, tb, tk, FALSE);
278         set_icon_geometry(sc, tb, tk);
279         x += taskw;
280     }
281
282 clear:
283     XSetWindowBackgroundPixmap(sc->dd, tb->win, tb->bg);
284     XClearWindow(sc->dd, tb->win);
285 }
286
287 void gui_move_taskbar(screen *sc, taskbar *tb)
288 {
289     tb->x = tb->y = 0;
290
291     if (tb->hidden)
292         tb->x = TEXTPAD - tb->w;
293
294     if (!tb->at_top)
295         tb->y = sc->height - tb->h;
296
297     XMoveWindow(sc->dd, tb->win, tb->x, tb->y);
298 }
299
300 static void set_icon_geometry(screen *sc, taskbar *tb, task *tk)
301 {
302     long coords[4];
303
304     coords[0] = tb->x + tk->pos_x;
305     coords[1] = tb->y;
306     coords[2] = MAX(tk->width, 1);
307     coords[3] = tb->h;
308
309     xprop_set_array(sc, tk->win, _NET_WM_ICON_GEOMETRY, coords, 4);
310 }