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"
37 extern WPreferences wPreferences
;
39 static void bevelImage(RImage
* image
, int relief
);
40 static RImage
* get_texture_image(WScreen
*scr
, const char *pixmap_file
);
42 WTexSolid
*wTextureMakeSolid(WScreen
* scr
, XColor
* color
)
48 texture
= wmalloc(sizeof(WTexture
));
50 texture
->type
= WTEX_SOLID
;
53 XAllocColor(dpy
, scr
->w_colormap
, color
);
54 texture
->normal
= *color
;
55 if (color
->red
== 0 && color
->blue
== 0 && color
->green
== 0) {
56 texture
->light
.red
= 0xb6da;
57 texture
->light
.green
= 0xb6da;
58 texture
->light
.blue
= 0xb6da;
59 texture
->dim
.red
= 0x6185;
60 texture
->dim
.green
= 0x6185;
61 texture
->dim
.blue
= 0x6185;
67 rgb
.red
= color
->red
>> 8;
68 rgb
.green
= color
->green
>> 8;
69 rgb
.blue
= color
->blue
>> 8;
70 RRGBtoHSV(&rgb
, &hsv
);
71 RHSVtoRGB(&hsv
, &rgb
);
74 v
= hsv
.value
* 16 / 10;
75 hsv
.value
= (v
> 255 ? 255 : v
);
76 RHSVtoRGB(&hsv
, &rgb
);
77 texture
->light
.red
= rgb
.red
<< 8;
78 texture
->light
.green
= rgb
.green
<< 8;
79 texture
->light
.blue
= rgb
.blue
<< 8;
81 hsv2
.value
= hsv2
.value
/ 2;
82 RHSVtoRGB(&hsv2
, &rgb
);
83 texture
->dim
.red
= rgb
.red
<< 8;
84 texture
->dim
.green
= rgb
.green
<< 8;
85 texture
->dim
.blue
= rgb
.blue
<< 8;
87 texture
->dark
.red
= 0;
88 texture
->dark
.green
= 0;
89 texture
->dark
.blue
= 0;
90 XAllocColor(dpy
, scr
->w_colormap
, &texture
->light
);
91 XAllocColor(dpy
, scr
->w_colormap
, &texture
->dim
);
92 XAllocColor(dpy
, scr
->w_colormap
, &texture
->dark
);
94 gcm
= GCForeground
| GCBackground
| GCGraphicsExposures
;
95 gcv
.graphics_exposures
= False
;
97 gcv
.background
= gcv
.foreground
= texture
->light
.pixel
;
98 texture
->light_gc
= XCreateGC(dpy
, scr
->w_win
, gcm
, &gcv
);
100 gcv
.background
= gcv
.foreground
= texture
->dim
.pixel
;
101 texture
->dim_gc
= XCreateGC(dpy
, scr
->w_win
, gcm
, &gcv
);
103 gcv
.background
= gcv
.foreground
= texture
->dark
.pixel
;
104 texture
->dark_gc
= XCreateGC(dpy
, scr
->w_win
, gcm
, &gcv
);
106 gcv
.background
= gcv
.foreground
= color
->pixel
;
107 texture
->normal_gc
= XCreateGC(dpy
, scr
->w_win
, gcm
, &gcv
);
112 static int dummyErrorHandler(Display
* foo
, XErrorEvent
* bar
)
117 void wTextureDestroy(WScreen
* scr
, WTexture
* texture
)
121 unsigned long colors
[8];
124 * some stupid servers don't like white or black being freed...
126 #define CANFREE(c) (c!=scr->black_pixel && c!=scr->white_pixel && c!=0)
127 switch (texture
->any
.type
) {
129 XFreeGC(dpy
, texture
->solid
.light_gc
);
130 XFreeGC(dpy
, texture
->solid
.dark_gc
);
131 XFreeGC(dpy
, texture
->solid
.dim_gc
);
132 if (CANFREE(texture
->solid
.light
.pixel
))
133 colors
[count
++] = texture
->solid
.light
.pixel
;
134 if (CANFREE(texture
->solid
.dim
.pixel
))
135 colors
[count
++] = texture
->solid
.dim
.pixel
;
136 if (CANFREE(texture
->solid
.dark
.pixel
))
137 colors
[count
++] = texture
->solid
.dark
.pixel
;
141 RReleaseImage(texture
->pixmap
.pixmap
);
144 case WTEX_MHGRADIENT
:
145 case WTEX_MVGRADIENT
:
146 case WTEX_MDGRADIENT
:
147 for (i
= 0; texture
->mgradient
.colors
[i
] != NULL
; i
++) {
148 wfree(texture
->mgradient
.colors
[i
]);
150 wfree(texture
->mgradient
.colors
);
153 case WTEX_THGRADIENT
:
154 case WTEX_TVGRADIENT
:
155 case WTEX_TDGRADIENT
:
156 RReleaseImage(texture
->tgradient
.pixmap
);
160 if (CANFREE(texture
->any
.color
.pixel
))
161 colors
[count
++] = texture
->any
.color
.pixel
;
163 XErrorHandler oldhandler
;
165 /* ignore error from buggy servers that don't know how
166 * to do reference counting for colors. */
168 oldhandler
= XSetErrorHandler(dummyErrorHandler
);
169 XFreeColors(dpy
, scr
->w_colormap
, colors
, count
, 0);
171 XSetErrorHandler(oldhandler
);
173 XFreeGC(dpy
, texture
->any
.gc
);
178 WTexGradient
*wTextureMakeGradient(WScreen
*scr
, int style
, const RColor
*from
, const RColor
*to
)
180 WTexGradient
*texture
;
183 texture
= wmalloc(sizeof(WTexture
));
184 texture
->type
= style
;
185 texture
->subtype
= 0;
187 texture
->color1
= *from
;
188 texture
->color2
= *to
;
190 texture
->normal
.red
= (from
->red
+ to
->red
) << 7;
191 texture
->normal
.green
= (from
->green
+ to
->green
) << 7;
192 texture
->normal
.blue
= (from
->blue
+ to
->blue
) << 7;
194 XAllocColor(dpy
, scr
->w_colormap
, &texture
->normal
);
195 gcv
.background
= gcv
.foreground
= texture
->normal
.pixel
;
196 gcv
.graphics_exposures
= False
;
197 texture
->normal_gc
= XCreateGC(dpy
, scr
->w_win
, GCForeground
| GCBackground
| GCGraphicsExposures
, &gcv
);
202 WTexIGradient
*wTextureMakeIGradient(WScreen
*scr
, int thickness1
, const RColor colors1
[2],
203 int thickness2
, const RColor colors2
[2])
205 WTexIGradient
*texture
;
209 texture
= wmalloc(sizeof(WTexture
));
210 texture
->type
= WTEX_IGRADIENT
;
211 for (i
= 0; i
< 2; i
++) {
212 texture
->colors1
[i
] = colors1
[i
];
213 texture
->colors2
[i
] = colors2
[i
];
215 texture
->thickness1
= thickness1
;
216 texture
->thickness2
= thickness2
;
217 if (thickness1
>= thickness2
) {
218 texture
->normal
.red
= (colors1
[0].red
+ colors1
[1].red
) << 7;
219 texture
->normal
.green
= (colors1
[0].green
+ colors1
[1].green
) << 7;
220 texture
->normal
.blue
= (colors1
[0].blue
+ colors1
[1].blue
) << 7;
222 texture
->normal
.red
= (colors2
[0].red
+ colors2
[1].red
) << 7;
223 texture
->normal
.green
= (colors2
[0].green
+ colors2
[1].green
) << 7;
224 texture
->normal
.blue
= (colors2
[0].blue
+ colors2
[1].blue
) << 7;
226 XAllocColor(dpy
, scr
->w_colormap
, &texture
->normal
);
227 gcv
.background
= gcv
.foreground
= texture
->normal
.pixel
;
228 gcv
.graphics_exposures
= False
;
229 texture
->normal_gc
= XCreateGC(dpy
, scr
->w_win
, GCForeground
| GCBackground
| GCGraphicsExposures
, &gcv
);
234 WTexMGradient
*wTextureMakeMGradient(WScreen
* scr
, int style
, RColor
** colors
)
236 WTexMGradient
*texture
;
240 texture
= wmalloc(sizeof(WTexture
));
241 texture
->type
= style
;
242 texture
->subtype
= 0;
245 while (colors
[i
] != NULL
)
248 texture
->normal
.red
= (colors
[0]->red
<< 8);
249 texture
->normal
.green
= (colors
[0]->green
<< 8);
250 texture
->normal
.blue
= (colors
[0]->blue
<< 8);
252 texture
->colors
= colors
;
254 XAllocColor(dpy
, scr
->w_colormap
, &texture
->normal
);
255 gcv
.background
= gcv
.foreground
= texture
->normal
.pixel
;
256 gcv
.graphics_exposures
= False
;
257 texture
->normal_gc
= XCreateGC(dpy
, scr
->w_win
, GCForeground
| GCBackground
| GCGraphicsExposures
, &gcv
);
262 WTexPixmap
*wTextureMakePixmap(WScreen
*scr
, int style
, const char *pixmap_file
, XColor
*color
)
268 image
= get_texture_image(scr
, pixmap_file
);
272 texture
= wmalloc(sizeof(WTexture
));
273 texture
->type
= WTEX_PIXMAP
;
274 texture
->subtype
= style
;
276 texture
->normal
= *color
;
278 XAllocColor(dpy
, scr
->w_colormap
, &texture
->normal
);
279 gcv
.background
= gcv
.foreground
= texture
->normal
.pixel
;
280 gcv
.graphics_exposures
= False
;
281 texture
->normal_gc
= XCreateGC(dpy
, scr
->w_win
, GCForeground
| GCBackground
| GCGraphicsExposures
, &gcv
);
283 texture
->pixmap
= image
;
288 WTexTGradient
*wTextureMakeTGradient(WScreen
*scr
, int style
, const RColor
*from
, const RColor
*to
,
289 const char *pixmap_file
, int opacity
)
291 WTexTGradient
*texture
;
295 image
= get_texture_image(scr
, pixmap_file
);
299 texture
= wmalloc(sizeof(WTexture
));
300 texture
->type
= style
;
302 texture
->opacity
= opacity
;
304 texture
->color1
= *from
;
305 texture
->color2
= *to
;
307 texture
->normal
.red
= (from
->red
+ to
->red
) << 7;
308 texture
->normal
.green
= (from
->green
+ to
->green
) << 7;
309 texture
->normal
.blue
= (from
->blue
+ to
->blue
) << 7;
311 XAllocColor(dpy
, scr
->w_colormap
, &texture
->normal
);
312 gcv
.background
= gcv
.foreground
= texture
->normal
.pixel
;
313 gcv
.graphics_exposures
= False
;
314 texture
->normal_gc
= XCreateGC(dpy
, scr
->w_win
, GCForeground
| GCBackground
| GCGraphicsExposures
, &gcv
);
316 texture
->pixmap
= image
;
321 static RImage
* get_texture_image(WScreen
*scr
, const char *pixmap_file
)
326 file
= FindImage(wPreferences
.pixmap_path
, pixmap_file
);
328 wwarning(_("image file \"%s\" used as texture could not be found."), pixmap_file
);
331 image
= RLoadImage(scr
->rcontext
, file
, 0);
333 wwarning(_("could not load texture pixmap \"%s\":%s"), file
, RMessageForError(RErrorCode
));
342 RImage
*wTextureRenderImage(WTexture
* texture
, int width
, int height
, int relief
)
344 RImage
*image
= NULL
;
349 switch (texture
->any
.type
) {
351 image
= RCreateImage(width
, height
, False
);
353 color1
.red
= texture
->solid
.normal
.red
>> 8;
354 color1
.green
= texture
->solid
.normal
.green
>> 8;
355 color1
.blue
= texture
->solid
.normal
.blue
>> 8;
358 RClearImage(image
, &color1
);
362 if (texture
->pixmap
.subtype
== WTP_TILE
) {
363 image
= RMakeTiledImage(texture
->pixmap
.pixmap
, width
, height
);
364 } else if (texture
->pixmap
.subtype
== WTP_CENTER
) {
365 color1
.red
= texture
->pixmap
.normal
.red
>> 8;
366 color1
.green
= texture
->pixmap
.normal
.green
>> 8;
367 color1
.blue
= texture
->pixmap
.normal
.blue
>> 8;
369 image
= RMakeCenteredImage(texture
->pixmap
.pixmap
, width
, height
, &color1
);
371 image
= RScaleImage(texture
->pixmap
.pixmap
, width
, height
);
376 image
= RRenderInterwovenGradient(width
, height
,
377 texture
->igradient
.colors1
,
378 texture
->igradient
.thickness1
,
379 texture
->igradient
.colors2
, texture
->igradient
.thickness2
);
383 subtype
= RGRD_HORIZONTAL
;
384 goto render_gradient
;
387 subtype
= RGRD_VERTICAL
;
388 goto render_gradient
;
391 subtype
= RGRD_DIAGONAL
;
394 image
= RRenderGradient(width
, height
, &texture
->gradient
.color1
,
395 &texture
->gradient
.color2
, subtype
);
398 case WTEX_MHGRADIENT
:
399 subtype
= RGRD_HORIZONTAL
;
400 goto render_mgradient
;
402 case WTEX_MVGRADIENT
:
403 subtype
= RGRD_VERTICAL
;
404 goto render_mgradient
;
406 case WTEX_MDGRADIENT
:
407 subtype
= RGRD_DIAGONAL
;
409 image
= RRenderMultiGradient(width
, height
, &(texture
->mgradient
.colors
[1]), subtype
);
412 case WTEX_THGRADIENT
:
413 subtype
= RGRD_HORIZONTAL
;
414 goto render_tgradient
;
416 case WTEX_TVGRADIENT
:
417 subtype
= RGRD_VERTICAL
;
418 goto render_tgradient
;
420 case WTEX_TDGRADIENT
:
421 subtype
= RGRD_DIAGONAL
;
426 image
= RMakeTiledImage(texture
->tgradient
.pixmap
, width
, height
);
430 grad
= RRenderGradient(width
, height
, &texture
->tgradient
.color1
,
431 &texture
->tgradient
.color2
, subtype
);
433 RReleaseImage(image
);
438 RCombineImagesWithOpaqueness(image
, grad
, texture
->tgradient
.opacity
);
443 puts("ERROR in wTextureRenderImage()");
451 wwarning(_("could not render texture: %s"), RMessageForError(RErrorCode
));
453 image
= RCreateImage(width
, height
, False
);
455 wwarning(_("could not allocate image buffer"));
463 RClearImage(image
, &gray
);
494 RBevelImage(image
, d
);
496 bevelImage(image
, -d
);
502 static void bevelImage(RImage
* image
, int relief
)
504 int width
= image
->width
;
505 int height
= image
->height
;
510 color
.red
= color
.green
= color
.blue
= 80;
512 /**/ ROperateLine(image
, RAddOperation
, 1, 0, width
- 2, 0, &color
);
513 /**/ ROperateLine(image
, RAddOperation
, 0, 0, 0, height
- 1, &color
);
515 color
.red
= color
.green
= color
.blue
= 40;
517 ROperateLine(image
, RSubtractOperation
, width
- 1, 0, width
- 1, height
- 1, &color
);
519 /**/ ROperateLine(image
, RSubtractOperation
, 1, height
- 2, width
- 2, height
- 2, &color
);
521 color
.red
= color
.green
= color
.blue
= 0;
523 RDrawLine(image
, 0, height
- 1, width
- 1, height
- 1, &color
);
529 void wDrawBevel(Drawable d
, unsigned width
, unsigned height
, WTexSolid
* texture
, int relief
)
534 if (relief
== WREL_FLAT
)
537 light
= texture
->light_gc
;
538 dim
= texture
->dim_gc
;
539 dark
= texture
->dark_gc
;
547 segs
[0].x2
= width
- 2;
548 segs
[0].y2
= segs
[0].y1
= height
- 2;
549 segs
[1].x1
= width
- 2;
551 segs
[1].x2
= width
- 2;
552 segs
[1].y2
= height
- 2;
553 if (wPreferences
.new_style
== TS_NEXT
) {
554 XDrawSegments(dpy
, d
, dark
, segs
, 2);
556 XDrawSegments(dpy
, d
, dim
, segs
, 2);
559 segs
[0].x2
= width
- 1;
560 segs
[0].y2
= segs
[0].y1
= height
- 1;
561 segs
[1].x1
= segs
[1].x2
= width
- 1;
563 segs
[1].y2
= height
- 1;
564 if (wPreferences
.new_style
== TS_NEXT
) {
565 XDrawSegments(dpy
, d
, light
, segs
, 2);
567 XDrawSegments(dpy
, d
, dark
, segs
, 2);
569 segs
[0].x1
= segs
[0].y1
= segs
[0].y2
= 0;
570 segs
[0].x2
= width
- 2;
571 segs
[1].x1
= segs
[1].y1
= 0;
573 segs
[1].y2
= height
- 2;
574 if (wPreferences
.new_style
== TS_NEXT
) {
575 XDrawSegments(dpy
, d
, dark
, segs
, 2);
577 XDrawSegments(dpy
, d
, light
, segs
, 2);
579 if (relief
== WREL_ICON
) {
580 segs
[0].x1
= segs
[0].y1
= segs
[0].y2
= 1;
581 segs
[0].x2
= width
- 2;
582 segs
[1].x1
= segs
[1].y1
= 1;
584 segs
[1].y2
= height
- 2;
585 XDrawSegments(dpy
, d
, light
, segs
, 2);