- add sources.
[platform/framework/web/crosswalk.git] / src / ui / views / examples / bubble_example.cc
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
3 // found in the LICENSE file.
4
5 #include "ui/views/examples/bubble_example.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/views/bubble/bubble_delegate.h"
9 #include "ui/views/controls/button/label_button.h"
10 #include "ui/views/controls/label.h"
11 #include "ui/views/layout/box_layout.h"
12 #include "ui/views/widget/widget.h"
13
14 namespace views {
15 namespace examples {
16
17 namespace {
18
19 SkColor colors[] = { SK_ColorWHITE, SK_ColorGRAY, SK_ColorCYAN, 0xFFC1B1E1 };
20
21 BubbleBorder::Arrow arrows[] = {
22     BubbleBorder::TOP_LEFT, BubbleBorder::TOP_CENTER,
23     BubbleBorder::TOP_RIGHT, BubbleBorder::RIGHT_TOP,
24     BubbleBorder::RIGHT_CENTER, BubbleBorder::RIGHT_BOTTOM,
25     BubbleBorder::BOTTOM_RIGHT, BubbleBorder::BOTTOM_CENTER,
26     BubbleBorder::BOTTOM_LEFT, BubbleBorder::LEFT_BOTTOM,
27     BubbleBorder::LEFT_CENTER, BubbleBorder::LEFT_TOP };
28
29 string16 GetArrowName(BubbleBorder::Arrow arrow) {
30   switch (arrow) {
31     case BubbleBorder::TOP_LEFT:      return ASCIIToUTF16("TOP_LEFT");
32     case BubbleBorder::TOP_RIGHT:     return ASCIIToUTF16("TOP_RIGHT");
33     case BubbleBorder::BOTTOM_LEFT:   return ASCIIToUTF16("BOTTOM_LEFT");
34     case BubbleBorder::BOTTOM_RIGHT:  return ASCIIToUTF16("BOTTOM_RIGHT");
35     case BubbleBorder::LEFT_TOP:      return ASCIIToUTF16("LEFT_TOP");
36     case BubbleBorder::RIGHT_TOP:     return ASCIIToUTF16("RIGHT_TOP");
37     case BubbleBorder::LEFT_BOTTOM:   return ASCIIToUTF16("LEFT_BOTTOM");
38     case BubbleBorder::RIGHT_BOTTOM:  return ASCIIToUTF16("RIGHT_BOTTOM");
39     case BubbleBorder::TOP_CENTER:    return ASCIIToUTF16("TOP_CENTER");
40     case BubbleBorder::BOTTOM_CENTER: return ASCIIToUTF16("BOTTOM_CENTER");
41     case BubbleBorder::LEFT_CENTER:   return ASCIIToUTF16("LEFT_CENTER");
42     case BubbleBorder::RIGHT_CENTER:  return ASCIIToUTF16("RIGHT_CENTER");
43     case BubbleBorder::NONE:          return ASCIIToUTF16("NONE");
44     case BubbleBorder::FLOAT:         return ASCIIToUTF16("FLOAT");
45   }
46   return ASCIIToUTF16("INVALID");
47 }
48
49 class ExampleBubble : public BubbleDelegateView {
50  public:
51    ExampleBubble(View* anchor, BubbleBorder::Arrow arrow)
52        : BubbleDelegateView(anchor, arrow) {}
53
54  protected:
55   virtual void Init() OVERRIDE {
56     SetLayoutManager(new BoxLayout(BoxLayout::kVertical, 50, 50, 0));
57     AddChildView(new Label(GetArrowName(arrow())));
58   }
59
60  private:
61   DISALLOW_COPY_AND_ASSIGN(ExampleBubble);
62 };
63
64 }  // namespace
65
66 BubbleExample::BubbleExample() : ExampleBase("Bubble") {}
67
68 BubbleExample::~BubbleExample() {}
69
70 void BubbleExample::CreateExampleView(View* container) {
71   PrintStatus("Click with optional modifiers: [Ctrl] for set_arrow(NONE), "
72      "[Alt] for set_arrow(FLOAT), or [Shift] to reverse the arrow iteration.");
73   container->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 10));
74   no_shadow_ = new LabelButton(this, ASCIIToUTF16("No Shadow"));
75   container->AddChildView(no_shadow_);
76   big_shadow_ = new LabelButton(this, ASCIIToUTF16("Big Shadow"));
77   container->AddChildView(big_shadow_);
78   small_shadow_ = new LabelButton(this, ASCIIToUTF16("Small Shadow"));
79   container->AddChildView(small_shadow_);
80   align_to_edge_ = new LabelButton(this, ASCIIToUTF16("Align To Edge"));
81   container->AddChildView(align_to_edge_);
82   persistent_ = new LabelButton(this, ASCIIToUTF16("Persistent"));
83   container->AddChildView(persistent_);
84   fade_in_ = new LabelButton(this, ASCIIToUTF16("Fade In"));
85   container->AddChildView(fade_in_);
86 }
87
88 void BubbleExample::ButtonPressed(Button* sender, const ui::Event& event) {
89   static int arrow_index = 0, color_index = 0;
90   static const int count = arraysize(arrows);
91   arrow_index = (arrow_index + count + (event.IsShiftDown() ? -1 : 1)) % count;
92   BubbleBorder::Arrow arrow = arrows[arrow_index];
93   if (event.IsControlDown())
94     arrow = BubbleBorder::NONE;
95   else if (event.IsAltDown())
96     arrow = BubbleBorder::FLOAT;
97
98   ExampleBubble* bubble = new ExampleBubble(sender, arrow);
99   bubble->set_color(colors[(color_index++) % arraysize(colors)]);
100
101   if (sender == no_shadow_)
102     bubble->set_shadow(BubbleBorder::NO_SHADOW);
103   else if (sender == big_shadow_)
104     bubble->set_shadow(BubbleBorder::BIG_SHADOW);
105   else if (sender == small_shadow_)
106     bubble->set_shadow(BubbleBorder::SMALL_SHADOW);
107
108   if (sender == persistent_) {
109     bubble->set_close_on_deactivate(false);
110     bubble->set_move_with_anchor(true);
111   }
112
113   BubbleDelegateView::CreateBubble(bubble);
114   if (sender == align_to_edge_)
115     bubble->SetAlignment(BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE);
116
117   if (sender == fade_in_)
118     bubble->StartFade(true);
119   else
120     bubble->GetWidget()->Show();
121 }
122
123 }  // namespace examples
124 }  // namespace views