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

Unified Diff: chrome/browser/ui/libgtk2ui/gtk2_ui.cc

Issue 1234223005: Initial gtk3 support (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed abort on GtkPrintUnixDialog Created 5 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/libgtk2ui/gtk2_ui.cc
diff --git a/chrome/browser/ui/libgtk2ui/gtk2_ui.cc b/chrome/browser/ui/libgtk2ui/gtk2_ui.cc
index 6f9e94100ee88bd8bdaf49128a54c00bd9f9ee8b..d2f84324ef6e1904e42feab82b3a522738060881 100644
--- a/chrome/browser/ui/libgtk2ui/gtk2_ui.cc
+++ b/chrome/browser/ui/libgtk2ui/gtk2_ui.cc
@@ -181,21 +181,21 @@ const int kAutocompleteImages[] = {
struct IDRGtkMapping {
int idr;
const char* stock_id;
- GtkStateType gtk_state;
+ GtkStateFlags gtk_state;
} const kGtkIcons[] = {
- { IDR_BACK, GTK_STOCK_GO_BACK, GTK_STATE_NORMAL },
- { IDR_BACK_D, GTK_STOCK_GO_BACK, GTK_STATE_INSENSITIVE },
+ { IDR_BACK, GTK_STOCK_GO_BACK, GTK_STATE_FLAG_NORMAL },
+ { IDR_BACK_D, GTK_STOCK_GO_BACK, GTK_STATE_FLAG_INSENSITIVE },
- { IDR_FORWARD, GTK_STOCK_GO_FORWARD, GTK_STATE_NORMAL },
- { IDR_FORWARD_D, GTK_STOCK_GO_FORWARD, GTK_STATE_INSENSITIVE },
+ { IDR_FORWARD, GTK_STOCK_GO_FORWARD, GTK_STATE_FLAG_NORMAL },
+ { IDR_FORWARD_D, GTK_STOCK_GO_FORWARD, GTK_STATE_FLAG_INSENSITIVE },
- { IDR_HOME, GTK_STOCK_HOME, GTK_STATE_NORMAL },
+ { IDR_HOME, GTK_STOCK_HOME, GTK_STATE_FLAG_NORMAL },
- { IDR_RELOAD, GTK_STOCK_REFRESH, GTK_STATE_NORMAL },
- { IDR_RELOAD_D, GTK_STOCK_REFRESH, GTK_STATE_INSENSITIVE },
+ { IDR_RELOAD, GTK_STOCK_REFRESH, GTK_STATE_FLAG_NORMAL },
+ { IDR_RELOAD_D, GTK_STOCK_REFRESH, GTK_STATE_FLAG_INSENSITIVE },
- { IDR_STOP, GTK_STOCK_STOP, GTK_STATE_NORMAL },
- { IDR_STOP_D, GTK_STOCK_STOP, GTK_STATE_INSENSITIVE },
+ { IDR_STOP, GTK_STOCK_STOP, GTK_STATE_FLAG_NORMAL },
+ { IDR_STOP_D, GTK_STOCK_STOP, GTK_STATE_FLAG_INSENSITIVE },
};
// The image resources that will be tinted by the 'button' tint value.
@@ -772,90 +772,6 @@ void Gtk2UI::GetScrollbarColors(GdkColor* thumb_active_color,
gdk_color_free(theme_trough_color);
return;
}
-
- // Create window containing scrollbar elements
- GtkWidget* window = gtk_window_new(GTK_WINDOW_POPUP);
- GtkWidget* fixed = gtk_fixed_new();
- GtkWidget* scrollbar = gtk_hscrollbar_new(NULL);
- gtk_container_add(GTK_CONTAINER(window), fixed);
- gtk_container_add(GTK_CONTAINER(fixed), scrollbar);
- gtk_widget_realize(window);
- gtk_widget_realize(scrollbar);
-
- // Draw scrollbar thumb part and track into offscreen image
- const int kWidth = 100;
- const int kHeight = 20;
- GtkStyle* style = gtk_rc_get_style(scrollbar);
- GdkWindow* gdk_window = gtk_widget_get_window(window);
- GdkPixmap* pm = gdk_pixmap_new(gdk_window, kWidth, kHeight, -1);
- GdkRectangle rect = { 0, 0, kWidth, kHeight };
- unsigned char data[3 * kWidth * kHeight];
- for (int i = 0; i < 3; ++i) {
- if (i < 2) {
- // Thumb part
- gtk_paint_slider(style, pm,
- i == 0 ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
- GTK_SHADOW_OUT, &rect, scrollbar, "slider", 0, 0,
- kWidth, kHeight, GTK_ORIENTATION_HORIZONTAL);
- } else {
- // Track
- gtk_paint_box(style, pm, GTK_STATE_ACTIVE, GTK_SHADOW_IN, &rect,
- scrollbar, "trough-upper", 0, 0, kWidth, kHeight);
- }
- GdkPixbuf* pb = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB,
- FALSE, 8, kWidth, kHeight,
- 3 * kWidth, 0, 0);
- gdk_pixbuf_get_from_drawable(pb, pm, NULL, 0, 0, 0, 0, kWidth, kHeight);
-
- // Sample pixels
- int components[3] = { 0 };
- for (int y = 2; y < kHeight - 2; ++y) {
- for (int c = 0; c < 3; ++c) {
- // Sample a vertical slice of pixels at about one-thirds from the
- // left edge. This allows us to avoid any fixed graphics that might be
- // located at the edges or in the center of the scrollbar.
- // Each pixel is made up of a red, green, and blue component; taking up
- // a total of three bytes.
- components[c] += data[3 * (kWidth / 3 + y * kWidth) + c];
- }
- }
- GdkColor* color = i == 0 ? thumb_active_color :
- i == 1 ? thumb_inactive_color :
- track_color;
- color->pixel = 0;
- // We sampled pixels across the full height of the image, ignoring a two
- // pixel border. In some themes, the border has a completely different
- // color which we do not want to factor into our average color computation.
- //
- // We now need to scale the colors from the 0..255 range, to the wider
- // 0..65535 range, and we need to actually compute the average color; so,
- // we divide by the total number of pixels in the sample.
- color->red = components[0] * 65535 / (255 * (kHeight - 4));
- color->green = components[1] * 65535 / (255 * (kHeight - 4));
- color->blue = components[2] * 65535 / (255 * (kHeight - 4));
-
- g_object_unref(pb);
- }
- g_object_unref(pm);
-
- gtk_widget_destroy(window);
-
- // Override any of the default colors with ones that were specified by the
- // theme.
- if (theme_thumb_active) {
- *thumb_active_color = *theme_thumb_active;
- gdk_color_free(theme_thumb_active);
- }
-
- if (theme_thumb_inactive) {
- *thumb_inactive_color = *theme_thumb_inactive;
- gdk_color_free(theme_thumb_inactive);
- }
-
- if (theme_trough_color) {
- *track_color = *theme_trough_color;
- gdk_color_free(theme_trough_color);
- }
}
void Gtk2UI::LoadGtkValues() {
@@ -1143,9 +1059,9 @@ SkBitmap Gtk2UI::GenerateGtkThemeBitmap(int id) const {
return GenerateGTKIcon(id);
}
case IDR_TOOLBAR_BEZEL_HOVER:
- return GenerateToolbarBezel(GTK_STATE_PRELIGHT, IDR_TOOLBAR_BEZEL_HOVER);
+ return GenerateToolbarBezel(GTK_STATE_FLAG_PRELIGHT, IDR_TOOLBAR_BEZEL_HOVER);
case IDR_TOOLBAR_BEZEL_PRESSED:
- return GenerateToolbarBezel(GTK_STATE_ACTIVE, IDR_TOOLBAR_BEZEL_PRESSED);
+ return GenerateToolbarBezel(GTK_STATE_FLAG_ACTIVE, IDR_TOOLBAR_BEZEL_PRESSED);
default: {
return GenerateTintedIcon(id, button_tint_);
}
@@ -1213,7 +1129,7 @@ SkBitmap Gtk2UI::GenerateTintedIcon(
SkBitmap Gtk2UI::GenerateGTKIcon(int base_id) const {
const char* stock_id = NULL;
- GtkStateType gtk_state = GTK_STATE_NORMAL;
+ GtkStateFlags gtk_state = GTK_STATE_FLAG_NORMAL;
for (unsigned int i = 0; i < arraysize(kGtkIcons); ++i) {
if (kGtkIcons[i].idr == base_id) {
stock_id = kGtkIcons[i].stock_id;
@@ -1237,7 +1153,7 @@ SkBitmap Gtk2UI::GenerateGTKIcon(int base_id) const {
icon_set,
style,
base::i18n::IsRTL() ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR,
- gtk_state,
+ gtk_state == GTK_STATE_FLAG_INSENSITIVE ? GTK_STATE_INSENSITIVE : GTK_STATE_NORMAL,
GTK_ICON_SIZE_SMALL_TOOLBAR,
fake_frame_,
NULL);
@@ -1258,15 +1174,6 @@ SkBitmap Gtk2UI::GenerateGTKIcon(int base_id) const {
SkCanvas canvas(retval);
- if (gtk_state == GTK_STATE_ACTIVE || gtk_state == GTK_STATE_PRELIGHT) {
- SkBitmap border = DrawGtkButtonBorder(gtk_state,
- false,
- false,
- default_bitmap.width(),
- default_bitmap.height());
- canvas.drawBitmap(border, 0, 0);
- }
-
canvas.drawBitmap(icon,
(default_bitmap.width() / 2) - (icon.width() / 2),
(default_bitmap.height() / 2) - (icon.height() / 2));
@@ -1286,8 +1193,6 @@ SkBitmap Gtk2UI::GenerateToolbarBezel(int gtk_state, int sizing_idr) const {
SkCanvas canvas(retval);
SkBitmap border = DrawGtkButtonBorder(
gtk_state,
- false,
- false,
default_bitmap.width(),
default_bitmap.height());
canvas.drawBitmap(border, 0, 0);
@@ -1327,13 +1232,19 @@ void Gtk2UI::GetSelectedEntryForegroundHSL(color_utils::HSL* tint) const {
}
SkBitmap Gtk2UI::DrawGtkButtonBorder(int gtk_state,
- bool focused,
- bool call_to_action,
int width,
int height) const {
+ SkBitmap border;
+ border.allocN32Pixels(width, height);
+ border.eraseColor(0);
+
// Create a temporary GTK button to snapshot
GtkWidget* window = gtk_offscreen_window_new();
- GtkWidget* button = gtk_button_new();
+ GtkWidget* button = gtk_toggle_button_new();
+
+ if (gtk_state & GTK_STATE_FLAG_ACTIVE)
+ gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), true);
+
gtk_widget_set_size_request(button, width, height);
gtk_container_add(GTK_CONTAINER(window), button);
gtk_widget_realize(window);
@@ -1341,41 +1252,19 @@ SkBitmap Gtk2UI::DrawGtkButtonBorder(int gtk_state,
gtk_widget_show(button);
gtk_widget_show(window);
- if (call_to_action)
- GTK_WIDGET_SET_FLAGS(button, GTK_HAS_DEFAULT);
+ gtk_widget_set_state_flags(button, (GtkStateFlags)gtk_state, FALSE);
- if (focused) {
- // We can't just use gtk_widget_grab_focus() here because that sets
- // gtk_widget_is_focus(), but not gtk_widget_has_focus(), which is what the
- // GtkButton's paint checks.
- GTK_WIDGET_SET_FLAGS(button, GTK_HAS_FOCUS);
- }
+ cairo_surface_t* surface = cairo_image_surface_create_for_data(
+ static_cast<unsigned char*>(border.getAddr(0, 0)),
+ CAIRO_FORMAT_ARGB32,
+ width, height,
+ width * 4);
+ cairo_t* cr = cairo_create(surface);
- gtk_widget_set_state(button, static_cast<GtkStateType>(gtk_state));
+ gtk_widget_draw(button, cr);
- GdkPixmap* pixmap;
- {
- // http://crbug.com/346740
- ANNOTATE_SCOPED_MEMORY_LEAK;
- pixmap = gtk_widget_get_snapshot(button, NULL);
- }
- int w, h;
- gdk_drawable_get_size(GDK_DRAWABLE(pixmap), &w, &h);
- DCHECK_EQ(w, width);
- DCHECK_EQ(h, height);
-
- // We render the Pixmap to a Pixbuf. This can be slow, as we're scrapping
- // bits from X.
- GdkColormap* colormap = gdk_drawable_get_colormap(pixmap);
- GdkPixbuf* pixbuf = gdk_pixbuf_get_from_drawable(NULL,
- GDK_DRAWABLE(pixmap),
- colormap,
- 0, 0, 0, 0, w, h);
-
- // Finally, we convert our pixbuf into a type we can use.
- SkBitmap border = GdkPixbufToImageSkia(pixbuf);
- g_object_unref(pixbuf);
- g_object_unref(pixmap);
+ cairo_destroy(cr);
+ cairo_surface_destroy(surface);
gtk_widget_destroy(window);
return border;

Powered by Google App Engine
This is Rietveld 408576698