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()
70 int KToolTipItem::type() const
75 QString
KToolTipItem::text() const
77 return data(Qt::DisplayRole
).toString();
80 QIcon
KToolTipItem::icon() const
82 return qvariant_cast
<QIcon
>(data(Qt::DecorationRole
));
85 QVariant
KToolTipItem::data(int role
) const
87 return d
->map
.value(role
);
90 void KToolTipItem::setData(int role
, const QVariant
&data
)
97 // ----------------------------------------------------------------------------
100 KStyleOptionToolTip::KStyleOptionToolTip()
101 : fontMetrics(QApplication::font())
106 // ----------------------------------------------------------------------------
110 KToolTipDelegate::KToolTipDelegate() : QObject()
114 KToolTipDelegate::~KToolTipDelegate()
118 QSize
KToolTipDelegate::sizeHint(const KStyleOptionToolTip
*option
, const KToolTipItem
*item
) const
121 size
.rwidth() = option
->fontMetrics
.width(item
->text());
122 size
.rheight() = option
->fontMetrics
.lineSpacing();
124 QIcon icon
= item
->icon();
125 if (!icon
.isNull()) {
126 const QSize iconSize
= icon
.actualSize(option
->decorationSize
);
127 size
.rwidth() += iconSize
.width() + 4;
128 size
.rheight() = qMax(size
.height(), iconSize
.height());
131 return size
+ QSize(20, 20);
135 void KToolTipDelegate::paint(QPainter
*painter
, const KStyleOptionToolTip
*option
,
136 const KToolTipItem
*item
) const
138 bool haveAlpha
= haveAlphaChannel();
139 painter
->setRenderHint(QPainter::Antialiasing
);
143 path
.addRoundRect(option
->rect
.adjusted(0, 0, -1, -1), 25);
145 path
.addRect(option
->rect
.adjusted(0, 0, -1, -1));
147 #if QT_VERSION >= 0x040400
148 QColor color
= option
->palette
.color(QPalette::ToolTipBase
);
150 QColor color
= option
->palette
.color(QPalette::Base
);
152 QColor from
= color
.lighter(105);
153 QColor to
= color
.darker(120);
155 QLinearGradient
gradient(0, 0, 0, 1);
156 gradient
.setCoordinateMode(QGradient::ObjectBoundingMode
);
157 gradient
.setColorAt(0, from
);
158 gradient
.setColorAt(1, to
);
160 painter
->translate(.5, .5);
161 painter
->setPen(QPen(Qt::black
, 1));
162 painter
->setBrush(gradient
);
163 painter
->drawPath(path
);
164 painter
->translate(-.5, -.5);
167 QLinearGradient
mask(0, 0, 0, 1);
168 gradient
.setCoordinateMode(QGradient::ObjectBoundingMode
);
169 gradient
.setColorAt(0, QColor(0, 0, 0, 192));
170 gradient
.setColorAt(1, QColor(0, 0, 0, 72));
171 painter
->setCompositionMode(QPainter::CompositionMode_DestinationIn
);
172 painter
->fillRect(option
->rect
, gradient
);
173 painter
->setCompositionMode(QPainter::CompositionMode_SourceOver
);
176 QRect textRect
= option
->rect
.adjusted(10, 10, -10, -10);
178 QIcon icon
= item
->icon();
179 if (!icon
.isNull()) {
180 const QSize iconSize
= icon
.actualSize(option
->decorationSize
);
181 painter
->drawPixmap(textRect
.topLeft(), icon
.pixmap(iconSize
));
182 textRect
.adjust(iconSize
.width() + 4, 0, 0, 0);
184 painter
->drawText(textRect
, Qt::AlignLeft
| Qt::AlignVCenter
, item
->text());
187 QRegion
KToolTipDelegate::inputShape(const KStyleOptionToolTip
*option
) const
189 return QRegion(option
->rect
);
192 QRegion
KToolTipDelegate::shapeMask(const KStyleOptionToolTip
*option
) const
194 return QRegion(option
->rect
);
197 bool KToolTipDelegate::haveAlphaChannel() const
199 return KToolTipManager::instance()->haveAlphaChannel();
204 // ----------------------------------------------------------------------------
208 class KAbstractToolTipLabel
211 KAbstractToolTipLabel() {}
212 virtual ~KAbstractToolTipLabel() {}
214 virtual void showTip(const QPoint
&pos
, const KToolTipItem
*item
) = 0;
215 virtual void moveTip(const QPoint
&pos
) = 0;
216 virtual void hideTip() = 0;
219 KStyleOptionToolTip
styleOption() const;
220 KToolTipDelegate
*delegate() const;
223 KStyleOptionToolTip
KAbstractToolTipLabel::styleOption() const
225 KStyleOptionToolTip option
;
226 KToolTipManager::instance()->initStyleOption(&option
);
230 KToolTipDelegate
*KAbstractToolTipLabel::delegate() const
232 return KToolTipManager::instance()->delegate();
236 // ----------------------------------------------------------------------------
240 class QWidgetLabel
: public QWidget
, public KAbstractToolTipLabel
243 QWidgetLabel() : QWidget(0, Qt::ToolTip
) {}
244 void showTip(const QPoint
&pos
, const KToolTipItem
*item
);
245 void moveTip(const QPoint
&pos
);
249 void paintEvent(QPaintEvent
*);
250 QSize
sizeHint() const;
253 const KToolTipItem
*currentItem
;
256 void QWidgetLabel::showTip(const QPoint
&pos
, const KToolTipItem
*item
)
263 void QWidgetLabel::hideTip()
269 void QWidgetLabel::moveTip(const QPoint
&pos
)
274 void QWidgetLabel::paintEvent(QPaintEvent
*)
276 KStyleOptionToolTip option
= styleOption();
277 option
.rect
= rect();
279 setMask(delegate()->shapeMask(&option
));
282 p
.setFont(option
.font
);
283 p
.setPen(QPen(option
.palette
.brush(QPalette::Text
), 0));
284 delegate()->paint(&p
, &option
, currentItem
);
287 QSize
QWidgetLabel::sizeHint() const
292 KStyleOptionToolTip option
= styleOption();
293 return delegate()->sizeHint(&option
, currentItem
);
298 // ----------------------------------------------------------------------------
304 // X11 specific label that displays the tip in an ARGB window.
305 class ArgbLabel
: public KAbstractToolTipLabel
308 ArgbLabel(Visual
*visual
, int depth
);
311 void showTip(const QPoint
&pos
, const KToolTipItem
*item
);
312 void moveTip(const QPoint
&pos
);
322 ArgbLabel::ArgbLabel(Visual
*visual
, int depth
)
324 Display
*dpy
= QX11Info::display();
325 Window root
= QX11Info::appRootWindow();
326 colormap
= XCreateColormap(dpy
, QX11Info::appRootWindow(), visual
, AllocNone
);
328 XSetWindowAttributes attr
;
329 attr
.border_pixel
= 0;
330 attr
.background_pixel
= 0;
331 attr
.colormap
= colormap
;
332 attr
.override_redirect
= True
;
334 window
= XCreateWindow(dpy
, root
, 0, 0, 1, 1, 0, depth
, InputOutput
, visual
,
335 CWBorderPixel
| CWBackPixel
| CWColormap
|
336 CWOverrideRedirect
, &attr
);
338 // ### TODO: Set the WM hints so KWin can identify this window as a
341 XRenderPictFormat
*format
= XRenderFindVisualFormat(dpy
, visual
);
342 picture
= XRenderCreatePicture(dpy
, window
, format
, 0, 0);
347 ArgbLabel::~ArgbLabel()
349 Display
*dpy
= QX11Info::display();
350 XRenderFreePicture(dpy
, picture
);
351 XDestroyWindow(dpy
, window
);
352 XFreeColormap(dpy
, colormap
);
355 void ArgbLabel::showTip(const QPoint
&pos
, const KToolTipItem
*item
)
357 Display
*dpy
= QX11Info::display();
358 KStyleOptionToolTip option
= styleOption();
359 const QSize size
= delegate()->sizeHint(&option
, item
);
360 option
.rect
= QRect(QPoint(), size
);
362 QPixmap
pixmap(size
);
363 pixmap
.fill(Qt::transparent
);
366 p
.setFont(option
.font
);
367 p
.setPen(QPen(option
.palette
.brush(QPalette::Text
), 0));
368 delegate()->paint(&p
, &option
, item
);
370 // Resize, position and show the window.
371 XMoveResizeWindow(dpy
, window
, pos
.x(), pos
.y(), size
.width(), size
.height());
373 if (KToolTipManager::instance()->haveAlphaChannel()) {
374 const QRegion region
= delegate()->inputShape(&option
);
375 XShapeCombineRegion(dpy
, window
, ShapeInput
, 0, 0, region
.handle(), ShapeSet
);
377 const QRegion region
= delegate()->shapeMask(&option
);
378 XShapeCombineRegion(dpy
, window
, ShapeBounding
, 0, 0, region
.handle(), ShapeSet
);
381 XMapWindow(dpy
, window
);
383 // Blit the pixmap with the tip contents to the window.
384 // Since the window is override-redirect and an ARGB32 window,
385 // which always has an offscreen pixmap, there's no need to
386 // wait for an Expose event, or to process those.
387 XRenderComposite(dpy
, PictOpSrc
, pixmap
.x11PictureHandle(), None
,
388 picture
, 0, 0, 0, 0, 0, 0, size
.width(), size
.height());
393 void ArgbLabel::moveTip(const QPoint
&pos
)
396 XMoveWindow(QX11Info::display(), window
, pos
.x(), pos
.y());
399 void ArgbLabel::hideTip()
402 Display
*dpy
= QX11Info::display();
403 XUnmapWindow(dpy
, window
);
413 // ----------------------------------------------------------------------------
418 KToolTipManager
*KToolTipManager::s_instance
= 0;
420 KToolTipManager::KToolTipManager()
421 : label(0), currentItem(0), m_delegate(0)
424 Display
*dpy
= QX11Info::display();
425 int screen
= DefaultScreen(dpy
);
426 int depth
= DefaultDepth(dpy
, screen
);
427 Visual
*visual
= DefaultVisual(dpy
, screen
);
428 net_wm_cm_s0
= XInternAtom(dpy
, "_NET_WM_CM_S0", False
);
429 haveArgbVisual
= false;
433 templ
.screen
= screen
;
435 templ
.c_class
= TrueColor
;
436 XVisualInfo
*xvi
= XGetVisualInfo(dpy
, VisualScreenMask
| VisualDepthMask
|
437 VisualClassMask
, &templ
, &nvi
);
439 for (int i
= 0; i
< nvi
; ++i
)
441 XRenderPictFormat
*format
= XRenderFindVisualFormat(dpy
, xvi
[i
].visual
);
442 if (format
->type
== PictTypeDirect
&& format
->direct
.alphaMask
)
444 visual
= xvi
[i
].visual
;
445 depth
= xvi
[i
].depth
;
446 haveArgbVisual
= true;
452 label
= new ArgbLabel(visual
, depth
);
455 label
= new QWidgetLabel();
458 KToolTipManager::~KToolTipManager()
464 void KToolTipManager::showTip(const QPoint
&pos
, KToolTipItem
*item
)
467 label
->showTip(pos
, item
);
471 void KToolTipManager::hideTip()
478 void KToolTipManager::initStyleOption(KStyleOptionToolTip
*option
) const
480 option
->direction
= QApplication::layoutDirection();
481 option
->fontMetrics
= QFontMetrics(QToolTip::font());
482 option
->activeCorner
= KStyleOptionToolTip::TopLeftCorner
;
483 option
->palette
= QToolTip::palette();
484 option
->font
= QToolTip::font();
485 option
->rect
= QRect();
486 option
->state
= QStyle::State_None
;
487 option
->decorationSize
= QSize(32, 32);
490 bool KToolTipManager::haveAlphaChannel() const
493 // ### This is a synchronous call - ideally we'd use a selection
494 // watcher to avoid it.
495 return haveArgbVisual
&&
496 XGetSelectionOwner(QX11Info::display(), net_wm_cm_s0
) != None
;
502 void KToolTipManager::setDelegate(KToolTipDelegate
*delegate
)
504 m_delegate
= delegate
;
507 KToolTipDelegate
*KToolTipManager::delegate() const
514 // ----------------------------------------------------------------------------
520 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
, const QRect
&rect
)
524 KToolTipItem
*item
= new KToolTipItem(text
);
525 KToolTipManager::instance()->showTip(pos
, item
);
528 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
)
530 showText(pos
, text
, widget
, QRect());
533 void showTip(const QPoint
&pos
, KToolTipItem
*item
)
535 KToolTipManager::instance()->showTip(pos
, item
);
540 KToolTipManager::instance()->hideTip();
543 void setToolTipDelegate(KToolTipDelegate
*delegate
)
545 KToolTipManager::instance()->setDelegate(delegate
);