kwin/kcmkwin/kwindecoration/preview.cpp

609 lines
14 KiB
C++
Raw Normal View History

/*
*
* Copyright (c) 2003 Lubos Lunak <l.lunak@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "preview.h"
#include <kapplication.h>
#include <klocale.h>
#include <kconfig.h>
#include <kglobal.h>
#include <QLabel>
#include <QStyle>
#include <QPainter>
#include <QMouseEvent>
#include <QResizeEvent>
#include <QVector>
#include <kicon.h>
#include <X11/Xlib.h>
#include <X11/extensions/shape.h>
#include <kdecorationfactory.h>
#include <kdecoration_plugins_p.h>
#include <QX11Info>
#include <kwindowsystem.h>
#include <QTextDocument>
2011-01-30 14:34:42 +00:00
KDecorationPreview::KDecorationPreview(QWidget* parent)
: QWidget(parent)
{
options = new KDecorationPreviewOptions;
2011-01-30 14:34:42 +00:00
bridge[Active] = new KDecorationPreviewBridge(this, true);
bridge[Inactive] = new KDecorationPreviewBridge(this, false);
deco[Active] = deco[Inactive] = NULL;
2011-01-30 14:34:42 +00:00
no_preview = new QLabel(i18n("No preview available.\n"
"Most probably there\n"
"was a problem loading the plugin."), this);
2011-01-30 14:34:42 +00:00
no_preview->setAlignment(Qt::AlignCenter);
2011-01-30 14:34:42 +00:00
setMinimumSize(100, 100);
no_preview->resize(size());
}
KDecorationPreview::~KDecorationPreview()
2011-01-30 14:34:42 +00:00
{
for (int i = 0; i < NumWindows; i++) {
delete deco[i];
delete bridge[i];
}
2011-01-30 14:34:42 +00:00
delete options;
}
2011-01-30 14:34:42 +00:00
bool KDecorationPreview::recreateDecoration(KDecorationPlugins* plugins)
{
for (int i = 0; i < NumWindows; i++) {
delete deco[i]; // deletes also window
2011-01-30 14:34:42 +00:00
deco[i] = plugins->createDecoration(bridge[i]);
deco[i]->init();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
if (deco[Active] == NULL || deco[Inactive] == NULL) {
return false;
2011-01-30 14:34:42 +00:00
}
positionPreviews();
//deco[Inactive]->widget()->show();
//deco[Active]->widget()->show();
//deco[Inactive]->widget()->render( this, deco[Inactive]->widget()->mapToParent( QPoint(0,0) ) );
return true;
2011-01-30 14:34:42 +00:00
}
void KDecorationPreview::enablePreview()
2011-01-30 14:34:42 +00:00
{
no_preview->hide();
2011-01-30 14:34:42 +00:00
}
void KDecorationPreview::disablePreview()
2011-01-30 14:34:42 +00:00
{
delete deco[Active];
delete deco[Inactive];
deco[Active] = deco[Inactive] = NULL;
no_preview->show();
2011-01-30 14:34:42 +00:00
}
KDecorationFactory *KDecorationPreview::factory() const
{
return deco[Active] ? deco[Active]->factory() : 0;
}
2011-01-30 14:34:42 +00:00
void KDecorationPreview::paintEvent(QPaintEvent* e)
{
Q_UNUSED(e);
QPainter painter(this);
QPoint delta = mapTo(window(), QPoint(0, 0));
2011-01-30 14:34:42 +00:00
if (deco[Inactive]) {
QWidget *w = deco[Inactive]->widget();
2011-01-30 14:34:42 +00:00
w->render(&painter, delta + w->mapToParent(QPoint(0, 0)));
}
if (deco[Active]) {
QWidget *w = deco[Active]->widget();
2011-01-30 14:34:42 +00:00
w->render(&painter, delta + w->mapToParent(QPoint(0, 0)));
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
QPixmap KDecorationPreview::preview(QTextDocument* document, QWidget* widget)
{
Q_UNUSED(document);
Q_UNUSED(widget);
QPixmap pixmap(size());
pixmap.fill(Qt::transparent);
2011-01-30 14:34:42 +00:00
if (deco[Inactive]) {
QWidget *w = deco[Inactive]->widget();
2011-01-30 14:34:42 +00:00
w->render(&pixmap, w->mapToParent(QPoint(0, 0)));
}
if (deco[Active]) {
QWidget *w = deco[Active]->widget();
2011-01-30 14:34:42 +00:00
w->render(&pixmap, w->mapToParent(QPoint(0, 0)));
}
2011-01-30 14:34:42 +00:00
return pixmap;
}
2011-01-30 14:34:42 +00:00
void KDecorationPreview::resizeEvent(QResizeEvent* e)
{
QWidget::resizeEvent(e);
positionPreviews();
2011-01-30 14:34:42 +00:00
}
void KDecorationPreview::positionPreviews()
2011-01-30 14:34:42 +00:00
{
int titleBarHeight, leftBorder, rightBorder, xoffset,
dummy1, dummy2, dummy3;
QRect geometry;
QSize size;
2011-01-30 14:34:42 +00:00
no_preview->resize(this->size());
2011-01-30 14:34:42 +00:00
if (!deco[Active] || !deco[Inactive])
return;
// don't have more than one reference to the same dummy variable in one borders() call.
2011-01-30 14:34:42 +00:00
deco[Active]->borders(dummy1, dummy2, titleBarHeight, dummy3);
deco[Inactive]->borders(leftBorder, rightBorder, dummy1, dummy2);
2011-01-30 14:34:42 +00:00
titleBarHeight = qMin(int(titleBarHeight * .9), 30);
xoffset = qMin(qMax(10, QApplication::isRightToLeft()
? leftBorder : rightBorder), 30);
// Resize the active window
2011-01-30 14:34:42 +00:00
size = QSize(width() - xoffset, height() - titleBarHeight)
.expandedTo(deco[Active]->minimumSize());
geometry = QRect(QPoint(0, titleBarHeight), size);
if (KDecorationUnstable *unstable = qobject_cast<KDecorationUnstable *>(deco[Active])) {
int padLeft, padRight, padTop, padBottom;
unstable->padding(padLeft, padRight, padTop, padBottom);
geometry.adjust(-padLeft, -padTop, padRight, padBottom);
}
geometry.adjust(10, 10, -10, -10);
2011-01-30 14:34:42 +00:00
deco[Active]->widget()->setGeometry(QStyle::visualRect(this->layoutDirection(), this->rect(), geometry));
// Resize the inactive window
2011-01-30 14:34:42 +00:00
size = QSize(width() - xoffset, height() - titleBarHeight)
.expandedTo(deco[Inactive]->minimumSize());
geometry = QRect(QPoint(xoffset, 0), size);
if (KDecorationUnstable *unstable = qobject_cast<KDecorationUnstable *>(deco[Inactive])) {
int padLeft, padRight, padTop, padBottom;
unstable->padding(padLeft, padRight, padTop, padBottom);
geometry.adjust(-padLeft, -padTop, padRight, padBottom);
}
geometry.adjust(10, 10, -10, -10);
2011-01-30 14:34:42 +00:00
deco[Inactive]->widget()->setGeometry(QStyle::visualRect(this->layoutDirection(), this->rect(), geometry));
}
2011-01-30 14:34:42 +00:00
void KDecorationPreview::setPreviewMask(const QRegion& reg, int mode, bool active)
{
QWidget *widget = active ? deco[Active]->widget() : deco[Inactive]->widget();
// FRAME duped from client.cpp
2011-01-30 14:34:42 +00:00
if (mode == Unsorted) {
XShapeCombineRegion(QX11Info::display(), widget->winId(), ShapeBounding, 0, 0,
reg.handle(), ShapeSet);
} else {
QVector< QRect > rects = reg.rects();
2011-01-30 14:34:42 +00:00
XRectangle* xrects = new XRectangle[ rects.count()];
for (int i = 0;
i < rects.count();
++i) {
xrects[ i ].x = rects[ i ].x();
xrects[ i ].y = rects[ i ].y();
xrects[ i ].width = rects[ i ].width();
xrects[ i ].height = rects[ i ].height();
}
2011-01-30 14:34:42 +00:00
XShapeCombineRectangles(QX11Info::display(), widget->winId(), ShapeBounding, 0, 0,
xrects, rects.count(), ShapeSet, mode);
delete[] xrects;
}
2011-01-30 14:34:42 +00:00
if (active)
mask = reg; // keep shape of the active window for unobscuredRegion()
}
2011-01-30 14:34:42 +00:00
QRect KDecorationPreview::windowGeometry(bool active) const
{
QWidget *widget = active ? deco[Active]->widget() : deco[Inactive]->widget();
return widget->geometry();
2011-01-30 14:34:42 +00:00
}
void KDecorationPreview::setTempBorderSize(KDecorationPlugins* plugin, KDecorationDefines::BorderSize size)
2011-01-30 14:34:42 +00:00
{
options->setCustomBorderSize(size);
2011-01-30 14:34:42 +00:00
if (plugin->factory()->reset(KDecorationDefines::SettingBorder)) {
// can't handle the change, recreate decorations then
recreateDecoration(plugin);
2011-01-30 14:34:42 +00:00
} else {
// handles the update, only update position...
positionPreviews();
}
2011-01-30 14:34:42 +00:00
}
void KDecorationPreview::setTempButtons(KDecorationPlugins* plugin, bool customEnabled, const QString &left, const QString &right)
2011-01-30 14:34:42 +00:00
{
options->setCustomTitleButtonsEnabled(customEnabled);
options->setCustomTitleButtons(left, right);
2011-01-30 14:34:42 +00:00
if (plugin->factory()->reset(KDecorationDefines::SettingButtons)) {
// can't handle the change, recreate decorations then
recreateDecoration(plugin);
2011-01-30 14:34:42 +00:00
} else {
// handles the update, only update position...
positionPreviews();
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
QRegion KDecorationPreview::unobscuredRegion(bool active, const QRegion& r) const
{
if (active) // this one is not obscured
return r;
2011-01-30 14:34:42 +00:00
else {
// copied from KWin core's code
QRegion ret = r;
QRegion r2 = mask;
2011-01-30 14:34:42 +00:00
if (r2.isEmpty())
r2 = QRegion(windowGeometry(true));
r2.translate(windowGeometry(true).x() - windowGeometry(false).x(),
windowGeometry(true).y() - windowGeometry(false).y());
ret -= r2;
return ret;
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
KDecorationPreviewBridge::KDecorationPreviewBridge(KDecorationPreview* p, bool a)
: preview(p), active(a)
{
}
QWidget* KDecorationPreviewBridge::initialParentWidget() const
2011-01-30 14:34:42 +00:00
{
return preview;
2011-01-30 14:34:42 +00:00
}
Qt::WFlags KDecorationPreviewBridge::initialWFlags() const
2011-01-30 14:34:42 +00:00
{
return 0;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isActive() const
2011-01-30 14:34:42 +00:00
{
return active;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isCloseable() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isMaximizable() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
KDecoration::MaximizeMode KDecorationPreviewBridge::maximizeMode() const
2011-01-30 14:34:42 +00:00
{
return KDecoration::MaximizeRestore;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isMinimizable() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::providesContextHelp() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
int KDecorationPreviewBridge::desktop() const
2011-01-30 14:34:42 +00:00
{
return 1;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isModal() const
2011-01-30 14:34:42 +00:00
{
return false;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isShadeable() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isShade() const
2011-01-30 14:34:42 +00:00
{
return false;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isSetShade() const
2011-01-30 14:34:42 +00:00
{
return false;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::keepAbove() const
2011-01-30 14:34:42 +00:00
{
return false;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::keepBelow() const
2011-01-30 14:34:42 +00:00
{
return false;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isMovable() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
bool KDecorationPreviewBridge::isResizable() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
NET::WindowType KDecorationPreviewBridge::windowType(unsigned long) const
{
return NET::Normal;
2011-01-30 14:34:42 +00:00
}
QIcon KDecorationPreviewBridge::icon() const
2011-01-30 14:34:42 +00:00
{
return KIcon("xorg");
}
QString KDecorationPreviewBridge::caption() const
2011-01-30 14:34:42 +00:00
{
return active ? i18n("Active Window") : i18n("Inactive Window");
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::processMousePressEvent(QMouseEvent*)
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::showWindowMenu(const QRect &)
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::showWindowMenu(const QPoint &)
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::performWindowOperation(WindowOperation)
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::setMask(const QRegion& reg, int mode)
{
preview->setPreviewMask(reg, mode, active);
}
bool KDecorationPreviewBridge::isPreview() const
2011-01-30 14:34:42 +00:00
{
return true;
2011-01-30 14:34:42 +00:00
}
QRect KDecorationPreviewBridge::geometry() const
2011-01-30 14:34:42 +00:00
{
return preview->windowGeometry(active);
}
QRect KDecorationPreviewBridge::iconGeometry() const
2011-01-30 14:34:42 +00:00
{
return QRect();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
QRegion KDecorationPreviewBridge::unobscuredRegion(const QRegion& r) const
{
return preview->unobscuredRegion(active, r);
}
WId KDecorationPreviewBridge::windowId() const
2011-01-30 14:34:42 +00:00
{
return 0; // no decorated window
2011-01-30 14:34:42 +00:00
}
void KDecorationPreviewBridge::closeWindow()
2011-01-30 14:34:42 +00:00
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::maximize(MaximizeMode)
{
}
void KDecorationPreviewBridge::minimize()
2011-01-30 14:34:42 +00:00
{
}
void KDecorationPreviewBridge::showContextHelp()
2011-01-30 14:34:42 +00:00
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::setDesktop(int)
{
}
void KDecorationPreviewBridge::titlebarDblClickOperation()
2011-01-30 14:34:42 +00:00
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::titlebarMouseWheelOperation(int)
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::setShade(bool)
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::setKeepAbove(bool)
{
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::setKeepBelow(bool)
{
}
int KDecorationPreviewBridge::currentDesktop() const
2011-01-30 14:34:42 +00:00
{
return 1;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void KDecorationPreviewBridge::grabXServer(bool)
{
}
bool KDecorationPreviewBridge::compositingActive() const
2011-01-30 14:34:42 +00:00
{
return KWindowSystem::compositingActive();
2011-01-30 14:34:42 +00:00
}
QRect KDecorationPreviewBridge::transparentRect() const
2011-01-30 14:34:42 +00:00
{
return QRect();
2011-01-30 14:34:42 +00:00
}
// Window tabbing
int KDecorationPreviewBridge::tabCount() const
2011-01-30 14:34:42 +00:00
{
return 1;
}
QString KDecorationPreviewBridge::caption(int) const
{
return active ? "Active Window" : "Inactive Window";
2011-01-30 14:34:42 +00:00
}
QIcon KDecorationPreviewBridge::icon(int) const
2011-01-30 14:34:42 +00:00
{
return icon();
2011-01-30 14:34:42 +00:00
}
long KDecorationPreviewBridge::tabId(int) const
2011-01-30 14:34:42 +00:00
{
return 0;
2011-01-30 14:34:42 +00:00
}
long KDecorationPreviewBridge::currentTabId() const
2011-01-30 14:34:42 +00:00
{
return 0;
2011-01-30 14:34:42 +00:00
}
void KDecorationPreviewBridge::setCurrentTab(long)
2011-01-30 14:34:42 +00:00
{
}
void KDecorationPreviewBridge::tab_A_before_B(long, long)
2011-01-30 14:34:42 +00:00
{
}
void KDecorationPreviewBridge::tab_A_behind_B(long, long)
2011-01-30 14:34:42 +00:00
{
}
void KDecorationPreviewBridge::untab(long, const QRect&)
2011-01-30 14:34:42 +00:00
{
}
void KDecorationPreviewBridge::closeTab(long)
2011-01-30 14:34:42 +00:00
{
}
void KDecorationPreviewBridge::closeTabGroup()
2011-01-30 14:34:42 +00:00
{
}
void KDecorationPreviewBridge::showWindowMenu(const QPoint &, long)
2011-01-30 14:34:42 +00:00
{
}
2011-01-30 14:34:42 +00:00
KDecoration::WindowOperation KDecorationPreviewBridge::buttonToWindowOperation(Qt::MouseButtons)
{
return KDecoration::NoOp;
2011-01-30 14:34:42 +00:00
}
KDecorationPreviewOptions::KDecorationPreviewOptions()
2011-01-30 14:34:42 +00:00
{
customBorderSize = BordersCount; // invalid
customButtonsChanged = false; // invalid
customButtons = true;
customTitleButtonsLeft.clear(); // invalid
customTitleButtonsRight.clear(); // invalid
updateSettings();
2011-01-30 14:34:42 +00:00
}
KDecorationPreviewOptions::~KDecorationPreviewOptions()
2011-01-30 14:34:42 +00:00
{
}
unsigned long KDecorationPreviewOptions::updateSettings()
2011-01-30 14:34:42 +00:00
{
KConfig cfg("kwinrc");
unsigned long changed = 0;
2011-01-30 14:34:42 +00:00
changed |= KDecorationOptions::updateSettings(&cfg);
// set custom border size/buttons
if (customBorderSize != BordersCount)
2011-01-30 14:34:42 +00:00
setBorderSize(customBorderSize);
if (customButtonsChanged)
2011-01-30 14:34:42 +00:00
setCustomButtonPositions(customButtons);
if (customButtons) {
2011-01-30 14:34:42 +00:00
if (!customTitleButtonsLeft.isNull())
setTitleButtonsLeft(customTitleButtonsLeft);
if (!customTitleButtonsRight.isNull())
setTitleButtonsRight(customTitleButtonsRight);
} else {
2011-01-30 14:34:42 +00:00
setTitleButtonsLeft(KDecorationOptions::defaultTitleButtonsLeft());
setTitleButtonsRight(KDecorationOptions::defaultTitleButtonsRight());
}
return changed;
2011-01-30 14:34:42 +00:00
}
void KDecorationPreviewOptions::setCustomBorderSize(BorderSize size)
2011-01-30 14:34:42 +00:00
{
customBorderSize = size;
updateSettings();
2011-01-30 14:34:42 +00:00
}
void KDecorationPreviewOptions::setCustomTitleButtonsEnabled(bool enabled)
{
customButtonsChanged = true;
customButtons = enabled;
updateSettings();
}
void KDecorationPreviewOptions::setCustomTitleButtons(const QString &left, const QString &right)
2011-01-30 14:34:42 +00:00
{
customTitleButtonsLeft = left;
customTitleButtonsRight = right;
updateSettings();
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
bool KDecorationPreviewPlugins::provides(Requirement)
{
return false;
2011-01-30 14:34:42 +00:00
}
#include "preview.moc"