| 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" | |
| 10 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 11 #include "base/gfx/gtk_util.h" | 10 #include "base/gfx/gtk_util.h" |
| 12 #include "chrome/common/notification_service.h" | 11 |
| 13 #include "grit/theme_resources.h" | 12 #include "grit/theme_resources.h" |
| 14 | 13 |
| 15 CustomDrawButtonBase::CustomDrawButtonBase(ThemeProvider* theme_provider, | 14 CustomDrawButtonBase::CustomDrawButtonBase( |
| 16 int normal_id, int active_id, int highlight_id, int depressed_id) | 15 int normal_id, |
| 17 : paint_override_(-1), | 16 int active_id, |
| 18 normal_id_(normal_id), | 17 int highlight_id, |
| 19 active_id_(active_id), | 18 int depressed_id) |
| 20 highlight_id_(highlight_id), | 19 : paint_override_(-1) { |
| 21 depressed_id_(depressed_id), | 20 // Load the button images from the resource bundle. |
| 22 theme_provider_(theme_provider) { | 21 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 23 if (theme_provider) { | 22 pixbufs_[GTK_STATE_NORMAL] = |
| 24 // Load images by pretending that we got a BROWSER_THEME_CHANGED | 23 normal_id ? rb.GetRTLEnabledPixbufNamed(normal_id) : NULL; |
| 25 // notification. | 24 pixbufs_[GTK_STATE_ACTIVE] = |
| 26 Observe(NotificationType::BROWSER_THEME_CHANGED, | 25 active_id ? rb.GetRTLEnabledPixbufNamed(active_id) : NULL; |
| 27 NotificationService::AllSources(), | 26 pixbufs_[GTK_STATE_PRELIGHT] = |
| 28 NotificationService::NoDetails()); | 27 highlight_id ? rb.GetRTLEnabledPixbufNamed(highlight_id) : NULL; |
| 29 | 28 pixbufs_[GTK_STATE_SELECTED] = NULL; |
| 30 registrar_.Add(this, | 29 pixbufs_[GTK_STATE_INSENSITIVE] = |
| 31 NotificationType::BROWSER_THEME_CHANGED, | 30 depressed_id ? rb.GetRTLEnabledPixbufNamed(depressed_id) : NULL; |
| 32 NotificationService::AllSources()); | |
| 33 } else { | |
| 34 // Load the button images from the resource bundle. | |
| 35 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 36 pixbufs_[GTK_STATE_NORMAL] = | |
| 37 normal_id ? rb.GetRTLEnabledPixbufNamed(normal_id) : NULL; | |
| 38 pixbufs_[GTK_STATE_ACTIVE] = | |
| 39 active_id ? rb.GetRTLEnabledPixbufNamed(active_id) : NULL; | |
| 40 pixbufs_[GTK_STATE_PRELIGHT] = | |
| 41 highlight_id ? rb.GetRTLEnabledPixbufNamed(highlight_id) : NULL; | |
| 42 pixbufs_[GTK_STATE_SELECTED] = NULL; | |
| 43 pixbufs_[GTK_STATE_INSENSITIVE] = | |
| 44 depressed_id ? rb.GetRTLEnabledPixbufNamed(depressed_id) : NULL; | |
| 45 } | |
| 46 } | 31 } |
| 47 | 32 |
| 48 CustomDrawButtonBase::~CustomDrawButtonBase() { | 33 CustomDrawButtonBase::~CustomDrawButtonBase() { |
| 49 } | 34 } |
| 50 | 35 |
| 51 gboolean CustomDrawButtonBase::OnExpose(GtkWidget* widget, GdkEventExpose* e) { | 36 gboolean CustomDrawButtonBase::OnExpose(GtkWidget* widget, GdkEventExpose* e) { |
| 52 GdkPixbuf* pixbuf = pixbufs_[paint_override_ >= 0 ? | 37 GdkPixbuf* pixbuf = pixbufs_[paint_override_ >= 0 ? |
| 53 paint_override_ : GTK_WIDGET_STATE(widget)]; | 38 paint_override_ : GTK_WIDGET_STATE(widget)]; |
| 54 | 39 |
| 55 // Fall back to the default image if we don't have one for this state. | 40 // Fall back to the default image if we don't have one for this state. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 69 int x = gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL ? | 54 int x = gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL ? |
| 70 widget_width - pixbuf_width : 0; | 55 widget_width - pixbuf_width : 0; |
| 71 | 56 |
| 72 gdk_cairo_set_source_pixbuf(cairo_context, pixbuf, x, 0); | 57 gdk_cairo_set_source_pixbuf(cairo_context, pixbuf, x, 0); |
| 73 cairo_paint(cairo_context); | 58 cairo_paint(cairo_context); |
| 74 cairo_destroy(cairo_context); | 59 cairo_destroy(cairo_context); |
| 75 | 60 |
| 76 return TRUE; | 61 return TRUE; |
| 77 } | 62 } |
| 78 | 63 |
| 79 void CustomDrawButtonBase::Observe(NotificationType type, | 64 CustomDrawButton::CustomDrawButton( |
| 80 const NotificationSource& source, const NotificationDetails& details) { | 65 int normal_id, |
| 81 DCHECK(theme_provider_); | 66 int active_id, |
| 82 DCHECK(NotificationType::BROWSER_THEME_CHANGED == type); | 67 int highlight_id, |
| 83 | 68 int depressed_id, |
| 84 // TODO(tc): Add GetRTLEnabledPixbufNamed to ThemeProviderGtk. | 69 const char* stock_id) |
| 85 pixbufs_[GTK_STATE_NORMAL] = | 70 : button_base_(normal_id, active_id, highlight_id, depressed_id), |
| 86 normal_id_ ? theme_provider_->GetPixbufNamed(normal_id_) : NULL; | |
| 87 pixbufs_[GTK_STATE_ACTIVE] = | |
| 88 active_id_ ? theme_provider_->GetPixbufNamed(active_id_) : NULL; | |
| 89 pixbufs_[GTK_STATE_PRELIGHT] = | |
| 90 highlight_id_ ? theme_provider_->GetPixbufNamed(highlight_id_) : NULL; | |
| 91 pixbufs_[GTK_STATE_SELECTED] = NULL; | |
| 92 pixbufs_[GTK_STATE_INSENSITIVE] = | |
| 93 depressed_id_ ? theme_provider_->GetPixbufNamed(depressed_id_) : NULL; | |
| 94 } | |
| 95 | |
| 96 CustomDrawButton::CustomDrawButton(int normal_id, int active_id, | |
| 97 int highlight_id, int depressed_id, const char* stock_id) | |
| 98 : button_base_(NULL, normal_id, active_id, highlight_id, depressed_id), | |
| 99 gtk_stock_name_(stock_id), | 71 gtk_stock_name_(stock_id), |
| 100 has_expose_signal_handler_(false) { | 72 has_expose_signal_handler_(false) { |
| 101 Init(); | 73 widget_.Own(gtk_button_new()); |
| 102 } | 74 GTK_WIDGET_UNSET_FLAGS(widget_.get(), GTK_CAN_FOCUS); |
| 103 | 75 SetUseSystemTheme(false); |
| 104 CustomDrawButton::CustomDrawButton(ThemeProvider* theme_provider, | |
| 105 int normal_id, int active_id, int highlight_id, int depressed_id, | |
| 106 const char* stock_id) | |
| 107 : button_base_(theme_provider, normal_id, active_id, highlight_id, | |
| 108 depressed_id), | |
| 109 gtk_stock_name_(stock_id), | |
| 110 has_expose_signal_handler_(false) { | |
| 111 Init(); | |
| 112 } | 76 } |
| 113 | 77 |
| 114 CustomDrawButton::~CustomDrawButton() { | 78 CustomDrawButton::~CustomDrawButton() { |
| 115 widget_.Destroy(); | 79 widget_.Destroy(); |
| 116 } | 80 } |
| 117 | 81 |
| 118 void CustomDrawButton::Init() { | |
| 119 widget_.Own(gtk_button_new()); | |
| 120 GTK_WIDGET_UNSET_FLAGS(widget_.get(), GTK_CAN_FOCUS); | |
| 121 SetUseSystemTheme(false); | |
| 122 } | |
| 123 | |
| 124 void CustomDrawButton::SetUseSystemTheme(bool use_gtk) { | 82 void CustomDrawButton::SetUseSystemTheme(bool use_gtk) { |
| 125 if (use_gtk && gtk_stock_name_) { | 83 if (use_gtk && gtk_stock_name_) { |
| 126 gtk_button_set_image( | 84 gtk_button_set_image( |
| 127 GTK_BUTTON(widget_.get()), | 85 GTK_BUTTON(widget_.get()), |
| 128 gtk_image_new_from_stock(gtk_stock_name_, GTK_ICON_SIZE_BUTTON)); | 86 gtk_image_new_from_stock(gtk_stock_name_, GTK_ICON_SIZE_BUTTON)); |
| 129 gtk_widget_set_size_request(widget_.get(), -1, -1); | 87 gtk_widget_set_size_request(widget_.get(), -1, -1); |
| 130 gtk_widget_set_app_paintable(widget_.get(), FALSE); | 88 gtk_widget_set_app_paintable(widget_.get(), FALSE); |
| 131 gtk_widget_set_double_buffered(widget_.get(), TRUE); | 89 gtk_widget_set_double_buffered(widget_.get(), TRUE); |
| 132 | 90 |
| 133 if (has_expose_signal_handler_) | 91 if (has_expose_signal_handler_) |
| (...skipping 30 matching lines...) Expand all Loading... |
| 164 return button->button_base_.OnExpose(widget, e); | 122 return button->button_base_.OnExpose(widget, e); |
| 165 } | 123 } |
| 166 | 124 |
| 167 // static | 125 // static |
| 168 CustomDrawButton* CustomDrawButton::CloseButton() { | 126 CustomDrawButton* CustomDrawButton::CloseButton() { |
| 169 CustomDrawButton* button = | 127 CustomDrawButton* button = |
| 170 new CustomDrawButton(IDR_CLOSE_BAR, IDR_CLOSE_BAR_P, | 128 new CustomDrawButton(IDR_CLOSE_BAR, IDR_CLOSE_BAR_P, |
| 171 IDR_CLOSE_BAR_H, 0, NULL); | 129 IDR_CLOSE_BAR_H, 0, NULL); |
| 172 return button; | 130 return button; |
| 173 } | 131 } |
| OLD | NEW |