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

Side by Side Diff: chrome/browser/ui/gtk/custom_button.cc

Issue 8769017: GTK: Move CairoCachedSurface from being owned by GtkThemeService to gfx::Image. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 9 years 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
OLDNEW
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
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 cairo_translate(cairo_context, widget->allocation.x, widget->allocation.y); 93 cairo_translate(cairo_context, widget->allocation.x, widget->allocation.y);
94 94
95 if (flipped_) { 95 if (flipped_) {
96 // Horizontally flip the image for non-LTR/RTL reasons. 96 // Horizontally flip the image for non-LTR/RTL reasons.
97 cairo_translate(cairo_context, widget->allocation.width, 0.0f); 97 cairo_translate(cairo_context, widget->allocation.width, 0.0f);
98 cairo_scale(cairo_context, -1.0f, 1.0f); 98 cairo_scale(cairo_context, -1.0f, 1.0f);
99 } 99 }
100 100
101 // The widget might be larger than the pixbuf. Paint the pixbuf flush with the 101 // The widget might be larger than the pixbuf. Paint the pixbuf flush with the
102 // start of the widget (left for LTR, right for RTL) and its bottom. 102 // start of the widget (left for LTR, right for RTL) and its bottom.
103 gfx::Rect bounds = gfx::Rect(0, 0, pixbuf->Width(), 0); 103 gfx::Rect bounds = gfx::Rect(0, 0, pixbuf->Width(), 0);
104 int x = gtk_util::MirroredLeftPointForRect(widget, bounds); 104 int x = gtk_util::MirroredLeftPointForRect(widget, bounds);
105 int y = widget->allocation.height - pixbuf->Height(); 105 int y = widget->allocation.height - pixbuf->Height();
106 106
107 if (background_image_->valid()) { 107 if (background_image_->valid()) {
108 background_image_->SetSource(cairo_context, x, y); 108 background_image_->SetSource(cairo_context, widget, x, y);
109 cairo_paint(cairo_context); 109 cairo_paint(cairo_context);
110 } 110 }
111 111
112 pixbuf->SetSource(cairo_context, x, y); 112 pixbuf->SetSource(cairo_context, widget, x, y);
113 cairo_paint(cairo_context); 113 cairo_paint(cairo_context);
114 114
115 if (animating_hover) { 115 if (animating_hover) {
116 hover_pixbuf->SetSource(cairo_context, x, y); 116 hover_pixbuf->SetSource(cairo_context, widget, x, y);
117 cairo_paint_with_alpha(cairo_context, hover_state); 117 cairo_paint_with_alpha(cairo_context, hover_state);
118 } 118 }
119 119
120 cairo_destroy(cairo_context); 120 cairo_destroy(cairo_context);
121 121
122 GtkWidget* child = gtk_bin_get_child(GTK_BIN(widget)); 122 GtkWidget* child = gtk_bin_get_child(GTK_BIN(widget));
123 if (child) 123 if (child)
124 gtk_container_propagate_expose(GTK_CONTAINER(widget), child, e); 124 gtk_container_propagate_expose(GTK_CONTAINER(widget), child, e);
125 125
126 return TRUE; 126 return TRUE;
(...skipping 25 matching lines...) Expand all
152 theme_service_->GetRTLEnabledPixbufNamed(normal_id_) : NULL); 152 theme_service_->GetRTLEnabledPixbufNamed(normal_id_) : NULL);
153 surfaces_[GTK_STATE_ACTIVE]->UsePixbuf(pressed_id_ ? 153 surfaces_[GTK_STATE_ACTIVE]->UsePixbuf(pressed_id_ ?
154 theme_service_->GetRTLEnabledPixbufNamed(pressed_id_) : NULL); 154 theme_service_->GetRTLEnabledPixbufNamed(pressed_id_) : NULL);
155 surfaces_[GTK_STATE_PRELIGHT]->UsePixbuf(hover_id_ ? 155 surfaces_[GTK_STATE_PRELIGHT]->UsePixbuf(hover_id_ ?
156 theme_service_->GetRTLEnabledPixbufNamed(hover_id_) : NULL); 156 theme_service_->GetRTLEnabledPixbufNamed(hover_id_) : NULL);
157 surfaces_[GTK_STATE_SELECTED]->UsePixbuf(NULL); 157 surfaces_[GTK_STATE_SELECTED]->UsePixbuf(NULL);
158 surfaces_[GTK_STATE_INSENSITIVE]->UsePixbuf(disabled_id_ ? 158 surfaces_[GTK_STATE_INSENSITIVE]->UsePixbuf(disabled_id_ ?
159 theme_service_->GetRTLEnabledPixbufNamed(disabled_id_) : NULL); 159 theme_service_->GetRTLEnabledPixbufNamed(disabled_id_) : NULL);
160 } 160 }
161 161
162 CairoCachedSurface* CustomDrawButtonBase::PixbufForState(int state) { 162 gfx::CairoCachedSurface* CustomDrawButtonBase::PixbufForState(int state) {
163 CairoCachedSurface* pixbuf = surfaces_[state].get(); 163 gfx::CairoCachedSurface* pixbuf = surfaces_[state].get();
164 164
165 // Fall back to the default image if we don't have one for this state. 165 // Fall back to the default image if we don't have one for this state.
166 if (!pixbuf || !pixbuf->valid()) 166 if (!pixbuf || !pixbuf->valid())
167 pixbuf = surfaces_[GTK_STATE_NORMAL].get(); 167 pixbuf = surfaces_[GTK_STATE_NORMAL].get();
168 168
169 return pixbuf; 169 return pixbuf;
170 } 170 }
171 171
172 // CustomDrawHoverController --------------------------------------------------- 172 // CustomDrawHoverController ---------------------------------------------------
173 173
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 } 357 }
358 358
359 gtk_chrome_button_set_use_gtk_rendering( 359 gtk_chrome_button_set_use_gtk_rendering(
360 GTK_CHROME_BUTTON(widget()), UseGtkTheme()); 360 GTK_CHROME_BUTTON(widget()), UseGtkTheme());
361 } 361 }
362 362
363 bool CustomDrawButton::UseGtkTheme() { 363 bool CustomDrawButton::UseGtkTheme() {
364 return !forcing_chrome_theme_ && theme_service_ && 364 return !forcing_chrome_theme_ && theme_service_ &&
365 theme_service_->UsingNativeTheme(); 365 theme_service_->UsingNativeTheme();
366 } 366 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698