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
3// found in the LICENSE file.
4
5#include "chrome/browser/chromeos/status/status_area_button.h"
6
7#include "content/common/notification_service.h"
8#include "grit/theme_resources.h"
9#include "ui/base/resource/resource_bundle.h"
10#include "ui/gfx/canvas.h"
11#include "ui/gfx/skbitmap_operations.h"
12#include "views/border.h"
13#include "views/view.h"
14
15namespace chromeos {
16
17// Colors for different text styles.
18static const SkColor kWhitePlainTextColor = 0x99ffffff;
19static const SkColor kWhiteHaloedTextColor = 0xb3ffffff;
20static const SkColor kWhiteHaloedHaloColor = 0xb3000000;
21static const SkColor kGrayEmbossedTextColor = 0xff4c4c4c;
22static const SkColor kGrayEmbossedShadowColor = 0x80ffffff;
23
24// Status area font is bigger.
25const int kFontSizeDelta = 1;
26
27////////////////////////////////////////////////////////////////////////////////
28// StatusAreaButton
29
30StatusAreaButton::StatusAreaButton(StatusAreaHost* host,
31                                   views::ViewMenuDelegate* menu_delegate)
32    : MenuButton(NULL, std::wstring(), menu_delegate, false),
33      use_menu_button_paint_(false),
34      active_(true),
35      host_(host) {
36  set_border(NULL);
37  set_use_menu_button_paint(true);
38  gfx::Font font =
39      ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont);
40  font = font.DeriveFont(3, gfx::Font::BOLD);
41  SetFont(font);
42  SetShowMultipleIconStates(false);
43  set_alignment(TextButton::ALIGN_CENTER);
44  set_border(NULL);
45
46  // Use an offset that is top aligned with toolbar.
47  set_menu_offset(0, 4);
48
49  UpdateTextStyle();
50}
51
52void StatusAreaButton::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) {
53  if (state() == BS_PUSHED) {
54    // Apply 10% white when pushed down.
55    canvas->FillRectInt(SkColorSetARGB(0x19, 0xFF, 0xFF, 0xFF),
56        0, 0, width(), height());
57  }
58
59  if (use_menu_button_paint_) {
60    views::MenuButton::PaintButton(canvas, mode);
61  } else {
62    canvas->DrawBitmapInt(icon(), horizontal_padding(), 0);
63    OnPaintFocusBorder(canvas);
64  }
65}
66
67void StatusAreaButton::SetText(const std::wstring& text) {
68  // TextButtons normally remember the max text size, so the button's preferred
69  // size will always be as large as the largest text ever put in it.
70  // We clear that max text size, so we can adjust the size to fit the text.
71  // The order is important.  ClearMaxTextSize sets the size to that of the
72  // current text, so it must be called after SetText.
73  views::MenuButton::SetText(text);
74  ClearMaxTextSize();
75  PreferredSizeChanged();
76}
77
78bool StatusAreaButton::Activate() {
79  if (active_) {
80    return views::MenuButton::Activate();
81  } else {
82    return true;
83  }
84}
85
86gfx::Size StatusAreaButton::GetPreferredSize() {
87  gfx::Insets insets = views::MenuButton::GetInsets();
88  gfx::Size prefsize(icon_width() + insets.width(),
89                     icon_height() + insets.height());
90
91  // Adjusts size when use menu button paint.
92  if (use_menu_button_paint_) {
93    gfx::Size menu_button_size = views::MenuButton::GetPreferredSize();
94    prefsize.SetSize(
95      std::max(prefsize.width(), menu_button_size.width()),
96      std::max(prefsize.height(), menu_button_size.height())
97    );
98
99    // Shift 1-pixel down for odd number of pixels in vertical space.
100    if ((prefsize.height() - menu_button_size.height()) % 2) {
101      insets_.Set(insets.top() + 1, insets.left(),
102          insets.bottom(), insets.right());
103    }
104  }
105
106  // Add padding.
107  prefsize.Enlarge(2 * horizontal_padding(), 0);
108
109  return prefsize;
110}
111
112gfx::Insets StatusAreaButton::GetInsets() const {
113  return insets_;
114}
115
116void StatusAreaButton::OnThemeChanged() {
117  UpdateTextStyle();
118}
119
120void StatusAreaButton::UpdateTextStyle() {
121  ClearEmbellishing();
122  switch (host_->GetTextStyle()) {
123    case StatusAreaHost::kWhitePlain:
124      SetEnabledColor(kWhitePlainTextColor);
125      break;
126    case StatusAreaHost::kWhiteHaloed:
127      SetEnabledColor(kWhiteHaloedTextColor);
128      SetTextHaloColor(kWhiteHaloedHaloColor);
129      break;
130    case StatusAreaHost::kGrayEmbossed:
131      SetEnabledColor(kGrayEmbossedTextColor);
132      SetTextShadowColors(kGrayEmbossedShadowColor, kGrayEmbossedShadowColor);
133      SetTextShadowOffset(0, 1);
134      break;
135  }
136}
137
138}  // namespace chromeos
139