OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_UI_LIBGTKUI_GTK_UTIL_H_ | 5 #ifndef CHROME_BROWSER_UI_LIBGTKUI_GTK_UTIL_H_ |
6 #define CHROME_BROWSER_UI_LIBGTKUI_GTK_UTIL_H_ | 6 #define CHROME_BROWSER_UI_LIBGTKUI_GTK_UTIL_H_ |
7 | 7 |
8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "ui/base/glib/glib_util.h" | |
11 #include "ui/native_theme/native_theme.h" | 12 #include "ui/native_theme/native_theme.h" |
12 | 13 |
13 namespace aura { | 14 namespace aura { |
14 class Window; | 15 class Window; |
15 } | 16 } |
16 | 17 |
17 namespace base { | 18 namespace base { |
18 class CommandLine; | 19 class CommandLine; |
19 class Environment; | 20 class Environment; |
20 } | 21 } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 | 109 |
109 private: | 110 private: |
110 cairo_surface_t* surface_; | 111 cairo_surface_t* surface_; |
111 cairo_t* cairo_; | 112 cairo_t* cairo_; |
112 }; | 113 }; |
113 | 114 |
114 // Returns true iff the runtime version of Gtk used meets | 115 // Returns true iff the runtime version of Gtk used meets |
115 // |major|.|minor|.|micro|. | 116 // |major|.|minor|.|micro|. |
116 bool GtkVersionCheck(int major, int minor = 0, int micro = 0); | 117 bool GtkVersionCheck(int major, int minor = 0, int micro = 0); |
117 | 118 |
118 // Similar in spirit to a std::unique_ptr. | 119 typedef ScopedGObject<GtkStyleContext> ScopedStyleContext; |
Lei Zhang
2017/04/06 03:59:54
While we are at it:
using ScopedStyleContext = Sc
Tom (Use chromium acct)
2017/04/06 05:05:35
Done.
| |
119 template <typename T> | 120 typedef ScopedGObject<GtkCssProvider> ScopedCssProvider; |
120 class ScopedGObject { | |
121 public: | |
122 explicit ScopedGObject(T* obj) : obj_(obj) { | |
123 // Remove the floating reference from |obj_| if it has one. | |
124 if (g_object_is_floating(obj_)) | |
125 g_object_ref_sink(obj_); | |
126 DCHECK(G_OBJECT(obj_)->ref_count == 1); | |
127 } | |
128 | 121 |
129 ScopedGObject(const ScopedGObject<T>& other) = delete; | 122 } // namespace libgtkui |
130 | 123 |
131 ScopedGObject(ScopedGObject<T>&& other) : obj_(other.obj_) { | 124 // Template override cannot be in the libgtkui namespace. |
132 other.obj_ = nullptr; | |
133 } | |
134 | |
135 ~ScopedGObject() { | |
136 if (obj_) | |
137 Unref(); | |
138 } | |
139 | |
140 ScopedGObject<T>& operator=(const ScopedGObject<T>& other) = delete; | |
141 | |
142 ScopedGObject<T>& operator=(ScopedGObject<T>&& other) { | |
143 g_object_unref(obj_); | |
144 obj_ = other.obj_; | |
145 other.obj_ = nullptr; | |
146 return *this; | |
147 } | |
148 | |
149 operator T*() { return obj_; } | |
150 | |
151 private: | |
152 void Unref() { g_object_unref(obj_); } | |
153 | |
154 T* obj_; | |
155 }; | |
156 | |
157 template <> | 125 template <> |
158 inline void ScopedGObject<GtkStyleContext>::Unref() { | 126 inline void libgtkui::ScopedStyleContext::Unref() { |
159 // Versions of GTK earlier than 3.15.4 had a bug where a g_assert | 127 // Versions of GTK earlier than 3.15.4 had a bug where a g_assert |
160 // would be triggered when trying to free a GtkStyleContext that had | 128 // would be triggered when trying to free a GtkStyleContext that had |
161 // a parent whose only reference was the child context in question. | 129 // a parent whose only reference was the child context in question. |
162 // This is a hack to work around that case. See GTK commit | 130 // This is a hack to work around that case. See GTK commit |
163 // "gtkstylecontext: Don't try to emit a signal when finalizing". | 131 // "gtkstylecontext: Don't try to emit a signal when finalizing". |
164 GtkStyleContext* context = obj_; | 132 GtkStyleContext* context = obj_; |
165 while (context) { | 133 while (context) { |
166 GtkStyleContext* parent = gtk_style_context_get_parent(context); | 134 GtkStyleContext* parent = gtk_style_context_get_parent(context); |
167 if (parent && G_OBJECT(context)->ref_count == 1 && | 135 if (parent && G_OBJECT(context)->ref_count == 1 && |
168 !GtkVersionCheck(3, 15, 4)) { | 136 !libgtkui::GtkVersionCheck(3, 15, 4)) { |
169 g_object_ref(parent); | 137 g_object_ref(parent); |
170 gtk_style_context_set_parent(context, nullptr); | 138 gtk_style_context_set_parent(context, nullptr); |
171 g_object_unref(context); | 139 g_object_unref(context); |
172 } else { | 140 } else { |
173 g_object_unref(context); | 141 g_object_unref(context); |
174 return; | 142 return; |
175 } | 143 } |
176 context = parent; | 144 context = parent; |
177 } | 145 } |
178 } | 146 } |
179 | 147 |
180 typedef ScopedGObject<GtkStyleContext> ScopedStyleContext; | 148 namespace libgtkui { |
181 typedef ScopedGObject<GtkCssProvider> ScopedCssProvider; | |
182 | 149 |
183 // Converts ui::NativeTheme::State to GtkStateFlags. | 150 // Converts ui::NativeTheme::State to GtkStateFlags. |
184 GtkStateFlags StateToStateFlags(ui::NativeTheme::State state); | 151 GtkStateFlags StateToStateFlags(ui::NativeTheme::State state); |
185 | 152 |
186 // If |context| is nullptr, creates a new top-level style context | 153 // If |context| is nullptr, creates a new top-level style context |
187 // specified by parsing |css_node|. Otherwise, creates the child | 154 // specified by parsing |css_node|. Otherwise, creates the child |
188 // context with |context| as the parent. | 155 // context with |context| as the parent. |
189 ScopedStyleContext AppendCssNodeToStyleContext(GtkStyleContext* context, | 156 ScopedStyleContext AppendCssNodeToStyleContext(GtkStyleContext* context, |
190 const std::string& css_node); | 157 const std::string& css_node); |
191 | 158 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 // the background-color property. | 194 // the background-color property. |
228 SkColor GetSelectionBgColor(const std::string& css_selector); | 195 SkColor GetSelectionBgColor(const std::string& css_selector); |
229 | 196 |
230 // Get the color of the GtkSeparator specified by |css_selector|. | 197 // Get the color of the GtkSeparator specified by |css_selector|. |
231 SkColor GetSeparatorColor(const std::string& css_selector); | 198 SkColor GetSeparatorColor(const std::string& css_selector); |
232 #endif | 199 #endif |
233 | 200 |
234 } // namespace libgtkui | 201 } // namespace libgtkui |
235 | 202 |
236 #endif // CHROME_BROWSER_UI_LIBGTKUI_GTK_UTIL_H_ | 203 #endif // CHROME_BROWSER_UI_LIBGTKUI_GTK_UTIL_H_ |
OLD | NEW |