Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(342)

Side by Side Diff: chrome/browser/ui/libgtkui/gtk_ui.cc

Issue 2715153002: Gtk3: Refactor GtkButtonImageSource::GetImageForScale (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/ui/libgtkui/gtk_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "chrome/browser/ui/libgtkui/gtk_ui.h" 5 #include "chrome/browser/ui/libgtkui/gtk_ui.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 #include <pango/pango.h> 8 #include <pango/pango.h>
9 #include <X11/Xcursor/Xcursor.h> 9 #include <X11/Xcursor/Xcursor.h>
10 10
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 ~GtkButtonImageSource() override {} 109 ~GtkButtonImageSource() override {}
110 110
111 gfx::ImageSkiaRep GetImageForScale(float scale) override { 111 gfx::ImageSkiaRep GetImageForScale(float scale) override {
112 int width = width_ * scale; 112 int width = width_ * scale;
113 int height = height_ * scale; 113 int height = height_ * scale;
114 114
115 SkBitmap border; 115 SkBitmap border;
116 border.allocN32Pixels(width, height); 116 border.allocN32Pixels(width, height);
117 border.eraseColor(0); 117 border.eraseColor(0);
118 118
119 cairo_surface_t* surface = cairo_image_surface_create_for_data(
120 static_cast<unsigned char*>(border.getAddr(0, 0)), CAIRO_FORMAT_ARGB32,
121 width, height, width * 4);
122 cairo_t* cr = cairo_create(surface);
123
124 #if GTK_MAJOR_VERSION == 2
119 // Create a temporary GTK button to snapshot 125 // Create a temporary GTK button to snapshot
120 GtkWidget* window = gtk_offscreen_window_new(); 126 GtkWidget* window = gtk_offscreen_window_new();
121 GtkWidget* button = gtk_toggle_button_new(); 127 GtkWidget* button = gtk_toggle_button_new();
122 128
123 if (state_ == ui::NativeTheme::kPressed) 129 if (state_ == ui::NativeTheme::kPressed)
124 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), true); 130 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), true);
125 else if (state_ == ui::NativeTheme::kDisabled) 131 else if (state_ == ui::NativeTheme::kDisabled)
126 gtk_widget_set_sensitive(button, false); 132 gtk_widget_set_sensitive(button, false);
127 133
128 gtk_widget_set_size_request(button, width, height); 134 gtk_widget_set_size_request(button, width, height);
129 gtk_container_add(GTK_CONTAINER(window), button); 135 gtk_container_add(GTK_CONTAINER(window), button);
130 136
131 if (is_blue_) 137 if (is_blue_)
132 TurnButtonBlue(button); 138 TurnButtonBlue(button);
133 139
134 gtk_widget_show_all(window); 140 gtk_widget_show_all(window);
135 141
136 cairo_surface_t* surface = cairo_image_surface_create_for_data(
137 static_cast<unsigned char*>(border.getAddr(0, 0)), CAIRO_FORMAT_ARGB32,
138 width, height, width * 4);
139 cairo_t* cr = cairo_create(surface);
140
141 #if GTK_MAJOR_VERSION == 2
142 if (focus_) 142 if (focus_)
143 GTK_WIDGET_SET_FLAGS(button, GTK_HAS_FOCUS); 143 GTK_WIDGET_SET_FLAGS(button, GTK_HAS_FOCUS);
144 144
145 int w, h; 145 int w, h;
146 GdkPixmap* pixmap; 146 GdkPixmap* pixmap;
147 147
148 { 148 {
149 // http://crbug.com/346740 149 // http://crbug.com/346740
150 ANNOTATE_SCOPED_MEMORY_LEAK; 150 ANNOTATE_SCOPED_MEMORY_LEAK;
151 pixmap = gtk_widget_get_snapshot(button, nullptr); 151 pixmap = gtk_widget_get_snapshot(button, nullptr);
152 } 152 }
153 153
154 gdk_drawable_get_size(GDK_DRAWABLE(pixmap), &w, &h); 154 gdk_drawable_get_size(GDK_DRAWABLE(pixmap), &w, &h);
155 GdkColormap* colormap = gdk_drawable_get_colormap(pixmap); 155 GdkColormap* colormap = gdk_drawable_get_colormap(pixmap);
156 GdkPixbuf* pixbuf = gdk_pixbuf_get_from_drawable( 156 GdkPixbuf* pixbuf = gdk_pixbuf_get_from_drawable(
157 nullptr, GDK_DRAWABLE(pixmap), colormap, 0, 0, 0, 0, w, h); 157 nullptr, GDK_DRAWABLE(pixmap), colormap, 0, 0, 0, 0, w, h);
158 158
159 gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0); 159 gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0);
160 cairo_paint(cr); 160 cairo_paint(cr);
161 161
162 g_object_unref(pixbuf); 162 g_object_unref(pixbuf);
163 g_object_unref(pixmap); 163 g_object_unref(pixmap);
164
165 gtk_widget_destroy(window);
164 #else 166 #else
165 GtkStyleContext* context = gtk_widget_get_style_context(button); 167 ScopedStyleContext context = GetStyleContextFromCss(
168 is_blue_ ? "GtkButton#button.default.suggested-action"
169 : "GtkButton#button");
170 GtkStateFlags state_flags = StateToStateFlags(state_);
171 if (focus_) {
172 state_flags =
173 static_cast<GtkStateFlags>(state_flags | GTK_STATE_FLAG_FOCUSED);
174 }
175 gtk_style_context_set_state(context, state_flags);
166 gtk_render_background(context, cr, 0, 0, width, height); 176 gtk_render_background(context, cr, 0, 0, width, height);
167 gtk_render_frame(context, cr, 0, 0, width, height); 177 gtk_render_frame(context, cr, 0, 0, width, height);
168 if (focus_) 178 if (focus_)
169 gtk_render_focus(context, cr, 0, 0, width, height); 179 gtk_render_focus(context, cr, 0, 0, width, height);
170 #endif 180 #endif
171 181
172 cairo_destroy(cr); 182 cairo_destroy(cr);
173 cairo_surface_destroy(surface); 183 cairo_surface_destroy(surface);
174 184
175 gtk_widget_destroy(window);
176
177 return gfx::ImageSkiaRep(border, scale); 185 return gfx::ImageSkiaRep(border, scale);
178 } 186 }
179 187
180 private: 188 private:
181 bool is_blue_; 189 bool is_blue_;
182 bool focus_; 190 bool focus_;
183 ui::NativeTheme::State state_; 191 ui::NativeTheme::State state_;
184 int width_; 192 int width_;
185 int height_; 193 int height_;
186 194
(...skipping 890 matching lines...) Expand 10 before | Expand all | Expand 10 after
1077 1085
1078 float GtkUi::GetDeviceScaleFactor() const { 1086 float GtkUi::GetDeviceScaleFactor() const {
1079 return device_scale_factor_; 1087 return device_scale_factor_;
1080 } 1088 }
1081 1089
1082 } // namespace libgtkui 1090 } // namespace libgtkui
1083 1091
1084 views::LinuxUI* BuildGtkUi() { 1092 views::LinuxUI* BuildGtkUi() {
1085 return new libgtkui::GtkUi; 1093 return new libgtkui::GtkUi;
1086 } 1094 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/libgtkui/gtk_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698