2003-04-25 12:29:49 +00:00
|
|
|
// FbPixmap.cc for FbTk - Fluxbox ToolKit
|
2005-01-24 18:02:34 +00:00
|
|
|
// Copyright (c) 2003 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot org)
|
2003-04-25 12:29:49 +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-04-25 12:29:49 +00:00
|
|
|
|
|
|
|
#include "FbPixmap.hh"
|
|
|
|
#include "App.hh"
|
2003-09-10 21:37:05 +00:00
|
|
|
#include "GContext.hh"
|
2003-04-25 12:29:49 +00:00
|
|
|
|
2003-04-27 23:56:13 +00:00
|
|
|
#include <X11/Xutil.h>
|
2004-09-09 14:29:10 +00:00
|
|
|
#include <X11/Xatom.h>
|
2003-04-27 23:56:13 +00:00
|
|
|
#include <iostream>
|
2004-12-10 03:42:53 +00:00
|
|
|
#include <string>
|
|
|
|
|
2003-04-27 23:56:13 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2003-04-25 12:29:49 +00:00
|
|
|
namespace FbTk {
|
|
|
|
|
2004-09-10 15:46:08 +00:00
|
|
|
FbPixmap::FbPixmap():m_pm(0),
|
|
|
|
m_width(0), m_height(0),
|
|
|
|
m_depth(0) {
|
|
|
|
}
|
2003-04-25 12:29:49 +00:00
|
|
|
|
2004-09-10 15:46:08 +00:00
|
|
|
FbPixmap::FbPixmap(const FbPixmap &the_copy):FbDrawable(), m_pm(0),
|
|
|
|
m_width(0), m_height(0),
|
|
|
|
m_depth(0){
|
2003-04-25 12:29:49 +00:00
|
|
|
copy(the_copy);
|
|
|
|
}
|
|
|
|
|
2004-09-10 15:46:08 +00:00
|
|
|
FbPixmap::FbPixmap(Pixmap pm):m_pm(0),
|
2003-04-27 00:12:17 +00:00
|
|
|
m_width(0), m_height(0),
|
|
|
|
m_depth(0) {
|
|
|
|
if (pm == 0)
|
|
|
|
return;
|
|
|
|
// assign X pixmap to this
|
|
|
|
(*this) = pm;
|
|
|
|
}
|
|
|
|
|
2004-09-10 15:46:08 +00:00
|
|
|
FbPixmap::FbPixmap(const FbDrawable &src,
|
2004-01-11 12:48:46 +00:00
|
|
|
unsigned int width, unsigned int height,
|
2004-09-10 15:46:08 +00:00
|
|
|
int depth):m_pm(0),
|
|
|
|
m_width(0), m_height(0),
|
2004-01-11 12:48:46 +00:00
|
|
|
m_depth(0) {
|
|
|
|
|
|
|
|
create(src.drawable(), width, height, depth);
|
|
|
|
}
|
|
|
|
|
2004-09-10 15:46:08 +00:00
|
|
|
FbPixmap::FbPixmap(Drawable src,
|
2003-04-25 12:29:49 +00:00
|
|
|
unsigned int width, unsigned int height,
|
2004-09-10 15:46:08 +00:00
|
|
|
int depth):m_pm(0),
|
|
|
|
m_width(0), m_height(0),
|
2003-04-25 12:29:49 +00:00
|
|
|
m_depth(0) {
|
|
|
|
|
|
|
|
create(src, width, height, depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
FbPixmap::~FbPixmap() {
|
|
|
|
free();
|
|
|
|
}
|
|
|
|
|
|
|
|
FbPixmap &FbPixmap::operator = (const FbPixmap &the_copy) {
|
|
|
|
copy(the_copy);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2003-04-27 00:12:17 +00:00
|
|
|
FbPixmap &FbPixmap::operator = (Pixmap pm) {
|
|
|
|
// free pixmap before we set new
|
2003-04-25 12:29:49 +00:00
|
|
|
free();
|
2003-04-27 00:12:17 +00:00
|
|
|
|
|
|
|
if (pm == 0)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
// get width, height and depth for the pixmap
|
|
|
|
Window root;
|
|
|
|
int x, y;
|
2004-09-10 15:46:08 +00:00
|
|
|
unsigned int border_width, bpp;
|
2004-09-11 23:01:34 +00:00
|
|
|
XGetGeometry(display(),
|
2003-04-27 00:12:17 +00:00
|
|
|
pm,
|
|
|
|
&root,
|
|
|
|
&x, &y,
|
|
|
|
&m_width, &m_height,
|
|
|
|
&border_width,
|
|
|
|
&bpp);
|
|
|
|
|
|
|
|
m_depth = bpp;
|
|
|
|
|
|
|
|
m_pm = pm;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FbPixmap::copy(const FbPixmap &the_copy) {
|
|
|
|
|
|
|
|
bool create_new = false;
|
|
|
|
|
|
|
|
if (the_copy.width() != width() ||
|
|
|
|
the_copy.height() != height() ||
|
|
|
|
the_copy.depth() != depth() ||
|
|
|
|
drawable() == 0)
|
|
|
|
create_new = true;
|
2004-09-10 15:46:08 +00:00
|
|
|
|
|
|
|
if (create_new)
|
2003-04-27 00:12:17 +00:00
|
|
|
free();
|
|
|
|
|
|
|
|
if (the_copy.drawable() != 0) {
|
|
|
|
if (create_new) {
|
2004-09-10 15:46:08 +00:00
|
|
|
create(the_copy.drawable(),
|
2003-04-27 00:12:17 +00:00
|
|
|
the_copy.width(), the_copy.height(),
|
|
|
|
the_copy.depth());
|
|
|
|
}
|
2004-09-10 15:46:08 +00:00
|
|
|
|
2003-04-27 00:12:17 +00:00
|
|
|
if (drawable()) {
|
2003-09-10 21:37:05 +00:00
|
|
|
GContext gc(drawable());
|
2004-09-10 15:46:08 +00:00
|
|
|
|
2003-04-27 00:12:17 +00:00
|
|
|
copyArea(the_copy.drawable(),
|
2003-09-10 21:37:05 +00:00
|
|
|
gc.gc(),
|
2003-04-27 00:12:17 +00:00
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
width(), height());
|
|
|
|
}
|
|
|
|
}
|
2003-04-25 12:29:49 +00:00
|
|
|
}
|
|
|
|
|
2003-08-11 14:59:07 +00:00
|
|
|
void FbPixmap::copy(Pixmap pm) {
|
|
|
|
free();
|
|
|
|
if (pm == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// get width, height and depth for the pixmap
|
|
|
|
Window root;
|
|
|
|
int x, y;
|
|
|
|
unsigned int border_width, bpp;
|
|
|
|
unsigned int new_width, new_height;
|
|
|
|
|
2004-09-11 23:01:34 +00:00
|
|
|
XGetGeometry(display(),
|
2003-08-11 14:59:07 +00:00
|
|
|
pm,
|
|
|
|
&root,
|
|
|
|
&x, &y,
|
|
|
|
&new_width, &new_height,
|
|
|
|
&border_width,
|
|
|
|
&bpp);
|
|
|
|
// create new pixmap and copy area
|
|
|
|
create(root, new_width, new_height, bpp);
|
2003-08-12 00:25:23 +00:00
|
|
|
|
2004-09-11 23:01:34 +00:00
|
|
|
GC gc = XCreateGC(display(), drawable(), 0, 0);
|
2003-08-12 00:25:23 +00:00
|
|
|
|
2004-09-11 23:01:34 +00:00
|
|
|
XCopyArea(display(), pm, drawable(), gc,
|
2003-08-11 14:59:07 +00:00
|
|
|
0, 0,
|
|
|
|
width(), height(),
|
|
|
|
0, 0);
|
2003-08-12 00:25:23 +00:00
|
|
|
|
2004-09-11 23:01:34 +00:00
|
|
|
XFreeGC(display(), gc);
|
2003-08-11 14:59:07 +00:00
|
|
|
}
|
|
|
|
|
2003-07-10 11:55:01 +00:00
|
|
|
void FbPixmap::rotate() {
|
|
|
|
|
|
|
|
// make an image copy
|
2004-09-11 23:01:34 +00:00
|
|
|
XImage *src_image = XGetImage(display(), drawable(),
|
2003-07-10 11:55:01 +00:00
|
|
|
0, 0, // pos
|
|
|
|
width(), height(), // size
|
|
|
|
~0, // plane mask
|
|
|
|
ZPixmap); // format
|
|
|
|
// reverse height/width for new pixmap
|
|
|
|
FbPixmap new_pm(drawable(), height(), width(), depth());
|
|
|
|
|
2003-09-10 21:37:05 +00:00
|
|
|
GContext gc(drawable());
|
2003-07-10 11:55:01 +00:00
|
|
|
|
|
|
|
// copy new area
|
2004-09-10 15:46:08 +00:00
|
|
|
for (unsigned int y = 0; y < height(); ++y) {
|
|
|
|
for (unsigned int x = 0; x < width(); ++x) {
|
2003-09-10 21:37:05 +00:00
|
|
|
gc.setForeground(XGetPixel(src_image, x, y));
|
2003-07-10 11:55:01 +00:00
|
|
|
// revers coordinates
|
2004-09-11 23:01:34 +00:00
|
|
|
XDrawPoint(display(), new_pm.drawable(), gc.gc(), y, x);
|
2003-07-10 11:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
XDestroyImage(src_image);
|
|
|
|
// free old pixmap and set new from new_pm
|
|
|
|
free();
|
|
|
|
|
|
|
|
m_width = new_pm.width();
|
|
|
|
m_height = new_pm.height();
|
|
|
|
m_depth = new_pm.depth();
|
|
|
|
m_pm = new_pm.release();
|
|
|
|
}
|
|
|
|
|
2003-04-27 23:56:13 +00:00
|
|
|
void FbPixmap::scale(unsigned int dest_width, unsigned int dest_height) {
|
2004-09-10 15:46:08 +00:00
|
|
|
|
|
|
|
if (drawable() == 0 ||
|
2003-04-27 23:56:13 +00:00
|
|
|
(dest_width == width() && dest_height == height()))
|
|
|
|
return;
|
|
|
|
|
2004-09-11 23:01:34 +00:00
|
|
|
XImage *src_image = XGetImage(display(), drawable(),
|
2003-04-27 23:56:13 +00:00
|
|
|
0, 0, // pos
|
|
|
|
width(), height(), // size
|
|
|
|
~0, // plane mask
|
|
|
|
ZPixmap); // format
|
|
|
|
if (src_image == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// create new pixmap with dest size
|
|
|
|
FbPixmap new_pm(drawable(), dest_width, dest_height, depth());
|
|
|
|
|
2003-09-10 21:37:05 +00:00
|
|
|
GContext gc(drawable());
|
2003-04-27 23:56:13 +00:00
|
|
|
// calc zoom
|
|
|
|
float zoom_x = static_cast<float>(width())/static_cast<float>(dest_width);
|
|
|
|
float zoom_y = static_cast<float>(height())/static_cast<float>(dest_height);
|
|
|
|
|
|
|
|
// start scaling
|
|
|
|
float src_x = 0, src_y = 0;
|
2004-09-10 15:46:08 +00:00
|
|
|
for (unsigned int tx=0; tx < dest_width; ++tx, src_x += zoom_x) {
|
2003-04-27 23:56:13 +00:00
|
|
|
src_y = 0;
|
2004-09-10 15:46:08 +00:00
|
|
|
for (unsigned int ty=0; ty < dest_height; ++ty, src_y += zoom_y) {
|
2003-09-10 21:37:05 +00:00
|
|
|
gc.setForeground(XGetPixel(src_image,
|
|
|
|
static_cast<int>(src_x),
|
|
|
|
static_cast<int>(src_y)));
|
2004-09-11 23:01:34 +00:00
|
|
|
XDrawPoint(display(), new_pm.drawable(), gc.gc(), tx, ty);
|
2003-04-27 23:56:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
XDestroyImage(src_image);
|
|
|
|
|
|
|
|
// free old pixmap and set new from new_pm
|
|
|
|
free();
|
|
|
|
|
|
|
|
m_width = new_pm.width();
|
|
|
|
m_height = new_pm.height();
|
|
|
|
m_depth = new_pm.depth();
|
|
|
|
m_pm = new_pm.release();
|
|
|
|
}
|
|
|
|
|
2004-07-05 23:51:57 +00:00
|
|
|
void FbPixmap::tile(unsigned int dest_width, unsigned int dest_height) {
|
2004-09-10 15:46:08 +00:00
|
|
|
if (drawable() == 0 ||
|
2004-07-05 23:51:57 +00:00
|
|
|
(dest_width == width() && dest_height == height()))
|
|
|
|
return;
|
2004-09-10 15:46:08 +00:00
|
|
|
|
2004-07-05 23:51:57 +00:00
|
|
|
FbPixmap new_pm(drawable(), width(), height(), depth());
|
|
|
|
|
|
|
|
new_pm.copy(m_pm);
|
|
|
|
|
2004-07-06 10:47:36 +00:00
|
|
|
resize(dest_width, dest_height);
|
2004-09-10 15:46:08 +00:00
|
|
|
|
2004-07-06 10:47:36 +00:00
|
|
|
FbTk::GContext gc(*this);
|
2004-09-10 15:46:08 +00:00
|
|
|
|
2004-07-06 10:47:36 +00:00
|
|
|
gc.setTile(new_pm);
|
|
|
|
gc.setFillStyle(FillTiled);
|
2004-07-05 23:51:57 +00:00
|
|
|
|
2004-07-06 10:47:36 +00:00
|
|
|
fillRectangle(gc.gc(), 0, 0, dest_width, dest_height);
|
2004-09-10 15:46:08 +00:00
|
|
|
|
2004-07-05 23:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-09-10 21:37:05 +00:00
|
|
|
void FbPixmap::resize(unsigned int width, unsigned int height) {
|
|
|
|
FbPixmap pm(drawable(), width, height, depth());
|
|
|
|
*this = pm.release();
|
|
|
|
}
|
|
|
|
|
2003-04-27 23:56:13 +00:00
|
|
|
Pixmap FbPixmap::release() {
|
|
|
|
Pixmap ret = m_pm;
|
|
|
|
m_pm = 0;
|
|
|
|
m_width = 0;
|
|
|
|
m_height = 0;
|
|
|
|
m_depth = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-09-09 14:29:10 +00:00
|
|
|
Pixmap FbPixmap::getRootPixmap(int screen_num) {
|
2004-09-10 15:46:08 +00:00
|
|
|
|
2004-09-09 14:29:10 +00:00
|
|
|
Atom real_type;
|
|
|
|
int real_format;
|
|
|
|
unsigned long items_read, items_left;
|
2005-01-31 16:55:38 +00:00
|
|
|
unsigned long *data;
|
2004-09-09 14:29:10 +00:00
|
|
|
|
2004-09-10 15:46:08 +00:00
|
|
|
unsigned int prop = 0;
|
|
|
|
static const char* prop_ids[] = {
|
|
|
|
"_XROOTPMAP_ID",
|
|
|
|
"_XSETROOT_ID",
|
|
|
|
0
|
|
|
|
};
|
|
|
|
static bool print_error = true; // print error_message only once
|
|
|
|
static const char* error_message = { "\n\n !!! WARNING WARNING WARNING WARNING !!!!!\n"
|
|
|
|
" if you experience problems with transparency:\n"
|
|
|
|
" you are using a wallpapersetter that \n"
|
|
|
|
" uses _XSETROOT_ID .. which we do not support.\n"
|
|
|
|
" consult 'fbsetbg -i' or try any other wallpapersetter\n"
|
|
|
|
" that uses _XROOTPMAP_ID !\n"
|
|
|
|
" !!! WARNING WARNING WARNING WARNING !!!!!!\n\n"
|
|
|
|
};
|
|
|
|
|
|
|
|
Pixmap root_pm = None;
|
|
|
|
for (prop = 0; prop_ids[prop]; prop++) {
|
2004-09-11 23:01:34 +00:00
|
|
|
if (XGetWindowProperty(display(),
|
|
|
|
RootWindow(display(), screen_num),
|
|
|
|
XInternAtom(display(), prop_ids[prop], False),
|
2005-04-10 18:18:14 +00:00
|
|
|
0l, 1l,
|
2004-09-10 15:46:08 +00:00
|
|
|
False, XA_PIXMAP,
|
|
|
|
&real_type, &real_format,
|
|
|
|
&items_read, &items_left,
|
2004-09-11 12:33:14 +00:00
|
|
|
(unsigned char **) &data) == Success) {
|
|
|
|
if (real_format == 32 && items_read == 1) {
|
|
|
|
if (print_error && strcmp(prop_ids[prop], "_XSETROOT_ID") == 0) {
|
2004-09-24 06:23:17 +00:00
|
|
|
cerr<<error_message;
|
2004-09-10 15:46:08 +00:00
|
|
|
print_error = false;
|
2004-09-11 12:33:14 +00:00
|
|
|
} else
|
|
|
|
root_pm = (Pixmap) (*data);
|
|
|
|
}
|
2004-09-10 15:46:08 +00:00
|
|
|
XFree(data);
|
2004-09-11 12:33:14 +00:00
|
|
|
if (root_pm != None)
|
|
|
|
break;
|
2004-09-10 15:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-09 14:29:10 +00:00
|
|
|
|
2004-09-10 15:46:08 +00:00
|
|
|
return root_pm;
|
2004-09-09 14:29:10 +00:00
|
|
|
}
|
|
|
|
|
2003-04-25 12:29:49 +00:00
|
|
|
void FbPixmap::free() {
|
|
|
|
if (m_pm != 0) {
|
2004-09-11 23:01:34 +00:00
|
|
|
XFreePixmap(display(), m_pm);
|
2003-04-25 12:29:49 +00:00
|
|
|
m_pm = 0;
|
|
|
|
}
|
|
|
|
m_width = 0;
|
|
|
|
m_height = 0;
|
|
|
|
m_depth = 0;
|
|
|
|
}
|
|
|
|
|
2004-09-10 15:46:08 +00:00
|
|
|
void FbPixmap::create(Drawable src,
|
|
|
|
unsigned int width, unsigned int height,
|
2003-04-25 12:29:49 +00:00
|
|
|
int depth) {
|
|
|
|
if (src == 0)
|
|
|
|
return;
|
|
|
|
|
2004-09-11 23:01:34 +00:00
|
|
|
m_pm = XCreatePixmap(display(),
|
2003-04-25 12:29:49 +00:00
|
|
|
src, width, height, depth);
|
|
|
|
if (m_pm == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_width = width;
|
|
|
|
m_height = height;
|
|
|
|
m_depth = depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // end namespace FbTk
|