Fix Debug building on Windows
[platform/framework/web/crosswalk-tizen.git] / atom / browser / ui / views / submenu_button.cc
1 // Copyright (c) 2014 GitHub, Inc.
2 // Use of this source code is governed by the MIT license that can be
3 // found in the LICENSE file.
4
5 #include "atom/browser/ui/views/submenu_button.h"
6
7 #include "base/strings/string_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/color_utils.h"
11 #include "ui/gfx/text_utils.h"
12 #include "ui/views/animation/flood_fill_ink_drop_ripple.h"
13 #include "ui/views/animation/ink_drop_impl.h"
14 #include "ui/views/animation/ink_drop_host_view.h"
15 #include "ui/views/controls/button/label_button_border.h"
16
17 namespace atom {
18
19 namespace {
20
21 // Filter out the "&" in menu label.
22 base::string16 FilterAccelerator(const base::string16& label) {
23   base::string16 out;
24   base::RemoveChars(label, base::ASCIIToUTF16("&").c_str(), &out);
25   return out;
26 }
27
28 }  // namespace
29
30 SubmenuButton::SubmenuButton(const base::string16& title,
31                              views::MenuButtonListener* menu_button_listener,
32                              const SkColor& background_color)
33     : views::MenuButton(FilterAccelerator(title),
34                         menu_button_listener, false),
35       accelerator_(0),
36       show_underline_(false),
37       underline_start_(-1),
38       underline_end_(-1),
39       text_width_(0),
40       text_height_(0),
41       underline_color_(SK_ColorBLACK),
42       background_color_(background_color) {
43 #if defined(OS_LINUX)
44   // Dont' use native style border.
45   SetBorder(std::move(CreateDefaultBorder()));
46 #endif
47
48   if (GetUnderlinePosition(title, &accelerator_, &underline_start_,
49                            &underline_end_))
50     gfx::Canvas::SizeStringInt(GetText(), GetFontList(), &text_width_,
51                                &text_height_, 0, 0);
52
53   SetInkDropMode(InkDropMode::ON);
54   set_ink_drop_base_color(
55       color_utils::BlendTowardOppositeLuma(background_color_, 0x61));
56 }
57
58 SubmenuButton::~SubmenuButton() {
59 }
60
61 std::unique_ptr<views::InkDropRipple> SubmenuButton::CreateInkDropRipple()
62     const {
63   std::unique_ptr<views::InkDropRipple> ripple(
64       new views::FloodFillInkDropRipple(
65           size(),
66           GetInkDropCenterBasedOnLastEvent(),
67           GetInkDropBaseColor(),
68           ink_drop_visible_opacity()));
69   return ripple;
70 }
71
72 std::unique_ptr<views::InkDrop> SubmenuButton::CreateInkDrop() {
73   std::unique_ptr<views::InkDropImpl> ink_drop =
74       CustomButton::CreateDefaultInkDropImpl();
75   ink_drop->SetShowHighlightOnHover(false);
76   return std::move(ink_drop);
77 }
78
79 void SubmenuButton::SetAcceleratorVisibility(bool visible) {
80   if (visible == show_underline_)
81     return;
82
83   show_underline_ = visible;
84   SchedulePaint();
85 }
86
87 void SubmenuButton::SetUnderlineColor(SkColor color) {
88   underline_color_ = color;
89 }
90
91 void SubmenuButton::OnPaint(gfx::Canvas* canvas) {
92   views::MenuButton::OnPaint(canvas);
93
94   if (show_underline_ && (underline_start_ != underline_end_)) {
95     int padding = (width() - text_width_) / 2;
96     int underline_height = (height() + text_height_) / 2 - 2;
97     canvas->DrawLine(gfx::Point(underline_start_ + padding, underline_height),
98                      gfx::Point(underline_end_ + padding, underline_height),
99                      underline_color_);
100   }
101 }
102
103 bool SubmenuButton::GetUnderlinePosition(const base::string16& text,
104                                          base::char16* accelerator,
105                                          int* start, int* end) {
106   int pos, span;
107   base::string16 trimmed = gfx::RemoveAcceleratorChar(text, '&', &pos, &span);
108   if (pos > -1 && span != 0) {
109     *accelerator = base::ToUpperASCII(trimmed[pos]);
110     GetCharacterPosition(trimmed, pos, start);
111     GetCharacterPosition(trimmed, pos + span, end);
112     return true;
113   }
114
115   return false;
116 }
117
118 void SubmenuButton::GetCharacterPosition(
119     const base::string16& text, int index, int* pos) {
120   int height;
121   gfx::Canvas::SizeStringInt(text.substr(0, index), GetFontList(), pos, &height,
122                              0, 0);
123 }
124
125 }  // namespace atom