1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtWidgets module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL21$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** $QT_END_LICENSE$
31 **
32 ****************************************************************************/
33 
34 #ifndef QGTKSTYLE_P_P_H
35 #define QGTKSTYLE_P_P_H
36 
37 //
38 //  W A R N I N G
39 //  -------------
40 //
41 // This file is not part of the Qt API.  It exists purely as an
42 // implementation detail.  This header file may change from version to
43 // version without notice, or even be removed.
44 //
45 // We mean it.
46 //
47 
48 #include <QtCore/qglobal.h>
49 
50 #include <QtCore/qstring.h>
51 #include <QtCore/qstringbuilder.h>
52 #include <QtCore/qcoreapplication.h>
53 
54 #include <QtWidgets/QFileDialog>
55 
56 #include "qgtkstyle_p.h"
57 #include <private/qcommonstyle_p.h>
58 #include "qgtkglobal_p.h"
59 
60 QT_BEGIN_NAMESPACE
61 
62 class QHashableLatin1Literal
63 {
64 public:
size()65     int size() const { return m_size; }
data()66     const char *data() const { return m_data; }
67 
68 #ifdef __SUNPRO_CC
QHashableLatin1Literal(const char * str)69         QHashableLatin1Literal(const char* str)
70         : m_size(strlen(str)), m_data(str) {}
71 #else
72     template <int N>
QHashableLatin1Literal(const char (& str)[N])73         QHashableLatin1Literal(const char (&str)[N])
74         : m_size(N - 1), m_data(str) {}
75 #endif
76 
QHashableLatin1Literal(const QHashableLatin1Literal & other)77     QHashableLatin1Literal(const QHashableLatin1Literal &other)
78         : m_size(other.m_size), m_data(other.m_data)
79     {}
80 
81     QHashableLatin1Literal &operator=(const QHashableLatin1Literal &other)
82     {
83         if (this == &other)
84             return *this;
85         *const_cast<int *>(&m_size) = other.m_size;
86         *const_cast<char **>(&m_data) = const_cast<char *>(other.m_data);
87         return *this;
88     }
89 
toString()90     QString toString() const { return QString::fromLatin1(m_data, m_size); }
91 
fromData(const char * str)92     static QHashableLatin1Literal fromData(const char *str)
93     {
94         return QHashableLatin1Literal(str, qstrlen(str));
95     }
96 
97 private:
QHashableLatin1Literal(const char * str,int length)98     QHashableLatin1Literal(const char *str, int length)
99         : m_size(length), m_data(str)
100     {}
101 
102     const int m_size;
103     const char *m_data;
104 };
105 
106 bool operator==(const QHashableLatin1Literal &l1, const QHashableLatin1Literal &l2);
107 inline bool operator!=(const QHashableLatin1Literal &l1, const QHashableLatin1Literal &l2) { return !operator==(l1, l2); }
108 uint qHash(const QHashableLatin1Literal &key);
109 
110 typedef void (*Ptr_ubuntu_gtk_set_use_overlay_scrollbar) (gboolean);
111 
112 class QGtkPainter;
113 class QGtkStylePrivate;
114 
115 class QGtkStyleFilter : public QObject
116 {
117 public:
QGtkStyleFilter(QGtkStylePrivate * sp)118     QGtkStyleFilter(QGtkStylePrivate* sp)
119         : stylePrivate(sp)
120     {}
121 private:
122     QGtkStylePrivate* stylePrivate;
123     bool eventFilter(QObject *obj, QEvent *e) Q_DECL_OVERRIDE;
124 };
125 
126 typedef enum {
127   GNOME_ICON_LOOKUP_FLAGS_NONE = 0,
128   GNOME_ICON_LOOKUP_FLAGS_EMBEDDING_TEXT = 1<<0,
129   GNOME_ICON_LOOKUP_FLAGS_SHOW_SMALL_IMAGES_AS_THEMSELVES = 1<<1,
130   GNOME_ICON_LOOKUP_FLAGS_ALLOW_SVG_AS_THEMSELVES = 1<<2
131 } GnomeIconLookupFlags;
132 
133 typedef enum {
134   GNOME_ICON_LOOKUP_RESULT_FLAGS_NONE = 0,
135   GNOME_ICON_LOOKUP_RESULT_FLAGS_THUMBNAIL = 1<<0
136 } GnomeIconLookupResultFlags;
137 
138 struct GnomeThumbnailFactory;
139 
140 class QGtkStylePrivate : public QCommonStylePrivate
141 {
142     Q_DECLARE_PUBLIC(QGtkStyle)
143 public:
144     QGtkStylePrivate();
145     ~QGtkStylePrivate();
146 
147     QGtkStyleFilter filter;
148 
149     static QGtkPainter* gtkPainter(QPainter *painter = 0);
150     static GtkWidget* gtkWidget(const QHashableLatin1Literal &path);
151     static GtkStyle* gtkStyle(const QHashableLatin1Literal &path = QHashableLatin1Literal("GtkWindow"));
152     static void gtkWidgetSetFocus(GtkWidget *widget, bool focus);
153 
154     virtual void initGtkMenu() const;
155     virtual void initGtkTreeview() const;
156     virtual void initGtkWidgets() const;
157 
158     static void cleanupGtkWidgets();
159 
160     static bool isKDE4Session();
161     void applyCustomPaletteHash();
162     static QFont getThemeFont();
isThemeAvailable()163     static bool isThemeAvailable() { return gtkStyle() != 0; }
164 
165     static QString getThemeName();
166     virtual int getSpinboxArrowSize() const;
167 
168     virtual QPalette gtkWidgetPalette(const QHashableLatin1Literal &gtkWidgetName) const;
169 
170     static Ptr_ubuntu_gtk_set_use_overlay_scrollbar ubuntu_gtk_set_use_overlay_scrollbar;
171 
172 protected:
173     typedef QHash<QHashableLatin1Literal, GtkWidget*> WidgetMap;
174 
destroyWidgetMap()175     static inline void destroyWidgetMap()
176     {
177         cleanupGtkWidgets();
178         delete widgetMap;
179         widgetMap = 0;
180     }
181 
gtkWidgetMap()182     static inline WidgetMap *gtkWidgetMap()
183     {
184         if (!widgetMap) {
185             widgetMap = new WidgetMap();
186             qAddPostRoutine(destroyWidgetMap);
187         }
188         return widgetMap;
189     }
190 
191     static QStringList extract_filter(const QString &rawFilter);
192 
193     virtual GtkWidget* getTextColorWidget() const;
194     static void setupGtkWidget(GtkWidget* widget);
195     static void addWidgetToMap(GtkWidget* widget);
196     static void addAllSubWidgets(GtkWidget *widget, gpointer v = 0);
197     static void addWidget(GtkWidget *widget);
198     static void removeWidgetFromMap(const QHashableLatin1Literal &path);
199 
200     virtual void init();
201 
202     enum {
203         menuItemFrame        =  2, // menu item frame width
204         menuItemHMargin      =  3, // menu item hor text margin
205         menuArrowHMargin     =  6, // menu arrow horizontal margin
206         menuItemVMargin      =  2, // menu item ver text margin
207         menuRightBorder      = 15, // right border on menus
208         menuCheckMarkWidth   = 12  // checkmarks width on menus
209     };
210 
211 private:
212     static QList<QGtkStylePrivate *> instances;
213     static WidgetMap *widgetMap;
214     friend class QGtkStyleUpdateScheduler;
215 };
216 
217 // Helper to ensure that we have polished all our gtk widgets
218 // before updating our own palettes
219 class QGtkStyleUpdateScheduler : public QObject
220 {
221     Q_OBJECT
222 public slots:
223     void updateTheme();
224 };
225 
226 QT_END_NAMESPACE
227 
228 #endif // QGTKSTYLE_P_P_H
229