2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
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.
24 #include <X11/Xutil.h>
32 #include "WindowMaker.h"
36 extern WPreferences wPreferences
;
38 static void bevelImage(RImage
* image
, int relief
);
39 static RImage
* get_texture_image(WScreen
*scr
, char *pixmap_file
);
41 WTexSolid
*wTextureMakeSolid(WScreen
* scr
, XColor
* color
)
47 texture
= wmalloc(sizeof(WTexture
));
49 texture
->type
= WTEX_SOLID
;
52 XAllocColor(dpy
, scr
->w_colormap
, color
);
53 texture
->normal
= *color
;
54 if (color
->red
== 0 && color
->blue
== 0 && color
->green
== 0) {
55 texture
->light
.red
= 0xb6da;
56 texture
->light
.green
= 0xb6da;
57 texture
->light
.blue
= 0xb6da;
58 texture
->dim
.red
= 0x6185;
59 texture
->dim
.green
= 0x6185;
60 texture
->dim
.blue
= 0x6185;
66 rgb
.red
= color
->red
>> 8;
67 rgb
.green
= color
->green
>> 8;
68 rgb
.blue
= color
->blue
>> 8;
69 RRGBtoHSV(&rgb
, &hsv
);
70 RHSVtoRGB(&hsv
, &rgb
);
73 v
= hsv
.value
* 16 / 10;
74 hsv
.value
= (v
> 255 ? 255 : v
);
75 RHSVtoRGB(&hsv
, &rgb
);
76 texture
->light
.red
= rgb
.red
<< 8;
77 texture
->light
.green
= rgb
.green
<< 8;
78 texture
->light
.blue
= rgb
.blue
<< 8;
80 hsv2
.value
= hsv2
.value
/ 2;
81 RHSVtoRGB(&hsv2
, &rgb
);
82 texture
->dim
.red
= rgb
.red
<< 8;
83 texture
->dim
.green
= rgb
.green
<< 8;
84 texture
->dim
.blue
= rgb
.blue
<< 8;
86 texture
->dark
.red
= 0;
87 texture
->dark
.green
= 0;
88 texture
->dark
.blue
= 0;
89 XAllocColor(dpy
, scr
->w_colormap
, &texture
->light
);
90 XAllocColor(dpy
, scr
->w_colormap
, &texture
->dim
);
91 XAllocColor(dpy
, scr
->w_colormap
, &texture
->dark
);
93 gcm
= GCForeground
| GCBackground
| GCGraphicsExposures
;
94 gcv
.graphics_exposures
= False
;
96 gcv
.background
= gcv
.foreground
= texture
->light
.pixel
;
97 texture
->light_gc
= XCreateGC(dpy
, scr
->w_win
, gcm
, &gcv
);
99 gcv
.background
= gcv
.foreground
= texture
->dim
.pixel
;
100 texture
->dim_gc
= XCreateGC(dpy
, scr
->w_win
, gcm
, &gcv
);
102 gcv
.background
= gcv
.foreground
= texture
->dark
.pixel
;
103 texture
->dark_gc
= XCreateGC(dpy
, scr
->w_win
, gcm
, &gcv
);
105 gcv
.background
= gcv
.foreground
= color
->pixel
;
106 texture
->normal_gc
= XCreateGC(dpy
, scr
->w_win
, gcm
, &gcv
);
111 static int dummyErrorHandler(Display
* foo
, XErrorEvent
* bar
)
116 void wTextureDestroy(WScreen
* scr
, WTexture
* texture
)
120 unsigned long colors
[8];
123 * some stupid servers don't like white or black being freed...
125 #define CANFREE(c) (c!=scr->black_pixel && c!=scr->white_pixel && c!=0)
126 switch (texture
->any
.type
) {
128 XFreeGC(dpy
, texture
->solid
.light_gc
);
129 XFreeGC(dpy
, texture
->solid
.dark_gc
);
130 XFreeGC(dpy
, texture
->solid
.dim_gc
);
131 if (CANFREE(texture
->solid
.light
.pixel
))
132 colors
[count
++] = texture
->solid
.light
.pixel
;
133 if (CANFREE(texture
->solid
.dim
.pixel
))
134 colors
[count
++] = texture
->solid
.dim
.pixel
;
135 if (CANFREE(texture
->solid
.dark
.pixel
))
136 colors
[count
++] = texture
->solid
.dark
.pixel
;
140 RReleaseImage(texture
->pixmap
.pixmap
);
143 case WTEX_MHGRADIENT
:
144 case WTEX_MVGRADIENT
:
145 case WTEX_MDGRADIENT
:
146 for (i
= 0; texture
->mgradient
.colors
[i
] != NULL
; i
++) {
147 wfree(texture
->mgradient
.colors
[i
]);
149 wfree(texture
->mgradient
.colors
);
152 case WTEX_THGRADIENT
:
153 case WTEX_TVGRADIENT
:
154 case WTEX_TDGRADIENT
:
155 RReleaseImage(texture
->tgradient
.pixmap
);
159 if (CANFREE(texture
->any
.color
.pixel
))
160 colors
[count
++] = texture
->any
.color
.pixel
;
162 XErrorHandler oldhandler
;
164 /* ignore error from buggy servers that don't know how
165 * to do reference counting for colors. */
167 oldhandler
= XSetErrorHandler(dummyErrorHandler
);
168 XFreeColors(dpy
, scr
->w_colormap
, colors
, count
, 0);
170 XSetErrorHandler(oldhandler
);
172 XFreeGC(dpy
, texture
->any
.gc
);
177 WTexGradient
*wTextureMakeGradient(WScreen
* scr
, int style
, RColor
* from
, RColor
* to
)
179 WTexGradient
*texture
;
182 texture
= wmalloc(sizeof(WTexture
));
183 texture
->type
= style
;
184 texture
->subtype
= 0;
186 texture
->color1
= *from
;
187 texture
->color2
= *to
;
189 texture
->normal
.red
= (from
->red
+ to
->red
) << 7;
190 texture
->normal
.green
= (from
->green
+ to
->green
) << 7;
191 texture
->normal
.blue
= (from
->blue
+ to
->blue
) << 7;
193 XAllocColor(dpy
, scr
->w_colormap
, &texture
->normal
);
194 gcv
.background
= gcv
.foreground
= texture
->normal
.pixel
;
195 gcv
.graphics_exposures
= False
;
196 texture
->normal_gc
= XCreateGC(dpy
, scr
->w_win
, GCForeground
| GCBackground
| GCGraphicsExposures
, &gcv
);
201 WTexIGradient
*wTextureMakeIGradient(WScreen
* scr
, int thickness1
, RColor colors1
[2],
202 int thickness2
, RColor colors2
[2])
204 WTexIGradient
*texture
;
208 texture
= wmalloc(sizeof(WTexture
));
209 texture
->type
= WTEX_IGRADIENT
;
210 for (i
= 0; i
< 2; i
++) {
211 texture
->colors1
[i
] = colors1
[i
];
212 texture
->colors2
[i
] = colors2
[i
];
214 texture
->thickness1
= thickness1
;
215 texture
->thickness2
= thickness2
;
216 if (thickness1
>= thickness2
) {
217 texture
->normal
.red
= (colors1
[0].red
+ colors1
[1].red
) << 7;
218 texture
->normal
.green
= (colors1
[0].green
+ colors1
[1].green
) << 7;
219 texture
->normal
.blue
= (colors1
[0].blue
+ colors1
[1].blue
) << 7;
221 texture
->normal
.red
= (colors2
[0].red
+ colors2
[1].red
) << 7;
222 texture
->normal
.green
= (colors2
[0].green
+ colors2
[1].green
) << 7;
223 texture
->normal
.blue
= (colors2
[0].blue
+ colors2
[1].blue
) << 7;
225 XAllocColor(dpy
, scr
->w_colormap
, &texture
->normal
);
226 gcv
.background
= gcv
.foreground
= texture
->normal
.pixel
;
227 gcv
.graphics_exposures
= False
;
228 texture
->normal_gc
= XCreateGC(dpy
, scr
->w_win
, GCForeground
| GCBackground
| GCGraphicsExposures
, &gcv
);
233 WTexMGradient
*wTextureMakeMGradient(WScreen
* scr
, int style
, RColor
** colors
)
235 WTexMGradient
*texture
;
239 texture
= wmalloc(sizeof(WTexture
));
240 texture
->type
= style
;
241 texture
->subtype
= 0;
244 while (colors
[i
] != NULL
)
247 texture
->normal
.red
= (colors
[0]->red
<< 8);
248 texture
->normal
.green
= (colors
[0]->green
<< 8);
249 texture
->normal
.blue
= (colors
[0]->blue
<< 8);
251 texture
->colors
= colors
;
253 XAllocColor(dpy
, scr
->w_colormap
, &texture
->normal
);
254 gcv
.background
= gcv
.foreground
= texture
->normal
.pixel
;
255 gcv
.graphics_exposures
= False
;
256 texture
->normal_gc
= XCreateGC(dpy
, scr
->w_win
, GCForeground
| GCBackground
| GCGraphicsExposures
, &gcv
);
261 WTexPixmap
*wTextureMakePixmap(WScreen
* scr
, int style
, char *pixmap_file
, XColor
* color
)
267 image
= get_texture_image(scr
, pixmap_file
);
271 texture
= wmalloc(sizeof(WTexture
));
272 texture
->type
= WTEX_PIXMAP
;
273 texture
->subtype
= style
;
275 texture
->normal
= *color
;
277 XAllocColor(dpy
, scr
->w_colormap
, &texture
->normal
);
278 gcv
.background
= gcv
.foreground
= texture
->normal
.pixel
;
279 gcv
.graphics_exposures
= False
;
280 texture
->normal_gc
= XCreateGC(dpy
, scr
->w_win
, GCForeground
| GCBackground
| GCGraphicsExposures
, &gcv
);
282 texture
->pixmap
= image
;
287 WTexTGradient
*wTextureMakeTGradient(WScreen
* scr
, int style
, RColor
* from
, RColor
* to
,
288 char *pixmap_file
, int opacity
)
290 WTexTGradient
*texture
;
294 image
= get_texture_image(scr
, pixmap_file
);
298 texture
= wmalloc(sizeof(WTexture
));
299 texture
->type
= style
;
301 texture
->opacity
= opacity
;
303 texture
->color1
= *from
;
304 texture
->color2
= *to
;
306 texture
->normal
.red
= (from
->red
+ to
->red
) << 7;
307 texture
->normal
.green
= (from
->green
+ to
->green
) << 7;
308 texture
->normal
.blue
= (from
->blue
+ to
->blue
) << 7;
310 XAllocColor(dpy
, scr
->w_colormap
, &texture
->normal
);
311 gcv
.background
= gcv
.foreground
= texture
->normal
.pixel
;
312 gcv
.graphics_exposures
= False
;
313 texture
->normal_gc
= XCreateGC(dpy
, scr
->w_win
, GCForeground
| GCBackground
| GCGraphicsExposures
, &gcv
);
315 texture
->pixmap
= image
;
320 static RImage
* get_texture_image(WScreen
*scr
, char *pixmap_file
)
325 file
= FindImage(wPreferences
.pixmap_path
, pixmap_file
);
327 wwarning(_("image file \"%s\" used as texture could not be found."), pixmap_file
);
330 image
= RLoadImage(scr
->rcontext
, file
, 0);
332 wwarning(_("could not load texture pixmap \"%s\":%s"), file
, RMessageForError(RErrorCode
));
341 RImage
*wTextureRenderImage(WTexture
* texture
, int width
, int height
, int relief
)
343 RImage
*image
= NULL
;
348 switch (texture
->any
.type
) {
350 image
= RCreateImage(width
, height
, False
);
352 color1
.red
= texture
->solid
.normal
.red
>> 8;
353 color1
.green
= texture
->solid
.normal
.green
>> 8;
354 color1
.blue
= texture
->solid
.normal
.blue
>> 8;
357 RClearImage(image
, &color1
);
361 if (texture
->pixmap
.subtype
== WTP_TILE
) {
362 image
= RMakeTiledImage(texture
->pixmap
.pixmap
, width
, height
);
363 } else if (texture
->pixmap
.subtype
== WTP_CENTER
) {
364 color1
.red
= texture
->pixmap
.normal
.red
>> 8;
365 color1
.green
= texture
->pixmap
.normal
.green
>> 8;
366 color1
.blue
= texture
->pixmap
.normal
.blue
>> 8;
368 image
= RMakeCenteredImage(texture
->pixmap
.pixmap
, width
, height
, &color1
);
370 image
= RScaleImage(texture
->pixmap
.pixmap
, width
, height
);
375 image
= RRenderInterwovenGradient(width
, height
,
376 texture
->igradient
.colors1
,
377 texture
->igradient
.thickness1
,
378 texture
->igradient
.colors2
, texture
->igradient
.thickness2
);
382 subtype
= RGRD_HORIZONTAL
;
383 goto render_gradient
;
386 subtype
= RGRD_VERTICAL
;
387 goto render_gradient
;
390 subtype
= RGRD_DIAGONAL
;
393 image
= RRenderGradient(width
, height
, &texture
->gradient
.color1
,
394 &texture
->gradient
.color2
, subtype
);
397 case WTEX_MHGRADIENT
:
398 subtype
= RGRD_HORIZONTAL
;
399 goto render_mgradient
;
401 case WTEX_MVGRADIENT
:
402 subtype
= RGRD_VERTICAL
;
403 goto render_mgradient
;
405 case WTEX_MDGRADIENT
:
406 subtype
= RGRD_DIAGONAL
;
408 image
= RRenderMultiGradient(width
, height
, &(texture
->mgradient
.colors
[1]), subtype
);
411 case WTEX_THGRADIENT
:
412 subtype
= RGRD_HORIZONTAL
;
413 goto render_tgradient
;
415 case WTEX_TVGRADIENT
:
416 subtype
= RGRD_VERTICAL
;
417 goto render_tgradient
;
419 case WTEX_TDGRADIENT
:
420 subtype
= RGRD_DIAGONAL
;
425 image
= RMakeTiledImage(texture
->tgradient
.pixmap
, width
, height
);
429 grad
= RRenderGradient(width
, height
, &texture
->tgradient
.color1
,
430 &texture
->tgradient
.color2
, subtype
);
432 RReleaseImage(image
);
437 RCombineImagesWithOpaqueness(image
, grad
, texture
->tgradient
.opacity
);
442 puts("ERROR in wTextureRenderImage()");
450 wwarning(_("could not render texture: %s"), RMessageForError(RErrorCode
));
452 image
= RCreateImage(width
, height
, False
);
454 wwarning(_("could not allocate image buffer"));
462 RClearImage(image
, &gray
);
493 RBevelImage(image
, d
);
495 bevelImage(image
, -d
);
501 static void bevelImage(RImage
* image
, int relief
)
503 int width
= image
->width
;
504 int height
= image
->height
;
509 color
.red
= color
.green
= color
.blue
= 80;
511 /**/ ROperateLine(image
, RAddOperation
, 1, 0, width
- 2, 0, &color
);
512 /**/ ROperateLine(image
, RAddOperation
, 0, 0, 0, height
- 1, &color
);
514 color
.red
= color
.green
= color
.blue
= 40;
516 ROperateLine(image
, RSubtractOperation
, width
- 1, 0, width
- 1, height
- 1, &color
);
518 /**/ ROperateLine(image
, RSubtractOperation
, 1, height
- 2, width
- 2, height
- 2, &color
);
520 color
.red
= color
.green
= color
.blue
= 0;
522 RDrawLine(image
, 0, height
- 1, width
- 1, height
- 1, &color
);
528 void wDrawBevel(Drawable d
, unsigned width
, unsigned height
, WTexSolid
* texture
, int relief
)
533 if (relief
== WREL_FLAT
)
536 light
= texture
->light_gc
;
537 dim
= texture
->dim_gc
;
538 dark
= texture
->dark_gc
;
546 segs
[0].x2
= width
- 2;
547 segs
[0].y2
= segs
[0].y1
= height
- 2;
548 segs
[1].x1
= width
- 2;
550 segs
[1].x2
= width
- 2;
551 segs
[1].y2
= height
- 2;
552 if (wPreferences
.new_style
== TS_NEXT
) {
553 XDrawSegments(dpy
, d
, dark
, segs
, 2);
555 XDrawSegments(dpy
, d
, dim
, segs
, 2);
558 segs
[0].x2
= width
- 1;
559 segs
[0].y2
= segs
[0].y1
= height
- 1;
560 segs
[1].x1
= segs
[1].x2
= width
- 1;
562 segs
[1].y2
= height
- 1;
563 if (wPreferences
.new_style
== TS_NEXT
) {
564 XDrawSegments(dpy
, d
, light
, segs
, 2);
566 XDrawSegments(dpy
, d
, dark
, segs
, 2);
568 segs
[0].x1
= segs
[0].y1
= segs
[0].y2
= 0;
569 segs
[0].x2
= width
- 2;
570 segs
[1].x1
= segs
[1].y1
= 0;
572 segs
[1].y2
= height
- 2;
573 if (wPreferences
.new_style
== TS_NEXT
) {
574 XDrawSegments(dpy
, d
, dark
, segs
, 2);
576 XDrawSegments(dpy
, d
, light
, segs
, 2);
578 if (relief
== WREL_ICON
) {
579 segs
[0].x1
= segs
[0].y1
= segs
[0].y2
= 1;
580 segs
[0].x2
= width
- 2;
581 segs
[1].x1
= segs
[1].y1
= 1;
583 segs
[1].y2
= height
- 2;
584 XDrawSegments(dpy
, d
, light
, segs
, 2);