]> Dogcows Code - chaz/openbox/blobdiff - otk/rendercontrol.cc
Add the "obsetroot" tool. Use it to set the root background.
[chaz/openbox] / otk / rendercontrol.cc
index 802c7f55aee4f64d7256f73eeffe0d3e73ed2402..8f9329688c04c4f09fffdd9db6967f466999c947 100644 (file)
 #include "surface.hh"
 #include "font.hh"
 #include "ustring.hh"
+#include "property.hh"
 
 extern "C" {
-#ifdef    HAVE_STDLIB_H
-#  include <stdlib.h>
-#endif // HAVE_STDLIB_H
+#ifdef    HAVE_SYS_WAIT_H
+#  include <sys/wait.h>
+#endif // HAVE_SYS_WAIT_H
+
+#ifdef    HAVE_UNISTD_H
+#  include <unistd.h>
+#endif // HAVE_UNISTD_H
 
 #include "../src/gettext.h"
 #define _(str) gettext(str)
 }
 
+#include <cstdlib>
+
 namespace otk {
 
 RenderControl *RenderControl::getRenderControl(int screen)
@@ -47,8 +54,10 @@ RenderControl *RenderControl::getRenderControl(int screen)
 
 RenderControl::RenderControl(int screen)
   : _screen(screen)
+    
 {
   printf("Initializing RenderControl\n");
+  
 }
 
 RenderControl::~RenderControl()
@@ -56,13 +65,6 @@ RenderControl::~RenderControl()
   printf("Destroying RenderControl\n");
 }
 
-void RenderControl::drawRoot(const RenderColor &color) const
-{
-  Window root = display->screenInfo(_screen)->rootWindow();
-  XSetWindowBackground(**display, root, color.pixel());
-  XClearWindow(**display, root);
-}
-
 void RenderControl::drawString(Surface& sf, const Font &font, int x, int y,
                               const RenderColor &color,
                                const ustring &string) const
@@ -285,12 +287,7 @@ void RenderControl::drawGradientBackground(
   }
 
   reduceDepth(sf, im);
-
-  im->data = (char*) data;
-
   sf.setPixmap(im);
-
-  im->data = NULL;
   XDestroyImage(im);
 }
 
@@ -441,42 +438,63 @@ void RenderControl::drawImage(Surface &sf, int w, int h,
                                   unsigned long *data) const
 {
   pixel32 *bg = sf.pixelData();
-  int startx, x, y, c;
-  unsigned int i, e;
-  x = (sf.size().width() - w) / 2;
-  y = (sf.size().height() - h) / 2;
+  int x, y, c, sfw, sfh;
+  unsigned int i, e, bgi;
+  sfw = sf.size().width();
+  sfh = sf.size().height();
+  x = (sfw - w) / 2;
+  y = (sfh - h) / 2;
 
   if (x < 0) x = 0;
   if (y < 0) y = 0;
 
-  // XX SCALING!@!&*(@! to make it fit on the surface
+  // Reduce the image size if its too big to make it fit on the surface
+  int oldw = w, oldh = h;
+  unsigned long *olddata = data;
+  if (w > sfw) w = sfw;
+  if (h > sfh) h = sfh;
+  unsigned long newdata[w*h];
+  if (w < oldw || h < oldh) {
+    double dx = oldw / (double)w;
+    double dy = oldh / (double)h;
+    double px = 0.0;
+    double py = 0.0;
+    int iy = 0;
+    for (i = 0, c = 0, e = w*h; i < e; ++i) {
+      newdata[i] = olddata[(int)px + iy];
+      if (++c >= w) {
+        c = 0;
+        px = 0;
+        py += dy;
+        iy = (int)py * oldw;
+      } else
+        px += dx;
+    }
+    data = newdata;
+  }
 
-  startx = x;
-  
-  for (i = 0, c = 0, e = w*h; i < e; ++i) {
+  for (i = 0, c = 0, bgi = y * sfw + x, e = w*h; i < e; ++i, ++bgi) {
     unsigned char alpha = data[i] >> 24;
     unsigned char r = data[i] >> 16;
     unsigned char g = data[i] >> 8;
     unsigned char b = data[i];
 
     // background color
-    unsigned char bgr = bg[i] >> default_red_shift;
-    unsigned char bgg = bg[i] >> default_green_shift;
-    unsigned char bgb = bg[i] >> default_blue_shift;
+    unsigned char bgr = bg[bgi] >> default_red_shift;
+    unsigned char bgg = bg[bgi] >> default_green_shift;
+    unsigned char bgb = bg[bgi] >> default_blue_shift;
       
-    r = bgr + (r - bgr) * alpha >> 8;
-    g = bgg + (g - bgg) * alpha >> 8;
-    b = bgb + (b - bgb) * alpha >> 8;
+    r = bgr + (((r - bgr) * alpha) >> 8);
+    g = bgg + (((g - bgg) * alpha) >> 8);
+    b = bgb + (((b - bgb) * alpha) >> 8);
 
-    bg[i] = (r << default_red_shift) | (g << default_green_shift) |
+    bg[bgi] = (r << default_red_shift) | (g << default_green_shift) |
       (b << default_blue_shift);
 
     if (++c >= w) {
-      ++y;
-      x = startx;
       c = 0;
-    } else
-      ++x;
+      bgi += sfw - w;
+    }
   }
 
   const ScreenInfo *info = display->screenInfo(_screen);
@@ -486,12 +504,7 @@ void RenderControl::drawImage(Surface &sf, int w, int h,
   im->byte_order = endian;
 
   reduceDepth(sf, im);
-
-  im->data = (char*) bg;
-
   sf.setPixmap(im);
-
-  im->data = NULL;
   XDestroyImage(im);
 }
 
This page took 0.025456 seconds and 4 git commands to generate.