fluxbox/src/Shape.cc

266 lines
8.3 KiB
C++
Raw Normal View History

2003-07-10 12:04:46 +00:00
// Shape.cc
2006-02-16 06:53:05 +00:00
// Copyright (c) 2003 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
//
2003-07-10 12:04:46 +00:00
// 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.
2004-11-19 11:37:27 +00:00
// $Id$
2003-07-10 12:04:46 +00:00
#include "Shape.hh"
2004-01-11 15:02:11 +00:00
2003-09-05 20:42:47 +00:00
#include "FbTk/FbWindow.hh"
#include "FbTk/App.hh"
#include "FbTk/GContext.hh"
2004-01-11 15:02:11 +00:00
#include "FbTk/FbPixmap.hh"
2003-07-10 12:04:46 +00:00
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H
2004-08-31 15:26:40 +00:00
#ifdef HAVE_CSTRING
#include <cstring>
#else
#include <string.h>
#endif
2003-07-10 12:04:46 +00:00
2003-09-17 14:16:53 +00:00
#include <X11/Xutil.h>
2004-08-10 19:34:35 +00:00
2003-07-10 12:04:46 +00:00
#ifdef SHAPE
#include <X11/extensions/shape.h>
#endif // SHAPE
#include <algorithm>
using std::min;
2003-07-10 12:04:46 +00:00
2007-08-08 15:41:10 +00:00
// ignore_border basically means do clip shape instead of bounding shape
FbTk::FbPixmap *Shape::createShape(bool ignore_border) {
if (m_win->window() == 0 || m_shapeplaces == 0 ||
m_win->width() < 3 || m_win->height() < 3)
2003-07-10 12:04:46 +00:00
return 0;
2003-07-10 12:04:46 +00:00
static char left_bits[] = { 0xc0, 0xf8, 0xfc, 0xfe, 0xfe, 0xfe, 0xff, 0xff };
static char right_bits[] = { 0x03, 0x1f, 0x3f, 0x7f, 0x7f, 0x7f, 0xff, 0xff};
static char bottom_left_bits[] = { 0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xfc, 0xf8, 0xc0 };
static char bottom_right_bits[] = { 0xff, 0xff, 0x7f, 0x7f, 0x7f, 0x3f, 0x1f, 0x03 };
2007-08-08 15:41:10 +00:00
const int borderw = m_win->borderWidth();
const int win_width = m_win->width() + (ignore_border?0:2*borderw);
const int win_height = m_win->height() + (ignore_border?0:2*borderw);
2003-07-10 12:04:46 +00:00
const int pixmap_width = min(8, win_width);
const int pixmap_height = min(8, win_height);
Display *disp = FbTk::App::instance()->display();
const size_t data_size = win_width * win_height;
// we use calloc here so we get consistent C alloc/free with XDestroyImage
// and no warnings in valgrind :)
char *data = (char *)calloc(data_size, sizeof (char));
2004-01-21 17:01:27 +00:00
if (data == 0)
return 0;
2003-07-10 12:04:46 +00:00
memset(data, 0xFF, data_size);
2003-07-10 12:04:46 +00:00
XImage *ximage = XCreateImage(disp,
2007-08-08 15:41:10 +00:00
DefaultVisual(disp, m_win->screenNumber()),
2003-07-10 12:04:46 +00:00
1,
XYPixmap, 0,
data,
win_width, win_height,
32, 0);
if (ximage == 0)
return 0;
XInitImage(ximage);
// shape corners
2007-08-08 15:41:10 +00:00
if (m_shapeplaces & Shape::TOPLEFT) {
2003-07-10 12:04:46 +00:00
for (int y=0; y<pixmap_height; y++) {
for (int x=0; x<pixmap_width; x++) {
XPutPixel(ximage, x, y, (left_bits[y] & (0x01 << x)) ? 1 : 0);
2003-07-10 12:04:46 +00:00
}
}
}
2007-08-08 15:41:10 +00:00
if (m_shapeplaces & Shape::TOPRIGHT) {
2003-07-10 12:04:46 +00:00
for (int y=0; y<pixmap_height; y++) {
for (int x=0; x<pixmap_width; x++) {
XPutPixel(ximage, x + win_width - pixmap_width, y,
2003-07-10 12:04:46 +00:00
(right_bits[y] & (0x01 << x)) ? 1 : 0);
}
2003-07-10 12:04:46 +00:00
}
}
2007-08-08 15:41:10 +00:00
if (m_shapeplaces & Shape::BOTTOMLEFT) {
2003-07-10 12:04:46 +00:00
for (int y=0; y<pixmap_height; y++) {
for (int x=0; x<pixmap_width; x++) {
XPutPixel(ximage, x, y + win_height - pixmap_height,
2003-07-10 12:04:46 +00:00
(bottom_left_bits[y] & (0x01 << x)) ? 1 : 0);
}
}
}
2007-08-08 15:41:10 +00:00
if (m_shapeplaces & Shape::BOTTOMRIGHT) {
2003-07-10 12:04:46 +00:00
for (int y=0; y<pixmap_height; y++) {
for (int x=0; x<pixmap_width; x++) {
XPutPixel(ximage, x + win_width - pixmap_width, y + win_height - pixmap_height,
(bottom_right_bits[y] & (0x01 << x)) ? 1 : 0);
}
}
}
2007-08-08 15:41:10 +00:00
FbTk::FbPixmap *pm = new FbTk::FbPixmap(*m_win, win_width, win_height, 1);
2004-01-11 15:02:11 +00:00
2003-07-10 12:04:46 +00:00
2004-01-11 15:02:11 +00:00
FbTk::GContext gc(*pm);
2004-01-11 15:02:11 +00:00
XPutImage(disp, pm->drawable(), gc.gc(), ximage, 0, 0, 0, 0,
2003-07-10 12:04:46 +00:00
win_width, win_height);
XDestroyImage(ximage);
return pm;
}
Shape::Shape(FbTk::FbWindow &win, int shapeplaces):
m_win(&win),
m_shapeplaces(shapeplaces) {
2007-08-08 15:41:10 +00:00
m_clipshape.reset(createShape(true));
m_boundingshape.reset(createShape(false));
2003-07-10 12:04:46 +00:00
}
Shape::~Shape() {
#ifdef SHAPE
if (m_win != 0 && m_win->window()) {
2004-01-11 15:02:11 +00:00
// Reset shape of window
XShapeCombineMask(FbTk::App::instance()->display(),
m_win->window(),
2007-08-08 15:41:10 +00:00
ShapeClip,
0, 0,
0,
ShapeSet);
2007-08-08 15:41:10 +00:00
// Reset shape of window
if (m_boundingshape.get()) {
XShapeCombineMask(FbTk::App::instance()->display(),
m_win->window(),
ShapeBounding,
0, 0,
0,
ShapeSet);
}
}
#endif // SHAPE
2003-07-10 12:04:46 +00:00
}
void Shape::setPlaces(int shapeplaces) {
m_shapeplaces = shapeplaces;
}
void Shape::update() {
if (m_win == 0 || m_win->window() == 0)
return;
#ifdef SHAPE
2007-08-08 15:41:10 +00:00
if (m_clipshape.get() == 0 ||
m_win->width() != clipWidth() ||
m_win->height() != clipHeight()) {
m_clipshape.reset(createShape(true));
}
if (m_boundingshape.get() == 0 ||
(m_win->width()+m_win->borderWidth()*2) != width() ||
(m_win->height()+m_win->borderWidth()*2) != height()) {
if (m_win->borderWidth() != 0)
m_boundingshape.reset(createShape(false));
else
m_boundingshape.reset(0);
2003-07-10 12:04:46 +00:00
}
2007-08-08 15:41:10 +00:00
// the m_shape can be = 0 which will just raeset the shape mask
// and make the window normal
XShapeCombineMask(FbTk::App::instance()->display(),
m_win->window(),
ShapeClip,
0, 0,
(m_clipshape.get() != 0)? m_clipshape->drawable() : 0,
ShapeSet);
// the m_shape can be = 0 which will just raeset the shape mask
// and make the window normal
2003-07-10 12:04:46 +00:00
XShapeCombineMask(FbTk::App::instance()->display(),
m_win->window(),
ShapeBounding,
-m_win->borderWidth(), -m_win->borderWidth(),
2007-08-08 15:41:10 +00:00
(m_boundingshape.get() != 0)? m_boundingshape->drawable() :
((m_clipshape.get() != 0)? m_clipshape->drawable(): 0),
2003-07-10 12:04:46 +00:00
ShapeSet);
2004-01-11 15:02:11 +00:00
2003-07-10 12:04:46 +00:00
#endif // SHAPE
}
void Shape::setWindow(FbTk::FbWindow &win) {
m_win = &win;
update();
}
2003-08-24 15:37:12 +00:00
void Shape::setShapeNotify(const FbTk::FbWindow &win) {
#ifdef SHAPE
XShapeSelectInput(FbTk::App::instance()->display(),
2003-08-24 15:37:12 +00:00
win.window(), ShapeNotifyMask);
#endif // SHAPE
}
bool Shape::isShaped(const FbTk::FbWindow &win) {
int shaped = 0;
#ifdef SHAPE
int not_used;
unsigned int not_used2;
XShapeQueryExtents(FbTk::App::instance()->display(),
win.window(),
2003-08-24 15:37:12 +00:00
&shaped, /// bShaped
&not_used, &not_used, // xbs, ybs
&not_used2, &not_used2, // wbs, hbs
&not_used, // cShaped
&not_used, &not_used, // xcs, ycs
&not_used2, &not_used2); // wcs, hcs
#endif // SHAPE
return (shaped != 0 ? true : false);
}
2004-01-11 15:02:11 +00:00
unsigned int Shape::width() const {
2007-08-08 15:41:10 +00:00
return m_boundingshape.get() ? m_boundingshape->width() : 0;
2004-01-11 15:02:11 +00:00
}
unsigned int Shape::height() const {
2007-08-08 15:41:10 +00:00
return m_boundingshape.get() ? m_boundingshape->height() : 0;
}
unsigned int Shape::clipWidth() const {
return m_clipshape.get() ? m_clipshape->width() : 0;
}
unsigned int Shape::clipHeight() const {
return m_clipshape.get() ? m_clipshape->height() : 0;
2004-01-11 15:02:11 +00:00
}