]> Dogcows Code - chaz/openbox/blob - otk/rendercontrol.cc
Add the "obsetroot" tool. Use it to set the root background.
[chaz/openbox] / otk / rendercontrol.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "config.h"
4
5 #include "rendercontrol.hh"
6 #include "truerendercontrol.hh"
7 #include "pseudorendercontrol.hh"
8 #include "rendertexture.hh"
9 #include "rendercolor.hh"
10 #include "renderstyle.hh"
11 #include "display.hh"
12 #include "screeninfo.hh"
13 #include "surface.hh"
14 #include "font.hh"
15 #include "ustring.hh"
16 #include "property.hh"
17
18 extern "C" {
19 #ifdef HAVE_SYS_WAIT_H
20 # include <sys/wait.h>
21 #endif // HAVE_SYS_WAIT_H
22
23 #ifdef HAVE_UNISTD_H
24 # include <unistd.h>
25 #endif // HAVE_UNISTD_H
26
27 #include "../src/gettext.h"
28 #define _(str) gettext(str)
29 }
30
31 #include <cstdlib>
32
33 namespace otk {
34
35 RenderControl *RenderControl::getRenderControl(int screen)
36 {
37 // get the visual on the screen and return the correct type of RenderControl
38 int vclass = display->screenInfo(screen)->visual()->c_class;
39 switch (vclass) {
40 case TrueColor:
41 return new TrueRenderControl(screen);
42 case PseudoColor:
43 case StaticColor:
44 return new PseudoRenderControl(screen);
45 case GrayScale:
46 case StaticGray:
47 return new PseudoRenderControl(screen);
48 default:
49 printf(_("RenderControl: Unsupported visual %d specified. Aborting.\n"),
50 vclass);
51 ::exit(1);
52 }
53 }
54
55 RenderControl::RenderControl(int screen)
56 : _screen(screen)
57
58 {
59 printf("Initializing RenderControl\n");
60
61 }
62
63 RenderControl::~RenderControl()
64 {
65 printf("Destroying RenderControl\n");
66 }
67
68 void RenderControl::drawString(Surface& sf, const Font &font, int x, int y,
69 const RenderColor &color,
70 const ustring &string) const
71 {
72 assert(sf._screen == _screen);
73 XftDraw *d = sf._xftdraw;
74 assert(d); // this means that the background hasn't been rendered yet!
75
76 if (font._shadow) {
77 XftColor c;
78 c.color.red = 0;
79 c.color.green = 0;
80 c.color.blue = 0;
81 c.color.alpha = font._tint | font._tint << 8; // transparent shadow
82 c.pixel = BlackPixel(**display, _screen);
83
84 if (string.utf8())
85 XftDrawStringUtf8(d, &c, font._xftfont, x + font._offset,
86 font._xftfont->ascent + y + font._offset,
87 (FcChar8*)string.c_str(), string.bytes());
88 else
89 XftDrawString8(d, &c, font._xftfont, x + font._offset,
90 font._xftfont->ascent + y + font._offset,
91 (FcChar8*)string.c_str(), string.bytes());
92 }
93
94 XftColor c;
95 c.color.red = color.red() | color.red() << 8;
96 c.color.green = color.green() | color.green() << 8;
97 c.color.blue = color.blue() | color.blue() << 8;
98 c.pixel = color.pixel();
99 c.color.alpha = 0xff | 0xff << 8; // no transparency in Color yet
100
101 if (string.utf8())
102 XftDrawStringUtf8(d, &c, font._xftfont, x, font._xftfont->ascent + y,
103 (FcChar8*)string.c_str(), string.bytes());
104 else
105 XftDrawString8(d, &c, font._xftfont, x, font._xftfont->ascent + y,
106 (FcChar8*)string.c_str(), string.bytes());
107 return;
108 }
109
110 void RenderControl::drawSolidBackground(Surface& sf,
111 const RenderTexture& texture) const
112 {
113 assert(_screen == sf._screen);
114 assert(_screen == texture.color().screen());
115
116 if (texture.parentRelative()) return;
117
118 sf.setPixmap(texture.color());
119
120 int width = sf.size().width(), height = sf.size().height();
121 int left = 0, top = 0, right = width - 1, bottom = height - 1;
122
123 if (texture.interlaced())
124 for (int i = 0; i < height; i += 2)
125 XDrawLine(**display, sf.pixmap(), texture.interlaceColor().gc(),
126 0, i, width, i);
127
128 switch (texture.relief()) {
129 case RenderTexture::Raised:
130 switch (texture.bevel()) {
131 case RenderTexture::Bevel1:
132 XDrawLine(**display, sf.pixmap(), texture.bevelDarkColor().gc(),
133 left, bottom, right, bottom);
134 XDrawLine(**display, sf.pixmap(), texture.bevelDarkColor().gc(),
135 right, bottom, right, top);
136
137 XDrawLine(**display, sf.pixmap(), texture.bevelLightColor().gc(),
138 left, top, right, top);
139 XDrawLine(**display, sf.pixmap(), texture.bevelLightColor().gc(),
140 left, bottom, left, top);
141 break;
142 case RenderTexture::Bevel2:
143 XDrawLine(**display, sf.pixmap(), texture.bevelDarkColor().gc(),
144 left + 1, bottom - 2, right - 2, bottom - 2);
145 XDrawLine(**display, sf.pixmap(), texture.bevelDarkColor().gc(),
146 right - 2, bottom - 2, right - 2, top + 1);
147
148 XDrawLine(**display, sf.pixmap(), texture.bevelLightColor().gc(),
149 left + 1, top + 1, right - 2, top + 1);
150 XDrawLine(**display, sf.pixmap(), texture.bevelLightColor().gc(),
151 left + 1, bottom - 2, left + 1, top + 1);
152 break;
153 default:
154 assert(false); // unhandled RenderTexture::BevelType
155 }
156 break;
157 case RenderTexture::Sunken:
158 switch (texture.bevel()) {
159 case RenderTexture::Bevel1:
160 XDrawLine(**display, sf.pixmap(), texture.bevelLightColor().gc(),
161 left, bottom, right, bottom);
162 XDrawLine(**display, sf.pixmap(), texture.bevelLightColor().gc(),
163 right, bottom, right, top);
164
165 XDrawLine(**display, sf.pixmap(), texture.bevelDarkColor().gc(),
166 left, top, right, top);
167 XDrawLine(**display, sf.pixmap(), texture.bevelDarkColor().gc(),
168 left, bottom, left, top);
169 break;
170 case RenderTexture::Bevel2:
171 XDrawLine(**display, sf.pixmap(), texture.bevelLightColor().gc(),
172 left + 1, bottom - 2, right - 2, bottom - 2);
173 XDrawLine(**display, sf.pixmap(), texture.bevelLightColor().gc(),
174 right - 2, bottom - 2, right - 2, top + 1);
175
176 XDrawLine(**display, sf.pixmap(), texture.bevelDarkColor().gc(),
177 left + 1, top + 1, right - 2, top + 1);
178 XDrawLine(**display, sf.pixmap(), texture.bevelDarkColor().gc(),
179 left + 1, bottom - 2, left + 1, top + 1);
180 break;
181 default:
182 assert(false); // unhandled RenderTexture::BevelType
183 }
184 break;
185 case RenderTexture::Flat:
186 if (texture.border())
187 XDrawRectangle(**display, sf.pixmap(), texture.borderColor().gc(),
188 left, top, right, bottom);
189 break;
190 default:
191 assert(false); // unhandled RenderTexture::ReliefType
192 }
193 }
194
195 void RenderControl::drawMask(Surface &sf, const RenderColor &color,
196 const PixmapMask &mask) const
197 {
198 assert(_screen == sf._screen);
199 assert(_screen == color.screen());
200
201 if (mask.mask == None) return; // no mask given
202
203 int width = sf.size().width(), height = sf.size().height();
204
205 // set the clip region
206 int x = (width - mask.w) / 2, y = (height - mask.h) / 2;
207 XSetClipMask(**display, color.gc(), mask.mask);
208 XSetClipOrigin(**display, color.gc(), x, y);
209
210 // fill in the clipped region
211 XFillRectangle(**display, sf.pixmap(), color.gc(), x, y,
212 x + mask.w, y + mask.h);
213
214 // unset the clip region
215 XSetClipMask(**display, color.gc(), None);
216 XSetClipOrigin(**display, color.gc(), 0, 0);
217 }
218
219 void RenderControl::drawGradientBackground(
220 Surface &sf, const RenderTexture &texture) const
221 {
222 unsigned int r,g,b;
223 int w = sf.size().width(), h = sf.size().height();
224 int off, x;
225
226 const ScreenInfo *info = display->screenInfo(_screen);
227 XImage *im = XCreateImage(**display, info->visual(), info->depth(),
228 ZPixmap, 0, NULL, w, h, 32, 0);
229 im->byte_order = endian;
230
231 switch (texture.gradient()) {
232 case RenderTexture::Vertical:
233 verticalGradient(sf, texture);
234 break;
235 case RenderTexture::Diagonal:
236 diagonalGradient(sf, texture);
237 break;
238 case RenderTexture::CrossDiagonal:
239 crossDiagonalGradient(sf, texture);
240 break;
241 default:
242 printf("unhandled gradient\n");
243 }
244
245 pixel32 *data = sf.pixelData();
246 pixel32 current;
247
248 if (texture.relief() == RenderTexture::Flat && texture.border()) {
249 r = texture.borderColor().red();
250 g = texture.borderColor().green();
251 b = texture.borderColor().blue();
252 current = (r << default_red_shift)
253 + (g << default_green_shift)
254 + (b << default_blue_shift);
255 for (off = 0, x = 0; x < w; ++x, off++) {
256 *(data + off) = current;
257 *(data + off + ((h-1) * w)) = current;
258 }
259 for (off = 0, x = 0; x < h; ++x, off++) {
260 *(data + (off * w)) = current;
261 *(data + (off * w) + w - 1) = current;
262 }
263 }
264
265 if (texture.relief() != RenderTexture::Flat) {
266 if (texture.bevel() == RenderTexture::Bevel1) {
267 for (off = 1, x = 1; x < w - 1; ++x, off++)
268 highlight(data + off,
269 data + off + (h-1) * w,
270 texture.relief()==RenderTexture::Raised);
271 for (off = 0, x = 0; x < h; ++x, off++)
272 highlight(data + off * w,
273 data + off * w + w - 1,
274 texture.relief()==RenderTexture::Raised);
275 }
276
277 if (texture.bevel() == RenderTexture::Bevel2) {
278 for (off = 2, x = 2; x < w - 2; ++x, off++)
279 highlight(data + off + w,
280 data + off + (h-2) * w,
281 texture.relief()==RenderTexture::Raised);
282 for (off = 1, x = 1; x < h-1; ++x, off++)
283 highlight(data + off * w + 1,
284 data + off * w + w - 2,
285 texture.relief()==RenderTexture::Raised);
286 }
287 }
288
289 reduceDepth(sf, im);
290 sf.setPixmap(im);
291 XDestroyImage(im);
292 }
293
294 void RenderControl::verticalGradient(Surface &sf,
295 const RenderTexture &texture) const
296 {
297 pixel32 *data = sf.pixelData();
298 pixel32 current;
299 float dr, dg, db;
300 unsigned int r,g,b;
301 int w = sf.size().width(), h = sf.size().height();
302
303 dr = (float)(texture.secondary_color().red() - texture.color().red());
304 dr/= (float)h;
305
306 dg = (float)(texture.secondary_color().green() - texture.color().green());
307 dg/= (float)h;
308
309 db = (float)(texture.secondary_color().blue() - texture.color().blue());
310 db/= (float)h;
311
312 for (int y = 0; y < h; ++y) {
313 r = texture.color().red() + (int)(dr * y);
314 g = texture.color().green() + (int)(dg * y);
315 b = texture.color().blue() + (int)(db * y);
316 current = (r << default_red_shift)
317 + (g << default_green_shift)
318 + (b << default_blue_shift);
319 for (int x = 0; x < w; ++x, ++data)
320 *data = current;
321 }
322 }
323
324 void RenderControl::diagonalGradient(Surface &sf,
325 const RenderTexture &texture) const
326 {
327 pixel32 *data = sf.pixelData();
328 pixel32 current;
329 float drx, dgx, dbx, dry, dgy, dby;
330 unsigned int r,g,b;
331 int w = sf.size().width(), h = sf.size().height();
332
333 for (int y = 0; y < h; ++y) {
334 drx = (float)(texture.secondary_color().red() - texture.color().red());
335 dry = drx/(float)h;
336 drx/= (float)w;
337
338 dgx = (float)(texture.secondary_color().green() - texture.color().green());
339 dgy = dgx/(float)h;
340 dgx/= (float)w;
341
342 dbx = (float)(texture.secondary_color().blue() - texture.color().blue());
343 dby = dbx/(float)h;
344 dbx/= (float)w;
345 for (int x = 0; x < w; ++x, ++data) {
346 r = texture.color().red() + ((int)(drx * x) + (int)(dry * y))/2;
347 g = texture.color().green() + ((int)(dgx * x) + (int)(dgy * y))/2;
348 b = texture.color().blue() + ((int)(dbx * x) + (int)(dby * y))/2;
349 current = (r << default_red_shift)
350 + (g << default_green_shift)
351 + (b << default_blue_shift);
352 *data = current;
353 }
354 }
355 }
356
357 void RenderControl::crossDiagonalGradient(
358 Surface &sf, const RenderTexture &texture) const
359 {
360 pixel32 *data = sf.pixelData();
361 pixel32 current;
362 float drx, dgx, dbx, dry, dgy, dby;
363 unsigned int r,g,b;
364 int w = sf.size().width(), h = sf.size().height();
365
366 for (int y = 0; y < h; ++y) {
367 drx = (float)(texture.secondary_color().red() - texture.color().red());
368 dry = drx/(float)h;
369 drx/= (float)w;
370
371 dgx = (float)(texture.secondary_color().green() - texture.color().green());
372 dgy = dgx/(float)h;
373 dgx/= (float)w;
374
375 dbx = (float)(texture.secondary_color().blue() - texture.color().blue());
376 dby = dbx/(float)h;
377 dbx/= (float)w;
378 for (int x = w; x > 0; --x, ++data) {
379 r = texture.color().red() + ((int)(drx * (x-1)) + (int)(dry * y))/2;
380 g = texture.color().green() + ((int)(dgx * (x-1)) + (int)(dgy * y))/2;
381 b = texture.color().blue() + ((int)(dbx * (x-1)) + (int)(dby * y))/2;
382 current = (r << default_red_shift)
383 + (g << default_green_shift)
384 + (b << default_blue_shift);
385 *data = current;
386 }
387 }
388 }
389
390 void RenderControl::highlight(pixel32 *x, pixel32 *y, bool raised) const
391 {
392 int r, g, b;
393
394 pixel32 *up, *down;
395 if (raised) {
396 up = x;
397 down = y;
398 } else {
399 up = y;
400 down = x;
401 }
402 r = (*up >> default_red_shift) & 0xFF;
403 r += r >> 1;
404 g = (*up >> default_green_shift) & 0xFF;
405 g += g >> 1;
406 b = (*up >> default_blue_shift) & 0xFF;
407 b += b >> 1;
408 if (r > 255) r = 255;
409 if (g > 255) g = 255;
410 if (b > 255) b = 255;
411 *up = (r << default_red_shift) + (g << default_green_shift)
412 + (b << default_blue_shift);
413
414 r = (*down >> default_red_shift) & 0xFF;
415 r = (r >> 1) + (r >> 2);
416 g = (*down >> default_green_shift) & 0xFF;
417 g = (g >> 1) + (g >> 2);
418 b = (*down >> default_blue_shift) & 0xFF;
419 b = (b >> 1) + (b >> 2);
420 *down = (r << default_red_shift) + (g << default_green_shift)
421 + (b << default_blue_shift);
422 }
423
424 void RenderControl::drawBackground(Surface& sf,
425 const RenderTexture &texture) const
426 {
427 assert(_screen == sf._screen);
428 assert(_screen == texture.color().screen());
429
430 if (texture.gradient() == RenderTexture::Solid)
431 drawSolidBackground(sf, texture);
432 else
433 drawGradientBackground(sf, texture);
434 }
435
436
437 void RenderControl::drawImage(Surface &sf, int w, int h,
438 unsigned long *data) const
439 {
440 pixel32 *bg = sf.pixelData();
441 int x, y, c, sfw, sfh;
442 unsigned int i, e, bgi;
443 sfw = sf.size().width();
444 sfh = sf.size().height();
445 x = (sfw - w) / 2;
446 y = (sfh - h) / 2;
447
448 if (x < 0) x = 0;
449 if (y < 0) y = 0;
450
451 // Reduce the image size if its too big to make it fit on the surface
452 int oldw = w, oldh = h;
453 unsigned long *olddata = data;
454 if (w > sfw) w = sfw;
455 if (h > sfh) h = sfh;
456 unsigned long newdata[w*h];
457 if (w < oldw || h < oldh) {
458 double dx = oldw / (double)w;
459 double dy = oldh / (double)h;
460 double px = 0.0;
461 double py = 0.0;
462 int iy = 0;
463 for (i = 0, c = 0, e = w*h; i < e; ++i) {
464 newdata[i] = olddata[(int)px + iy];
465 if (++c >= w) {
466 c = 0;
467 px = 0;
468 py += dy;
469 iy = (int)py * oldw;
470 } else
471 px += dx;
472 }
473 data = newdata;
474 }
475
476 for (i = 0, c = 0, bgi = y * sfw + x, e = w*h; i < e; ++i, ++bgi) {
477 unsigned char alpha = data[i] >> 24;
478 unsigned char r = data[i] >> 16;
479 unsigned char g = data[i] >> 8;
480 unsigned char b = data[i];
481
482 // background color
483 unsigned char bgr = bg[bgi] >> default_red_shift;
484 unsigned char bgg = bg[bgi] >> default_green_shift;
485 unsigned char bgb = bg[bgi] >> default_blue_shift;
486
487 r = bgr + (((r - bgr) * alpha) >> 8);
488 g = bgg + (((g - bgg) * alpha) >> 8);
489 b = bgb + (((b - bgb) * alpha) >> 8);
490
491 bg[bgi] = (r << default_red_shift) | (g << default_green_shift) |
492 (b << default_blue_shift);
493
494 if (++c >= w) {
495 c = 0;
496 bgi += sfw - w;
497 }
498 }
499
500 const ScreenInfo *info = display->screenInfo(_screen);
501 XImage *im = XCreateImage(**display, info->visual(), info->depth(),
502 ZPixmap, 0, NULL, sf.size().width(),
503 sf.size().height(), 32, 0);
504 im->byte_order = endian;
505
506 reduceDepth(sf, im);
507 sf.setPixmap(im);
508 XDestroyImage(im);
509 }
510
511 }
This page took 0.060987 seconds and 4 git commands to generate.