From b232cd86458249cb307b3b065225d788d8956561 Mon Sep 17 00:00:00 2001 From: fluxgen Date: Sun, 20 Apr 2003 13:27:16 +0000 Subject: for transparent drawable --- src/FbTk/Transparent.cc | 233 ++++++++++++++++++++++++++++++++++++++++++++++++ src/FbTk/Transparent.hh | 62 +++++++++++++ 2 files changed, 295 insertions(+) create mode 100644 src/FbTk/Transparent.cc create mode 100644 src/FbTk/Transparent.hh diff --git a/src/FbTk/Transparent.cc b/src/FbTk/Transparent.cc new file mode 100644 index 0000000..e3e1698 --- /dev/null +++ b/src/FbTk/Transparent.cc @@ -0,0 +1,233 @@ +// Transparent.cc for FbTk - Fluxbox Toolkit +// Copyright (c) 2003 Henrik Kinnunen (fluxgen(at)users.sourceforge.net) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: Transparent.cc,v 1.1 2003/04/20 13:27:16 fluxgen Exp $ + +#include "Transparent.hh" +#include "App.hh" + +// #ifdef HAVE_XRENDER +#include +// #endif // HAVE_XRENDER + +#include +using namespace std; + +namespace { + +Picture createAlphaPic(Window drawable, unsigned char alpha) { + Display *disp = FbTk::App::instance()->display(); + + // try to find a specific render format + XRenderPictFormat pic_format; + pic_format.type = PictTypeDirect; + pic_format.depth = 8; // alpha with bit depth 8 + pic_format.direct.alphaMask = 0xff; + XRenderPictFormat *format = XRenderFindFormat(disp, PictFormatType | + PictFormatDepth | PictFormatAlphaMask, + &pic_format, 0); + if (format == 0) { + cerr<<"Warning! FbTk::Transparent: Failed to find valid format for alpha."<display(); + + XRenderPictFormat *format = + XRenderFindVisualFormat(disp, + DefaultVisual(disp, screen_num)); + + + if (src != 0 && format != 0) { + m_src_pic = XRenderCreatePicture(disp, src, format, + 0, 0); + } + + if (dest != 0 && format != 0) { + m_dest_pic = XRenderCreatePicture(disp, dest, format, + 0, 0); + } + +} + +Transparent::~Transparent() { + if (m_alpha_pic != 0) + freeAlpha(); + + Display *disp = FbTk::App::instance()->display(); + + if (m_dest_pic != 0) + XRenderFreePicture(disp, m_dest_pic); + + if (m_src_pic != 0) + XRenderFreePicture(disp, m_src_pic); +} + +void Transparent::setAlpha(unsigned char alpha) { + if (m_source == 0) + return; + + freeAlpha(); + allocAlpha(alpha); +} + +void Transparent::setDest(Drawable dest, int screen_num) { + if (m_dest == dest) + return; + + Display *disp = FbTk::App::instance()->display(); + + if (m_dest_pic != 0) { + XRenderFreePicture(disp, m_dest_pic); + m_dest_pic = 0; + } + // create new dest pic if we have a valid dest drawable + if (dest != 0) { + + XRenderPictFormat *format = + XRenderFindVisualFormat(disp, + DefaultVisual(disp, screen_num)); + if (format == 0) + cerr<<"Warning! FbTk::Transparent: Failed to find format for screen("<display(); + + if (m_src_pic != 0) { + XRenderFreePicture(disp, m_src_pic); + m_src_pic = 0; + } + + m_source = source; + allocAlpha(m_alpha); + + // create new source pic if we have a valid source drawable + if (m_source != 0) { + + XRenderPictFormat *format = + XRenderFindVisualFormat(disp, + DefaultVisual(disp, screen_num)); + if (format == 0) + cerr<<"Warning! FbTk::Transparent: Failed to find format for screen("<display(), + PictOpOver, + m_src_pic, + m_alpha_pic, + m_dest_pic, + src_x, src_y, + 0, 0, + dest_x, dest_y, + width, height); + + +} + +void Transparent::allocAlpha(unsigned char alpha) { + if (m_source == 0) + return; + if (m_alpha_pic != 0) + freeAlpha(); + + m_alpha_pic = createAlphaPic(m_source, alpha); + m_alpha = alpha; +} + +void Transparent::freeAlpha() { + XRenderFreePicture(FbTk::App::instance()->display(), m_alpha_pic); + m_alpha_pic = 0; + m_alpha = 255; +} + +}; // end namespace FbTk + + + diff --git a/src/FbTk/Transparent.hh b/src/FbTk/Transparent.hh new file mode 100644 index 0000000..8652568 --- /dev/null +++ b/src/FbTk/Transparent.hh @@ -0,0 +1,62 @@ +// Transparent.hh for FbTk - Fluxbox Toolkit +// Copyright (c) 2003 Henrik Kinnunen (fluxgen(at)users.sourceforge.net) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +// $Id: Transparent.hh,v 1.1 2003/04/20 13:27:16 fluxgen Exp $ + +#ifndef FBTK_TRANSPARENT_HH +#define FBTK_TRANSPARENT_HH + +#include + +namespace FbTk { + +/// renders to drawable together with an alpha mask +class Transparent { +public: + Transparent(Drawable source, Drawable dest, unsigned char alpha, int screen_num); + ~Transparent(); + /// sets alpha value + void setAlpha(unsigned char alpha); + /// sets source drawable + void setSource(Drawable src, int screen_num); + /// sets destination drawable + void setDest(Drawable dest, int screen_num); + /** + renders to dest from src with specified coordinates and size + */ + void render(int src_x, int src_y, + int dest_x, int dest_y, + unsigned int width, unsigned int height) const; + unsigned char alpha() const { return m_alpha; } +private: + void freeAlpha(); + void allocAlpha(unsigned char newval); + unsigned long m_alpha_pic; + unsigned long m_src_pic; + unsigned long m_dest_pic; + Drawable m_source, m_dest; + unsigned char m_alpha; +}; + +}; // end namespace FbTk + +#endif // FBTK_TRANSPARENT_HH + -- cgit v0.11.2