1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
5 #include "rendercontrol.hh"
6 #include "truerendercontrol.hh"
7 #include "pseudorendercontrol.hh"
8 #include "rendertexture.hh"
9 #include "rendercolor.hh"
10 #include "renderstyle.hh"
12 #include "screeninfo.hh"
20 #endif // HAVE_STDLIB_H
22 #include "../src/gettext.h"
23 #define _(str) gettext(str)
28 RenderControl *RenderControl::getRenderControl(int screen)
30 // get the visual on the screen and return the correct type of RenderControl
31 int vclass = display->screenInfo(screen)->visual()->c_class;
34 return new TrueRenderControl(screen);
37 return new PseudoRenderControl(screen);
40 return new PseudoRenderControl(screen);
42 printf(_("RenderControl: Unsupported visual %d specified. Aborting.\n"),
48 RenderControl::RenderControl(int screen)
51 printf("Initializing RenderControl\n");
54 RenderControl::~RenderControl()
56 printf("Destroying RenderControl\n");
59 void RenderControl::drawRoot(const RenderColor &color) const
61 Window root = display->screenInfo(_screen)->rootWindow();
62 XSetWindowBackground(**display, root, color.pixel());
63 XClearWindow(**display, root);
66 void RenderControl::drawString(Surface& sf, const Font &font, int x, int y,
67 const RenderColor &color,
68 const ustring &string) const
70 assert(sf._screen == _screen);
71 XftDraw *d = sf._xftdraw;
72 assert(d); // this means that the background hasn't been rendered yet!
79 c.color.alpha = font._tint | font._tint << 8; // transparent shadow
80 c.pixel = BlackPixel(**display, _screen);
83 XftDrawStringUtf8(d, &c, font._xftfont, x + font._offset,
84 font._xftfont->ascent + y + font._offset,
85 (FcChar8*)string.c_str(), string.bytes());
87 XftDrawString8(d, &c, font._xftfont, x + font._offset,
88 font._xftfont->ascent + y + font._offset,
89 (FcChar8*)string.c_str(), string.bytes());
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
100 XftDrawStringUtf8(d, &c, font._xftfont, x, font._xftfont->ascent + y,
101 (FcChar8*)string.c_str(), string.bytes());
103 XftDrawString8(d, &c, font._xftfont, x, font._xftfont->ascent + y,
104 (FcChar8*)string.c_str(), string.bytes());
108 void RenderControl::drawSolidBackground(Surface& sf,
109 const RenderTexture& texture) const
111 assert(_screen == sf._screen);
112 assert(_screen == texture.color().screen());
114 if (texture.parentRelative()) return;
116 sf.setPixmap(texture.color());
118 int width = sf.size().width(), height = sf.size().height();
119 int left = 0, top = 0, right = width - 1, bottom = height - 1;
121 if (texture.interlaced())
122 for (int i = 0; i < height; i += 2)
123 XDrawLine(**display, sf.pixmap(), texture.interlaceColor().gc(),
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);
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);
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);
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);
152 assert(false); // unhandled RenderTexture::BevelType
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);
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);
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);
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);
180 assert(false); // unhandled RenderTexture::BevelType
183 case RenderTexture::Flat:
184 if (texture.border())
185 XDrawRectangle(**display, sf.pixmap(), texture.borderColor().gc(),
186 left, top, right, bottom);
189 assert(false); // unhandled RenderTexture::ReliefType
193 void RenderControl::drawMask(Surface &sf, const RenderColor &color,
194 const PixmapMask &mask) const
196 assert(_screen == sf._screen);
197 assert(_screen == color.screen());
199 if (mask.mask == None) return; // no mask given
201 int width = sf.size().width(), height = sf.size().height();
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);
208 // fill in the clipped region
209 XFillRectangle(**display, sf.pixmap(), color.gc(), x, y,
210 x + mask.w, y + mask.h);
212 // unset the clip region
213 XSetClipMask(**display, color.gc(), None);
214 XSetClipOrigin(**display, color.gc(), 0, 0);
217 void RenderControl::drawGradientBackground(
218 Surface &sf, const RenderTexture &texture) const
221 int w = sf.size().width(), h = sf.size().height();
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;
229 switch (texture.gradient()) {
230 case RenderTexture::Vertical:
231 verticalGradient(sf, texture);
233 case RenderTexture::Diagonal:
234 diagonalGradient(sf, texture);
236 case RenderTexture::CrossDiagonal:
237 crossDiagonalGradient(sf, texture);
240 printf("unhandled gradient\n");
243 pixel32 *data = sf.pixelData();
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;
257 for (off = 0, x = 0; x < h; ++x, off++) {
258 *(data + (off * w)) = current;
259 *(data + (off * w) + w - 1) = current;
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);
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);
289 im->data = (char*) data;
297 void RenderControl::verticalGradient(Surface &sf,
298 const RenderTexture &texture) const
300 pixel32 *data = sf.pixelData();
304 int w = sf.size().width(), h = sf.size().height();
306 dr = (float)(texture.secondary_color().red() - texture.color().red());
309 dg = (float)(texture.secondary_color().green() - texture.color().green());
312 db = (float)(texture.secondary_color().blue() - texture.color().blue());
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)
327 void RenderControl::diagonalGradient(Surface &sf,
328 const RenderTexture &texture) const
330 pixel32 *data = sf.pixelData();
332 float drx, dgx, dbx, dry, dgy, dby;
334 int w = sf.size().width(), h = sf.size().height();
336 for (int y = 0; y < h; ++y) {
337 drx = (float)(texture.secondary_color().red() - texture.color().red());
341 dgx = (float)(texture.secondary_color().green() - texture.color().green());
345 dbx = (float)(texture.secondary_color().blue() - texture.color().blue());
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);
360 void RenderControl::crossDiagonalGradient(
361 Surface &sf, const RenderTexture &texture) const
363 pixel32 *data = sf.pixelData();
365 float drx, dgx, dbx, dry, dgy, dby;
367 int w = sf.size().width(), h = sf.size().height();
369 for (int y = 0; y < h; ++y) {
370 drx = (float)(texture.secondary_color().red() - texture.color().red());
374 dgx = (float)(texture.secondary_color().green() - texture.color().green());
378 dbx = (float)(texture.secondary_color().blue() - texture.color().blue());
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);
393 void RenderControl::highlight(pixel32 *x, pixel32 *y, bool raised) const
405 r = (*up >> default_red_shift) & 0xFF;
407 g = (*up >> default_green_shift) & 0xFF;
409 b = (*up >> default_blue_shift) & 0xFF;
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);
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);
427 void RenderControl::drawBackground(Surface& sf,
428 const RenderTexture &texture) const
430 assert(_screen == sf._screen);
431 assert(_screen == texture.color().screen());
433 if (texture.gradient() == RenderTexture::Solid)
434 drawSolidBackground(sf, texture);
436 drawGradientBackground(sf, texture);
440 void RenderControl::drawImage(Surface &sf, int w, int h,
441 unsigned long *data) const
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();
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;
466 for (i = 0, c = 0, e = w*h; i < e; ++i) {
467 newdata[i] = olddata[(int)px + iy];
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];
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;
490 r = bgr + (((r - bgr) * alpha) >> 8);
491 g = bgg + (((g - bgg) * alpha) >> 8);
492 b = bgb + (((b - bgb) * alpha) >> 8);
494 bg[bgi] = (r << default_red_shift) | (g << default_green_shift) |
495 (b << default_blue_shift);
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;
511 im->data = (char*) bg;