From 4a8da96088a5aa35d7377c97746f6151ff0e6457 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Wed, 11 Sep 2013 04:46:36 +0200 Subject: [PATCH] BLayoutUtils: Add GetLayoutTreeDump() Returns a debug output string listing the basic layout properties of the view/item hierarchy. --- headers/os/interface/LayoutUtils.h | 18 ++++- src/kits/interface/LayoutUtils.cpp | 115 ++++++++++++++++++++++++++++- 2 files changed, 131 insertions(+), 2 deletions(-) diff --git a/headers/os/interface/LayoutUtils.h b/headers/os/interface/LayoutUtils.h index cac5c2e46b..24e91c0337 100644 --- a/headers/os/interface/LayoutUtils.h +++ b/headers/os/interface/LayoutUtils.h @@ -1,16 +1,21 @@ /* - * Copyright 2006, Haiku, Inc. All rights reserved. + * Copyright 2006-2013, Haiku, Inc. All rights reserved. * Distributed under the terms of the MIT License. */ #ifndef _LAYOUT_UTILS_H #define _LAYOUT_UTILS_H + #include #include #include +#include + +class BLayoutItem; class BView; + class BLayoutUtils { public: // static float AddSizesFloat(float a, float b); @@ -36,6 +41,17 @@ public: BAlignment alignment); static void AlignInFrame(BView* view, BRect frame); static BRect MoveIntoFrame(BRect rect, BSize frameSize); + + // debugging + static BString GetLayoutTreeDump(BView* view); + static BString GetLayoutTreeDump(BLayoutItem* item); + +private: + static void _GetLayoutTreeDump(BView* view, int level, + BString& _output); + static void _GetLayoutTreeDump(BLayoutItem* item, + int level, bool isViewLayout, + BString& _output); }; #endif // _LAYOUT_UTILS_H diff --git a/src/kits/interface/LayoutUtils.cpp b/src/kits/interface/LayoutUtils.cpp index 577da47d82..67e2bccd9f 100644 --- a/src/kits/interface/LayoutUtils.cpp +++ b/src/kits/interface/LayoutUtils.cpp @@ -1,12 +1,17 @@ /* - * Copyright 2006-2010, Ingo Weinhold, ingo_weinhold@gmx.de. + * Copyright 2006-2013, Ingo Weinhold, ingo_weinhold@gmx.de. * Distributed under the terms of the MIT License. */ #include +#include + +#include #include +#include "ViewLayoutItem.h" + // // AddSizesFloat // float @@ -230,3 +235,111 @@ BLayoutUtils::MoveIntoFrame(BRect rect, BSize frameSize) return rect.OffsetToSelf(leftTop); } + + +/*static*/ BString +BLayoutUtils::GetLayoutTreeDump(BView* view) +{ + BString result; + _GetLayoutTreeDump(view, 0, result); + return result; +} + + +/*static*/ BString +BLayoutUtils::GetLayoutTreeDump(BLayoutItem* item) +{ + BString result; + _GetLayoutTreeDump(item, 0, false, result); + return result; +} + + +/*static*/ void +BLayoutUtils::_GetLayoutTreeDump(BView* view, int level, BString& _output) +{ + BString indent; + indent.SetTo(' ', level * 2); + + if (view == NULL) { + _output << indent << "\n"; + return; + } + + BRect frame = view->Frame(); + BSize min = view->MinSize(); + BSize max = view->MinSize(); + BSize preferred = view->PreferredSize(); + _output << BString().SetToFormat( + "%sview %p (%s):\n" + "%s frame: (%f, %f, %f, %f)\n" + "%s min: (%f, %f)\n" + "%s max: (%f, %f)\n" + "%s pref: (%f, %f)\n", + indent.String(), view, typeid(*view).name(), + indent.String(), frame.left, frame.top, frame.right, frame.bottom, + indent.String(), min.width, min.height, + indent.String(), max.width, max.height, + indent.String(), preferred.width, preferred.height); + + if (BLayout* layout = view->GetLayout()) { + _GetLayoutTreeDump(layout, level, true, _output); + return; + } + + int32 count = view->CountChildren(); + for (int32 i = 0; i < count; i++) { + _output << indent << " ---\n"; + _GetLayoutTreeDump(view->ChildAt(i), level + 1, _output); + } +} + + +/*static*/ void +BLayoutUtils::_GetLayoutTreeDump(BLayoutItem* item, int level, + bool isViewLayout, BString& _output) +{ + if (BViewLayoutItem* viewItem = dynamic_cast(item)) { + _GetLayoutTreeDump(viewItem->View(), level, _output); + return; + } + + BString indent; + indent.SetTo(' ', level * 2); + + if (item == NULL) { + _output << indent << "\n"; + return; + } + + BLayout* layout = dynamic_cast(item); + BRect frame = item->Frame(); + BSize min = item->MinSize(); + BSize max = item->MinSize(); + BSize preferred = item->PreferredSize(); + if (isViewLayout) { + _output << indent << BString().SetToFormat(" [layout %p (%s)]\n", + layout, typeid(*layout).name()); + } else { + _output << indent << BString().SetToFormat("item %p (%s):\n", + item, typeid(*item).name()); + } + _output << BString().SetToFormat( + "%s frame: (%f, %f, %f, %f)\n" + "%s min: (%f, %f)\n" + "%s max: (%f, %f)\n" + "%s pref: (%f, %f)\n", + indent.String(), frame.left, frame.top, frame.right, frame.bottom, + indent.String(), min.width, min.height, + indent.String(), max.width, max.height, + indent.String(), preferred.width, preferred.height); + + if (layout == NULL) + return; + + int32 count = layout->CountItems(); + for (int32 i = 0; i < count; i++) { + _output << indent << " ---\n"; + _GetLayoutTreeDump(layout->ItemAt(i), level + 1, false, _output); + } +}