| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/gtk/custom_button.h" | 5 #include "chrome/browser/gtk/custom_button.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "app/theme_provider.h" | 9 #include "app/theme_provider.h" |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/gfx/gtk_util.h" | 11 #include "base/gfx/gtk_util.h" |
| 12 #include "chrome/browser/gtk/cairo_cached_surface.h" |
| 12 #include "chrome/browser/gtk/gtk_chrome_button.h" | 13 #include "chrome/browser/gtk/gtk_chrome_button.h" |
| 13 #include "chrome/browser/gtk/gtk_theme_provider.h" | 14 #include "chrome/browser/gtk/gtk_theme_provider.h" |
| 14 #include "chrome/common/gtk_util.h" | 15 #include "chrome/common/gtk_util.h" |
| 15 #include "chrome/common/notification_service.h" | 16 #include "chrome/common/notification_service.h" |
| 16 #include "grit/theme_resources.h" | 17 #include "grit/theme_resources.h" |
| 17 #include "skia/ext/image_operations.h" | 18 #include "skia/ext/image_operations.h" |
| 18 | 19 |
| 19 CustomDrawButtonBase::CustomDrawButtonBase(GtkThemeProvider* theme_provider, | 20 CustomDrawButtonBase::CustomDrawButtonBase(GtkThemeProvider* theme_provider, |
| 20 int normal_id, int active_id, int highlight_id, int depressed_id) | 21 int normal_id, int active_id, int highlight_id, int depressed_id) |
| 21 : background_image_(NULL), | 22 : background_image_(NULL), |
| 22 paint_override_(-1), | 23 paint_override_(-1), |
| 23 normal_id_(normal_id), | 24 normal_id_(normal_id), |
| 24 active_id_(active_id), | 25 active_id_(active_id), |
| 25 highlight_id_(highlight_id), | 26 highlight_id_(highlight_id), |
| 26 depressed_id_(depressed_id), | 27 depressed_id_(depressed_id), |
| 27 theme_provider_(theme_provider) { | 28 theme_provider_(theme_provider) { |
| 29 for (int i = 0; i < (GTK_STATE_INSENSITIVE + 1); ++i) |
| 30 surfaces_[i].reset(new CairoCachedSurface); |
| 31 background_image_.reset(new CairoCachedSurface); |
| 32 |
| 28 if (theme_provider) { | 33 if (theme_provider) { |
| 29 // Load images by pretending that we got a BROWSER_THEME_CHANGED | 34 // Load images by pretending that we got a BROWSER_THEME_CHANGED |
| 30 // notification. | 35 // notification. |
| 31 theme_provider->InitThemesFor(this); | 36 theme_provider->InitThemesFor(this); |
| 32 | 37 |
| 33 registrar_.Add(this, | 38 registrar_.Add(this, |
| 34 NotificationType::BROWSER_THEME_CHANGED, | 39 NotificationType::BROWSER_THEME_CHANGED, |
| 35 NotificationService::AllSources()); | 40 NotificationService::AllSources()); |
| 36 } else { | 41 } else { |
| 37 // Load the button images from the resource bundle. | 42 // Load the button images from the resource bundle. |
| 38 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 43 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 39 pixbufs_[GTK_STATE_NORMAL] = | 44 surfaces_[GTK_STATE_NORMAL]->UsePixbuf( |
| 40 normal_id ? rb.GetRTLEnabledPixbufNamed(normal_id) : NULL; | 45 normal_id ? rb.GetRTLEnabledPixbufNamed(normal_id) : NULL); |
| 41 pixbufs_[GTK_STATE_ACTIVE] = | 46 surfaces_[GTK_STATE_ACTIVE]->UsePixbuf( |
| 42 active_id ? rb.GetRTLEnabledPixbufNamed(active_id) : NULL; | 47 active_id ? rb.GetRTLEnabledPixbufNamed(active_id) : NULL); |
| 43 pixbufs_[GTK_STATE_PRELIGHT] = | 48 surfaces_[GTK_STATE_PRELIGHT]->UsePixbuf( |
| 44 highlight_id ? rb.GetRTLEnabledPixbufNamed(highlight_id) : NULL; | 49 highlight_id ? rb.GetRTLEnabledPixbufNamed(highlight_id) : NULL); |
| 45 pixbufs_[GTK_STATE_SELECTED] = NULL; | 50 surfaces_[GTK_STATE_SELECTED]->UsePixbuf(NULL); |
| 46 pixbufs_[GTK_STATE_INSENSITIVE] = | 51 surfaces_[GTK_STATE_INSENSITIVE]->UsePixbuf( |
| 47 depressed_id ? rb.GetRTLEnabledPixbufNamed(depressed_id) : NULL; | 52 depressed_id ? rb.GetRTLEnabledPixbufNamed(depressed_id) : NULL); |
| 48 } | 53 } |
| 49 } | 54 } |
| 50 | 55 |
| 51 CustomDrawButtonBase::~CustomDrawButtonBase() { | 56 CustomDrawButtonBase::~CustomDrawButtonBase() { |
| 52 if (background_image_) { | 57 } |
| 53 g_object_unref(background_image_); | 58 |
| 54 background_image_ = NULL; | 59 int CustomDrawButtonBase::Width() const { |
| 55 } | 60 return surfaces_[0]->Width(); |
| 61 } |
| 62 |
| 63 int CustomDrawButtonBase::Height() const { |
| 64 return surfaces_[0]->Height(); |
| 56 } | 65 } |
| 57 | 66 |
| 58 gboolean CustomDrawButtonBase::OnExpose(GtkWidget* widget, GdkEventExpose* e) { | 67 gboolean CustomDrawButtonBase::OnExpose(GtkWidget* widget, GdkEventExpose* e) { |
| 59 GdkPixbuf* pixbuf = pixbufs_[paint_override_ >= 0 ? | 68 CairoCachedSurface* pixbuf = |
| 60 paint_override_ : GTK_WIDGET_STATE(widget)]; | 69 surfaces_[paint_override_ >= 0 ? |
| 70 paint_override_ : GTK_WIDGET_STATE(widget)].get(); |
| 61 | 71 |
| 62 // Fall back to the default image if we don't have one for this state. | 72 // Fall back to the default image if we don't have one for this state. |
| 63 if (!pixbuf) | 73 if (!pixbuf || !pixbuf->valid()) |
| 64 pixbuf = pixbufs_[GTK_STATE_NORMAL]; | 74 pixbuf = surfaces_[GTK_STATE_NORMAL].get(); |
| 65 | 75 |
| 66 if (!pixbuf) | 76 if (!pixbuf || !pixbuf->valid()) |
| 67 return FALSE; | 77 return FALSE; |
| 68 | 78 |
| 69 cairo_t* cairo_context = gdk_cairo_create(GDK_DRAWABLE(widget->window)); | 79 cairo_t* cairo_context = gdk_cairo_create(GDK_DRAWABLE(widget->window)); |
| 70 cairo_translate(cairo_context, widget->allocation.x, widget->allocation.y); | 80 cairo_translate(cairo_context, widget->allocation.x, widget->allocation.y); |
| 71 | 81 |
| 72 // The widget might be larger than the pixbuf. Paint the pixbuf flush with the | 82 // The widget might be larger than the pixbuf. Paint the pixbuf flush with the |
| 73 // start of the widget (left for LTR, right for RTL) and its bottom. | 83 // start of the widget (left for LTR, right for RTL) and its bottom. |
| 74 gfx::Rect bounds = gfx::Rect(0, 0, gdk_pixbuf_get_width(pixbuf), 0); | 84 gfx::Rect bounds = gfx::Rect(0, 0, pixbuf->Width(), 0); |
| 75 int x = gtk_util::MirroredLeftPointForRect(widget, bounds); | 85 int x = gtk_util::MirroredLeftPointForRect(widget, bounds); |
| 76 int y = widget->allocation.height - gdk_pixbuf_get_height(pixbuf); | 86 int y = widget->allocation.height - pixbuf->Height(); |
| 77 | 87 |
| 78 if (background_image_) { | 88 if (background_image_->valid()) { |
| 79 gdk_cairo_set_source_pixbuf(cairo_context, background_image_, x, y); | 89 background_image_->SetSource(cairo_context, x, y); |
| 80 cairo_paint(cairo_context); | 90 cairo_paint(cairo_context); |
| 81 } | 91 } |
| 82 | 92 |
| 83 gdk_cairo_set_source_pixbuf(cairo_context, pixbuf, x, y); | 93 pixbuf->SetSource(cairo_context, x, y); |
| 84 cairo_paint(cairo_context); | 94 cairo_paint(cairo_context); |
| 85 cairo_destroy(cairo_context); | 95 cairo_destroy(cairo_context); |
| 86 | 96 |
| 87 return TRUE; | 97 return TRUE; |
| 88 } | 98 } |
| 89 | 99 |
| 90 void CustomDrawButtonBase::SetBackground(SkColor color, | 100 void CustomDrawButtonBase::SetBackground(SkColor color, |
| 91 SkBitmap* image, SkBitmap* mask) { | 101 SkBitmap* image, SkBitmap* mask) { |
| 92 if (!image || !mask) { | 102 if (!image || !mask) { |
| 93 if (background_image_) { | 103 if (background_image_->valid()) { |
| 94 g_object_unref(background_image_); | 104 background_image_->UsePixbuf(NULL); |
| 95 background_image_ = NULL; | |
| 96 } | 105 } |
| 97 } else { | 106 } else { |
| 98 SkBitmap img = skia::ImageOperations::CreateButtonBackground(color, | 107 SkBitmap img = skia::ImageOperations::CreateButtonBackground(color, |
| 99 *image, *mask); | 108 *image, *mask); |
| 100 background_image_ = gfx::GdkPixbufFromSkBitmap(&img); | 109 |
| 110 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&img); |
| 111 background_image_->UsePixbuf(pixbuf); |
| 112 g_object_unref(pixbuf); |
| 101 } | 113 } |
| 102 } | 114 } |
| 103 | 115 |
| 104 void CustomDrawButtonBase::Observe(NotificationType type, | 116 void CustomDrawButtonBase::Observe(NotificationType type, |
| 105 const NotificationSource& source, const NotificationDetails& details) { | 117 const NotificationSource& source, const NotificationDetails& details) { |
| 106 DCHECK(theme_provider_); | 118 DCHECK(theme_provider_); |
| 107 DCHECK(NotificationType::BROWSER_THEME_CHANGED == type); | 119 DCHECK(NotificationType::BROWSER_THEME_CHANGED == type); |
| 108 | 120 |
| 109 pixbufs_[GTK_STATE_NORMAL] = normal_id_ ? | 121 surfaces_[GTK_STATE_NORMAL]->UsePixbuf(normal_id_ ? |
| 110 theme_provider_->GetRTLEnabledPixbufNamed(normal_id_) : NULL; | 122 theme_provider_->GetRTLEnabledPixbufNamed(normal_id_) : NULL); |
| 111 pixbufs_[GTK_STATE_ACTIVE] = active_id_ ? | 123 surfaces_[GTK_STATE_ACTIVE]->UsePixbuf(active_id_ ? |
| 112 theme_provider_->GetRTLEnabledPixbufNamed(active_id_) : NULL; | 124 theme_provider_->GetRTLEnabledPixbufNamed(active_id_) : NULL); |
| 113 pixbufs_[GTK_STATE_PRELIGHT] = highlight_id_ ? | 125 surfaces_[GTK_STATE_PRELIGHT]->UsePixbuf(highlight_id_ ? |
| 114 theme_provider_->GetRTLEnabledPixbufNamed(highlight_id_) : NULL; | 126 theme_provider_->GetRTLEnabledPixbufNamed(highlight_id_) : NULL); |
| 115 pixbufs_[GTK_STATE_SELECTED] = NULL; | 127 surfaces_[GTK_STATE_SELECTED]->UsePixbuf(NULL); |
| 116 pixbufs_[GTK_STATE_INSENSITIVE] = depressed_id_ ? | 128 surfaces_[GTK_STATE_INSENSITIVE]->UsePixbuf(depressed_id_ ? |
| 117 theme_provider_->GetRTLEnabledPixbufNamed(depressed_id_) : NULL; | 129 theme_provider_->GetRTLEnabledPixbufNamed(depressed_id_) : NULL); |
| 118 } | 130 } |
| 119 | 131 |
| 120 CustomDrawButton::CustomDrawButton(int normal_id, int active_id, | 132 CustomDrawButton::CustomDrawButton(int normal_id, int active_id, |
| 121 int highlight_id, int depressed_id) | 133 int highlight_id, int depressed_id) |
| 122 : button_base_(NULL, normal_id, active_id, highlight_id, depressed_id), | 134 : button_base_(NULL, normal_id, active_id, highlight_id, depressed_id), |
| 123 theme_provider_(NULL), | 135 theme_provider_(NULL), |
| 124 gtk_stock_name_(NULL), | 136 gtk_stock_name_(NULL), |
| 125 icon_size_(GTK_ICON_SIZE_INVALID) { | 137 icon_size_(GTK_ICON_SIZE_INVALID) { |
| 126 Init(); | 138 Init(); |
| 127 | 139 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 bool use_gtk = theme_provider_ && theme_provider_->UseGtkTheme(); | 216 bool use_gtk = theme_provider_ && theme_provider_->UseGtkTheme(); |
| 205 | 217 |
| 206 if (use_gtk && gtk_stock_name_) { | 218 if (use_gtk && gtk_stock_name_) { |
| 207 gtk_button_set_image( | 219 gtk_button_set_image( |
| 208 GTK_BUTTON(widget_.get()), | 220 GTK_BUTTON(widget_.get()), |
| 209 gtk_image_new_from_stock(gtk_stock_name_, icon_size_)); | 221 gtk_image_new_from_stock(gtk_stock_name_, icon_size_)); |
| 210 gtk_widget_set_size_request(widget_.get(), -1, -1); | 222 gtk_widget_set_size_request(widget_.get(), -1, -1); |
| 211 gtk_widget_set_app_paintable(widget_.get(), FALSE); | 223 gtk_widget_set_app_paintable(widget_.get(), FALSE); |
| 212 gtk_widget_set_double_buffered(widget_.get(), TRUE); | 224 gtk_widget_set_double_buffered(widget_.get(), TRUE); |
| 213 } else { | 225 } else { |
| 214 gtk_widget_set_size_request(widget_.get(), | 226 gtk_widget_set_size_request(widget_.get(), button_base_.Width(), |
| 215 gdk_pixbuf_get_width(button_base_.pixbufs(0)), | 227 button_base_.Height()); |
| 216 gdk_pixbuf_get_height(button_base_.pixbufs(0))); | |
| 217 | 228 |
| 218 gtk_widget_set_app_paintable(widget_.get(), TRUE); | 229 gtk_widget_set_app_paintable(widget_.get(), TRUE); |
| 219 // We effectively double-buffer by virtue of having only one image... | 230 // We effectively double-buffer by virtue of having only one image... |
| 220 gtk_widget_set_double_buffered(widget_.get(), FALSE); | 231 gtk_widget_set_double_buffered(widget_.get(), FALSE); |
| 221 } | 232 } |
| 222 | 233 |
| 223 gtk_chrome_button_set_use_gtk_rendering( | 234 gtk_chrome_button_set_use_gtk_rendering( |
| 224 GTK_CHROME_BUTTON(widget_.get()), use_gtk); | 235 GTK_CHROME_BUTTON(widget_.get()), use_gtk); |
| 225 } | 236 } |
| OLD | NEW |