summaryrefslogtreecommitdiffstats
path: root/src/plugins/platformthemes/gtk3/qgtk3interface_p.h
blob: c43932a4fa3bd95fe2181164164af931374f5457 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QGTK3INTERFACE_H
#define QGTK3INTERFACE_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtCore/QString>
#include <QtCore/QCache>
#include <private/qflatmap_p.h>
#include <QtCore/QObject>
#include <QtGui/QIcon>
#include <QtGui/QPalette>
#include <QtWidgets/QWidget>
#include <QtCore/QLoggingCategory>
#include <QtGui/QPixmap>
#include <qpa/qplatformtheme.h>

#undef signals // Collides with GTK symbols
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <glib.h>

QT_BEGIN_NAMESPACE

Q_DECLARE_LOGGING_CATEGORY(lcQGtk3Interface);

using namespace Qt::StringLiterals;

class QGtk3Storage;

/*!
    \internal
    \brief The QGtk3Interface class centralizes communication with the GTK3 library.

    By encapsulating all GTK version specific syntax and conversions, it makes Qt's GTK theme
    independent from GTK versions.

    \note
    Including GTK3 headers requires #undef signals, which disables Qt signal/slot handling.
 */

class QGtk3Interface
{
    Q_GADGET
public:
    QGtk3Interface(QGtk3Storage *);
    ~QGtk3Interface();

    /*!
     *  \internal
        \enum QGtk3Interface::QGtkWidget
        \brief Represents GTK widget types used to obtain color information.

        \note The enum value gtk_Default refers to the GTK default style, rather than to a specific widget.
     */
    enum class QGtkWidget {
        gtk_menu_bar,
        gtk_menu,
        gtk_button,
        gtk_button_box,
        gtk_check_button,
        gtk_radio_button,
        gtk_frame,
        gtk_statusbar,
        gtk_entry,
        gtk_popup,
        gtk_notebook,
        gtk_toolbar,
        gtk_tree_view,
        gtk_combo_box,
        gtk_combo_box_text,
        gtk_progress_bar,
        gtk_fixed,
        gtk_separator_menu_item,
        gtk_Default,
        gtk_offscreen_window
    };
    Q_ENUM(QGtkWidget)

    /*!
        \internal
        \enum QGtk3Interface::QGtkColorSource
        \brief The QGtkColorSource enum represents the source of a color within a GTK widgets style context.

        If the current GTK theme provides such a color for a given widget, the color can be read
        from the style context by passing the enum's key as a property name to the GTK method
        gtk_style_context_lookup_color. The method will return false, if no color has been found.
     */
    enum class QGtkColorSource {
        Foreground,
        Background,
        Text,
        Base,
        Border
    };
    Q_ENUM(QGtkColorSource)

    /*!
        \internal
        \enum QGtk3Interface::QGtkColorDefault
        \brief The QGtkColorDefault enum represents generic GTK colors.

        The GTK3 methods gtk_style_context_get_color, gtk_style_context_get_background_color, and
        gtk_style_context_get_foreground_color always return the respective colors with a widget's
        style context. Unless set as a property by the current GTK theme, GTK's default colors will
        be returned.
        These generic default colors, represented by the GtkColorDefault enum, are used as a
        back, if a specific color property is requested but not defined in the current GTK theme.
     */
    enum class QGtkColorDefault {
        Foreground,
        Background,
        Border
    };
    Q_ENUM(QGtkColorDefault)

    // Create a brush from GTK widget type, color source and color state
    QBrush brush(QGtkWidget wtype, QGtkColorSource source, GtkStateFlags state) const;

    // Font & icon getters
    QImage standardPixmap(QPlatformTheme::StandardPixmap standardPixmap) const;
    QFont font(QPlatformTheme::Font type) const;
    QIcon fileIcon(const QFileInfo &fileInfo) const;

    // Return current GTK theme name
    QString themeName() const;

    // Derive color scheme from default colors
    Qt::ColorScheme colorSchemeByColors() const;

    // Convert GTK state to/from string
    static int toGtkState(const QString &state);
    static const QLatin1String fromGtkState(GtkStateFlags state);

private:

    // Map colors to GTK property names and default to generic color getters
    struct ColorKey {
        QGtkColorSource colorSource = QGtkColorSource::Background;
        GtkStateFlags state = GTK_STATE_FLAG_NORMAL;

        // struct becomes key of a map, so operator< is needed
        bool operator<(const ColorKey& other) const {
           return std::tie(colorSource, state) <
                  std::tie(other.colorSource, other.state);
        }

        QDebug operator<<(QDebug dbg)
        {
            return dbg << "QGtk3Interface::ColorKey(colorSource=" << colorSource << ", GTK state=" << fromGtkState(state) << ")";
        }
    };

    struct ColorValue {
        QString propertyName = QString();
        QGtkColorDefault genericSource = QGtkColorDefault::Background;

        QDebug operator<<(QDebug dbg)
        {
            return dbg << "QGtk3Interface::ColorValue(propertyName=" << propertyName << ", genericSource=" << genericSource << ")";
        }
    };

    typedef QFlatMap<ColorKey, ColorValue> ColorMap;
    ColorMap gtkColorMap;
    void initColorMap();

    GdkRGBA genericColor(GtkStyleContext *con, GtkStateFlags state, QGtkColorDefault def) const;

    // Cache for GTK widgets
    mutable QFlatMap<QGtkWidget, GtkWidget *> cache;

    // Converters for GTK icon and GDK pixbuf
    QImage qt_gtk_get_icon(const char *iconName) const;
    QImage qt_convert_gdk_pixbuf(GdkPixbuf *buf) const;

    // Create new GTK widget object
    GtkWidget *qt_new_gtkWidget(QGtkWidget type) const;

    // Deliver GTK Widget from cache or create new
    GtkWidget *widget(QGtkWidget type) const;

    // Get a GTK widget's style context. Default settings style context if nullptr
    GtkStyleContext *context(GtkWidget *widget = nullptr) const;

    // Convert GTK color into QColor
    static inline QColor fromGdkColor (const GdkRGBA &c)
    { return QColor::fromRgbF(c.red, c.green, c.blue, c.alpha); }

    // get a QColor of a GTK widget (default settings style if nullptr)
    QColor color (GtkWidget *widget, QGtkColorSource source, GtkStateFlags state) const;

    // Mappings for GTK fonts
    inline static constexpr QGtkWidget toWidgetType(QPlatformTheme::Font);
    inline static constexpr QFont::Style toFontStyle(PangoStyle style);
    inline static constexpr int toFontWeight(PangoWeight weight);

};
QT_END_NAMESPACE
#endif // QGTK3INTERFACE_H