]>
Dogcows Code - chaz/openbox/blob - otk/rendercontrol.cc
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"
16 #include "property.hh"
19 #ifdef HAVE_SYS_WAIT_H
20 # include <sys/wait.h>
21 #endif // HAVE_SYS_WAIT_H
25 #endif // HAVE_UNISTD_H
27 #include "../src/gettext.h"
28 #define _(str) gettext(str)
35 RenderControl
*RenderControl::getRenderControl(int screen
)
37 // get the visual on the screen and return the correct type of RenderControl
38 int vclass
= display
->screenInfo(screen
)->visual()->c_class
;
41 return new TrueRenderControl(screen
);
44 return new PseudoRenderControl(screen
);
47 return new PseudoRenderControl(screen
);
49 printf(_("RenderControl: Unsupported visual %d specified. Aborting.\n"),
55 RenderControl::RenderControl(int screen
)
59 printf("Initializing RenderControl\n");
63 RenderControl::~RenderControl()
65 printf("Destroying RenderControl\n");
68 void RenderControl::drawString(Surface
& sf
, const Font
&font
, int x
, int y
,
69 const RenderColor
&color
,
70 const ustring
&string
) const
72 assert(sf
._screen
== _screen
);
73 XftDraw
*d
= sf
._xftdraw
;
74 assert(d
); // this means that the background hasn't been rendered yet!
81 c
.color
.alpha
= font
._tint
| font
._tint
<< 8; // transparent shadow
82 c
.pixel
= BlackPixel(**display
, _screen
);
85 XftDrawStringUtf8(d
, &c
, font
._xftfont
, x
+ font
._offset
,
86 font
._xftfont
->ascent
+ y
+ font
._offset
,
87 (FcChar8
*)string
.c_str(), string
.bytes());
89 XftDrawString8(d
, &c
, font
._xftfont
, x
+ font
._offset
,
90 font
._xftfont
->ascent
+ y
+ font
._offset
,
91 (FcChar8
*)string
.c_str(), string
.bytes());
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
102 XftDrawStringUtf8(d
, &c
, font
._xftfont
, x
, font
._xftfont
->ascent
+ y
,
103 (FcChar8
*)string
.c_str(), string
.bytes());
105 XftDrawString8(d
, &c
, font
._xftfont
, x
, font
._xftfont
->ascent
+ y
,
106 (FcChar8
*)string
.c_str(), string
.bytes());
110 void RenderControl::drawSolidBackground(Surface
& sf
,
111 const RenderTexture
& texture
) const
113 assert(_screen
== sf
._screen
);
114 assert(_screen
== texture
.color().screen());
116 if (texture
.parentRelative()) return;
118 sf
.setPixmap(texture
.color());
120 int width
= sf
.size().width(), height
= sf
.size().height();
121 int left
= 0, top
= 0, right
= width
- 1, bottom
= height
- 1;
123 if (texture
.interlaced())
124 for (int i
= 0; i
< height
; i
+= 2)
125 XDrawLine(**display
, sf
.pixmap(), texture
.interlaceColor().gc(),
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
);
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
);
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);
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);
154 assert(false); // unhandled RenderTexture::BevelType
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
);
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
);
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);
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);
182 assert(false); // unhandled RenderTexture::BevelType
185 case RenderTexture::Flat
:
186 if (texture
.border())
187 XDrawRectangle(**display
, sf
.pixmap(), texture
.borderColor().gc(),
188 left
, top
, right
, bottom
);
191 assert(false); // unhandled RenderTexture::ReliefType
195 void RenderControl::drawMask(Surface
&sf
, const RenderColor
&color
,
196 const PixmapMask
&mask
) const
198 assert(_screen
== sf
._screen
);
199 assert(_screen
== color
.screen());
201 if (mask
.mask
== None
) return; // no mask given
203 int width
= sf
.size().width(), height
= sf
.size().height();
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
);
210 // fill in the clipped region
211 XFillRectangle(**display
, sf
.pixmap(), color
.gc(), x
, y
,
212 x
+ mask
.w
, y
+ mask
.h
);
214 // unset the clip region
215 XSetClipMask(**display
, color
.gc(), None
);
216 XSetClipOrigin(**display
, color
.gc(), 0, 0);
219 void RenderControl::drawGradientBackground(
220 Surface
&sf
, const RenderTexture
&texture
) const
223 int w
= sf
.size().width(), h
= sf
.size().height();
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
;
231 switch (texture
.gradient()) {
232 case RenderTexture::Vertical
:
233 verticalGradient(sf
, texture
);
235 case RenderTexture::Diagonal
:
236 diagonalGradient(sf
, texture
);
238 case RenderTexture::CrossDiagonal
:
239 crossDiagonalGradient(sf
, texture
);
242 printf("unhandled gradient\n");
245 pixel32
*data
= sf
.pixelData();
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
;
259 for (off
= 0, x
= 0; x
< h
; ++x
, off
++) {
260 *(data
+ (off
* w
)) = current
;
261 *(data
+ (off
* w
) + w
- 1) = current
;
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
);
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
);
294 void RenderControl::verticalGradient(Surface
&sf
,
295 const RenderTexture
&texture
) const
297 pixel32
*data
= sf
.pixelData();
301 int w
= sf
.size().width(), h
= sf
.size().height();
303 dr
= (float)(texture
.secondary_color().red() - texture
.color().red());
306 dg
= (float)(texture
.secondary_color().green() - texture
.color().green());
309 db
= (float)(texture
.secondary_color().blue() - texture
.color().blue());
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
)
324 void RenderControl::diagonalGradient(Surface
&sf
,
325 const RenderTexture
&texture
) const
327 pixel32
*data
= sf
.pixelData();
329 float drx
, dgx
, dbx
, dry
, dgy
, dby
;
331 int w
= sf
.size().width(), h
= sf
.size().height();
333 for (int y
= 0; y
< h
; ++y
) {
334 drx
= (float)(texture
.secondary_color().red() - texture
.color().red());
338 dgx
= (float)(texture
.secondary_color().green() - texture
.color().green());
342 dbx
= (float)(texture
.secondary_color().blue() - texture
.color().blue());
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
);
357 void RenderControl::crossDiagonalGradient(
358 Surface
&sf
, const RenderTexture
&texture
) const
360 pixel32
*data
= sf
.pixelData();
362 float drx
, dgx
, dbx
, dry
, dgy
, dby
;
364 int w
= sf
.size().width(), h
= sf
.size().height();
366 for (int y
= 0; y
< h
; ++y
) {
367 drx
= (float)(texture
.secondary_color().red() - texture
.color().red());
371 dgx
= (float)(texture
.secondary_color().green() - texture
.color().green());
375 dbx
= (float)(texture
.secondary_color().blue() - texture
.color().blue());
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
);
390 void RenderControl::highlight(pixel32
*x
, pixel32
*y
, bool raised
) const
402 r
= (*up
>> default_red_shift
) & 0xFF;
404 g
= (*up
>> default_green_shift
) & 0xFF;
406 b
= (*up
>> default_blue_shift
) & 0xFF;
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
);
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
);
424 void RenderControl::drawBackground(Surface
& sf
,
425 const RenderTexture
&texture
) const
427 assert(_screen
== sf
._screen
);
428 assert(_screen
== texture
.color().screen());
430 if (texture
.gradient() == RenderTexture::Solid
)
431 drawSolidBackground(sf
, texture
);
433 drawGradientBackground(sf
, texture
);
437 void RenderControl::drawImage(Surface
&sf
, int w
, int h
,
438 unsigned long *data
) const
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();
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
;
463 for (i
= 0, c
= 0, e
= w
*h
; i
< e
; ++i
) {
464 newdata
[i
] = olddata
[(int)px
+ iy
];
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
];
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
;
487 r
= bgr
+ (((r
- bgr
) * alpha
) >> 8);
488 g
= bgg
+ (((g
- bgg
) * alpha
) >> 8);
489 b
= bgb
+ (((b
- bgb
) * alpha
) >> 8);
491 bg
[bgi
] = (r
<< default_red_shift
) | (g
<< default_green_shift
) |
492 (b
<< default_blue_shift
);
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
;
This page took 0.061243 seconds and 4 git commands to generate.