]> icculus.org git repositories - dana/openbox.git/blob - otk/rendercontrol.cc
support for pixmap icons, kwm_win_icon and the icon in wmhints
[dana/openbox.git] / 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 c, sfw, sfh;
442   unsigned int i, e, bgi;
443   sfw = sf.size().width();
444   sfh = sf.size().height();
445
446   if (w && h) {
447     // scale it
448     unsigned long *olddata = data;
449     unsigned long newdata[sfw*sfh];
450     double dx = w / (double)sfw;
451     double dy = h / (double)sfh;
452     double px = 0.0;
453     double py = 0.0;
454     int iy = 0;
455     for (i = 0, c = 0, e = sfw*sfh; i < e; ++i) {
456       newdata[i] = olddata[(int)px + iy];
457       if (++c >= sfw) {
458         c = 0;
459         px = 0;
460         py += dy;
461         iy = (int)py * w;
462       } else
463         px += dx;
464     }
465     data = newdata;
466
467     // apply the alpha channel
468     for (i = 0, c = 0, e = sfw*sfh; i < e; ++i, ++bgi) {
469       unsigned char alpha = data[i] >> 24;
470       unsigned char r = data[i] >> 16;
471       unsigned char g = data[i] >> 8;
472       unsigned char b = data[i];
473
474       // background color
475       unsigned char bgr = bg[i] >> default_red_shift;
476       unsigned char bgg = bg[i] >> default_green_shift;
477       unsigned char bgb = bg[i] >> default_blue_shift;
478       
479       r = bgr + (((r - bgr) * alpha) >> 8);
480       g = bgg + (((g - bgg) * alpha) >> 8);
481       b = bgb + (((b - bgb) * alpha) >> 8);
482
483       bg[i] = (r << default_red_shift) | (g << default_green_shift) |
484         (b << default_blue_shift);
485     }
486   }
487
488   const ScreenInfo *info = display->screenInfo(_screen);
489   XImage *im = XCreateImage(**display, info->visual(), info->depth(),
490                             ZPixmap, 0, NULL, sf.size().width(),
491                             sf.size().height(), 32, 0);
492   im->byte_order = endian;
493
494   reduceDepth(sf, im);
495   sf.setPixmap(im);
496   XDestroyImage(im);
497 }
498
499 void RenderControl::drawImage(Surface &sf, Pixmap pixmap, Pixmap mask) const
500 {
501   int junk, sfw, sfh, w, h, depth, mw, mh, mdepth;
502   Window wjunk;
503   const ScreenInfo *info = display->screenInfo(_screen);
504   GC mgc = 0;
505
506   assert(pixmap != None);
507
508   sfw = sf.size().width();
509   sfh = sf.size().height();
510
511   XGetGeometry(**display, pixmap, &wjunk, &junk, &junk,
512                (unsigned int*)&w, (unsigned int*)&h,
513                (unsigned int*)&junk, (unsigned int*)&depth);
514   if (mask != None) {
515     XGetGeometry(**display, mask, &wjunk, &junk, &junk,
516                  (unsigned int*)&mw, (unsigned int*)&mh,
517                  (unsigned int*)&junk, (unsigned int*)&mdepth);
518     if (mw != w || mh != h || mdepth != 1)
519       return;
520   }
521
522   Pixmap p = XCreatePixmap(**display, info->rootWindow(), sfw, sfh,
523                            info->depth());
524   Pixmap m;
525   if (mask == None)
526     m = None;
527   else {
528     m = XCreatePixmap(**display, info->rootWindow(), sfw, sfh, 1);
529     XGCValues gcv;
530     gcv.subwindow_mode = IncludeInferiors;
531     gcv.graphics_exposures = false;
532     mgc = XCreateGC(**display, m, GCGraphicsExposures |
533                     GCSubwindowMode, &gcv);
534   }
535
536   // scale it
537   for (int y = sfh - 1; y >= 0; --y) {
538     int yy = y * h / sfh;
539     for (int x = sfw - 1; x >= 0; --x) {
540       int xx = x * w / sfw;
541       if (depth != info->depth()) {
542         XCopyPlane(**display, pixmap, p, DefaultGC(**display, _screen),
543                    xx, yy, 1, 1, x, y, 1);
544       } else {
545         XCopyArea(**display, pixmap, p, DefaultGC(**display, _screen),
546                   xx, yy, 1, 1, x, y);
547       }
548       if (mask != None)
549         XCopyArea(**display, mask, m, mgc, xx, yy, 1, 1, x, y);
550     }
551   }
552
553   XSetClipMask(**display, DefaultGC(**display, _screen), m);
554   XSetClipOrigin(**display, DefaultGC(**display, _screen), 0, 0);
555   XCopyArea(**display, p, sf.pixmap(), DefaultGC(**display, _screen), 0, 0,
556             sfw, sfh, 0, 0);
557   XSetClipMask(**display, DefaultGC(**display, _screen), None);
558
559   XFreePixmap(**display, p);
560   if (m != None) XFreePixmap(**display, m);
561 }
562
563 }