layout/xul/grid/nsGridRowLayout.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 //
michael@0 7 // Eric Vaughan
michael@0 8 // Netscape Communications
michael@0 9 //
michael@0 10 // See documentation in associated header file
michael@0 11 //
michael@0 12
michael@0 13 #include "nsGridRowLayout.h"
michael@0 14 #include "nsBoxLayoutState.h"
michael@0 15 #include "nsIScrollableFrame.h"
michael@0 16 #include "nsBox.h"
michael@0 17 #include "nsStackLayout.h"
michael@0 18 #include "nsGrid.h"
michael@0 19
michael@0 20 nsGridRowLayout::nsGridRowLayout():nsSprocketLayout()
michael@0 21 {
michael@0 22 }
michael@0 23
michael@0 24 void
michael@0 25 nsGridRowLayout::ChildrenInserted(nsIFrame* aBox, nsBoxLayoutState& aState,
michael@0 26 nsIFrame* aPrevBox,
michael@0 27 const nsFrameList::Slice& aNewChildren)
michael@0 28 {
michael@0 29 ChildAddedOrRemoved(aBox, aState);
michael@0 30 }
michael@0 31
michael@0 32 void
michael@0 33 nsGridRowLayout::ChildrenAppended(nsIFrame* aBox, nsBoxLayoutState& aState,
michael@0 34 const nsFrameList::Slice& aNewChildren)
michael@0 35 {
michael@0 36 ChildAddedOrRemoved(aBox, aState);
michael@0 37 }
michael@0 38
michael@0 39 void
michael@0 40 nsGridRowLayout::ChildrenRemoved(nsIFrame* aBox, nsBoxLayoutState& aState, nsIFrame* aChildList)
michael@0 41 {
michael@0 42 ChildAddedOrRemoved(aBox, aState);
michael@0 43 }
michael@0 44
michael@0 45 void
michael@0 46 nsGridRowLayout::ChildrenSet(nsIFrame* aBox, nsBoxLayoutState& aState, nsIFrame* aChildList)
michael@0 47 {
michael@0 48 ChildAddedOrRemoved(aBox, aState);
michael@0 49 }
michael@0 50
michael@0 51 nsIGridPart*
michael@0 52 nsGridRowLayout::GetParentGridPart(nsIFrame* aBox, nsIFrame** aParentBox)
michael@0 53 {
michael@0 54 // go up and find our parent gridRow. Skip and non gridRow
michael@0 55 // parents.
michael@0 56 *aParentBox = nullptr;
michael@0 57
michael@0 58 // walk up through any scrollboxes
michael@0 59 aBox = nsGrid::GetScrollBox(aBox);
michael@0 60
michael@0 61 // get the parent
michael@0 62 if (aBox)
michael@0 63 aBox = aBox->GetParentBox();
michael@0 64
michael@0 65 if (aBox)
michael@0 66 {
michael@0 67 nsIGridPart* parentGridRow = nsGrid::GetPartFromBox(aBox);
michael@0 68 if (parentGridRow && parentGridRow->CanContain(this)) {
michael@0 69 *aParentBox = aBox;
michael@0 70 return parentGridRow;
michael@0 71 }
michael@0 72 }
michael@0 73
michael@0 74 return nullptr;
michael@0 75 }
michael@0 76
michael@0 77
michael@0 78 nsGrid*
michael@0 79 nsGridRowLayout::GetGrid(nsIFrame* aBox, int32_t* aIndex, nsGridRowLayout* aRequestor)
michael@0 80 {
michael@0 81
michael@0 82 if (aRequestor == nullptr)
michael@0 83 {
michael@0 84 nsIFrame* parentBox; // nsIFrame is implemented by nsIFrame and is not refcounted.
michael@0 85 nsIGridPart* parent = GetParentGridPart(aBox, &parentBox);
michael@0 86 if (parent)
michael@0 87 return parent->GetGrid(parentBox, aIndex, this);
michael@0 88 return nullptr;
michael@0 89 }
michael@0 90
michael@0 91 int32_t index = -1;
michael@0 92 nsIFrame* child = aBox->GetChildBox();
michael@0 93 int32_t count = 0;
michael@0 94 while(child)
michael@0 95 {
michael@0 96 // if there is a scrollframe walk inside it to its child
michael@0 97 nsIFrame* childBox = nsGrid::GetScrolledBox(child);
michael@0 98
michael@0 99 nsBoxLayout* layout = childBox->GetLayoutManager();
michael@0 100 nsIGridPart* gridRow = nsGrid::GetPartFromBox(childBox);
michael@0 101 if (gridRow)
michael@0 102 {
michael@0 103 if (layout == aRequestor) {
michael@0 104 index = count;
michael@0 105 break;
michael@0 106 }
michael@0 107 count += gridRow->GetRowCount();
michael@0 108 } else
michael@0 109 count++;
michael@0 110
michael@0 111 child = child->GetNextBox();
michael@0 112 }
michael@0 113
michael@0 114 // if we didn't find ourselves then the tree isn't properly formed yet
michael@0 115 // this could happen during initial construction so lets just
michael@0 116 // fail.
michael@0 117 if (index == -1) {
michael@0 118 *aIndex = -1;
michael@0 119 return nullptr;
michael@0 120 }
michael@0 121
michael@0 122 (*aIndex) += index;
michael@0 123
michael@0 124 nsIFrame* parentBox; // nsIFrame is implemented by nsIFrame and is not refcounted.
michael@0 125 nsIGridPart* parent = GetParentGridPart(aBox, &parentBox);
michael@0 126 if (parent)
michael@0 127 return parent->GetGrid(parentBox, aIndex, this);
michael@0 128
michael@0 129 return nullptr;
michael@0 130 }
michael@0 131
michael@0 132 nsMargin
michael@0 133 nsGridRowLayout::GetTotalMargin(nsIFrame* aBox, bool aIsHorizontal)
michael@0 134 {
michael@0 135 // get our parents margin
michael@0 136 nsMargin margin(0,0,0,0);
michael@0 137 nsIFrame* parent = nullptr;
michael@0 138 nsIGridPart* part = GetParentGridPart(aBox, &parent);
michael@0 139 if (part && parent) {
michael@0 140 // if we are the first or last child walk upward and add margins.
michael@0 141
michael@0 142 // make sure we check for a scrollbox
michael@0 143 aBox = nsGrid::GetScrollBox(aBox);
michael@0 144
michael@0 145 // see if we have a next to see if we are last
michael@0 146 nsIFrame* next = aBox->GetNextBox();
michael@0 147
michael@0 148 // get the parent first child to see if we are first
michael@0 149 nsIFrame* child = parent->GetChildBox();
michael@0 150
michael@0 151 margin = part->GetTotalMargin(parent, aIsHorizontal);
michael@0 152
michael@0 153 // if first or last
michael@0 154 if (child == aBox || next == nullptr) {
michael@0 155
michael@0 156 // if it's not the first child remove the top margin
michael@0 157 // we don't need it.
michael@0 158 if (child != aBox)
michael@0 159 {
michael@0 160 if (aIsHorizontal)
michael@0 161 margin.top = 0;
michael@0 162 else
michael@0 163 margin.left = 0;
michael@0 164 }
michael@0 165
michael@0 166 // if it's not the last child remove the bottom margin
michael@0 167 // we don't need it.
michael@0 168 if (next != nullptr)
michael@0 169 {
michael@0 170 if (aIsHorizontal)
michael@0 171 margin.bottom = 0;
michael@0 172 else
michael@0 173 margin.right = 0;
michael@0 174 }
michael@0 175
michael@0 176 }
michael@0 177 }
michael@0 178
michael@0 179 // add ours to it.
michael@0 180 nsMargin ourMargin;
michael@0 181 aBox->GetMargin(ourMargin);
michael@0 182 margin += ourMargin;
michael@0 183
michael@0 184 return margin;
michael@0 185 }
michael@0 186
michael@0 187 NS_IMPL_ADDREF_INHERITED(nsGridRowLayout, nsBoxLayout)
michael@0 188 NS_IMPL_RELEASE_INHERITED(nsGridRowLayout, nsBoxLayout)
michael@0 189
michael@0 190 NS_INTERFACE_MAP_BEGIN(nsGridRowLayout)
michael@0 191 NS_INTERFACE_MAP_ENTRY(nsIGridPart)
michael@0 192 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIGridPart)
michael@0 193 NS_INTERFACE_MAP_END_INHERITING(nsBoxLayout)

mercurial