~ubuntu-branches/ubuntu/precise/unity-2d/precise-updates

« back to all changes in this revision

Viewing changes to libunity-2d-private/src/panelstyle.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2011-08-25 18:16:57 UTC
  • mfrom: (1.1.19 upstream)
  • Revision ID: james.westby@ubuntu.com-20110825181657-opb29rqsxae3i98f
Tags: 4.2.0-0ubuntu1
* New upstream release:
  - [panel] application menus do not appear on first hover after dismissal
    (lp: #825262)
  - [dash] Lens icons badly scaled (lp: #825368)
  - [panel] indicators are shifted offscreen to the right (lp: #827673)
  - [panel] scrubbing from system indicators to menubar should be possible
    (lp: #706903)
  - [launcher] Closed applications don't get their launcher counts cleared
    (lp: #767367)
  - [dash] selected item should not be underlined but use the same 
    treatment as unity (lp: #817456)
  - [dash] categories should be collapsed by default (lp: #827214)
  - [dash] Ratings filter (lp: #831855)
  - [dash] Multirange filter (lp: #831856)
  - [dash] ToggleButton filter (lp: #831857)
  - [dash] Icon size must be bigger to match the mockups (lp: #831858)
  - [dash] "Refine search" right margin should be 15 pixels (lp: #832058)
  - [dash] "Refine search" should be "Filter results" (lp: #832060)
  - [dash] Font sizes should match new design (lp: #832114)
  - [panel] Glitch: application menu appearing when pressing the BFB 
    (lp: #825060)
  - [panel] Glitch: application menus are quickly opened after a drag gesture
    (lp: #825267)
  - [dash] File thumbnails aspect ratio is not respected (lp: #832204)
* debian/control: require current the current versions of nux and unity

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of unity-2d
 
3
 *
 
4
 * Copyright 2011 Canonical Ltd.
 
5
 *
 
6
 * Authors:
 
7
 * - Aurélien Gâteau <aurelien.gateau@canonical.com>
 
8
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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/>.
 
20
 */
 
21
// Self
 
22
#include "panelstyle.h"
 
23
 
 
24
// libunity-2d
 
25
#include <cairoutils.h>
 
26
#include <debug_p.h>
 
27
#include <gconnector.h>
 
28
#include <gscopedpointer.h>
 
29
 
 
30
// Qt
 
31
#include <QApplication>
 
32
#include <QPalette>
 
33
#include <QStyle>
 
34
 
 
35
// GTK
 
36
#include <gtk/gtk.h>
 
37
 
 
38
static const char* METACITY_THEME_DIR = "/usr/share/themes/%1/metacity-1";
 
39
 
 
40
class PanelStylePrivate
 
41
{
 
42
public:
 
43
    PanelStyle* q;
 
44
    GObjectScopedPointer<GtkStyleContext> m_styleContext;
 
45
    GConnector m_gConnector;
 
46
 
 
47
    QString m_themeName;
 
48
 
 
49
    static void onThemeChanged(GObject*, GParamSpec*, gpointer data)
 
50
    {
 
51
        PanelStylePrivate* priv = reinterpret_cast<PanelStylePrivate*>(data);
 
52
        priv->updatePalette();
 
53
    }
 
54
 
 
55
    void updatePalette()
 
56
    {
 
57
        gchar* themeName = 0;
 
58
        g_object_get(gtk_settings_get_default(), "gtk-theme-name", &themeName, NULL);
 
59
        m_themeName = QString::fromUtf8(themeName);
 
60
        g_free(themeName);
 
61
 
 
62
        GtkStyleContext* context = m_styleContext.data();
 
63
        gtk_style_context_invalidate(context);
 
64
 
 
65
        // Without this line, it seems the GtkStyleContext is not correctly
 
66
        // initialized and we get some uninitialized pixels in the background
 
67
        // brush.
 
68
        gtk_style_context_get(context, GTK_STATE_FLAG_NORMAL, NULL);
 
69
 
 
70
        QPalette pal;
 
71
        pal.setBrush(QPalette::Window, generateBackgroundBrush());
 
72
        QApplication::setPalette(pal);
 
73
    }
 
74
 
 
75
    QBrush generateBackgroundBrush()
 
76
    {
 
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());
 
82
        return QBrush(image);
 
83
    }
 
84
 
 
85
    QPixmap windowButtonPixmapFromWMTheme(PanelStyle::WindowButtonType type, PanelStyle::WindowButtonState state)
 
86
    {
 
87
        QString dir = QString(METACITY_THEME_DIR).arg(m_themeName);
 
88
 
 
89
        QString typeString, stateString;
 
90
        switch (type) {
 
91
        case PanelStyle::CloseWindowButton:
 
92
            typeString = "close";
 
93
            break;
 
94
        case PanelStyle::MinimizeWindowButton:
 
95
            typeString = "minimize";
 
96
            break;
 
97
        case PanelStyle::UnmaximizeWindowButton:
 
98
            typeString = "unmaximize";
 
99
            break;
 
100
        }
 
101
 
 
102
        switch (state) {
 
103
        case PanelStyle::NormalState:
 
104
            stateString = "";
 
105
            break;
 
106
        case PanelStyle::PrelightState:
 
107
            stateString = "_focused_prelight";
 
108
            break;
 
109
        case PanelStyle::PressedState:
 
110
            stateString = "_focused_pressed";
 
111
            break;
 
112
        }
 
113
 
 
114
        QString path = QString("%1/%2%3.png")
 
115
            .arg(dir)
 
116
            .arg(typeString)
 
117
            .arg(stateString);
 
118
        return QPixmap(path);
 
119
    }
 
120
 
 
121
    QPixmap genericWindowButtonPixmap(PanelStyle::WindowButtonType type, PanelStyle::WindowButtonState state)
 
122
    {
 
123
        QStyle::StandardPixmap standardIcon;
 
124
        switch (type) {
 
125
        case PanelStyle::CloseWindowButton:
 
126
            standardIcon = QStyle::SP_TitleBarCloseButton;
 
127
            break;
 
128
        case PanelStyle::MinimizeWindowButton:
 
129
            standardIcon = QStyle::SP_TitleBarMinButton;
 
130
            break;
 
131
        case PanelStyle::UnmaximizeWindowButton:
 
132
            standardIcon = QStyle::SP_TitleBarNormalButton;
 
133
            break;
 
134
        }
 
135
 
 
136
        QIcon icon = QApplication::style()->standardIcon(standardIcon);
 
137
        const int extent = 22;
 
138
        switch (state) {
 
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);
 
145
        }
 
146
        // Silence compiler
 
147
        return QPixmap();
 
148
    }
 
149
};
 
150
 
 
151
PanelStyle::PanelStyle(QObject* parent)
 
152
: d(new PanelStylePrivate)
 
153
{
 
154
    d->q = this;
 
155
    d->m_styleContext.reset(gtk_style_context_new());
 
156
 
 
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");
 
160
 
 
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");
 
164
 
 
165
    gtk_widget_path_free (widgetPath);
 
166
 
 
167
    d->m_gConnector.connect(gtk_settings_get_default(), "notify::gtk-theme-name",
 
168
        G_CALLBACK(PanelStylePrivate::onThemeChanged), d);
 
169
 
 
170
    d->updatePalette();
 
171
}
 
172
 
 
173
PanelStyle::~PanelStyle()
 
174
{
 
175
    delete d;
 
176
}
 
177
 
 
178
PanelStyle* PanelStyle::instance()
 
179
{
 
180
    static PanelStyle style;
 
181
    return &style;
 
182
}
 
183
 
 
184
GtkStyleContext* PanelStyle::styleContext() const
 
185
{
 
186
    return d->m_styleContext.data();
 
187
}
 
188
 
 
189
QPixmap PanelStyle::windowButtonPixmap(PanelStyle::WindowButtonType type, PanelStyle::WindowButtonState state)
 
190
{
 
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);
 
196
    } else {
 
197
        return d->genericWindowButtonPixmap(type, state);
 
198
    }
 
199
}
 
200
 
 
201
#include "panelstyle.moc"