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 anchor_point; |
25 views::BubbleBorder::ArrowLocation arrow; | 22 views::BubbleBorder::ArrowLocation arrow; |
| 23 bool fade_in; |
26 bool fade_out; | 24 bool fade_out; |
27 }; | 25 }; |
28 | 26 |
29 // Create three types of bubbles, one without arrow, one with | 27 // Create four types of bubbles, one without arrow, one with an arrow, one |
30 // arrow, and the third has a fade animation. | 28 // that fades in, and another that fades out and won't close on the escape key. |
31 BubbleConfig kRoundBubbleConfig = { | 29 BubbleConfig kRoundConfig = { ASCIIToUTF16("RoundBubble"), 0xFFC1B1E1, |
32 ASCIIToUTF16("RoundBubble"), 0xFFC1B1E1, gfx::Rect(50, 350, 100, 50), | 30 gfx::Point(), views::BubbleBorder::NONE, false, false }; |
33 views::BubbleBorder::NONE, false }; | 31 BubbleConfig kArrowConfig = { ASCIIToUTF16("ArrowBubble"), SK_ColorGRAY, |
34 BubbleConfig kPointyBubbleConfig = { | 32 gfx::Point(), views::BubbleBorder::TOP_LEFT, false, false }; |
35 ASCIIToUTF16("PointyBubble"), SK_ColorLTGRAY, gfx::Rect(250, 350, 180, 180), | 33 BubbleConfig kFadeInConfig = { ASCIIToUTF16("FadeInBubble"), SK_ColorYELLOW, |
36 views::BubbleBorder::TOP_LEFT, false }; | 34 gfx::Point(), views::BubbleBorder::BOTTOM_RIGHT, true, false }; |
37 BubbleConfig kFadeBubbleConfig = { | 35 BubbleConfig kFadeOutConfig = { ASCIIToUTF16("FadeOutBubble"), SK_ColorWHITE, |
38 ASCIIToUTF16("FadeBubble"), SK_ColorYELLOW, gfx::Rect(500, 350, 200, 100), | 36 gfx::Point(), views::BubbleBorder::BOTTOM_RIGHT, false, true }; |
39 views::BubbleBorder::BOTTOM_RIGHT, true }; | |
40 | 37 |
41 class ExampleBubbleDelegateView : public views::BubbleDelegateView { | 38 class ExampleBubbleDelegateView : public views::BubbleDelegateView { |
42 public: | 39 public: |
43 ExampleBubbleDelegateView(const BubbleConfig& config, views::Widget* widget) | 40 ExampleBubbleDelegateView(const BubbleConfig& config) |
44 : BubbleDelegateView(widget), config_(config) {} | 41 : config_(config) {} |
45 | 42 |
46 virtual views::View* GetContentsView() { return this; } | 43 virtual gfx::Point GetAnchorPoint() const OVERRIDE { |
47 | 44 return config_.anchor_point; |
48 SkColor GetFrameBackgroundColor() { | |
49 return config_.color; | |
50 } | |
51 gfx::Rect GetBounds() { | |
52 return config_.bound; | |
53 } | 45 } |
54 | 46 |
55 views::BubbleBorder::ArrowLocation GetFrameArrowLocation() { | 47 views::BubbleBorder::ArrowLocation GetArrowLocation() const OVERRIDE { |
56 return config_.arrow; | 48 return config_.arrow; |
57 } | 49 } |
58 | 50 |
| 51 SkColor GetColor() const OVERRIDE { |
| 52 return config_.color; |
| 53 } |
| 54 |
| 55 protected: |
| 56 virtual void Init() OVERRIDE { |
| 57 SetLayoutManager(new views::FillLayout()); |
| 58 views::Label* label = new views::Label(config_.label); |
| 59 label->set_border(views::Border::CreateSolidBorder(10, config_.color)); |
| 60 AddChildView(label); |
| 61 } |
| 62 |
59 private: | 63 private: |
60 const BubbleConfig& config_; | 64 const BubbleConfig config_; |
61 }; | 65 }; |
62 | 66 |
63 BubbleExample::BubbleExample(ExamplesMain* main) | 67 BubbleExample::BubbleExample(ExamplesMain* main) |
64 : ExampleBase(main, "Bubble") { | 68 : ExampleBase(main, "Bubble") {} |
65 } | |
66 | 69 |
67 BubbleExample::~BubbleExample() { | 70 BubbleExample::~BubbleExample() {} |
68 } | |
69 | 71 |
70 void BubbleExample::CreateExampleView(views::View* container) { | 72 void BubbleExample::CreateExampleView(views::View* container) { |
71 layout_ = new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 1); | 73 container->SetLayoutManager( |
72 container->SetLayoutManager(layout_); | 74 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 1)); |
73 round_ = new views::TextButton(this, | 75 round_ = new views::TextButton(this, UTF16ToWideHack(kRoundConfig.label)); |
74 UTF16ToWideHack(kRoundBubbleConfig.label)); | 76 arrow_ = new views::TextButton(this, UTF16ToWideHack(kArrowConfig.label)); |
75 pointy_ = new views::TextButton(this, | 77 fade_in_ = new views::TextButton(this, UTF16ToWideHack(kFadeInConfig.label)); |
76 UTF16ToWideHack(kPointyBubbleConfig.label)); | 78 fade_out_ = new views::TextButton(this, |
77 fade_ = new views::TextButton(this, | 79 UTF16ToWideHack(kFadeOutConfig.label)); |
78 UTF16ToWideHack(kFadeBubbleConfig.label)); | |
79 container->AddChildView(round_); | 80 container->AddChildView(round_); |
80 container->AddChildView(pointy_); | 81 container->AddChildView(arrow_); |
81 container->AddChildView(fade_); | 82 container->AddChildView(fade_in_); |
| 83 container->AddChildView(fade_out_); |
82 } | 84 } |
83 | 85 |
84 void BubbleExample::ButtonPressed(views::Button* sender, | 86 void BubbleExample::ButtonPressed(views::Button* sender, |
85 const views::Event& event) { | 87 const views::Event& event) { |
86 if (sender == round_) { | 88 BubbleConfig config; |
87 round_bubble_ = AddBubbleButton(kRoundBubbleConfig); | 89 if (sender == round_) |
88 round_bubble_->Show(); | 90 config = kRoundConfig; |
89 } else if (sender == pointy_) { | 91 else if (sender == arrow_) |
90 pointy_bubble_ = AddBubbleButton(kPointyBubbleConfig); | 92 config = kArrowConfig; |
91 pointy_bubble_->Show(); | 93 else if (sender == fade_in_) |
92 } else if (sender == fade_) { | 94 config = kFadeInConfig; |
93 fade_bubble_ = AddBubbleButton(kFadeBubbleConfig); | 95 else if (sender == fade_out_) |
94 fade_bubble_->Show(); | 96 config = kFadeOutConfig; |
95 // Start fade animation. | 97 |
96 fade_bubble_->client_view()->AsBubbleView()->set_animation_delegate(this); | 98 config.anchor_point.set_x(sender->width() / 2); |
97 fade_bubble_->client_view()->AsBubbleView()->StartFade(); | 99 config.anchor_point.set_y(sender->height() / 2); |
| 100 views::View::ConvertPointToScreen(sender, &config.anchor_point); |
| 101 |
| 102 views::Widget* bubble = views::BubbleDelegateView::CreateBubble( |
| 103 new ExampleBubbleDelegateView(config), example_view()->GetWidget()); |
| 104 |
| 105 if (config.fade_in) |
| 106 bubble->client_view()->AsBubbleView()->StartFade(true); |
| 107 else |
| 108 bubble->Show(); |
| 109 |
| 110 if (config.fade_out) { |
| 111 bubble->client_view()->AsBubbleView()->set_close_on_esc(false); |
| 112 bubble->client_view()->AsBubbleView()->StartFade(false); |
98 } | 113 } |
99 example_view()->SchedulePaint(); | |
100 } | |
101 | |
102 views::Widget* BubbleExample::AddBubbleButton(const BubbleConfig& config) { | |
103 // Create a Label. | |
104 views::Label* bubble_message = new views::Label( | |
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 | |
110 views::Widget* bubble_widget = new views::Widget(); | |
111 views::Widget::InitParams params(views::Widget::InitParams::TYPE_BUBBLE); | |
112 params.delegate = new ExampleBubbleDelegateView(config, bubble_widget); | |
113 params.transparent = true; | |
114 params.bounds = config.bound; | |
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 } | 114 } |
124 | 115 |
125 } // namespace examples | 116 } // namespace examples |
OLD | NEW |