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