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 using base::ASCIIToUTF16; 15 16 namespace views { 17 namespace examples { 18 19 namespace { 20 21 SkColor colors[] = { SK_ColorWHITE, SK_ColorGRAY, SK_ColorCYAN, 0xFFC1B1E1 }; 22 23 BubbleBorder::Arrow arrows[] = { 24 BubbleBorder::TOP_LEFT, BubbleBorder::TOP_CENTER, 25 BubbleBorder::TOP_RIGHT, BubbleBorder::RIGHT_TOP, 26 BubbleBorder::RIGHT_CENTER, BubbleBorder::RIGHT_BOTTOM, 27 BubbleBorder::BOTTOM_RIGHT, BubbleBorder::BOTTOM_CENTER, 28 BubbleBorder::BOTTOM_LEFT, BubbleBorder::LEFT_BOTTOM, 29 BubbleBorder::LEFT_CENTER, BubbleBorder::LEFT_TOP }; 30 31 base::string16 GetArrowName(BubbleBorder::Arrow arrow) { 32 switch (arrow) { 33 case BubbleBorder::TOP_LEFT: return ASCIIToUTF16("TOP_LEFT"); 34 case BubbleBorder::TOP_RIGHT: return ASCIIToUTF16("TOP_RIGHT"); 35 case BubbleBorder::BOTTOM_LEFT: return ASCIIToUTF16("BOTTOM_LEFT"); 36 case BubbleBorder::BOTTOM_RIGHT: return ASCIIToUTF16("BOTTOM_RIGHT"); 37 case BubbleBorder::LEFT_TOP: return ASCIIToUTF16("LEFT_TOP"); 38 case BubbleBorder::RIGHT_TOP: return ASCIIToUTF16("RIGHT_TOP"); 39 case BubbleBorder::LEFT_BOTTOM: return ASCIIToUTF16("LEFT_BOTTOM"); 40 case BubbleBorder::RIGHT_BOTTOM: return ASCIIToUTF16("RIGHT_BOTTOM"); 41 case BubbleBorder::TOP_CENTER: return ASCIIToUTF16("TOP_CENTER"); 42 case BubbleBorder::BOTTOM_CENTER: return ASCIIToUTF16("BOTTOM_CENTER"); 43 case BubbleBorder::LEFT_CENTER: return ASCIIToUTF16("LEFT_CENTER"); 44 case BubbleBorder::RIGHT_CENTER: return ASCIIToUTF16("RIGHT_CENTER"); 45 case BubbleBorder::NONE: return ASCIIToUTF16("NONE"); 46 case BubbleBorder::FLOAT: return ASCIIToUTF16("FLOAT"); 47 } 48 return ASCIIToUTF16("INVALID"); 49 } 50 51 class ExampleBubble : public BubbleDelegateView { 52 public: 53 ExampleBubble(View* anchor, BubbleBorder::Arrow arrow) 54 : BubbleDelegateView(anchor, arrow) {} 55 56 protected: 57 virtual void Init() OVERRIDE { 58 SetLayoutManager(new BoxLayout(BoxLayout::kVertical, 50, 50, 0)); 59 AddChildView(new Label(GetArrowName(arrow()))); 60 } 61 62 private: 63 DISALLOW_COPY_AND_ASSIGN(ExampleBubble); 64 }; 65 66 } // namespace 67 68 BubbleExample::BubbleExample() : ExampleBase("Bubble") {} 69 70 BubbleExample::~BubbleExample() {} 71 72 void BubbleExample::CreateExampleView(View* container) { 73 PrintStatus("Click with optional modifiers: [Ctrl] for set_arrow(NONE), " 74 "[Alt] for set_arrow(FLOAT), or [Shift] to reverse the arrow iteration."); 75 container->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 10)); 76 no_shadow_ = new LabelButton(this, ASCIIToUTF16("No Shadow")); 77 container->AddChildView(no_shadow_); 78 big_shadow_ = new LabelButton(this, ASCIIToUTF16("Big Shadow")); 79 container->AddChildView(big_shadow_); 80 small_shadow_ = new LabelButton(this, ASCIIToUTF16("Small Shadow")); 81 container->AddChildView(small_shadow_); 82 align_to_edge_ = new LabelButton(this, ASCIIToUTF16("Align To Edge")); 83 container->AddChildView(align_to_edge_); 84 persistent_ = new LabelButton(this, ASCIIToUTF16("Persistent")); 85 container->AddChildView(persistent_); 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 111 BubbleDelegateView::CreateBubble(bubble); 112 if (sender == align_to_edge_) 113 bubble->SetAlignment(BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE); 114 115 bubble->GetWidget()->Show(); 116 } 117 118 } // namespace examples 119 } // namespace views 120