OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "views/examples/bubble_example.h" | 5 #include "views/examples/bubble_example.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "third_party/skia/include/core/SkColor.h" | |
9 #include "views/bubble/bubble_border.h" | |
10 #include "views/bubble/bubble_delegate.h" | 8 #include "views/bubble/bubble_delegate.h" |
11 #include "views/bubble/bubble_view.h" | 9 #include "views/bubble/bubble_view.h" |
12 #include "views/controls/button/checkbox.h" | |
13 #include "views/controls/button/text_button.h" | 10 #include "views/controls/button/text_button.h" |
14 #include "views/controls/label.h" | 11 #include "views/controls/label.h" |
15 #include "views/layout/box_layout.h" | 12 #include "views/layout/box_layout.h" |
16 #include "views/view.h" | 13 #include "views/layout/fill_layout.h" |
17 #include "views/widget/widget.h" | 14 #include "views/widget/widget.h" |
18 | 15 |
19 namespace examples { | 16 namespace examples { |
20 | 17 |
21 struct BubbleConfig { | 18 struct BubbleConfig { |
22 string16 label; | 19 string16 label; |
23 SkColor color; | 20 SkColor color; |
24 gfx::Rect bound; | 21 gfx::Point arrow_point; |
25 views::BubbleBorder::ArrowLocation arrow; | 22 views::BubbleBorder::ArrowLocation arrow; |
26 bool fade_out; | 23 bool fade_out; |
27 }; | 24 }; |
28 | 25 |
29 // Create three types of bubbles, one without arrow, one with | 26 // Create three types of bubbles, one without arrow, one with |
30 // arrow, and the third has a fade animation. | 27 // arrow, and the third has a fade animation. |
31 BubbleConfig kRoundBubbleConfig = { | 28 BubbleConfig kRoundBubbleConfig = { ASCIIToUTF16("RoundBubble"), 0xFFC1B1E1, |
32 ASCIIToUTF16("RoundBubble"), 0xFFC1B1E1, gfx::Rect(50, 350, 100, 50), | 29 gfx::Point(), views::BubbleBorder::NONE, false }; |
33 views::BubbleBorder::NONE, false }; | 30 BubbleConfig kArrowBubbleConfig = { ASCIIToUTF16("ArrowBubble"), SK_ColorGRAY, |
34 BubbleConfig kPointyBubbleConfig = { | 31 gfx::Point(), views::BubbleBorder::TOP_LEFT, false }; |
35 ASCIIToUTF16("PointyBubble"), SK_ColorLTGRAY, gfx::Rect(250, 350, 180, 180), | 32 BubbleConfig kFadeBubbleConfig = { ASCIIToUTF16("FadeBubble"), SK_ColorYELLOW, |
36 views::BubbleBorder::TOP_LEFT, false }; | 33 gfx::Point(), views::BubbleBorder::BOTTOM_RIGHT, true }; |
37 BubbleConfig kFadeBubbleConfig = { | |
38 ASCIIToUTF16("FadeBubble"), SK_ColorYELLOW, gfx::Rect(500, 350, 200, 100), | |
39 views::BubbleBorder::BOTTOM_RIGHT, true }; | |
40 | 34 |
41 class ExampleBubbleDelegateView : public views::BubbleDelegateView { | 35 class ExampleBubbleDelegateView : public views::BubbleDelegateView { |
42 public: | 36 public: |
43 ExampleBubbleDelegateView(const BubbleConfig& config, views::Widget* widget) | 37 ExampleBubbleDelegateView(const BubbleConfig& config) |
44 : BubbleDelegateView(widget), config_(config) {} | 38 : config_(config) {} |
45 | 39 |
46 virtual views::View* GetContentsView() { return this; } | 40 virtual gfx::Point GetArrowPoint() const OVERRIDE { |
47 | 41 return config_.arrow_point; |
48 SkColor GetFrameBackgroundColor() { | |
49 return config_.color; | |
50 } | |
51 gfx::Rect GetBounds() { | |
52 return config_.bound; | |
53 } | 42 } |
54 | 43 |
55 views::BubbleBorder::ArrowLocation GetFrameArrowLocation() { | 44 views::BubbleBorder::ArrowLocation GetArrowLocation() const OVERRIDE { |
56 return config_.arrow; | 45 return config_.arrow; |
57 } | 46 } |
58 | 47 |
| 48 SkColor GetColor() const OVERRIDE { |
| 49 return config_.color; |
| 50 } |
| 51 |
| 52 virtual void Init() OVERRIDE { |
| 53 SetLayoutManager(new views::FillLayout()); |
| 54 views::Label* label = new views::Label(config_.label); |
| 55 label->set_border(views::Border::CreateSolidBorder(10, config_.color)); |
| 56 AddChildView(label); |
| 57 } |
| 58 |
59 private: | 59 private: |
60 const BubbleConfig& config_; | 60 const BubbleConfig& config_; |
61 }; | 61 }; |
62 | 62 |
63 BubbleExample::BubbleExample(ExamplesMain* main) | 63 BubbleExample::BubbleExample(ExamplesMain* main) |
64 : ExampleBase(main, "Bubble") { | 64 : ExampleBase(main, "Bubble") {} |
65 } | |
66 | 65 |
67 BubbleExample::~BubbleExample() { | 66 BubbleExample::~BubbleExample() {} |
68 } | |
69 | 67 |
70 void BubbleExample::CreateExampleView(views::View* container) { | 68 void BubbleExample::CreateExampleView(views::View* container) { |
71 layout_ = new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 1); | 69 container->SetLayoutManager( |
72 container->SetLayoutManager(layout_); | 70 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 1)); |
73 round_ = new views::TextButton(this, | 71 round_ = new views::TextButton(this, |
74 UTF16ToWideHack(kRoundBubbleConfig.label)); | 72 UTF16ToWideHack(kRoundBubbleConfig.label)); |
75 pointy_ = new views::TextButton(this, | 73 arrow_ = new views::TextButton(this, |
76 UTF16ToWideHack(kPointyBubbleConfig.label)); | 74 UTF16ToWideHack(kArrowBubbleConfig.label)); |
77 fade_ = new views::TextButton(this, | 75 fade_ = new views::TextButton(this, |
78 UTF16ToWideHack(kFadeBubbleConfig.label)); | 76 UTF16ToWideHack(kFadeBubbleConfig.label)); |
79 container->AddChildView(round_); | 77 container->AddChildView(round_); |
80 container->AddChildView(pointy_); | 78 container->AddChildView(arrow_); |
81 container->AddChildView(fade_); | 79 container->AddChildView(fade_); |
82 } | 80 } |
83 | 81 |
84 void BubbleExample::ButtonPressed(views::Button* sender, | 82 void BubbleExample::ButtonPressed(views::Button* sender, |
85 const views::Event& event) { | 83 const views::Event& event) { |
86 if (sender == round_) { | 84 BubbleConfig config; |
87 round_bubble_ = AddBubbleButton(kRoundBubbleConfig); | 85 if (sender == round_) |
88 round_bubble_->Show(); | 86 config = kRoundBubbleConfig; |
89 } else if (sender == pointy_) { | 87 else if (sender == arrow_) |
90 pointy_bubble_ = AddBubbleButton(kPointyBubbleConfig); | 88 config = kArrowBubbleConfig; |
91 pointy_bubble_->Show(); | 89 else if (sender == fade_) |
92 } else if (sender == fade_) { | 90 config = kFadeBubbleConfig; |
93 fade_bubble_ = AddBubbleButton(kFadeBubbleConfig); | |
94 fade_bubble_->Show(); | |
95 // Start fade animation. | |
96 fade_bubble_->client_view()->AsBubbleView()->set_animation_delegate(this); | |
97 fade_bubble_->client_view()->AsBubbleView()->StartFade(); | |
98 } | |
99 example_view()->SchedulePaint(); | |
100 } | |
101 | 91 |
102 views::Widget* BubbleExample::AddBubbleButton(const BubbleConfig& config) { | 92 config.arrow_point.set_x(sender->width() / 2); |
103 // Create a Label. | 93 config.arrow_point.set_y(sender->height() / 2); |
104 views::Label* bubble_message = new views::Label( | 94 views::View::ConvertPointToScreen(sender, &config.arrow_point); |
105 ASCIIToUTF16("I am a ") + config.label); | |
106 bubble_message->SetMultiLine(true); | |
107 bubble_message->SetAllowCharacterBreak(true); | |
108 bubble_message->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | |
109 | 95 |
110 views::Widget* bubble_widget = new views::Widget(); | 96 views::Widget* bubble = views::BubbleDelegateView::ConstructBubble( |
111 views::Widget::InitParams params(views::Widget::InitParams::TYPE_BUBBLE); | 97 new ExampleBubbleDelegateView(config), example_view()->GetWidget()); |
112 params.delegate = new ExampleBubbleDelegateView(config, bubble_widget); | 98 bubble->Show(); |
113 params.transparent = true; | 99 if (config.fade_out) |
114 params.bounds = config.bound; | 100 bubble->client_view()->AsBubbleView()->StartFade(); |
115 params.parent = example_view()->GetWidget()->GetNativeView(); | |
116 bubble_widget->Init(params); | |
117 bubble_widget->client_view()->AsBubbleView()->AddChildView(bubble_message); | |
118 return bubble_widget; | |
119 } | |
120 | |
121 void BubbleExample::AnimationEnded(const ui::Animation* animation) { | |
122 PrintStatus("Done Bubble Animation"); | |
123 } | 101 } |
124 | 102 |
125 } // namespace examples | 103 } // namespace examples |
OLD | NEW |