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 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 | 91 |
92 // Creates a new cairo surface with the given size. The memory for | 92 // Creates a new cairo surface with the given size. The memory for |
93 // this surface is deallocated when this CairoSurface is destroyed. | 93 // this surface is deallocated when this CairoSurface is destroyed. |
94 explicit CairoSurface(const gfx::Size& size); | 94 explicit CairoSurface(const gfx::Size& size); |
95 | 95 |
96 ~CairoSurface(); | 96 ~CairoSurface(); |
97 | 97 |
98 // Get the drawing context for GTK to use. | 98 // Get the drawing context for GTK to use. |
99 cairo_t* cairo() { return cairo_; } | 99 cairo_t* cairo() { return cairo_; } |
100 | 100 |
101 // If |only_frame_pixels| is false, returns the average of all | 101 // Returns the average of all pixels in the surface. If |frame| is |
102 // pixels in the surface, otherwise returns the average of only the | 102 // true, the resulting alpha will be the average alpha, otherwise it |
103 // edge pixels. | 103 // will be the max alpha across all pixels. |
104 SkColor GetAveragePixelValue(bool only_frame_pixels); | 104 SkColor GetAveragePixelValue(bool frame); |
105 | 105 |
106 private: | 106 private: |
107 cairo_surface_t* surface_; | 107 cairo_surface_t* surface_; |
108 cairo_t* cairo_; | 108 cairo_t* cairo_; |
109 }; | 109 }; |
110 | 110 |
111 // Returns true iff the runtime version of Gtk used meets | 111 // Returns true iff the runtime version of Gtk used meets |
112 // |major|.|minor|.|micro|. | 112 // |major|.|minor|.|micro|. |
113 bool GtkVersionCheck(int major, int minor = 0, int micro = 0); | 113 bool GtkVersionCheck(int major, int minor = 0, int micro = 0); |
114 | 114 |
| 115 // Similar in spirit to a std::unique_ptr. |
115 template <typename T> | 116 template <typename T> |
116 class ScopedGObject { | 117 class ScopedGObject { |
117 public: | 118 public: |
118 explicit ScopedGObject(T* obj) : obj_(obj) { | 119 explicit ScopedGObject(T* obj) : obj_(obj) { |
119 // Increase the reference count of |obj_|, removing the floating | 120 // Remove the floating reference from |obj_| if it has one. |
120 // reference if it has one. | 121 if (g_object_is_floating(obj_)) |
121 g_object_ref_sink(obj_); | 122 g_object_ref_sink(obj_); |
| 123 DCHECK(G_OBJECT(obj_)->ref_count == 1); |
122 } | 124 } |
123 | 125 |
124 ScopedGObject(const ScopedGObject<T>& other) : obj_(other.obj_) { | 126 ScopedGObject(const ScopedGObject<T>& other) = delete; |
125 g_object_ref(obj_); | |
126 } | |
127 | 127 |
128 ScopedGObject(ScopedGObject<T>&& other) : obj_(other.obj_) { | 128 ScopedGObject(ScopedGObject<T>&& other) : obj_(other.obj_) { |
129 other.obj_ = nullptr; | 129 other.obj_ = nullptr; |
130 } | 130 } |
131 | 131 |
132 ~ScopedGObject() { | 132 ~ScopedGObject() { |
133 if (obj_) | 133 if (obj_) |
134 g_object_unref(obj_); | 134 Unref(); |
135 } | 135 } |
136 | 136 |
137 ScopedGObject<T>& operator=(const ScopedGObject<T>& other) { | 137 ScopedGObject<T>& operator=(const ScopedGObject<T>& other) = delete; |
138 g_object_ref(other.obj_); | |
139 g_object_unref(obj_); | |
140 obj_ = other.obj_; | |
141 return *this; | |
142 } | |
143 | 138 |
144 ScopedGObject<T>& operator=(ScopedGObject<T>&& other) { | 139 ScopedGObject<T>& operator=(ScopedGObject<T>&& other) { |
145 g_object_unref(obj_); | 140 g_object_unref(obj_); |
146 obj_ = other.obj_; | 141 obj_ = other.obj_; |
147 other.obj_ = nullptr; | 142 other.obj_ = nullptr; |
148 return *this; | 143 return *this; |
149 } | 144 } |
150 | 145 |
151 operator T*() { return obj_; } | 146 operator T*() { return obj_; } |
152 | 147 |
153 private: | 148 private: |
| 149 void Unref() { g_object_unref(obj_); } |
| 150 |
154 T* obj_; | 151 T* obj_; |
155 }; | 152 }; |
156 | 153 |
| 154 template <> |
| 155 inline void ScopedGObject<GtkStyleContext>::Unref() { |
| 156 // Versions of GTK earlier than 3.15.4 had a bug where a g_assert |
| 157 // would be triggered when trying to free a GtkStyleContext that had |
| 158 // a parent whose only reference was the child context in question. |
| 159 // This is a hack to work around that case. See GTK commit |
| 160 // "gtkstylecontext: Don't try to emit a signal when finalizing". |
| 161 GtkStyleContext* context = obj_; |
| 162 while (context) { |
| 163 GtkStyleContext* parent = gtk_style_context_get_parent(context); |
| 164 if (parent && G_OBJECT(context)->ref_count == 1 && |
| 165 !GtkVersionCheck(3, 15, 4)) { |
| 166 g_object_ref(parent); |
| 167 gtk_style_context_set_parent(context, nullptr); |
| 168 } else { |
| 169 g_object_unref(context); |
| 170 return; |
| 171 } |
| 172 context = parent; |
| 173 } |
| 174 } |
| 175 |
157 typedef ScopedGObject<GtkStyleContext> ScopedStyleContext; | 176 typedef ScopedGObject<GtkStyleContext> ScopedStyleContext; |
| 177 typedef ScopedGObject<GtkCssProvider> ScopedCssProvider; |
158 | 178 |
159 // If |context| is NULL, creates a new top-level style context | 179 // If |context| is NULL, creates a new top-level style context |
160 // specified by parsing |css_node|. Otherwise, creates the child | 180 // specified by parsing |css_node|. Otherwise, creates the child |
161 // context with |context| as the parent. | 181 // context with |context| as the parent. |
162 ScopedStyleContext AppendCssNodeToStyleContext(GtkStyleContext* context, | 182 ScopedStyleContext AppendCssNodeToStyleContext(GtkStyleContext* context, |
163 const std::string& css_node); | 183 const std::string& css_node); |
164 | 184 |
165 // Parses |css_selector| into a GtkStyleContext. The format is a | 185 // Parses |css_selector| into a GtkStyleContext. The format is a |
166 // sequence of whitespace-separated objects. Each object may have at | 186 // sequence of whitespace-separated objects. Each object may have at |
167 // most one object name at the beginning of the string, and any number | 187 // most one object name at the beginning of the string, and any number |
168 // of '.'-prefixed classes and ':'-prefixed pseudoclasses. An example | 188 // of '.'-prefixed classes and ':'-prefixed pseudoclasses. An example |
169 // is "GtkButton.button.suggested-action:hover:active". The caller | 189 // is "GtkButton.button.suggested-action:hover:active". The caller |
170 // must g_object_unref() the returned context. | 190 // must g_object_unref() the returned context. |
171 ScopedStyleContext GetStyleContextFromCss(const char* css_selector); | 191 ScopedStyleContext GetStyleContextFromCss(const char* css_selector); |
172 | 192 |
173 SkColor SkColorFromStyleContext(GtkStyleContext* context); | 193 SkColor GetFgColorFromStyleContext(GtkStyleContext* context); |
174 | 194 |
175 // Removes all border-type properties on |context| and all of its parents. | 195 // Overrides properties on |context| and all its parents with those |
176 void RemoveBorders(GtkStyleContext* context); | 196 // provided by |css|. |
| 197 void ApplyCssToContext(GtkStyleContext* context, const char* css); |
177 | 198 |
178 // Get the 'color' property from the style context created by | 199 // Get the 'color' property from the style context created by |
179 // GetStyleContextFromCss(|css_selector|). | 200 // GetStyleContextFromCss(|css_selector|). |
180 SkColor GetFgColor(const char* css_selector); | 201 SkColor GetFgColor(const char* css_selector); |
181 | 202 |
182 // Renders the backgrounds of all ancestors of |context|, then renders | 203 // Renders the backgrounds of all ancestors of |context|, then renders |
183 // the background for |context| itself. | 204 // the background for |context| itself. |
184 void RenderBackground(const gfx::Size& size, | 205 void RenderBackground(const gfx::Size& size, |
185 cairo_t* cr, | 206 cairo_t* cr, |
186 GtkStyleContext* context); | 207 GtkStyleContext* context); |
187 | 208 |
188 // Renders a background from the style context created by | 209 // Renders a background from the style context created by |
189 // GetStyleContextFromCss(|css_selector|) into a single pixel and | 210 // GetStyleContextFromCss(|css_selector|) into a 24x24 bitmap and |
190 // returns the color. | 211 // returns the average color. |
191 SkColor GetBgColor(const char* css_selector); | 212 SkColor GetBgColor(const char* css_selector); |
192 | 213 |
193 // If there is a border, renders the border from the style context | 214 // Renders the border from the style context created by |
194 // created by GetStyleContextFromCss(|css_selector|) into a single | 215 // GetStyleContextFromCss(|css_selector|) into a 24x24 bitmap and |
195 // pixel and returns the color. Otherwise returns kInvalidColor. | 216 // returns the average color. |
196 SkColor GetBorderColor(const char* css_selector); | 217 SkColor GetBorderColor(const char* css_selector); |
197 | 218 |
| 219 SkColor GetSelectedTextColor(const char* css_selector); |
| 220 SkColor GetSelectedBgColor(const char* css_selector); |
| 221 |
198 // Get the color of the GtkSeparator specified by |css_selector|. | 222 // Get the color of the GtkSeparator specified by |css_selector|. |
199 SkColor GetSeparatorColor(const char* css_selector); | 223 SkColor GetSeparatorColor(const char* css_selector); |
200 #endif | 224 #endif |
201 | 225 |
202 } // namespace libgtkui | 226 } // namespace libgtkui |
203 | 227 |
204 #endif // CHROME_BROWSER_UI_LIBGTKUI_GTK_UTIL_H_ | 228 #endif // CHROME_BROWSER_UI_LIBGTKUI_GTK_UTIL_H_ |
OLD | NEW |