android: disable NLS when building yasm.
[chromium-blink-merge.git] / ash / ime / candidate_view.cc
blob83c930f4b7465b5b68877f1ebb3fca16278e4a02
1 // Copyright 2014 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.
5 #include "ash/ime/candidate_view.h"
7 #include "ash/ime/candidate_window_constants.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "ui/base/ime/candidate_window.h"
10 #include "ui/gfx/color_utils.h"
11 #include "ui/native_theme/native_theme.h"
12 #include "ui/views/background.h"
13 #include "ui/views/border.h"
14 #include "ui/views/controls/label.h"
15 #include "ui/views/widget/widget.h"
17 namespace ash {
18 namespace ime {
20 namespace {
22 // VerticalCandidateLabel is used for rendering candidate text in
23 // the vertical candidate window.
24 class VerticalCandidateLabel : public views::Label {
25 public:
26 VerticalCandidateLabel() {}
28 private:
29 virtual ~VerticalCandidateLabel() {}
31 // Returns the preferred size, but guarantees that the width has at
32 // least kMinCandidateLabelWidth pixels.
33 virtual gfx::Size GetPreferredSize() OVERRIDE {
34 gfx::Size size = Label::GetPreferredSize();
35 size.SetToMax(gfx::Size(kMinCandidateLabelWidth, 0));
36 size.SetToMin(gfx::Size(kMaxCandidateLabelWidth, size.height()));
37 return size;
40 DISALLOW_COPY_AND_ASSIGN(VerticalCandidateLabel);
43 // Creates the shortcut label, and returns it (never returns NULL).
44 // The label text is not set in this function.
45 views::Label* CreateShortcutLabel(
46 ui::CandidateWindow::Orientation orientation,
47 const ui::NativeTheme& theme) {
48 // Create the shortcut label. The label will be owned by
49 // |wrapped_shortcut_label|, hence it's deleted when
50 // |wrapped_shortcut_label| is deleted.
51 views::Label* shortcut_label = new views::Label;
53 if (orientation == ui::CandidateWindow::VERTICAL) {
54 shortcut_label->SetFontList(
55 shortcut_label->font_list().Derive(kFontSizeDelta, gfx::Font::BOLD));
56 } else {
57 shortcut_label->SetFontList(
58 shortcut_label->font_list().DeriveWithSizeDelta(kFontSizeDelta));
60 // TODO(satorux): Maybe we need to use language specific fonts for
61 // candidate_label, like Chinese font for Chinese input method?
62 shortcut_label->SetEnabledColor(theme.GetSystemColor(
63 ui::NativeTheme::kColorId_LabelEnabledColor));
64 shortcut_label->SetDisabledColor(theme.GetSystemColor(
65 ui::NativeTheme::kColorId_LabelDisabledColor));
67 // Setup paddings.
68 const gfx::Insets kVerticalShortcutLabelInsets(1, 6, 1, 6);
69 const gfx::Insets kHorizontalShortcutLabelInsets(1, 3, 1, 0);
70 const gfx::Insets insets =
71 (orientation == ui::CandidateWindow::VERTICAL ?
72 kVerticalShortcutLabelInsets :
73 kHorizontalShortcutLabelInsets);
74 shortcut_label->SetBorder(views::Border::CreateEmptyBorder(
75 insets.top(), insets.left(), insets.bottom(), insets.right()));
77 // Add decoration based on the orientation.
78 if (orientation == ui::CandidateWindow::VERTICAL) {
79 // Set the background color.
80 SkColor blackish = color_utils::AlphaBlend(
81 SK_ColorBLACK,
82 theme.GetSystemColor(ui::NativeTheme::kColorId_WindowBackground),
83 0x40);
84 SkColor transparent_blakish = color_utils::AlphaBlend(
85 SK_ColorTRANSPARENT, blackish, 0xE0);
86 shortcut_label->set_background(
87 views::Background::CreateSolidBackground(transparent_blakish));
90 return shortcut_label;
93 // Creates the candidate label, and returns it (never returns NULL).
94 // The label text is not set in this function.
95 views::Label* CreateCandidateLabel(
96 ui::CandidateWindow::Orientation orientation) {
97 views::Label* candidate_label = NULL;
99 // Create the candidate label. The label will be added to |this| as a
100 // child view, hence it's deleted when |this| is deleted.
101 if (orientation == ui::CandidateWindow::VERTICAL) {
102 candidate_label = new VerticalCandidateLabel;
103 } else {
104 candidate_label = new views::Label;
107 // Change the font size.
108 candidate_label->SetFontList(
109 candidate_label->font_list().DeriveWithSizeDelta(kFontSizeDelta));
110 candidate_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
112 return candidate_label;
115 // Creates the annotation label, and return it (never returns NULL).
116 // The label text is not set in this function.
117 views::Label* CreateAnnotationLabel(
118 ui::CandidateWindow::Orientation orientation,
119 const ui::NativeTheme& theme) {
120 // Create the annotation label.
121 views::Label* annotation_label = new views::Label;
123 // Change the font size and color.
124 annotation_label->SetFontList(
125 annotation_label->font_list().DeriveWithSizeDelta(kFontSizeDelta));
126 annotation_label->SetEnabledColor(theme.GetSystemColor(
127 ui::NativeTheme::kColorId_LabelDisabledColor));
128 annotation_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
130 return annotation_label;
133 } // namespace
135 CandidateView::CandidateView(
136 views::ButtonListener* listener,
137 ui::CandidateWindow::Orientation orientation)
138 : views::CustomButton(listener),
139 orientation_(orientation),
140 shortcut_label_(NULL),
141 candidate_label_(NULL),
142 annotation_label_(NULL),
143 infolist_icon_(NULL) {
144 SetBorder(views::Border::CreateEmptyBorder(1, 1, 1, 1));
146 const ui::NativeTheme& theme = *GetNativeTheme();
147 shortcut_label_ = CreateShortcutLabel(orientation, theme);
148 candidate_label_ = CreateCandidateLabel(orientation);
149 annotation_label_ = CreateAnnotationLabel(orientation, theme);
151 AddChildView(shortcut_label_);
152 AddChildView(candidate_label_);
153 AddChildView(annotation_label_);
155 if (orientation == ui::CandidateWindow::VERTICAL) {
156 infolist_icon_ = new views::View;
157 infolist_icon_->set_background(
158 views::Background::CreateSolidBackground(theme.GetSystemColor(
159 ui::NativeTheme::kColorId_FocusedBorderColor)));
160 AddChildView(infolist_icon_);
164 void CandidateView::GetPreferredWidths(int* shortcut_width,
165 int* candidate_width) {
166 *shortcut_width = shortcut_label_->GetPreferredSize().width();
167 *candidate_width = candidate_label_->GetPreferredSize().width();
170 void CandidateView::SetWidths(int shortcut_width, int candidate_width) {
171 shortcut_width_ = shortcut_width;
172 shortcut_label_->SetVisible(shortcut_width_ != 0);
173 candidate_width_ = candidate_width;
176 void CandidateView::SetEntry(const ui::CandidateWindow::Entry& entry) {
177 base::string16 label = entry.label;
178 if (!label.empty() && orientation_ != ui::CandidateWindow::VERTICAL)
179 label += base::ASCIIToUTF16(".");
180 shortcut_label_->SetText(label);
181 candidate_label_->SetText(entry.value);
182 annotation_label_->SetText(entry.annotation);
185 void CandidateView::SetInfolistIcon(bool enable) {
186 if (infolist_icon_)
187 infolist_icon_->SetVisible(enable);
188 SchedulePaint();
191 void CandidateView::StateChanged() {
192 shortcut_label_->SetEnabled(state() != STATE_DISABLED);
193 if (state() == STATE_PRESSED) {
194 ui::NativeTheme* theme = GetNativeTheme();
195 set_background(
196 views::Background::CreateSolidBackground(theme->GetSystemColor(
197 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused)));
198 SetBorder(views::Border::CreateSolidBorder(
200 theme->GetSystemColor(ui::NativeTheme::kColorId_FocusedBorderColor)));
202 // Cancel currently focused one.
203 for (int i = 0; i < parent()->child_count(); ++i) {
204 CandidateView* view =
205 static_cast<CandidateView*>((parent()->child_at(i)));
206 if (view != this && view->state() == STATE_PRESSED)
207 view->SetState(STATE_NORMAL);
209 } else {
210 set_background(NULL);
211 SetBorder(views::Border::CreateEmptyBorder(1, 1, 1, 1));
215 bool CandidateView::OnMouseDragged(const ui::MouseEvent& event) {
216 if (!HitTestPoint(event.location())) {
217 // Moves the drag target to the sibling view.
218 gfx::Point location_in_widget(event.location());
219 ConvertPointToWidget(this, &location_in_widget);
220 for (int i = 0; i < parent()->child_count(); ++i) {
221 views::View* sibling = parent()->child_at(i);
222 if (sibling == this)
223 continue;
224 gfx::Point location_in_sibling(location_in_widget);
225 ConvertPointFromWidget(sibling, &location_in_sibling);
226 if (sibling->HitTestPoint(location_in_sibling)) {
227 GetWidget()->GetRootView()->SetMouseHandler(sibling);
228 return sibling->OnMouseDragged(event);
232 return false;
235 return views::CustomButton::OnMouseDragged(event);
238 void CandidateView::Layout() {
239 const int padding_width =
240 orientation_ == ui::CandidateWindow::VERTICAL ? 4 : 6;
241 int x = 0;
242 shortcut_label_->SetBounds(x, 0, shortcut_width_, height());
243 if (shortcut_width_ > 0)
244 x += shortcut_width_ + padding_width;
245 candidate_label_->SetBounds(x, 0, candidate_width_, height());
246 x += candidate_width_ + padding_width;
248 int right = bounds().right();
249 if (infolist_icon_ && infolist_icon_->visible()) {
250 infolist_icon_->SetBounds(
251 right - kInfolistIndicatorIconWidth - kInfolistIndicatorIconPadding,
252 kInfolistIndicatorIconPadding,
253 kInfolistIndicatorIconWidth,
254 height() - kInfolistIndicatorIconPadding * 2);
255 right -= kInfolistIndicatorIconWidth + kInfolistIndicatorIconPadding * 2;
257 annotation_label_->SetBounds(x, 0, right - x, height());
260 gfx::Size CandidateView::GetPreferredSize() {
261 const int padding_width =
262 orientation_ == ui::CandidateWindow::VERTICAL ? 4 : 6;
263 gfx::Size size;
264 if (shortcut_label_->visible()) {
265 size = shortcut_label_->GetPreferredSize();
266 size.SetToMax(gfx::Size(shortcut_width_, 0));
267 size.Enlarge(padding_width, 0);
269 gfx::Size candidate_size = candidate_label_->GetPreferredSize();
270 candidate_size.SetToMax(gfx::Size(candidate_width_, 0));
271 size.Enlarge(candidate_size.width() + padding_width, 0);
272 size.SetToMax(candidate_size);
273 if (annotation_label_->visible()) {
274 gfx::Size annotation_size = annotation_label_->GetPreferredSize();
275 size.Enlarge(annotation_size.width() + padding_width, 0);
276 size.SetToMax(annotation_size);
279 // Reserves the margin for infolist_icon even if it's not visible.
280 size.Enlarge(
281 kInfolistIndicatorIconWidth + kInfolistIndicatorIconPadding * 2, 0);
282 return size;
285 } // namespace ime
286 } // namespace ash