OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/panels/panel_browser_titlebar_gtk.h" | 5 #include "chrome/browser/ui/panels/panel_browser_titlebar_gtk.h" |
6 | 6 |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/ui/browser.h" |
7 #include "chrome/browser/ui/gtk/custom_button.h" | 9 #include "chrome/browser/ui/gtk/custom_button.h" |
| 10 #include "chrome/browser/ui/gtk/gtk_util.h" |
| 11 #include "chrome/browser/ui/gtk/theme_service_gtk.h" |
8 #include "chrome/browser/ui/panels/panel.h" | 12 #include "chrome/browser/ui/panels/panel.h" |
9 #include "chrome/browser/ui/panels/panel_browser_window_gtk.h" | 13 #include "chrome/browser/ui/panels/panel_browser_window_gtk.h" |
10 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
11 #include "grit/theme_resources.h" | 15 #include "grit/theme_resources.h" |
| 16 #include "ui/base/gtk/gtk_compat.h" |
| 17 #include "ui/gfx/skia_utils_gtk.h" |
12 | 18 |
13 namespace { | 19 namespace { |
14 | 20 |
15 // Spacing between buttons of panel's titlebar. | 21 // Spacing between buttons of panel's titlebar. |
16 const int kPanelButtonSpacing = 7; | 22 const int kPanelButtonSpacing = 7; |
17 | 23 |
18 // Spacing around outside of panel's titlebar buttons. | 24 // Spacing around outside of panel's titlebar buttons. |
19 const int kPanelButtonOuterPadding = 7; | 25 const int kPanelButtonOuterPadding = 7; |
20 | 26 |
| 27 // Markup for painting title as bold. |
| 28 const char* const kTitleMarkupPrefix = "<span font_weight='bold'>"; |
| 29 const char* const kTitleMarkupSuffix = "</span>"; |
| 30 |
| 31 // Colors used to draw title. |
| 32 const SkColor kActiveTitleTextDefaultColor = SK_ColorBLACK; |
| 33 const SkColor kInactiveTitleTextDefaultColor = SkColorSetRGB(0x88, 0x88, 0x88); |
| 34 const SkColor kAttentionTitleTextDefaultColor = SK_ColorWHITE; |
| 35 |
| 36 // Alpha value used in drawing inactive titlebar under non-default theme. |
| 37 const U8CPU kInactiveAlphaBlending = 0x80; |
| 38 |
| 39 SkColor BlendSkColorWithAlpha(SkColor fg_color, SkColor bg_color, U8CPU alpha) { |
| 40 if (alpha == 255) |
| 41 return fg_color; |
| 42 double fg_ratio = alpha / 255.0; |
| 43 double bg_ratio = 1.0 - fg_ratio; |
| 44 return SkColorSetRGB( |
| 45 (SkColorGetR(fg_color) * fg_ratio + SkColorGetR(bg_color) * bg_ratio), |
| 46 (SkColorGetG(fg_color) * fg_ratio + SkColorGetG(bg_color) * bg_ratio), |
| 47 (SkColorGetB(fg_color) * fg_ratio + SkColorGetB(bg_color) * bg_ratio)); |
| 48 } |
| 49 |
21 } // namespace | 50 } // namespace |
22 | 51 |
23 PanelBrowserTitlebarGtk::PanelBrowserTitlebarGtk( | 52 PanelBrowserTitlebarGtk::PanelBrowserTitlebarGtk( |
24 PanelBrowserWindowGtk* browser_window, GtkWindow* window) | 53 PanelBrowserWindowGtk* browser_window, GtkWindow* window) |
25 : BrowserTitlebar(browser_window, window), | 54 : BrowserTitlebar(browser_window, window), |
26 browser_window_(browser_window) { | 55 browser_window_(browser_window) { |
27 } | 56 } |
28 | 57 |
29 PanelBrowserTitlebarGtk::~PanelBrowserTitlebarGtk() { | 58 PanelBrowserTitlebarGtk::~PanelBrowserTitlebarGtk() { |
30 } | 59 } |
31 | 60 |
| 61 SkColor PanelBrowserTitlebarGtk::GetTextColor() const { |
| 62 PanelBrowserWindowGtk::PaintState paint_state = |
| 63 browser_window_->GetPaintState(); |
| 64 if (paint_state == PanelBrowserWindowGtk::PAINT_FOR_ATTENTION) |
| 65 return kAttentionTitleTextDefaultColor; |
| 66 |
| 67 if (theme_service()->UsingDefaultTheme()) { |
| 68 return (paint_state == PanelBrowserWindowGtk::PAINT_AS_ACTIVE) ? |
| 69 kActiveTitleTextDefaultColor : kInactiveTitleTextDefaultColor; |
| 70 } else { |
| 71 return (paint_state == PanelBrowserWindowGtk::PAINT_AS_ACTIVE) ? |
| 72 theme_service()->GetColor(ThemeService::COLOR_TAB_TEXT) : |
| 73 BlendSkColorWithAlpha( |
| 74 theme_service()->GetColor(ThemeService::COLOR_BACKGROUND_TAB_TEXT), |
| 75 theme_service()->GetColor(ThemeService::COLOR_TOOLBAR), |
| 76 kInactiveAlphaBlending); |
| 77 } |
| 78 } |
| 79 |
| 80 void PanelBrowserTitlebarGtk::UpdateButtonBackground(CustomDrawButton* button) { |
| 81 // Don't need to update background since we're using transparent background. |
| 82 } |
| 83 |
| 84 void PanelBrowserTitlebarGtk::UpdateTitleAndIcon() { |
| 85 DCHECK(app_mode_title()); |
| 86 |
| 87 std::string title = |
| 88 UTF16ToUTF8(browser_window_->browser()->GetWindowTitleForCurrentTab()); |
| 89 |
| 90 // Add the markup to show the title as bold. |
| 91 gchar* escaped_title = g_markup_escape_text(title.c_str(), -1); |
| 92 gchar* title_with_markup = g_strconcat(kTitleMarkupPrefix, |
| 93 escaped_title, |
| 94 kTitleMarkupSuffix, |
| 95 NULL); |
| 96 gtk_label_set_markup(GTK_LABEL(app_mode_title()), title_with_markup); |
| 97 g_free(escaped_title); |
| 98 g_free(title_with_markup); |
| 99 } |
| 100 |
32 bool PanelBrowserTitlebarGtk::BuildButton(const std::string& button_token, | 101 bool PanelBrowserTitlebarGtk::BuildButton(const std::string& button_token, |
33 bool left_side) { | 102 bool left_side) { |
34 // Panel only shows close and minimize/restore buttons. | 103 // Panel only shows close and minimize/restore buttons. |
35 if (button_token != "close" && button_token != "minimize") | 104 if (button_token != "close" && button_token != "minimize") |
36 return false; | 105 return false; |
37 | 106 |
38 // Create unminimze button in order to show it to expand the minimized panel. | 107 // Create unminimze button in order to show it to expand the minimized panel. |
39 if (button_token == "minimize") | 108 if (button_token == "minimize") |
40 unminimize_button_.reset(CreateTitlebarButton("unminimize", left_side)); | 109 unminimize_button_.reset(CreateTitlebarButton("unminimize", left_side)); |
41 | 110 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 browser_window_->panel()->Close(); | 156 browser_window_->panel()->Close(); |
88 else if (minimize_button() && minimize_button()->widget() == button) | 157 else if (minimize_button() && minimize_button()->widget() == button) |
89 browser_window_->panel()->Minimize(); | 158 browser_window_->panel()->Minimize(); |
90 else if (unminimize_button_.get() && unminimize_button_->widget() == button) | 159 else if (unminimize_button_.get() && unminimize_button_->widget() == button) |
91 browser_window_->panel()->Restore(); | 160 browser_window_->panel()->Restore(); |
92 } | 161 } |
93 | 162 |
94 void PanelBrowserTitlebarGtk::ShowFaviconMenu(GdkEventButton* event) { | 163 void PanelBrowserTitlebarGtk::ShowFaviconMenu(GdkEventButton* event) { |
95 // Favicon menu is not supported in panels. | 164 // Favicon menu is not supported in panels. |
96 } | 165 } |
| 166 |
| 167 void PanelBrowserTitlebarGtk::UpdateTextColor() { |
| 168 DCHECK(app_mode_title()); |
| 169 |
| 170 GdkColor text_color = gfx::SkColorToGdkColor(GetTextColor()); |
| 171 gtk_util::SetLabelColor(app_mode_title(), &text_color); |
| 172 } |
| 173 |
| 174 void PanelBrowserTitlebarGtk::SendEnterNotifyToCloseButtonIfUnderMouse() { |
| 175 if (!close_button()) |
| 176 return; |
| 177 |
| 178 gint x; |
| 179 gint y; |
| 180 GtkAllocation widget_allocation = close_button()->WidgetAllocation(); |
| 181 gtk_widget_get_pointer(GTK_WIDGET(close_button()->widget()), &x, &y); |
| 182 |
| 183 gfx::Rect button_rect(0, 0, widget_allocation.width, |
| 184 widget_allocation.height); |
| 185 if (!button_rect.Contains(x, y)) { |
| 186 // Mouse is not over the close button. |
| 187 return; |
| 188 } |
| 189 |
| 190 // Create and emit an enter-notify-event on close button. |
| 191 GValue return_value; |
| 192 return_value.g_type = G_TYPE_BOOLEAN; |
| 193 g_value_set_boolean(&return_value, false); |
| 194 |
| 195 GdkEvent* event = gdk_event_new(GDK_ENTER_NOTIFY); |
| 196 event->crossing.window = |
| 197 gtk_button_get_event_window(GTK_BUTTON(close_button()->widget())); |
| 198 event->crossing.send_event = FALSE; |
| 199 event->crossing.subwindow = gtk_widget_get_window(close_button()->widget()); |
| 200 event->crossing.time = gtk_util::XTimeNow(); |
| 201 event->crossing.x = x; |
| 202 event->crossing.y = y; |
| 203 event->crossing.x_root = widget_allocation.x; |
| 204 event->crossing.y_root = widget_allocation.y; |
| 205 event->crossing.mode = GDK_CROSSING_NORMAL; |
| 206 event->crossing.detail = GDK_NOTIFY_ANCESTOR; |
| 207 event->crossing.focus = true; |
| 208 event->crossing.state = 0; |
| 209 |
| 210 g_signal_emit_by_name(GTK_OBJECT(close_button()->widget()), |
| 211 "enter-notify-event", event, |
| 212 &return_value); |
| 213 } |
OLD | NEW |