summaryrefslogtreecommitdiffstats
path: root/ui/views/touchui/touch_editing_menu.cc
blob: 156c5fad4fec9a009ddb3e157cd673d2a88a950b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/touchui/touch_editing_menu.h"

#include "base/utf_string_conversions.h"
#include "grit/ui_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/insets.h"
#include "ui/gfx/text_utils.h"
#include "ui/views/bubble/bubble_border.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/controls/button/custom_button.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/label_button_border.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/widget/widget.h"

namespace {

const int kMenuCommands[] = {IDS_APP_CUT,
                             IDS_APP_COPY,
                             IDS_APP_PASTE};
const int kSpacingBetweenButtons = 0;
const int kButtonSeparatorColor = SkColorSetARGB(13, 0, 0, 0);
const int kMenuButtonHeight = 40;
const int kMenuButtonWidth = 65;
const SkColor kMenuButtonColorNormal = SkColorSetARGB(102, 255, 255, 255);
const SkColor kMenuButtonColorHover = SkColorSetARGB(13, 0, 0, 0);

const char* kEllipsesButtonText = "...";
const int kEllipsesButtonTag = -1;
}  // namespace

namespace views {

class TouchEditingMenuButtonBorder : public LabelButtonBorder {
 public:
  TouchEditingMenuButtonBorder(Button::ButtonStyle style,
                               const gfx::Insets& insets)
      : LabelButtonBorder(style),
        insets_(insets) {
  }

  virtual ~TouchEditingMenuButtonBorder() {
  }

 private:
  // Overridden from LabelButtonBorder
  virtual gfx::Insets GetInsets() const OVERRIDE {
    return insets_;
  }

  gfx::Insets insets_;

  DISALLOW_COPY_AND_ASSIGN(TouchEditingMenuButtonBorder);
};

TouchEditingMenuView::TouchEditingMenuView(
    TouchEditingMenuController* controller,
    gfx::Rect anchor_rect,
    gfx::NativeView context)
    : BubbleDelegateView(NULL, views::BubbleBorder::BOTTOM_CENTER),
      controller_(controller) {
  set_anchor_rect(anchor_rect);
  set_shadow(views::BubbleBorder::SMALL_SHADOW);
  set_parent_window(context);
  set_margins(gfx::Insets());
  set_use_focusless(true);
  set_adjust_if_offscreen(true);

  SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 0, 0,
      kSpacingBetweenButtons));
  CreateButtons();
  views::BubbleDelegateView::CreateBubble(this);
  GetBubbleFrameView()->set_background(NULL);
  GetWidget()->Show();
}

TouchEditingMenuView::~TouchEditingMenuView() {
}

void TouchEditingMenuView::Close() {
  if (GetWidget()) {
    controller_ = NULL;
    GetWidget()->Close();
  }
}

void TouchEditingMenuView::WindowClosing() {
  views::BubbleDelegateView::WindowClosing();
  if (controller_)
    controller_->OnMenuClosed(this);
}

void TouchEditingMenuView::ButtonPressed(Button* sender,
                                         const ui::Event& event) {
  if (controller_) {
    if (sender->tag() != kEllipsesButtonTag)
      controller_->ExecuteCommand(sender->tag(), event.flags());
    else
      controller_->OpenContextMenu();
  }
}

void TouchEditingMenuView::OnPaint(gfx::Canvas* canvas) {
  BubbleDelegateView::OnPaint(canvas);

  // Draw separator bars.
  int x = 0;
  for (int i = 0; i < child_count() - 1; ++i) {
    View* child = child_at(i);
    x += child->width();
    canvas->FillRect(gfx::Rect(x, 0, 1, child->height()),
        kButtonSeparatorColor);
  }
}

void TouchEditingMenuView::CreateButtons() {
  RemoveAllChildViews(true);
  for (size_t i = 0; i < arraysize(kMenuCommands); i++) {
    int command_id = kMenuCommands[i];
    if (controller_ && controller_->IsCommandIdEnabled(command_id)) {
      Button* button = CreateButton(l10n_util::GetStringUTF16(command_id),
          command_id);
      AddChildView(button);
    }
  }

  // Finally, add ellipses button.
  AddChildView(CreateButton(
      UTF8ToUTF16(kEllipsesButtonText), kEllipsesButtonTag));
  Layout();
}

Button* TouchEditingMenuView::CreateButton(const string16& title, int tag) {
  string16 label = gfx::RemoveAcceleratorChar(title, '&', NULL, NULL);
  LabelButton* button = new LabelButton(this, label);
  button->set_focusable(true);
  button->set_request_focus_on_press(false);
  gfx::Font font = ui::ResourceBundle::GetSharedInstance().GetFont(
      ui::ResourceBundle::SmallFont);
  int v_border = (kMenuButtonHeight - font.GetHeight()) / 2;
  int h_border = (kMenuButtonWidth - font.GetStringWidth(label)) / 2;
  button->set_border(new TouchEditingMenuButtonBorder(button->style(),
      gfx::Insets(v_border, h_border, v_border, h_border)));
  button->SetFont(font);
  button->set_tag(tag);
  return button;
}

}  // namespace views