aboutsummaryrefslogtreecommitdiff
path: root/src/Shape.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/Shape.cc')
-rw-r--r--src/Shape.cc219
1 files changed, 219 insertions, 0 deletions
diff --git a/src/Shape.cc b/src/Shape.cc
new file mode 100644
index 0000000..de03ab2
--- /dev/null
+++ b/src/Shape.cc
@@ -0,0 +1,219 @@
1// Shape.cc
2// Copyright (c) 2003 Henrik Kinnunen (fluxgen(at)users.sourceforge.net)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: Shape.cc,v 1.9 2004/01/11 15:02:11 fluxgen Exp $
23
24#include "Shape.hh"
25
26#include "FbTk/FbWindow.hh"
27#include "FbTk/App.hh"
28#include "FbTk/GContext.hh"
29#include "FbTk/FbPixmap.hh"
30
31#include <cstring>
32
33#include <X11/Xutil.h>
34#ifdef SHAPE
35#include <X11/extensions/shape.h>
36#endif // SHAPE
37
38#include <iostream>
39#include <algorithm>
40using namespace std;
41
42namespace {
43
44FbTk::FbPixmap *createShape(FbTk::FbWindow &win, int place) {
45 if (win.window() == 0 || place == 0 ||
46 win.width() < 3 || win.height() < 3)
47 return 0;
48
49 static char left_bits[] = { 0xc0, 0xf8, 0xfc, 0xfe, 0xfe, 0xfe, 0xff, 0xff };
50 static char right_bits[] = { 0x03, 0x1f, 0x3f, 0x7f, 0x7f, 0x7f, 0xff, 0xff};
51 static char bottom_left_bits[] = { 0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xfc, 0xf8, 0xc0 };
52 static char bottom_right_bits[] = { 0xff, 0xff, 0x7f, 0x7f, 0x7f, 0x3f, 0x1f, 0x03 };
53
54 const int borderw = win.borderWidth();
55 const int win_width = win.width() + 2*borderw;
56 const int win_height = win.height() + 2*borderw;
57 const int pixmap_width = min(8, win_width);
58 const int pixmap_height = min(8, win_height);
59
60 Display *disp = FbTk::App::instance()->display();
61 const size_t data_size = win_width * win_height;
62 // we use calloc here so we get consistent C alloc/free with XDestroyImage
63 // and no warnings in valgrind :)
64 char *data = (char *)calloc(data_size, sizeof (char));
65 memset(data, 0xFF, data_size);
66
67 XImage *ximage = XCreateImage(disp,
68 DefaultVisual(disp, win.screenNumber()),
69 1,
70 XYPixmap, 0,
71 data,
72 win_width, win_height,
73 32, 0);
74 if (ximage == 0)
75 return 0;
76
77 XInitImage(ximage);
78
79 // shape corners
80
81 if (place & Shape::TOPLEFT) {
82 for (int y=0; y<pixmap_height; y++) {
83 for (int x=0; x<pixmap_width; x++) {
84 XPutPixel(ximage, x, y, (left_bits[y] & (0x01 << x)) ? 1 : 0);
85 }
86 }
87 }
88
89 if (place & Shape::TOPRIGHT) {
90 for (int y=0; y<pixmap_height; y++) {
91 for (int x=0; x<pixmap_width; x++) {
92 XPutPixel(ximage, x + win_width - pixmap_width, y,
93 (right_bits[y] & (0x01 << x)) ? 1 : 0);
94 }
95 }
96 }
97
98 if (place & Shape::BOTTOMLEFT) {
99 for (int y=0; y<pixmap_height; y++) {
100 for (int x=0; x<pixmap_width; x++) {
101 XPutPixel(ximage, x, y + win_height - pixmap_height,
102 (bottom_left_bits[y] & (0x01 << x)) ? 1 : 0);
103 }
104 }
105 }
106
107 if (place & Shape::BOTTOMRIGHT) {
108 for (int y=0; y<pixmap_height; y++) {
109 for (int x=0; x<pixmap_width; x++) {
110 XPutPixel(ximage, x + win_width - pixmap_width, y + win_height - pixmap_height,
111 (bottom_right_bits[y] & (0x01 << x)) ? 1 : 0);
112 }
113 }
114 }
115
116 FbTk::FbPixmap *pm = new FbTk::FbPixmap(win, win_width, win_height, 1);
117
118
119 FbTk::GContext gc(*pm);
120
121 XPutImage(disp, pm->drawable(), gc.gc(), ximage, 0, 0, 0, 0,
122 win_width, win_height);
123
124 XDestroyImage(ximage);
125
126 return pm;
127
128}
129
130} // end anonymous namespace
131
132Shape::Shape(FbTk::FbWindow &win, int shapeplaces):
133 m_win(&win),
134 m_shapeplaces(shapeplaces) {
135
136 m_shape.reset(createShape(win, shapeplaces));
137}
138
139Shape::~Shape() {
140
141#ifdef SHAPE
142 if (m_win != 0 && m_win->window()) {
143 // Reset shape of window
144 XShapeCombineMask(FbTk::App::instance()->display(),
145 m_win->window(),
146 ShapeBounding,
147 0, 0,
148 0,
149 ShapeSet);
150 }
151#endif // SHAPE
152}
153
154void Shape::setPlaces(int shapeplaces) {
155 m_shapeplaces = shapeplaces;
156}
157
158void Shape::update() {
159 if (m_win == 0 || m_win->window() == 0)
160 return;
161#ifdef SHAPE
162 if (m_shape.get() == 0 ||
163 m_win->width() != width() ||
164 m_win->height() != height()) {
165 m_shape.reset(createShape(*m_win, m_shapeplaces));
166 }
167
168 // the m_shape can be = 0 which will just reset the shape mask
169 // and make the window normal
170 XShapeCombineMask(FbTk::App::instance()->display(),
171 m_win->window(),
172 ShapeBounding,
173 -m_win->borderWidth(), -m_win->borderWidth(),
174 m_shape.get() ? m_shape->drawable() : 0,
175 ShapeSet);
176
177
178#endif // SHAPE
179
180}
181
182void Shape::setWindow(FbTk::FbWindow &win) {
183 m_win = &win;
184 update();
185}
186
187void Shape::setShapeNotify(const FbTk::FbWindow &win) {
188#ifdef SHAPE
189 XShapeSelectInput(FbTk::App::instance()->display(),
190 win.window(), ShapeNotifyMask);
191#endif // SHAPE
192}
193
194bool Shape::isShaped(const FbTk::FbWindow &win) {
195 int shaped = 0;
196
197#ifdef SHAPE
198 int not_used;
199 unsigned int not_used2;
200 XShapeQueryExtents(FbTk::App::instance()->display(),
201 win.window(),
202 &shaped, /// bShaped
203 &not_used, &not_used, // xbs, ybs
204 &not_used2, &not_used2, // wbs, hbs
205 &not_used, // cShaped
206 &not_used, &not_used, // xcs, ycs
207 &not_used2, &not_used2); // wcs, hcs
208#endif // SHAPE
209
210 return (shaped != 0 ? true : false);
211}
212
213unsigned int Shape::width() const {
214 return m_shape.get() ? m_shape->width() : 0;
215}
216
217unsigned int Shape::height() const {
218 return m_shape.get() ? m_shape->height() : 0;
219}