1 /***************************************************************************
2 * Copyright (C) 2008 by Fredrik Höglund <fredrik@kde.org> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
22 #include <QApplication>
34 # include <X11/Xlib.h>
35 # include <X11/extensions/Xrender.h>
36 # include <X11/extensions/shape.h>
39 #include "ktooltip_p.h"
42 // compile with XShape older than 1.0
44 const int ShapeInput
= 2;
48 class KToolTipItemPrivate
51 QMap
<int, QVariant
> map
;
55 KToolTipItem::KToolTipItem(const QString
&text
, int type
)
56 : d(new KToolTipItemPrivate
)
58 d
->map
[Qt::DisplayRole
] = text
;
62 KToolTipItem::KToolTipItem(const QIcon
&icon
, const QString
&text
, int type
)
63 : d(new KToolTipItemPrivate
)
65 d
->map
[Qt::DecorationRole
] = icon
;
66 d
->map
[Qt::DisplayRole
] = text
;
70 KToolTipItem::~KToolTipItem()
75 int KToolTipItem::type() const
80 QString
KToolTipItem::text() const
82 return data(Qt::DisplayRole
).toString();
85 QIcon
KToolTipItem::icon() const
87 return qvariant_cast
<QIcon
>(data(Qt::DecorationRole
));
90 QVariant
KToolTipItem::data(int role
) const
92 return d
->map
.value(role
);
95 void KToolTipItem::setData(int role
, const QVariant
&data
)
102 // ----------------------------------------------------------------------------
105 KStyleOptionToolTip::KStyleOptionToolTip()
106 : fontMetrics(QApplication::font())
111 // ----------------------------------------------------------------------------
115 KToolTipDelegate::KToolTipDelegate() : QObject()
119 KToolTipDelegate::~KToolTipDelegate()
123 QSize
KToolTipDelegate::sizeHint(const KStyleOptionToolTip
*option
, const KToolTipItem
*item
) const
126 size
.rwidth() = option
->fontMetrics
.width(item
->text());
127 size
.rheight() = option
->fontMetrics
.lineSpacing();
129 QIcon icon
= item
->icon();
130 if (!icon
.isNull()) {
131 const QSize iconSize
= icon
.actualSize(option
->decorationSize
);
132 size
.rwidth() += iconSize
.width() + 4;
133 size
.rheight() = qMax(size
.height(), iconSize
.height());
136 return size
+ QSize(20, 20);
140 void KToolTipDelegate::paint(QPainter
*painter
, const KStyleOptionToolTip
*option
,
141 const KToolTipItem
*item
) const
143 bool haveAlpha
= haveAlphaChannel();
144 painter
->setRenderHint(QPainter::Antialiasing
);
148 path
.addRoundRect(option
->rect
.adjusted(0, 0, -1, -1), 25);
150 path
.addRect(option
->rect
.adjusted(0, 0, -1, -1));
152 #if QT_VERSION >= 0x040400
153 QColor color
= option
->palette
.color(QPalette::ToolTipBase
);
155 QColor color
= option
->palette
.color(QPalette::Base
);
157 QColor from
= color
.lighter(105);
158 QColor to
= color
.darker(120);
160 QLinearGradient
gradient(0, 0, 0, 1);
161 gradient
.setCoordinateMode(QGradient::ObjectBoundingMode
);
162 gradient
.setColorAt(0, from
);
163 gradient
.setColorAt(1, to
);
165 painter
->translate(.5, .5);
166 painter
->setPen(QPen(Qt::black
, 1));
167 painter
->setBrush(gradient
);
168 painter
->drawPath(path
);
169 painter
->translate(-.5, -.5);
172 QLinearGradient
mask(0, 0, 0, 1);
173 gradient
.setCoordinateMode(QGradient::ObjectBoundingMode
);
174 gradient
.setColorAt(0, QColor(0, 0, 0, 192));
175 gradient
.setColorAt(1, QColor(0, 0, 0, 72));
176 painter
->setCompositionMode(QPainter::CompositionMode_DestinationIn
);
177 painter
->fillRect(option
->rect
, gradient
);
178 painter
->setCompositionMode(QPainter::CompositionMode_SourceOver
);
181 QRect textRect
= option
->rect
.adjusted(10, 10, -10, -10);
183 QIcon icon
= item
->icon();
184 if (!icon
.isNull()) {
185 const QSize iconSize
= icon
.actualSize(option
->decorationSize
);
186 painter
->drawPixmap(textRect
.topLeft(), icon
.pixmap(iconSize
));
187 textRect
.adjust(iconSize
.width() + 4, 0, 0, 0);
189 painter
->drawText(textRect
, Qt::AlignLeft
| Qt::AlignVCenter
, item
->text());
192 QRegion
KToolTipDelegate::inputShape(const KStyleOptionToolTip
*option
) const
194 return QRegion(option
->rect
);
197 QRegion
KToolTipDelegate::shapeMask(const KStyleOptionToolTip
*option
) const
199 return QRegion(option
->rect
);
202 bool KToolTipDelegate::haveAlphaChannel() const
204 return KToolTipManager::instance()->haveAlphaChannel();
209 // ----------------------------------------------------------------------------
213 class KAbstractToolTipLabel
216 KAbstractToolTipLabel() {}
217 virtual ~KAbstractToolTipLabel() {}
219 virtual void showTip(const QPoint
&pos
, const KToolTipItem
*item
) = 0;
220 virtual void moveTip(const QPoint
&pos
) = 0;
221 virtual void hideTip() = 0;
224 KStyleOptionToolTip
styleOption() const;
225 KToolTipDelegate
*delegate() const;
228 KStyleOptionToolTip
KAbstractToolTipLabel::styleOption() const
230 KStyleOptionToolTip option
;
231 KToolTipManager::instance()->initStyleOption(&option
);
235 KToolTipDelegate
*KAbstractToolTipLabel::delegate() const
237 return KToolTipManager::instance()->delegate();
241 // ----------------------------------------------------------------------------
245 class QWidgetLabel
: public QWidget
, public KAbstractToolTipLabel
248 QWidgetLabel() : QWidget(0, Qt::ToolTip
) {}
249 void showTip(const QPoint
&pos
, const KToolTipItem
*item
);
250 void moveTip(const QPoint
&pos
);
254 void paintEvent(QPaintEvent
*);
255 QSize
sizeHint() const;
258 const KToolTipItem
*currentItem
;
261 void QWidgetLabel::showTip(const QPoint
&pos
, const KToolTipItem
*item
)
268 void QWidgetLabel::hideTip()
274 void QWidgetLabel::moveTip(const QPoint
&pos
)
279 void QWidgetLabel::paintEvent(QPaintEvent
*)
281 KStyleOptionToolTip option
= styleOption();
282 option
.rect
= rect();
284 setMask(delegate()->shapeMask(&option
));
287 p
.setFont(option
.font
);
288 p
.setPen(QPen(option
.palette
.brush(QPalette::Text
), 0));
289 delegate()->paint(&p
, &option
, currentItem
);
292 QSize
QWidgetLabel::sizeHint() const
297 KStyleOptionToolTip option
= styleOption();
298 return delegate()->sizeHint(&option
, currentItem
);
303 // ----------------------------------------------------------------------------
309 // X11 specific label that displays the tip in an ARGB window.
310 class ArgbLabel
: public KAbstractToolTipLabel
313 ArgbLabel(Visual
*visual
, int depth
);
316 void showTip(const QPoint
&pos
, const KToolTipItem
*item
);
317 void moveTip(const QPoint
&pos
);
327 ArgbLabel::ArgbLabel(Visual
*visual
, int depth
)
329 Display
*dpy
= QX11Info::display();
330 Window root
= QX11Info::appRootWindow();
331 colormap
= XCreateColormap(dpy
, QX11Info::appRootWindow(), visual
, AllocNone
);
333 XSetWindowAttributes attr
;
334 attr
.border_pixel
= 0;
335 attr
.background_pixel
= 0;
336 attr
.colormap
= colormap
;
337 attr
.override_redirect
= True
;
339 window
= XCreateWindow(dpy
, root
, 0, 0, 1, 1, 0, depth
, InputOutput
, visual
,
340 CWBorderPixel
| CWBackPixel
| CWColormap
|
341 CWOverrideRedirect
, &attr
);
343 // ### TODO: Set the WM hints so KWin can identify this window as a
346 XRenderPictFormat
*format
= XRenderFindVisualFormat(dpy
, visual
);
347 picture
= XRenderCreatePicture(dpy
, window
, format
, 0, 0);
352 ArgbLabel::~ArgbLabel()
354 Display
*dpy
= QX11Info::display();
355 XRenderFreePicture(dpy
, picture
);
356 XDestroyWindow(dpy
, window
);
357 XFreeColormap(dpy
, colormap
);
360 void ArgbLabel::showTip(const QPoint
&pos
, const KToolTipItem
*item
)
362 Display
*dpy
= QX11Info::display();
363 KStyleOptionToolTip option
= styleOption();
364 const QSize size
= delegate()->sizeHint(&option
, item
);
365 option
.rect
= QRect(QPoint(), size
);
367 QPixmap
pixmap(size
);
368 pixmap
.fill(Qt::transparent
);
371 p
.setFont(option
.font
);
372 p
.setPen(QPen(option
.palette
.brush(QPalette::Text
), 0));
373 delegate()->paint(&p
, &option
, item
);
375 // Resize, position and show the window.
376 XMoveResizeWindow(dpy
, window
, pos
.x(), pos
.y(), size
.width(), size
.height());
378 if (KToolTipManager::instance()->haveAlphaChannel()) {
379 const QRegion region
= delegate()->inputShape(&option
);
380 XShapeCombineRegion(dpy
, window
, ShapeInput
, 0, 0, region
.handle(), ShapeSet
);
382 const QRegion region
= delegate()->shapeMask(&option
);
383 XShapeCombineRegion(dpy
, window
, ShapeBounding
, 0, 0, region
.handle(), ShapeSet
);
386 XMapWindow(dpy
, window
);
388 // Blit the pixmap with the tip contents to the window.
389 // Since the window is override-redirect and an ARGB32 window,
390 // which always has an offscreen pixmap, there's no need to
391 // wait for an Expose event, or to process those.
392 XRenderComposite(dpy
, PictOpSrc
, pixmap
.x11PictureHandle(), None
,
393 picture
, 0, 0, 0, 0, 0, 0, size
.width(), size
.height());
398 void ArgbLabel::moveTip(const QPoint
&pos
)
401 XMoveWindow(QX11Info::display(), window
, pos
.x(), pos
.y());
404 void ArgbLabel::hideTip()
407 Display
*dpy
= QX11Info::display();
408 XUnmapWindow(dpy
, window
);
418 // ----------------------------------------------------------------------------
423 KToolTipManager
*KToolTipManager::s_instance
= 0;
425 KToolTipManager::KToolTipManager()
426 : label(0), currentItem(0), m_delegate(0)
429 Display
*dpy
= QX11Info::display();
430 int screen
= DefaultScreen(dpy
);
431 int depth
= DefaultDepth(dpy
, screen
);
432 Visual
*visual
= DefaultVisual(dpy
, screen
);
433 net_wm_cm_s0
= XInternAtom(dpy
, "_NET_WM_CM_S0", False
);
434 haveArgbVisual
= false;
438 templ
.screen
= screen
;
440 templ
.c_class
= TrueColor
;
441 XVisualInfo
*xvi
= XGetVisualInfo(dpy
, VisualScreenMask
| VisualDepthMask
|
442 VisualClassMask
, &templ
, &nvi
);
444 for (int i
= 0; i
< nvi
; ++i
)
446 XRenderPictFormat
*format
= XRenderFindVisualFormat(dpy
, xvi
[i
].visual
);
447 if (format
->type
== PictTypeDirect
&& format
->direct
.alphaMask
)
449 visual
= xvi
[i
].visual
;
450 depth
= xvi
[i
].depth
;
451 haveArgbVisual
= true;
457 label
= new ArgbLabel(visual
, depth
);
460 label
= new QWidgetLabel();
463 KToolTipManager::~KToolTipManager()
469 void KToolTipManager::showTip(const QPoint
&pos
, KToolTipItem
*item
)
472 label
->showTip(pos
, item
);
476 void KToolTipManager::hideTip()
483 void KToolTipManager::initStyleOption(KStyleOptionToolTip
*option
) const
485 option
->direction
= QApplication::layoutDirection();
486 option
->fontMetrics
= QFontMetrics(QToolTip::font());
487 option
->activeCorner
= KStyleOptionToolTip::TopLeftCorner
;
488 option
->palette
= QToolTip::palette();
489 option
->font
= QToolTip::font();
490 option
->rect
= QRect();
491 option
->state
= QStyle::State_None
;
492 option
->decorationSize
= QSize(32, 32);
495 bool KToolTipManager::haveAlphaChannel() const
498 // ### This is a synchronous call - ideally we'd use a selection
499 // watcher to avoid it.
500 return haveArgbVisual
&&
501 XGetSelectionOwner(QX11Info::display(), net_wm_cm_s0
) != None
;
507 void KToolTipManager::setDelegate(KToolTipDelegate
*delegate
)
509 m_delegate
= delegate
;
512 KToolTipDelegate
*KToolTipManager::delegate() const
519 // ----------------------------------------------------------------------------
525 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
, const QRect
&rect
)
529 KToolTipItem
*item
= new KToolTipItem(text
);
530 KToolTipManager::instance()->showTip(pos
, item
);
533 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
)
535 showText(pos
, text
, widget
, QRect());
538 void showTip(const QPoint
&pos
, KToolTipItem
*item
)
540 KToolTipManager::instance()->showTip(pos
, item
);
545 KToolTipManager::instance()->hideTip();
548 void setToolTipDelegate(KToolTipDelegate
*delegate
)
550 KToolTipManager::instance()->setDelegate(delegate
);