summaryrefslogtreecommitdiffstats
path: root/src/plugins/platformthemes/gtk3/qgtk3interface_p.h
blob: 84119cc638d41ea122cc2e0a3e9ad121124820e5 (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
// 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/QLibrary>
#include <QtCore/QCache>
#include <private/qflatmap_p.h>
#include <QtCore/QObject>
#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;
class QGtk3Interface
{
    Q_GADGET
public:
    QGtk3Interface(QGtk3Storage *);
    ~QGtk3Interface();

    // Enum representing GTK widget types
    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)

    // Enum representing color sources of a GTK theme
    enum class QGtkColorSource {
        Foreground,
        Background,
        Text,
        Base,
        Border
    };
    Q_ENUM(QGtkColorSource)

    // Enum for default color getter
    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
    const QString themeName() const;

    // Derive appearance from default colors
    Qt::Appearance appearanceByColors() 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