2
* This file is part of unity-2d
4
* Copyright 2011 Canonical Ltd.
7
* - Aurélien Gâteau <aurelien.gateau@canonical.com>
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; version 3.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22
#include "panelstyle.h"
25
#include <cairoutils.h>
27
#include <gconnector.h>
28
#include <gscopedpointer.h>
31
#include <QApplication>
38
static const char* METACITY_THEME_DIR = "/usr/share/themes/%1/metacity-1";
40
class PanelStylePrivate
44
GObjectScopedPointer<GtkStyleContext> m_styleContext;
45
GConnector m_gConnector;
49
static void onThemeChanged(GObject*, GParamSpec*, gpointer data)
51
PanelStylePrivate* priv = reinterpret_cast<PanelStylePrivate*>(data);
52
priv->updatePalette();
58
g_object_get(gtk_settings_get_default(), "gtk-theme-name", &themeName, NULL);
59
m_themeName = QString::fromUtf8(themeName);
62
GtkStyleContext* context = m_styleContext.data();
63
gtk_style_context_invalidate(context);
65
// Without this line, it seems the GtkStyleContext is not correctly
66
// initialized and we get some uninitialized pixels in the background
68
gtk_style_context_get(context, GTK_STATE_FLAG_NORMAL, NULL);
71
pal.setBrush(QPalette::Window, generateBackgroundBrush());
72
QApplication::setPalette(pal);
75
QBrush generateBackgroundBrush()
77
QImage image(100, 24, QImage::Format_ARGB32_Premultiplied); // FIXME: Hardcoded
78
image.fill(Qt::transparent);
79
CairoUtils::SurfacePointer surface(CairoUtils::createSurfaceForQImage(&image));
80
CairoUtils::Pointer cr(cairo_create(surface.data()));
81
gtk_render_background(m_styleContext.data(), cr.data(), 0, 0, image.width(), image.height());
85
QPixmap windowButtonPixmapFromWMTheme(PanelStyle::WindowButtonType type, PanelStyle::WindowButtonState state)
87
QString dir = QString(METACITY_THEME_DIR).arg(m_themeName);
89
QString typeString, stateString;
91
case PanelStyle::CloseWindowButton:
94
case PanelStyle::MinimizeWindowButton:
95
typeString = "minimize";
97
case PanelStyle::UnmaximizeWindowButton:
98
typeString = "unmaximize";
103
case PanelStyle::NormalState:
106
case PanelStyle::PrelightState:
107
stateString = "_focused_prelight";
109
case PanelStyle::PressedState:
110
stateString = "_focused_pressed";
114
QString path = QString("%1/%2%3.png")
118
return QPixmap(path);
121
QPixmap genericWindowButtonPixmap(PanelStyle::WindowButtonType type, PanelStyle::WindowButtonState state)
123
QStyle::StandardPixmap standardIcon;
125
case PanelStyle::CloseWindowButton:
126
standardIcon = QStyle::SP_TitleBarCloseButton;
128
case PanelStyle::MinimizeWindowButton:
129
standardIcon = QStyle::SP_TitleBarMinButton;
131
case PanelStyle::UnmaximizeWindowButton:
132
standardIcon = QStyle::SP_TitleBarNormalButton;
136
QIcon icon = QApplication::style()->standardIcon(standardIcon);
137
const int extent = 22;
139
case PanelStyle::NormalState:
140
return icon.pixmap(extent);
141
case PanelStyle::PrelightState:
142
return icon.pixmap(extent, QIcon::Active);
143
case PanelStyle::PressedState:
144
return icon.pixmap(extent, QIcon::Active, QIcon::On);
151
PanelStyle::PanelStyle(QObject* parent)
152
: d(new PanelStylePrivate)
155
d->m_styleContext.reset(gtk_style_context_new());
157
GtkWidgetPath* widgetPath = gtk_widget_path_new ();
158
gtk_widget_path_append_type(widgetPath, GTK_TYPE_WINDOW);
159
gtk_widget_path_iter_set_name(widgetPath, -1 , "UnityPanelWidget");
161
gtk_style_context_set_path(d->m_styleContext.data(), widgetPath);
162
gtk_style_context_add_class(d->m_styleContext.data(), "gnome-panel-menu-bar");
163
gtk_style_context_add_class(d->m_styleContext.data(), "unity-panel");
165
gtk_widget_path_free (widgetPath);
167
d->m_gConnector.connect(gtk_settings_get_default(), "notify::gtk-theme-name",
168
G_CALLBACK(PanelStylePrivate::onThemeChanged), d);
173
PanelStyle::~PanelStyle()
178
PanelStyle* PanelStyle::instance()
180
static PanelStyle style;
184
GtkStyleContext* PanelStyle::styleContext() const
186
return d->m_styleContext.data();
189
QPixmap PanelStyle::windowButtonPixmap(PanelStyle::WindowButtonType type, PanelStyle::WindowButtonState state)
191
// According to Unity PanelStyle code, the buttons of some WM themes do not
192
// match well with the panel background. So except for themes we provide,
193
// fallback to generic button pixmaps.
194
if (d->m_themeName == "Ambiance" || d->m_themeName == "Radiance") {
195
return d->windowButtonPixmapFromWMTheme(type, state);
197
return d->genericWindowButtonPixmap(type, state);
201
#include "panelstyle.moc"