| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/gtk/custom_button.h" | 5 #include "chrome/browser/ui/gtk/custom_button.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "chrome/browser/ui/gtk/cairo_cached_surface.h" | |
| 10 #include "chrome/browser/ui/gtk/gtk_chrome_button.h" | 9 #include "chrome/browser/ui/gtk/gtk_chrome_button.h" |
| 11 #include "chrome/browser/ui/gtk/gtk_theme_service.h" | 10 #include "chrome/browser/ui/gtk/gtk_theme_service.h" |
| 12 #include "chrome/browser/ui/gtk/gtk_util.h" | 11 #include "chrome/browser/ui/gtk/gtk_util.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | 12 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "content/public/browser/notification_source.h" | 13 #include "content/public/browser/notification_source.h" |
| 15 #include "grit/theme_resources_standard.h" | 14 #include "grit/theme_resources_standard.h" |
| 16 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 #include "ui/base/resource/resource_bundle.h" | 16 #include "ui/base/resource/resource_bundle.h" |
| 18 #include "ui/gfx/gtk_util.h" | 17 #include "ui/gfx/gtk_util.h" |
| 18 #include "ui/gfx/image/cairo_cached_surface.h" |
| 19 #include "ui/gfx/skbitmap_operations.h" | 19 #include "ui/gfx/skbitmap_operations.h" |
| 20 | 20 |
| 21 CustomDrawButtonBase::CustomDrawButtonBase(GtkThemeService* theme_provider, | 21 CustomDrawButtonBase::CustomDrawButtonBase(GtkThemeService* theme_provider, |
| 22 int normal_id, | 22 int normal_id, |
| 23 int pressed_id, | 23 int pressed_id, |
| 24 int hover_id, | 24 int hover_id, |
| 25 int disabled_id) | 25 int disabled_id) |
| 26 : background_image_(NULL), | 26 : background_image_(NULL), |
| 27 paint_override_(-1), | 27 paint_override_(-1), |
| 28 normal_id_(normal_id), | 28 normal_id_(normal_id), |
| 29 pressed_id_(pressed_id), | 29 pressed_id_(pressed_id), |
| 30 hover_id_(hover_id), | 30 hover_id_(hover_id), |
| 31 disabled_id_(disabled_id), | 31 disabled_id_(disabled_id), |
| 32 theme_service_(theme_provider), | 32 theme_service_(theme_provider), |
| 33 flipped_(false) { | 33 flipped_(false) { |
| 34 for (int i = 0; i < (GTK_STATE_INSENSITIVE + 1); ++i) | 34 for (int i = 0; i < (GTK_STATE_INSENSITIVE + 1); ++i) |
| 35 surfaces_[i].reset(new CairoCachedSurface); | 35 surfaces_[i].reset(new gfx::CairoCachedSurface); |
| 36 background_image_.reset(new CairoCachedSurface); | 36 background_image_.reset(new gfx::CairoCachedSurface); |
| 37 | 37 |
| 38 if (theme_provider) { | 38 if (theme_provider) { |
| 39 // Load images by pretending that we got a BROWSER_THEME_CHANGED | 39 // Load images by pretending that we got a BROWSER_THEME_CHANGED |
| 40 // notification. | 40 // notification. |
| 41 theme_provider->InitThemesFor(this); | 41 theme_provider->InitThemesFor(this); |
| 42 | 42 |
| 43 registrar_.Add(this, | 43 registrar_.Add(this, |
| 44 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 44 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 45 content::Source<ThemeService>(theme_provider)); | 45 content::Source<ThemeService>(theme_provider)); |
| 46 } else { | 46 } else { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 74 gdouble hover_state) { | 74 gdouble hover_state) { |
| 75 int paint_state = paint_override_ >= 0 ? | 75 int paint_state = paint_override_ >= 0 ? |
| 76 paint_override_ : gtk_widget_get_state(widget); | 76 paint_override_ : gtk_widget_get_state(widget); |
| 77 | 77 |
| 78 // If the paint state is PRELIGHT then set it to NORMAL (we will paint the | 78 // If the paint state is PRELIGHT then set it to NORMAL (we will paint the |
| 79 // hover state according to |hover_state_|). | 79 // hover state according to |hover_state_|). |
| 80 if (paint_state == GTK_STATE_PRELIGHT) | 80 if (paint_state == GTK_STATE_PRELIGHT) |
| 81 paint_state = GTK_STATE_NORMAL; | 81 paint_state = GTK_STATE_NORMAL; |
| 82 bool animating_hover = hover_state > 0.0 && | 82 bool animating_hover = hover_state > 0.0 && |
| 83 paint_state == GTK_STATE_NORMAL; | 83 paint_state == GTK_STATE_NORMAL; |
| 84 CairoCachedSurface* pixbuf = PixbufForState(paint_state); | 84 gfx::CairoCachedSurface* pixbuf = PixbufForState(paint_state); |
| 85 CairoCachedSurface* hover_pixbuf = PixbufForState(GTK_STATE_PRELIGHT); | 85 gfx::CairoCachedSurface* hover_pixbuf = PixbufForState(GTK_STATE_PRELIGHT); |
| 86 | 86 |
| 87 if (!pixbuf || !pixbuf->valid()) | 87 if (!pixbuf || !pixbuf->valid()) |
| 88 return FALSE; | 88 return FALSE; |
| 89 if (animating_hover && (!hover_pixbuf || !hover_pixbuf->valid())) | 89 if (animating_hover && (!hover_pixbuf || !hover_pixbuf->valid())) |
| 90 return FALSE; | 90 return FALSE; |
| 91 | 91 |
| 92 cairo_t* cairo_context = gdk_cairo_create(GDK_DRAWABLE(widget->window)); | 92 cairo_t* cairo_context = gdk_cairo_create(GDK_DRAWABLE(widget->window)); |
| 93 GtkAllocation allocation; | 93 GtkAllocation allocation; |
| 94 gtk_widget_get_allocation(widget, &allocation); | 94 gtk_widget_get_allocation(widget, &allocation); |
| 95 cairo_translate(cairo_context, allocation.x, allocation.y); | 95 cairo_translate(cairo_context, allocation.x, allocation.y); |
| 96 | 96 |
| 97 if (flipped_) { | 97 if (flipped_) { |
| 98 // Horizontally flip the image for non-LTR/RTL reasons. | 98 // Horizontally flip the image for non-LTR/RTL reasons. |
| 99 cairo_translate(cairo_context, allocation.width, 0.0f); | 99 cairo_translate(cairo_context, allocation.width, 0.0f); |
| 100 cairo_scale(cairo_context, -1.0f, 1.0f); | 100 cairo_scale(cairo_context, -1.0f, 1.0f); |
| 101 } | 101 } |
| 102 | 102 |
| 103 // The widget might be larger than the pixbuf. Paint the pixbuf flush with the | 103 // The widget might be larger than the pixbuf. Paint the pixbuf flush with the |
| 104 // start of the widget (left for LTR, right for RTL) and its bottom. | 104 // start of the widget (left for LTR, right for RTL) and its bottom. |
| 105 gfx::Rect bounds = gfx::Rect(0, 0, pixbuf->Width(), 0); | 105 gfx::Rect bounds = gfx::Rect(0, 0, pixbuf->Width(), 0); |
| 106 int x = gtk_util::MirroredLeftPointForRect(widget, bounds); | 106 int x = gtk_util::MirroredLeftPointForRect(widget, bounds); |
| 107 int y = allocation.height - pixbuf->Height(); | 107 int y = allocation.height - pixbuf->Height(); |
| 108 | 108 |
| 109 if (background_image_->valid()) { | 109 if (background_image_->valid()) { |
| 110 background_image_->SetSource(cairo_context, x, y); | 110 background_image_->SetSource(cairo_context, widget, x, y); |
| 111 cairo_paint(cairo_context); | 111 cairo_paint(cairo_context); |
| 112 } | 112 } |
| 113 | 113 |
| 114 pixbuf->SetSource(cairo_context, x, y); | 114 pixbuf->SetSource(cairo_context, widget, x, y); |
| 115 cairo_paint(cairo_context); | 115 cairo_paint(cairo_context); |
| 116 | 116 |
| 117 if (animating_hover) { | 117 if (animating_hover) { |
| 118 hover_pixbuf->SetSource(cairo_context, x, y); | 118 hover_pixbuf->SetSource(cairo_context, widget, x, y); |
| 119 cairo_paint_with_alpha(cairo_context, hover_state); | 119 cairo_paint_with_alpha(cairo_context, hover_state); |
| 120 } | 120 } |
| 121 | 121 |
| 122 cairo_destroy(cairo_context); | 122 cairo_destroy(cairo_context); |
| 123 | 123 |
| 124 GtkWidget* child = gtk_bin_get_child(GTK_BIN(widget)); | 124 GtkWidget* child = gtk_bin_get_child(GTK_BIN(widget)); |
| 125 if (child) | 125 if (child) |
| 126 gtk_container_propagate_expose(GTK_CONTAINER(widget), child, e); | 126 gtk_container_propagate_expose(GTK_CONTAINER(widget), child, e); |
| 127 | 127 |
| 128 return TRUE; | 128 return TRUE; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 154 theme_service_->GetRTLEnabledPixbufNamed(normal_id_) : NULL); | 154 theme_service_->GetRTLEnabledPixbufNamed(normal_id_) : NULL); |
| 155 surfaces_[GTK_STATE_ACTIVE]->UsePixbuf(pressed_id_ ? | 155 surfaces_[GTK_STATE_ACTIVE]->UsePixbuf(pressed_id_ ? |
| 156 theme_service_->GetRTLEnabledPixbufNamed(pressed_id_) : NULL); | 156 theme_service_->GetRTLEnabledPixbufNamed(pressed_id_) : NULL); |
| 157 surfaces_[GTK_STATE_PRELIGHT]->UsePixbuf(hover_id_ ? | 157 surfaces_[GTK_STATE_PRELIGHT]->UsePixbuf(hover_id_ ? |
| 158 theme_service_->GetRTLEnabledPixbufNamed(hover_id_) : NULL); | 158 theme_service_->GetRTLEnabledPixbufNamed(hover_id_) : NULL); |
| 159 surfaces_[GTK_STATE_SELECTED]->UsePixbuf(NULL); | 159 surfaces_[GTK_STATE_SELECTED]->UsePixbuf(NULL); |
| 160 surfaces_[GTK_STATE_INSENSITIVE]->UsePixbuf(disabled_id_ ? | 160 surfaces_[GTK_STATE_INSENSITIVE]->UsePixbuf(disabled_id_ ? |
| 161 theme_service_->GetRTLEnabledPixbufNamed(disabled_id_) : NULL); | 161 theme_service_->GetRTLEnabledPixbufNamed(disabled_id_) : NULL); |
| 162 } | 162 } |
| 163 | 163 |
| 164 CairoCachedSurface* CustomDrawButtonBase::PixbufForState(int state) { | 164 gfx::CairoCachedSurface* CustomDrawButtonBase::PixbufForState(int state) { |
| 165 CairoCachedSurface* pixbuf = surfaces_[state].get(); | 165 gfx::CairoCachedSurface* pixbuf = surfaces_[state].get(); |
| 166 | 166 |
| 167 // Fall back to the default image if we don't have one for this state. | 167 // Fall back to the default image if we don't have one for this state. |
| 168 if (!pixbuf || !pixbuf->valid()) | 168 if (!pixbuf || !pixbuf->valid()) |
| 169 pixbuf = surfaces_[GTK_STATE_NORMAL].get(); | 169 pixbuf = surfaces_[GTK_STATE_NORMAL].get(); |
| 170 | 170 |
| 171 return pixbuf; | 171 return pixbuf; |
| 172 } | 172 } |
| 173 | 173 |
| 174 // CustomDrawHoverController --------------------------------------------------- | 174 // CustomDrawHoverController --------------------------------------------------- |
| 175 | 175 |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 } | 365 } |
| 366 | 366 |
| 367 gtk_chrome_button_set_use_gtk_rendering( | 367 gtk_chrome_button_set_use_gtk_rendering( |
| 368 GTK_CHROME_BUTTON(widget()), UseGtkTheme()); | 368 GTK_CHROME_BUTTON(widget()), UseGtkTheme()); |
| 369 } | 369 } |
| 370 | 370 |
| 371 bool CustomDrawButton::UseGtkTheme() { | 371 bool CustomDrawButton::UseGtkTheme() { |
| 372 return !forcing_chrome_theme_ && theme_service_ && | 372 return !forcing_chrome_theme_ && theme_service_ && |
| 373 theme_service_->UsingNativeTheme(); | 373 theme_service_->UsingNativeTheme(); |
| 374 } | 374 } |
| OLD | NEW |