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