Index: views/layout/center_layout.cc |
diff --git a/views/layout/center_layout.cc b/views/layout/center_layout.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..16c5a11d4abfd33e93212ebcd5ae66a685034b9b |
--- /dev/null |
+++ b/views/layout/center_layout.cc |
@@ -0,0 +1,56 @@ |
+// Copyright (c) 2011 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 "views/layout/center_layout.h" |
+ |
+#include "base/logging.h" |
+ |
+namespace views { |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// CenterLayout, public: |
+ |
+CenterLayout::CenterLayout() { |
+} |
+ |
+CenterLayout::~CenterLayout() { |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// CenterLayout, LayoutManager implementation: |
+ |
+void CenterLayout::Layout(View* host) { |
+ if (!host->has_children()) |
+ return; |
+ |
+ DCHECK_EQ(1, host->child_count()); |
+ View* child = host->child_at(0); |
+ gfx::Size size(child->GetPreferredSize()); |
+ gfx::Rect child_area(host->GetLocalBounds()); |
+ child_area.Inset(host->GetInsets()); |
+ size.set_width(std::min(size.width(), child_area.width())); |
+ int child_height = child->GetHeightForWidth(size.width()); |
+ size.set_height(std::min(child_height, child_area.height())); |
+ gfx::Point origin((child_area.width() - size.width()) / 2, |
+ (child_area.height() - size.height()) / 2); |
+ child->SetBoundsRect(gfx::Rect(origin, size)); |
+} |
+ |
+gfx::Size CenterLayout::GetPreferredSize(View* host) { |
+ if (!host->has_children()) |
+ return gfx::Size(); |
+ |
+ DCHECK_EQ(1, host->child_count()); |
+ return host->child_at(0)->GetPreferredSize(); |
+} |
+ |
+int CenterLayout::GetPreferredHeightForWidth(View* host, int width) { |
+ if (!host->has_children()) |
+ LayoutManager::GetPreferredHeightForWidth(host, width); |
+ |
+ DCHECK_EQ(1, host->child_count()); |
+ return host->child_at(0)->GetHeightForWidth(width); |
+} |
+ |
+} // namespace views |