| 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/gtk_chrome_button.h" |
| 12 #include "chrome/browser/gtk/gtk_theme_provider.h" | 13 #include "chrome/browser/gtk/gtk_theme_provider.h" |
| 13 #include "chrome/common/notification_service.h" | 14 #include "chrome/common/notification_service.h" |
| 14 #include "grit/theme_resources.h" | 15 #include "grit/theme_resources.h" |
| 15 | 16 |
| 16 CustomDrawButtonBase::CustomDrawButtonBase(GtkThemeProvider* theme_provider, | 17 CustomDrawButtonBase::CustomDrawButtonBase(GtkThemeProvider* theme_provider, |
| 17 int normal_id, int active_id, int highlight_id, int depressed_id) | 18 int normal_id, int active_id, int highlight_id, int depressed_id) |
| 18 : paint_override_(-1), | 19 : paint_override_(-1), |
| 19 normal_id_(normal_id), | 20 normal_id_(normal_id), |
| 20 active_id_(active_id), | 21 active_id_(active_id), |
| 21 highlight_id_(highlight_id), | 22 highlight_id_(highlight_id), |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 pixbufs_[GTK_STATE_PRELIGHT] = highlight_id_ ? | 88 pixbufs_[GTK_STATE_PRELIGHT] = highlight_id_ ? |
| 88 theme_provider_->GetRTLEnabledPixbufNamed(highlight_id_) : NULL; | 89 theme_provider_->GetRTLEnabledPixbufNamed(highlight_id_) : NULL; |
| 89 pixbufs_[GTK_STATE_SELECTED] = NULL; | 90 pixbufs_[GTK_STATE_SELECTED] = NULL; |
| 90 pixbufs_[GTK_STATE_INSENSITIVE] = depressed_id_ ? | 91 pixbufs_[GTK_STATE_INSENSITIVE] = depressed_id_ ? |
| 91 theme_provider_->GetRTLEnabledPixbufNamed(depressed_id_) : NULL; | 92 theme_provider_->GetRTLEnabledPixbufNamed(depressed_id_) : NULL; |
| 92 } | 93 } |
| 93 | 94 |
| 94 CustomDrawButton::CustomDrawButton(int normal_id, int active_id, | 95 CustomDrawButton::CustomDrawButton(int normal_id, int active_id, |
| 95 int highlight_id, int depressed_id, const char* stock_id) | 96 int highlight_id, int depressed_id, const char* stock_id) |
| 96 : button_base_(NULL, normal_id, active_id, highlight_id, depressed_id), | 97 : button_base_(NULL, normal_id, active_id, highlight_id, depressed_id), |
| 97 gtk_stock_name_(stock_id), | 98 theme_provider_(NULL), |
| 98 has_expose_signal_handler_(false) { | 99 gtk_stock_name_(stock_id) { |
| 99 Init(); | 100 Init(); |
| 100 | 101 |
| 101 // Initialize the theme stuff with no theme_provider. | 102 // Initialize the theme stuff with no theme_provider. |
| 102 SetBrowserTheme(NULL); | 103 SetBrowserTheme(NULL); |
| 103 } | 104 } |
| 104 | 105 |
| 105 CustomDrawButton::CustomDrawButton(GtkThemeProvider* theme_provider, | 106 CustomDrawButton::CustomDrawButton(GtkThemeProvider* theme_provider, |
| 106 int normal_id, int active_id, int highlight_id, int depressed_id, | 107 int normal_id, int active_id, int highlight_id, int depressed_id, |
| 107 const char* stock_id) | 108 const char* stock_id) |
| 108 : button_base_(theme_provider, normal_id, active_id, highlight_id, | 109 : button_base_(theme_provider, normal_id, active_id, highlight_id, |
| 109 depressed_id), | 110 depressed_id), |
| 110 gtk_stock_name_(stock_id), | 111 theme_provider_(theme_provider), |
| 111 has_expose_signal_handler_(false) { | 112 gtk_stock_name_(stock_id) { |
| 112 Init(); | 113 Init(); |
| 113 | 114 |
| 114 theme_provider->InitThemesFor(this); | 115 theme_provider->InitThemesFor(this); |
| 115 registrar_.Add(this, | 116 registrar_.Add(this, |
| 116 NotificationType::BROWSER_THEME_CHANGED, | 117 NotificationType::BROWSER_THEME_CHANGED, |
| 117 NotificationService::AllSources()); | 118 NotificationService::AllSources()); |
| 118 } | 119 } |
| 119 | 120 |
| 120 CustomDrawButton::~CustomDrawButton() { | 121 CustomDrawButton::~CustomDrawButton() { |
| 121 widget_.Destroy(); | 122 widget_.Destroy(); |
| 122 } | 123 } |
| 123 | 124 |
| 124 void CustomDrawButton::Init() { | 125 void CustomDrawButton::Init() { |
| 125 widget_.Own(gtk_button_new()); | 126 widget_.Own(gtk_chrome_button_new()); |
| 126 GTK_WIDGET_UNSET_FLAGS(widget_.get(), GTK_CAN_FOCUS); | 127 GTK_WIDGET_UNSET_FLAGS(widget_.get(), GTK_CAN_FOCUS); |
| 128 g_signal_connect(G_OBJECT(widget_.get()), "expose-event", |
| 129 G_CALLBACK(OnCustomExpose), this); |
| 127 } | 130 } |
| 128 | 131 |
| 129 void CustomDrawButton::Observe(NotificationType type, | 132 void CustomDrawButton::Observe(NotificationType type, |
| 130 const NotificationSource& source, const NotificationDetails& details) { | 133 const NotificationSource& source, const NotificationDetails& details) { |
| 131 DCHECK(NotificationType::BROWSER_THEME_CHANGED == type); | 134 DCHECK(NotificationType::BROWSER_THEME_CHANGED == type); |
| 132 | 135 |
| 133 GtkThemeProvider* provider = static_cast<GtkThemeProvider*>( | 136 GtkThemeProvider* provider = static_cast<GtkThemeProvider*>( |
| 134 Source<GtkThemeProvider>(source).ptr()); | 137 Source<GtkThemeProvider>(source).ptr()); |
| 135 SetBrowserTheme(provider); | 138 SetBrowserTheme(provider); |
| 136 } | 139 } |
| 137 | 140 |
| 138 void CustomDrawButton::SetPaintOverride(GtkStateType state) { | 141 void CustomDrawButton::SetPaintOverride(GtkStateType state) { |
| 139 button_base_.set_paint_override(state); | 142 button_base_.set_paint_override(state); |
| 143 gtk_chrome_button_set_paint_state(GTK_CHROME_BUTTON(widget_.get()), state); |
| 140 gtk_widget_queue_draw(widget_.get()); | 144 gtk_widget_queue_draw(widget_.get()); |
| 141 } | 145 } |
| 142 | 146 |
| 143 void CustomDrawButton::UnsetPaintOverride() { | 147 void CustomDrawButton::UnsetPaintOverride() { |
| 144 button_base_.set_paint_override(-1); | 148 button_base_.set_paint_override(-1); |
| 149 gtk_chrome_button_unset_paint_state(GTK_CHROME_BUTTON(widget_.get())); |
| 145 gtk_widget_queue_draw(widget_.get()); | 150 gtk_widget_queue_draw(widget_.get()); |
| 146 } | 151 } |
| 147 | 152 |
| 148 // static | 153 // static |
| 149 gboolean CustomDrawButton::OnCustomExpose(GtkWidget* widget, | 154 gboolean CustomDrawButton::OnCustomExpose(GtkWidget* widget, |
| 150 GdkEventExpose* e, | 155 GdkEventExpose* e, |
| 151 CustomDrawButton* button) { | 156 CustomDrawButton* button) { |
| 152 return button->button_base_.OnExpose(widget, e); | 157 if (button->theme_provider_ && button->theme_provider_->UseGtkTheme() ) { |
| 158 // Continue processing this expose event. |
| 159 return FALSE; |
| 160 } else { |
| 161 return button->button_base_.OnExpose(widget, e); |
| 162 } |
| 153 } | 163 } |
| 154 | 164 |
| 155 // static | 165 // static |
| 156 CustomDrawButton* CustomDrawButton::CloseButton() { | 166 CustomDrawButton* CustomDrawButton::CloseButton() { |
| 157 CustomDrawButton* button = | 167 CustomDrawButton* button = |
| 158 new CustomDrawButton(IDR_CLOSE_BAR, IDR_CLOSE_BAR_P, | 168 new CustomDrawButton(IDR_CLOSE_BAR, IDR_CLOSE_BAR_P, |
| 159 IDR_CLOSE_BAR_H, 0, NULL); | 169 IDR_CLOSE_BAR_H, 0, NULL); |
| 160 return button; | 170 return button; |
| 161 } | 171 } |
| 162 | 172 |
| 163 void CustomDrawButton::SetBrowserTheme(GtkThemeProvider* theme_provider) { | 173 void CustomDrawButton::SetBrowserTheme(GtkThemeProvider* theme_provider) { |
| 164 if (theme_provider && theme_provider->UseGtkTheme() && gtk_stock_name_) { | 174 bool use_gtk = theme_provider && theme_provider->UseGtkTheme(); |
| 175 |
| 176 if (use_gtk && gtk_stock_name_) { |
| 165 gtk_button_set_image( | 177 gtk_button_set_image( |
| 166 GTK_BUTTON(widget_.get()), | 178 GTK_BUTTON(widget_.get()), |
| 167 gtk_image_new_from_stock(gtk_stock_name_, GTK_ICON_SIZE_BUTTON)); | 179 gtk_image_new_from_stock(gtk_stock_name_, GTK_ICON_SIZE_BUTTON)); |
| 168 gtk_widget_set_size_request(widget_.get(), -1, -1); | 180 gtk_widget_set_size_request(widget_.get(), -1, -1); |
| 169 gtk_widget_set_app_paintable(widget_.get(), FALSE); | 181 gtk_widget_set_app_paintable(widget_.get(), FALSE); |
| 170 gtk_widget_set_double_buffered(widget_.get(), TRUE); | 182 gtk_widget_set_double_buffered(widget_.get(), TRUE); |
| 171 | |
| 172 if (has_expose_signal_handler_) | |
| 173 gtk_signal_disconnect_by_data(GTK_OBJECT(widget_.get()), this); | |
| 174 has_expose_signal_handler_ = false; | |
| 175 } else { | 183 } else { |
| 176 gtk_widget_set_size_request(widget_.get(), | 184 gtk_widget_set_size_request(widget_.get(), |
| 177 gdk_pixbuf_get_width(button_base_.pixbufs(0)), | 185 gdk_pixbuf_get_width(button_base_.pixbufs(0)), |
| 178 gdk_pixbuf_get_height(button_base_.pixbufs(0))); | 186 gdk_pixbuf_get_height(button_base_.pixbufs(0))); |
| 179 | 187 |
| 180 gtk_widget_set_app_paintable(widget_.get(), TRUE); | 188 gtk_widget_set_app_paintable(widget_.get(), TRUE); |
| 181 // We effectively double-buffer by virtue of having only one image... | 189 // We effectively double-buffer by virtue of having only one image... |
| 182 gtk_widget_set_double_buffered(widget_.get(), FALSE); | 190 gtk_widget_set_double_buffered(widget_.get(), FALSE); |
| 183 g_signal_connect(G_OBJECT(widget_.get()), "expose-event", | |
| 184 G_CALLBACK(OnCustomExpose), this); | |
| 185 has_expose_signal_handler_ = true; | |
| 186 } | 191 } |
| 192 |
| 193 gtk_chrome_button_set_use_gtk_rendering( |
| 194 GTK_CHROME_BUTTON(widget_.get()), use_gtk); |
| 187 } | 195 } |
| OLD | NEW |