4 * Copyright (c) Tuomo Valkonen 1999-2007.
6 * Ion is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
15 #include <ioncore/global.h>
16 #include <ioncore/common.h>
17 #include <ioncore/gr.h>
22 #include <X11/extensions/shape.h>
25 /*{{{ Colour group lookup */
28 static DEColourGroup *destyle_get_colour_group2(DEStyle *style,
29 const GrStyleSpec *a1,
30 const GrStyleSpec *a2)
32 int i, score, maxscore=0;
33 DEColourGroup *maxg=&(style->cgrp);
36 for(i=0; i<style->n_extra_cgrps; i++){
37 score=gr_stylespec_score2(&style->extra_cgrps[i].spec, a1, a2);
40 maxg=&(style->extra_cgrps[i]);
44 style=style->based_on;
51 DEColourGroup *debrush_get_colour_group2(DEBrush *brush,
52 const GrStyleSpec *a1,
53 const GrStyleSpec *a2)
55 return destyle_get_colour_group2(brush->d, a1, a2);
59 DEColourGroup *debrush_get_colour_group(DEBrush *brush, const GrStyleSpec *attr)
61 return destyle_get_colour_group2(brush->d, attr, NULL);
65 DEColourGroup *debrush_get_current_colour_group(DEBrush *brush)
67 return debrush_get_colour_group(brush, debrush_get_current_attr(brush));
77 /* Draw a border at x, y with outer width w x h. Top and left 'tl' pixels
78 * wide with color 'tlc' and bottom and right 'br' pixels with colors 'brc'.
80 static void do_draw_border(Window win, GC gc, int x, int y, int w, int h,
81 uint tl, uint br, DEColour tlc, DEColour brc)
89 XSetForeground(ioncore_g.dpy, gc, tlc);
96 points[0].x=x+i; points[0].y=y+h+1-b;
97 points[1].x=x+i; points[1].y=y+i;
98 points[2].x=x+w+1-a; points[2].y=y+i;
105 XDrawLines(ioncore_g.dpy, win, gc, points, 3, CoordModeOrigin);
109 XSetForeground(ioncore_g.dpy, gc, brc);
115 points[0].x=x+w-i; points[0].y=y+b;
116 points[1].x=x+w-i; points[1].y=y+h-i;
117 points[2].x=x+a; points[2].y=y+h-i;
124 XDrawLines(ioncore_g.dpy, win, gc, points, 3, CoordModeOrigin);
129 static void draw_border(Window win, GC gc, WRectangle *geom,
130 uint tl, uint br, DEColour tlc, DEColour brc)
132 do_draw_border(win, gc, geom->x, geom->y, geom->w, geom->h,
141 void debrush_do_draw_border(DEBrush *brush, WRectangle geom,
144 DEBorder *bd=&(brush->d->border);
145 GC gc=brush->d->normal_gc;
146 Window win=brush->win;
150 draw_border(win, gc, &geom, bd->hl, bd->sh, cg->hl, cg->sh);
151 case DEBORDER_INLAID:
152 draw_border(win, gc, &geom, bd->pad, bd->pad, cg->pad, cg->pad);
153 draw_border(win, gc, &geom, bd->sh, bd->hl, cg->sh, cg->hl);
155 case DEBORDER_GROOVE:
156 draw_border(win, gc, &geom, bd->sh, bd->hl, cg->sh, cg->hl);
157 draw_border(win, gc, &geom, bd->pad, bd->pad, cg->pad, cg->pad);
158 draw_border(win, gc, &geom, bd->hl, bd->sh, cg->hl, cg->sh);
160 case DEBORDER_ELEVATED:
162 draw_border(win, gc, &geom, bd->hl, bd->sh, cg->hl, cg->sh);
163 draw_border(win, gc, &geom, bd->pad, bd->pad, cg->pad, cg->pad);
171 void debrush_draw_border(DEBrush *brush,
172 const WRectangle *geom)
174 DEColourGroup *cg=debrush_get_current_colour_group(brush);
176 debrush_do_draw_border(brush, *geom, cg);
180 static void draw_borderline(Window win, GC gc, WRectangle *geom,
181 uint tl, uint br, DEColour tlc, DEColour brc,
184 if(line==GR_BORDERLINE_LEFT && geom->h>0){
185 XSetForeground(ioncore_g.dpy, gc, tlc);
186 XDrawRectangle(ioncore_g.dpy, win, gc, geom->x, geom->y, tl, geom->h);
188 }else if(line==GR_BORDERLINE_TOP && geom->w>0){
189 XSetForeground(ioncore_g.dpy, gc, tlc);
190 XDrawRectangle(ioncore_g.dpy, win, gc, geom->x, geom->y, geom->w, tl);
192 }else if(line==GR_BORDERLINE_RIGHT && geom->h>0){
193 XSetForeground(ioncore_g.dpy, gc, brc);
194 XDrawRectangle(ioncore_g.dpy, win, gc, geom->x+geom->w-1-br, geom->y, br, geom->h);
196 }else if(line==GR_BORDERLINE_BOTTOM && geom->w>0){
197 XSetForeground(ioncore_g.dpy, gc, brc);
198 XDrawRectangle(ioncore_g.dpy, win, gc, geom->x, geom->y+geom->h-1-br, geom->w, br);
204 void debrush_do_draw_borderline(DEBrush *brush, WRectangle geom,
205 DEColourGroup *cg, GrBorderLine line)
207 DEBorder *bd=&(brush->d->border);
208 GC gc=brush->d->normal_gc;
209 Window win=brush->win;
213 draw_borderline(win, gc, &geom, bd->hl, bd->sh, cg->hl, cg->sh, line);
214 case DEBORDER_INLAID:
215 draw_borderline(win, gc, &geom, bd->pad, bd->pad, cg->pad, cg->pad, line);
216 draw_borderline(win, gc, &geom, bd->sh, bd->hl, cg->sh, cg->hl, line);
218 case DEBORDER_GROOVE:
219 draw_borderline(win, gc, &geom, bd->sh, bd->hl, cg->sh, cg->hl, line);
220 draw_borderline(win, gc, &geom, bd->pad, bd->pad, cg->pad, cg->pad, line);
221 draw_borderline(win, gc, &geom, bd->hl, bd->sh, cg->hl, cg->sh, line);
223 case DEBORDER_ELEVATED:
225 draw_borderline(win, gc, &geom, bd->hl, bd->sh, cg->hl, cg->sh, line);
226 draw_borderline(win, gc, &geom, bd->pad, bd->pad, cg->pad, cg->pad, line);
232 void debrush_draw_borderline(DEBrush *brush, const WRectangle *geom,
235 DEColourGroup *cg=debrush_get_current_colour_group(brush);
237 debrush_do_draw_borderline(brush, *geom, cg, line);
247 static void copy_masked(DEBrush *brush, Drawable src, Drawable dst,
248 int src_x, int src_y, int w, int h,
249 int dst_x, int dst_y)
252 GC copy_gc=brush->d->copy_gc;
254 XSetClipMask(ioncore_g.dpy, copy_gc, src);
255 XSetClipOrigin(ioncore_g.dpy, copy_gc, dst_x, dst_y);
256 XCopyPlane(ioncore_g.dpy, src, dst, copy_gc, src_x, src_y, w, h,
261 static GrStyleSpec dragged_spec=GR_STYLESPEC_INIT;
262 static GrStyleSpec tagged_spec=GR_STYLESPEC_INIT;
263 static GrStyleSpec submenu_spec=GR_STYLESPEC_INIT;
266 void debrush_tab_extras(DEBrush *brush, const WRectangle *g,
267 DEColourGroup *cg, GrBorderWidths *bdw,
269 const GrStyleSpec *a1,
270 const GrStyleSpec *a2,
275 /* Not thread-safe, but neither is the rest of the drawing code
278 static bool swapped=FALSE;
280 ENSURE_INITSPEC(dragged_spec, "dragged");
281 ENSURE_INITSPEC(tagged_spec, "tagged");
284 if(!MATCHES2(dragged_spec, a1, a2))
288 d->normal_gc=d->stipple_gc;
291 XClearArea(ioncore_g.dpy, brush->win, g->x, g->y, g->w, g->h, False);
295 if(MATCHES2(tagged_spec, a1, a2)){
296 XSetForeground(ioncore_g.dpy, d->copy_gc, cg->fg);
298 copy_masked(brush, d->tag_pixmap, brush->win, 0, 0,
299 d->tag_pixmap_w, d->tag_pixmap_h,
300 g->x+g->w-bdw->right-d->tag_pixmap_w,
306 d->normal_gc=d->stipple_gc;
310 /*if(MATCHES2("*-*-*-dragged", a1, a2)){
311 XFillRectangle(ioncore_g.dpy, win, d->stipple_gc,
312 g->x, g->y, g->w, g->h);
317 void debrush_menuentry_extras(DEBrush *brush,
322 const GrStyleSpec *a1,
323 const GrStyleSpec *a2,
331 ENSURE_INITSPEC(submenu_spec, "submenu");
333 if(!MATCHES2(submenu_spec, a1, a2))
336 ty=(g->y+bdw->top+fnte->baseline
337 +(g->h-bdw->top-bdw->bottom-fnte->max_height)/2);
338 tx=g->x+g->w-bdw->right;
340 debrush_do_draw_string(brush, tx, ty, DE_SUB_IND, DE_SUB_IND_LEN,
345 void debrush_do_draw_box(DEBrush *brush, const WRectangle *geom,
346 DEColourGroup *cg, bool needfill)
348 GC gc=brush->d->normal_gc;
350 if(TRUE/*needfill*/){
351 XSetForeground(ioncore_g.dpy, gc, cg->bg);
352 XFillRectangle(ioncore_g.dpy, brush->win, gc, geom->x, geom->y,
356 debrush_do_draw_border(brush, *geom, cg);
360 static void debrush_do_draw_textbox(DEBrush *brush,
361 const WRectangle *geom,
365 const GrStyleSpec *a1,
366 const GrStyleSpec *a2)
373 grbrush_get_border_widths(&(brush->grbrush), &bdw);
374 grbrush_get_font_extents(&(brush->grbrush), &fnte);
376 if(brush->extras_fn!=NULL)
377 brush->extras_fn(brush, geom, cg, &bdw, &fnte, a1, a2, TRUE);
379 debrush_do_draw_box(brush, geom, cg, needfill);
390 if(brush->d->textalign!=DEALIGN_LEFT){
391 tw=grbrush_get_text_width((GrBrush*)brush, text, len);
393 if(brush->d->textalign==DEALIGN_CENTER)
394 tx=geom->x+bdw.left+(geom->w-bdw.left-bdw.right-tw)/2;
396 tx=geom->x+geom->w-bdw.right-tw;
401 ty=(geom->y+bdw.top+fnte.baseline
402 +(geom->h-bdw.top-bdw.bottom-fnte.max_height)/2);
404 debrush_do_draw_string(brush, tx, ty, text, len, FALSE, cg);
407 if(brush->extras_fn!=NULL)
408 brush->extras_fn(brush, geom, cg, &bdw, &fnte, a1, a2, FALSE);
412 void debrush_draw_textbox(DEBrush *brush, const WRectangle *geom,
413 const char *text, bool needfill)
415 GrStyleSpec *attr=debrush_get_current_attr(brush);
416 DEColourGroup *cg=debrush_get_colour_group(brush, attr);
419 debrush_do_draw_textbox(brush, geom, text, cg, needfill,
425 void debrush_draw_textboxes(DEBrush *brush, const WRectangle *geom,
426 int n, const GrTextElem *elem,
429 GrStyleSpec *common_attrib;
435 common_attrib=debrush_get_current_attr(brush);
437 grbrush_get_border_widths(&(brush->grbrush), &bdw);
440 g.w=bdw.left+elem[i].iw+bdw.right;
441 cg=debrush_get_colour_group2(brush, common_attrib, &elem[i].attr);
444 debrush_do_draw_textbox(brush, &g, elem[i].text, cg, needfill,
445 common_attrib, &elem[i].attr);
452 if(bdw.spacing>0 && needfill){
453 XClearArea(ioncore_g.dpy, brush->win, g.x, g.y,
454 brush->d->spacing, g.h, False);
468 void debrush_set_window_shape(DEBrush *brush, bool rough,
469 int n, const WRectangle *rects)
471 XRectangle r[MAXSHAPE];
478 /* n==0 should clear the shape. As there's absolutely no
479 * documentation for XShape (as is typical of all sucky X
480 * extensions), I don't know how the shape should properly
481 * be cleared. Thus we just use a huge rectangle.
486 r[0].width=USHRT_MAX;
487 r[0].height=USHRT_MAX;
492 r[i].width=rects[i].w;
493 r[i].height=rects[i].h;
497 XShapeCombineRectangles(ioncore_g.dpy, brush->win,
498 ShapeBounding, 0, 0, r, n,
503 void debrush_enable_transparency(DEBrush *brush, GrTransparency mode)
505 XSetWindowAttributes attr;
508 if(mode==GR_TRANSPARENCY_DEFAULT)
509 mode=brush->d->transparency_mode;
511 if(mode==GR_TRANSPARENCY_YES){
512 attrflags=CWBackPixmap;
513 attr.background_pixmap=ParentRelative;
515 attrflags=CWBackPixel;
516 attr.background_pixel=brush->d->cgrp.bg;
519 XChangeWindowAttributes(ioncore_g.dpy, brush->win, attrflags, &attr);
520 XClearWindow(ioncore_g.dpy, brush->win);
524 void debrush_fill_area(DEBrush *brush, const WRectangle *geom)
526 DEColourGroup *cg=debrush_get_current_colour_group(brush);
527 GC gc=brush->d->normal_gc;
532 XSetForeground(ioncore_g.dpy, gc, cg->bg);
533 XFillRectangle(ioncore_g.dpy, brush->win, gc,
534 geom->x, geom->y, geom->w, geom->h);
538 void debrush_clear_area(DEBrush *brush, const WRectangle *geom)
540 XClearArea(ioncore_g.dpy, brush->win,
541 geom->x, geom->y, geom->w, geom->h, False);
548 /*{{{ Clipping rectangles */
550 /* Should actually set the clipping rectangle for all GC:s and use
551 * window-specific GC:s to do this correctly...
554 static void debrush_set_clipping_rectangle(DEBrush *brush,
555 const WRectangle *geom)
559 assert(!brush->clip_set);
566 XSetClipRectangles(ioncore_g.dpy, brush->d->normal_gc,
567 0, 0, &rect, 1, Unsorted);
568 brush->clip_set=TRUE;
572 static void debrush_clear_clipping_rectangle(DEBrush *brush)
575 XSetClipMask(ioncore_g.dpy, brush->d->normal_gc, None);
576 brush->clip_set=FALSE;
584 /*{{{ debrush_begin/end */
587 void debrush_begin(DEBrush *brush, const WRectangle *geom, int flags)
590 if(flags&GRBRUSH_AMEND)
591 flags|=GRBRUSH_NO_CLEAR_OK;
593 if(!(flags&GRBRUSH_KEEP_ATTR))
594 debrush_init_attr(brush, NULL);
596 if(!(flags&GRBRUSH_NO_CLEAR_OK))
597 debrush_clear_area(brush, geom);
599 if(flags&GRBRUSH_NEED_CLIP)
600 debrush_set_clipping_rectangle(brush, geom);
604 void debrush_end(DEBrush *brush)
606 debrush_clear_clipping_rectangle(brush);