Index: ui/views/examples/anchor_example.cc |
diff --git a/ui/views/examples/anchor_example.cc b/ui/views/examples/anchor_example.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5237c4d82c96c0f29b9d98e0f32f41ede15288c8 |
--- /dev/null |
+++ b/ui/views/examples/anchor_example.cc |
@@ -0,0 +1,188 @@ |
+// Copyright (c) 2012 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/examples/anchor_example.h" |
+ |
+#include "base/macros.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "build/build_config.h" |
+#include "ui/views/background.h" |
+#include "ui/views/border.h" |
+#include "ui/views/controls/button/checkbox.h" |
+#include "ui/views/controls/button/md_text_button.h" |
+#include "ui/views/controls/button/radio_button.h" |
+#include "ui/views/layout/align_layout.h" |
+#include "ui/views/view.h" |
+#include "ui/views/widget/widget.h" |
+ |
+using base::ASCIIToUTF16; |
+ |
+namespace views { |
+namespace examples { |
+ |
+static constexpr Fill Fill_None = |
+ static_cast<Fill>(static_cast<int>(Fill::Content) + 1); |
+ |
+AnchorExample::AnchorExample() : ExampleBase("Anchor") {} |
+ |
+AnchorExample::~AnchorExample() {} |
+ |
+void AnchorExample::CreateExampleView(View* container) { |
+ container->SetLayoutManager(new AlignLayout()); |
+ work_panel_ = new View(); |
+ work_panel_->SetLayoutManager(new AlignLayout()); |
+ work_panel_->SetBounds( |
+ 0, 0, container->parent()->GetContentsBounds().width() > 0 |
+ ? container->parent()->GetContentsBounds().width() - 175 |
+ : 600, |
+ 10); |
+ work_panel_->attributes().Add( |
+ base::WrapUnique(new FillAttribute(Fill::Right))); |
+ work_panel_->attributes().Add(base::WrapUnique(new AnchorAttribute( |
+ {Anchor::Left, Anchor::Top, Anchor::Right, Anchor::Bottom}))); |
+ work_panel_->SetBorder(Border::CreateSolidBorder(1, SK_ColorGRAY)); |
+ container->AddChildView(work_panel_); |
+ for (int i = 0; i < 4; i++) { |
+ Anchor anchor = static_cast<Anchor>(i); |
+ Checkbox* checkbox = new Checkbox(AnchorText(anchor)); |
+ checkbox->set_listener(this); |
+ checkbox->SetFocusForPlatform(); |
+ checkbox->SetGroup(10); |
+ checkbox->set_id(i); |
+ checkbox->SetBounds(10, 20 * i, 150, 20); |
+ checkbox->SetChecked(current_anchors_.Contains(anchor)); |
+ container->AddChildView(checkbox); |
+ } |
+ for (int i = 0; i < 6; i++) { |
+ Fill fill = static_cast<Fill>(i); |
+ RadioButton* radiobutton = new RadioButton(FillText(fill), 5); |
+ radiobutton->set_listener(this); |
+ radiobutton->SetFocusForPlatform(); |
+ radiobutton->set_id(i); |
+ radiobutton->SetBounds(10, 20 * i + 100, 150, 20); |
+ container->AddChildView(radiobutton); |
+ } |
+ LabelButton* reset_button = MdTextButton::Create(this, ASCIIToUTF16("Reset")); |
+ reset_button->SetFocusForPlatform(); |
+ reset_button->SetBounds(20, 240, 80, 25); |
+ container->AddChildView(reset_button); |
+ anchored_view_label_ = new Label(AnchorsText(current_anchors_)); |
+ anchored_view_label_->SetBounds(125, 150, 150, 30); |
+ anchored_view_label_->SetBorder(Border::CreateSolidBorder(1, SK_ColorGRAY)); |
+ anchored_view_label_->attributes().Add( |
+ base::WrapUnique(new AnchorAttribute(current_anchors_))); |
+ work_panel_->AddChildView(anchored_view_label_); |
+} |
+ |
+void AnchorExample::ButtonPressed(Button* sender, const ui::Event& event) { |
+ base::string16 class_name = ASCIIToUTF16(sender->GetClassName()); |
+ if (class_name == ASCIIToUTF16("Checkbox")) { |
+ AnchorAttribute* attribute = nullptr; |
+ Checkbox* checkbox = static_cast<Checkbox*>(sender); |
+ Anchor anchor = static_cast<Anchor>(checkbox->id()); |
+ if (checkbox->checked()) |
+ current_anchors_.Include(anchor); |
+ else |
+ current_anchors_.Exclude(anchor); |
+ if (anchored_view_label_->attributes().Find(attribute)) { |
+ anchored_view_label_->SetText(AnchorsText(current_anchors_)); |
+ attribute->GetContent().SetAnchors(current_anchors_); |
+ work_panel_->Layout(); |
+ } |
+ } else if (class_name == ASCIIToUTF16("RadioButton")) { |
+ FillAttribute* attribute = nullptr; |
+ AnchorAttribute* anchors = nullptr; |
+ RadioButton* radiobutton = static_cast<RadioButton*>(sender); |
+ Fill fill = static_cast<Fill>(radiobutton->id()); |
+ if (fill == Fill_None) { |
+ anchored_view_label_->attributes().Remove( |
+ GetAttributeId<FillAttribute>()); |
+ } else { |
+ if (!anchored_view_label_->attributes().Find(attribute)) |
+ anchored_view_label_->attributes().Add( |
+ base::WrapUnique(new FillAttribute(fill))); |
+ else |
+ attribute->SetFill(fill); |
+ } |
+ if (anchored_view_label_->attributes().Find(anchors)) |
+ ReflectAnchors(sender->parent(), anchors); |
+ work_panel_->Layout(); |
+ } else if (class_name == ASCIIToUTF16("LabelButton")) { |
+ AnchorAttribute* anchors = nullptr; |
+ anchored_view_label_->attributes().Remove(GetAttributeId<FillAttribute>()); |
+ anchored_view_label_->SetBounds(125, 150, 150, 30); |
+ if (anchored_view_label_->attributes().Find(anchors)) |
+ ReflectAnchors(sender->parent(), anchors); |
+ ClearAlignButtons(sender->parent()); |
+ } |
+} |
+ |
+base::string16 AnchorExample::FillText(Fill fill) { |
+ switch (fill) { |
+ case Fill::Left: |
+ return ASCIIToUTF16("Fill::Left"); |
+ case Fill::Top: |
+ return ASCIIToUTF16("Fill::Top"); |
+ case Fill::Right: |
+ return ASCIIToUTF16("Fill::Right"); |
+ case Fill::Bottom: |
+ return ASCIIToUTF16("Fill::Bottom"); |
+ case Fill::Content: |
+ return ASCIIToUTF16("Fill::Content"); |
+ } |
+ return ASCIIToUTF16("Fill_None"); |
+} |
+ |
+base::string16 AnchorExample::AnchorText(Anchor anchor) { |
+ switch (anchor) { |
+ case Anchor::Left: |
+ return ASCIIToUTF16("Anchor::Left"); |
+ case Anchor::Top: |
+ return ASCIIToUTF16("Anchor::Top"); |
+ case Anchor::Right: |
+ return ASCIIToUTF16("Anchor::Right"); |
+ case Anchor::Bottom: |
+ return ASCIIToUTF16("Anchor::Bottom"); |
+ } |
+ return ASCIIToUTF16("Invalid"); |
+} |
+ |
+base::string16 AnchorExample::AnchorsText(Anchors anchors) { |
+ base::string16 result; |
+ for (int i = 0; i < 4; i++) { |
+ Anchor anchor = static_cast<Anchor>(i); |
+ if (anchors.Contains(anchor)) { |
+ if (!result.empty()) |
+ result.append(ASCIIToUTF16(", ")); |
+ result.append(AnchorText(anchor)); |
+ } |
+ } |
+ return result; |
+} |
+ |
+void AnchorExample::ClearAlignButtons(View* view) { |
+ for (int i = 0; i < view->child_count(); i++) { |
+ View* child = view->child_at(i); |
+ base::string16 classname = ASCIIToUTF16(child->GetClassName()); |
+ if (classname == ASCIIToUTF16("RadioButton")) { |
+ RadioButton* radiobutton = static_cast<RadioButton*>(child); |
+ radiobutton->SetChecked(false); |
+ } |
+ } |
+} |
+ |
+void AnchorExample::ReflectAnchors(View* view, AnchorAttribute* attribute) { |
+ for (int i = 0; i < view->child_count(); i++) { |
+ View* child = view->child_at(i); |
+ base::string16 classname = ASCIIToUTF16(child->GetClassName()); |
+ if (classname == ASCIIToUTF16("Checkbox")) { |
+ Checkbox* checkbox = static_cast<Checkbox*>(child); |
+ Anchor anchor = static_cast<Anchor>(checkbox->id()); |
+ checkbox->SetChecked(attribute->GetContent().anchors().Contains(anchor)); |
+ } |
+ } |
+} |
+ |
+} // namespace examples |
+} // namespace views |