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"
43 class KToolTipItemPrivate
46 QMap
<int, QVariant
> map
;
50 KToolTipItem::KToolTipItem(const QString
&text
, int type
)
51 : d(new KToolTipItemPrivate
)
53 d
->map
[Qt::DisplayRole
] = text
;
57 KToolTipItem::KToolTipItem(const QIcon
&icon
, const QString
&text
, int type
)
58 : d(new KToolTipItemPrivate
)
60 d
->map
[Qt::DecorationRole
] = icon
;
61 d
->map
[Qt::DisplayRole
] = text
;
65 KToolTipItem::~KToolTipItem()
69 int KToolTipItem::type() const
74 QString
KToolTipItem::text() const
76 return data(Qt::DisplayRole
).toString();
79 QIcon
KToolTipItem::icon() const
81 return qvariant_cast
<QIcon
>(data(Qt::DecorationRole
));
84 QVariant
KToolTipItem::data(int role
) const
86 return d
->map
.value(role
);
89 void KToolTipItem::setData(int role
, const QVariant
&data
)
96 // ----------------------------------------------------------------------------
99 KStyleOptionToolTip::KStyleOptionToolTip()
100 : fontMetrics(QApplication::font())
105 // ----------------------------------------------------------------------------
109 KToolTipDelegate::KToolTipDelegate() : QObject()
113 KToolTipDelegate::~KToolTipDelegate()
117 QSize
KToolTipDelegate::sizeHint(const KStyleOptionToolTip
*option
, const KToolTipItem
*item
) const
120 size
.rwidth() = option
->fontMetrics
.width(item
->text());
121 size
.rheight() = option
->fontMetrics
.lineSpacing();
123 QIcon icon
= item
->icon();
124 if (!icon
.isNull()) {
125 const QSize iconSize
= icon
.actualSize(option
->decorationSize
);
126 size
.rwidth() += iconSize
.width() + 4;
127 size
.rheight() = qMax(size
.height(), iconSize
.height());
130 return size
+ QSize(20, 20);
134 void KToolTipDelegate::paint(QPainter
*painter
, const KStyleOptionToolTip
*option
,
135 const KToolTipItem
*item
) const
137 bool haveAlpha
= haveAlphaChannel();
138 painter
->setRenderHint(QPainter::Antialiasing
);
142 path
.addRoundRect(option
->rect
.adjusted(0, 0, -1, -1), 25);
144 path
.addRect(option
->rect
.adjusted(0, 0, -1, -1));
146 #if QT_VERSION >= 0x040400
147 QColor color
= option
->palette
.color(QPalette::ToolTipBase
);
149 QColor color
= option
->palette
.color(QPalette::Base
);
151 QColor from
= color
.lighter(105);
152 QColor to
= color
.darker(120);
154 QLinearGradient
gradient(0, 0, 0, 1);
155 gradient
.setCoordinateMode(QGradient::ObjectBoundingMode
);
156 gradient
.setColorAt(0, from
);
157 gradient
.setColorAt(1, to
);
159 painter
->translate(.5, .5);
160 painter
->setPen(QPen(Qt::black
, 1));
161 painter
->setBrush(gradient
);
162 painter
->drawPath(path
);
163 painter
->translate(-.5, -.5);
166 QLinearGradient
mask(0, 0, 0, 1);
167 gradient
.setCoordinateMode(QGradient::ObjectBoundingMode
);
168 gradient
.setColorAt(0, QColor(0, 0, 0, 192));
169 gradient
.setColorAt(1, QColor(0, 0, 0, 72));
170 painter
->setCompositionMode(QPainter::CompositionMode_DestinationIn
);
171 painter
->fillRect(option
->rect
, gradient
);
172 painter
->setCompositionMode(QPainter::CompositionMode_SourceOver
);
175 QRect textRect
= option
->rect
.adjusted(10, 10, -10, -10);
177 QIcon icon
= item
->icon();
178 if (!icon
.isNull()) {
179 const QSize iconSize
= icon
.actualSize(option
->decorationSize
);
180 painter
->drawPixmap(textRect
.topLeft(), icon
.pixmap(iconSize
));
181 textRect
.adjust(iconSize
.width() + 4, 0, 0, 0);
183 painter
->drawText(textRect
, Qt::AlignLeft
| Qt::AlignVCenter
, item
->text());
186 QRegion
KToolTipDelegate::inputShape(const KStyleOptionToolTip
*option
) const
188 return QRegion(option
->rect
);
191 QRegion
KToolTipDelegate::shapeMask(const KStyleOptionToolTip
*option
) const
193 return QRegion(option
->rect
);
196 bool KToolTipDelegate::haveAlphaChannel() const
198 return KToolTipManager::instance()->haveAlphaChannel();
203 // ----------------------------------------------------------------------------
207 class KAbstractToolTipLabel
210 KAbstractToolTipLabel() {}
211 virtual ~KAbstractToolTipLabel() {}
213 virtual void showTip(const QPoint
&pos
, const KToolTipItem
*item
) = 0;
214 virtual void moveTip(const QPoint
&pos
) = 0;
215 virtual void hideTip() = 0;
218 KStyleOptionToolTip
styleOption() const;
219 KToolTipDelegate
*delegate() const;
222 KStyleOptionToolTip
KAbstractToolTipLabel::styleOption() const
224 KStyleOptionToolTip option
;
225 KToolTipManager::instance()->initStyleOption(&option
);
229 KToolTipDelegate
*KAbstractToolTipLabel::delegate() const
231 return KToolTipManager::instance()->delegate();
235 // ----------------------------------------------------------------------------
239 class QWidgetLabel
: public QWidget
, public KAbstractToolTipLabel
242 QWidgetLabel() : QWidget(0, Qt::ToolTip
) {}
243 void showTip(const QPoint
&pos
, const KToolTipItem
*item
);
244 void moveTip(const QPoint
&pos
);
248 void paintEvent(QPaintEvent
*);
249 QSize
sizeHint() const;
252 const KToolTipItem
*currentItem
;
255 void QWidgetLabel::showTip(const QPoint
&pos
, const KToolTipItem
*item
)
262 void QWidgetLabel::hideTip()
268 void QWidgetLabel::moveTip(const QPoint
&pos
)
273 void QWidgetLabel::paintEvent(QPaintEvent
*)
275 KStyleOptionToolTip option
= styleOption();
276 option
.rect
= rect();
278 setMask(delegate()->shapeMask(&option
));
281 p
.setFont(option
.font
);
282 p
.setPen(QPen(option
.palette
.brush(QPalette::Text
), 0));
283 delegate()->paint(&p
, &option
, currentItem
);
286 QSize
QWidgetLabel::sizeHint() const
291 KStyleOptionToolTip option
= styleOption();
292 return delegate()->sizeHint(&option
, currentItem
);
297 // ----------------------------------------------------------------------------
303 // X11 specific label that displays the tip in an ARGB window.
304 class ArgbLabel
: public KAbstractToolTipLabel
307 ArgbLabel(Visual
*visual
, int depth
);
310 void showTip(const QPoint
&pos
, const KToolTipItem
*item
);
311 void moveTip(const QPoint
&pos
);
321 ArgbLabel::ArgbLabel(Visual
*visual
, int depth
)
323 Display
*dpy
= QX11Info::display();
324 Window root
= QX11Info::appRootWindow();
325 colormap
= XCreateColormap(dpy
, QX11Info::appRootWindow(), visual
, AllocNone
);
327 XSetWindowAttributes attr
;
328 attr
.border_pixel
= 0;
329 attr
.background_pixel
= 0;
330 attr
.colormap
= colormap
;
331 attr
.override_redirect
= True
;
333 window
= XCreateWindow(dpy
, root
, 0, 0, 1, 1, 0, depth
, InputOutput
, visual
,
334 CWBorderPixel
| CWBackPixel
| CWColormap
|
335 CWOverrideRedirect
, &attr
);
337 // ### TODO: Set the WM hints so KWin can identify this window as a
340 XRenderPictFormat
*format
= XRenderFindVisualFormat(dpy
, visual
);
341 picture
= XRenderCreatePicture(dpy
, window
, format
, 0, 0);
346 ArgbLabel::~ArgbLabel()
348 Display
*dpy
= QX11Info::display();
349 XRenderFreePicture(dpy
, picture
);
350 XDestroyWindow(dpy
, window
);
351 XFreeColormap(dpy
, colormap
);
354 void ArgbLabel::showTip(const QPoint
&pos
, const KToolTipItem
*item
)
356 Display
*dpy
= QX11Info::display();
357 KStyleOptionToolTip option
= styleOption();
358 const QSize size
= delegate()->sizeHint(&option
, item
);
359 option
.rect
= QRect(QPoint(), size
);
361 QPixmap
pixmap(size
);
362 pixmap
.fill(Qt::transparent
);
365 p
.setFont(option
.font
);
366 p
.setPen(QPen(option
.palette
.brush(QPalette::Text
), 0));
367 delegate()->paint(&p
, &option
, item
);
369 // Resize, position and show the window.
370 XMoveResizeWindow(dpy
, window
, pos
.x(), pos
.y(), size
.width(), size
.height());
372 if (KToolTipManager::instance()->haveAlphaChannel()) {
373 const QRegion region
= delegate()->inputShape(&option
);
374 XShapeCombineRegion(dpy
, window
, ShapeInput
, 0, 0, region
.handle(), ShapeSet
);
376 const QRegion region
= delegate()->shapeMask(&option
);
377 XShapeCombineRegion(dpy
, window
, ShapeBounding
, 0, 0, region
.handle(), ShapeSet
);
380 XMapWindow(dpy
, window
);
382 // Blit the pixmap with the tip contents to the window.
383 // Since the window is override-redirect and an ARGB32 window,
384 // which always has an offscreen pixmap, there's no need to
385 // wait for an Expose event, or to process those.
386 XRenderComposite(dpy
, PictOpSrc
, pixmap
.x11PictureHandle(), None
,
387 picture
, 0, 0, 0, 0, 0, 0, size
.width(), size
.height());
392 void ArgbLabel::moveTip(const QPoint
&pos
)
395 XMoveWindow(QX11Info::display(), window
, pos
.x(), pos
.y());
398 void ArgbLabel::hideTip()
401 Display
*dpy
= QX11Info::display();
402 XUnmapWindow(dpy
, window
);
412 // ----------------------------------------------------------------------------
417 KToolTipManager
*KToolTipManager::s_instance
= 0;
419 KToolTipManager::KToolTipManager()
420 : label(0), currentItem(0)
423 Display
*dpy
= QX11Info::display();
424 int screen
= DefaultScreen(dpy
);
425 int depth
= DefaultDepth(dpy
, screen
);
426 Visual
*visual
= DefaultVisual(dpy
, screen
);
427 net_wm_cm_s0
= XInternAtom(dpy
, "_NET_WM_CM_S0", False
);
428 haveArgbVisual
= false;
432 templ
.screen
= screen
;
434 templ
.c_class
= TrueColor
;
435 XVisualInfo
*xvi
= XGetVisualInfo(dpy
, VisualScreenMask
| VisualDepthMask
|
436 VisualClassMask
, &templ
, &nvi
);
438 for (int i
= 0; i
< nvi
; ++i
)
440 XRenderPictFormat
*format
= XRenderFindVisualFormat(dpy
, xvi
[i
].visual
);
441 if (format
->type
== PictTypeDirect
&& format
->direct
.alphaMask
)
443 visual
= xvi
[i
].visual
;
444 depth
= xvi
[i
].depth
;
445 haveArgbVisual
= true;
451 label
= new ArgbLabel(visual
, depth
);
454 label
= new QWidgetLabel();
457 KToolTipManager::~KToolTipManager()
463 void KToolTipManager::showTip(const QPoint
&pos
, KToolTipItem
*item
)
466 label
->showTip(pos
, item
);
470 void KToolTipManager::hideTip()
477 void KToolTipManager::initStyleOption(KStyleOptionToolTip
*option
) const
479 option
->direction
= QApplication::layoutDirection();
480 option
->fontMetrics
= QFontMetrics(QToolTip::font());
481 option
->activeCorner
= KStyleOptionToolTip::TopLeftCorner
;
482 option
->palette
= QToolTip::palette();
483 option
->font
= QToolTip::font();
484 option
->rect
= QRect();
485 option
->state
= QStyle::State_None
;
486 option
->decorationSize
= QSize(32, 32);
489 bool KToolTipManager::haveAlphaChannel() const
492 // ### This is a synchronous call - ideally we'd use a selection
493 // watcher to avoid it.
494 return haveArgbVisual
&&
495 XGetSelectionOwner(QX11Info::display(), net_wm_cm_s0
) != None
;
501 void KToolTipManager::setDelegate(KToolTipDelegate
*delegate
)
503 m_delegate
= delegate
;
506 KToolTipDelegate
*KToolTipManager::delegate() const
513 // ----------------------------------------------------------------------------
519 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
, const QRect
&rect
)
523 KToolTipItem
*item
= new KToolTipItem(text
);
524 KToolTipManager::instance()->showTip(pos
, item
);
527 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
)
529 showText(pos
, text
, widget
, QRect());
532 void showTip(const QPoint
&pos
, KToolTipItem
*item
)
534 KToolTipManager::instance()->showTip(pos
, item
);
539 KToolTipManager::instance()->hideTip();
542 void setToolTipDelegate(KToolTipDelegate
*delegate
)
544 KToolTipManager::instance()->setDelegate(delegate
);