Back to index

lightning-sunbird  0.9+nobinonly
nsGridRowLayout.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 //
00039 // Eric Vaughan
00040 // Netscape Communications
00041 //
00042 // See documentation in associated header file
00043 //
00044 
00045 #include "nsGridRowLayout.h"
00046 #include "nsBoxLayoutState.h"
00047 #include "nsIScrollableFrame.h"
00048 #include "nsBox.h"
00049 #include "nsStackLayout.h"
00050 #include "nsGrid.h"
00051 
00052 nsGridRowLayout::nsGridRowLayout(nsIPresShell* aPresShell):nsSprocketLayout()
00053 {
00054 }
00055 
00056 NS_IMETHODIMP
00057 nsGridRowLayout::ChildrenInserted(nsIBox* aBox, nsBoxLayoutState& aState, nsIBox* aPrevBox, nsIBox* aChildList)
00058 {
00059   return ChildAddedOrRemoved(aBox, aState);
00060 }
00061 
00062 NS_IMETHODIMP
00063 nsGridRowLayout::ChildrenAppended(nsIBox* aBox, nsBoxLayoutState& aState, nsIBox* aChildList)
00064 {
00065   return ChildAddedOrRemoved(aBox, aState);
00066 }
00067 
00068 NS_IMETHODIMP
00069 nsGridRowLayout::ChildrenRemoved(nsIBox* aBox, nsBoxLayoutState& aState, nsIBox* aChildList)
00070 {
00071   return ChildAddedOrRemoved(aBox, aState);
00072 }
00073 
00074 NS_IMETHODIMP
00075 nsGridRowLayout::ChildrenSet(nsIBox* aBox, nsBoxLayoutState& aState, nsIBox* aChildList)
00076 {
00077   return ChildAddedOrRemoved(aBox, aState);
00078 }
00079 
00080 NS_IMETHODIMP
00081 nsGridRowLayout::GetParentGridPart(nsIBox* aBox, nsIBox** aParentBox, nsIGridPart** aParentGridPart)
00082 {
00083   // go up and find our parent gridRow. Skip and non gridRow
00084   // parents.
00085   nsCOMPtr<nsIBoxLayout> layout;
00086   nsCOMPtr<nsIGridPart> parentGridRow;
00087   nsresult rv = NS_OK;
00088   *aParentGridPart = nsnull;
00089   
00090   // walk up through any scrollboxes
00091   aBox = nsGrid::GetScrollBox(aBox);
00092 
00093   // get the parent
00094   if (aBox)
00095       aBox->GetParentBox(&aBox);
00096 
00097   if (aBox)
00098   {
00099       aBox->GetLayoutManager(getter_AddRefs(layout));
00100       parentGridRow = do_QueryInterface(layout);
00101       *aParentGridPart = parentGridRow.get();
00102       *aParentBox = aBox;
00103       NS_IF_ADDREF(*aParentGridPart);
00104       return NS_OK;
00105   }
00106 
00107   *aParentGridPart = nsnull;
00108   *aParentBox = nsnull;
00109   
00110   return rv;
00111 }
00112 
00113 
00114 NS_IMETHODIMP
00115 nsGridRowLayout::GetGrid(nsIBox* aBox, nsGrid** aList, PRInt32* aIndex, nsGridRowLayout* aRequestor)
00116 {
00117 
00118    if (aRequestor == nsnull)
00119    {
00120       nsCOMPtr<nsIGridPart> parent;
00121       nsIBox* parentBox; // nsIBox is implemented by nsIFrame and is not refcounted.
00122       GetParentGridPart(aBox, &parentBox, getter_AddRefs(parent));
00123       if (parent)
00124          return parent->GetGrid(parentBox, aList, aIndex, this);
00125       else
00126          return NS_OK;
00127    }
00128 
00129    nsresult rv = NS_OK;
00130 
00131    PRInt32 index = -1;
00132    nsIBox* child = nsnull;
00133    aBox->GetChildBox(&child);
00134    PRInt32 count = 0;
00135    while(child)
00136    {
00137      // if there is a scrollframe walk inside it to its child
00138      nsIBox* childBox = nsGrid::GetScrolledBox(child);
00139 
00140      nsCOMPtr<nsIBoxLayout> layout;
00141      // childBox might be null if child is a scrollframe around a non-box.  But
00142      // in that case I guess we can count this as a single grid row.  Or
00143      // something.
00144      if (childBox) {
00145        childBox->GetLayoutManager(getter_AddRefs(layout));
00146      }
00147      
00148      // find our requester
00149      nsCOMPtr<nsIGridPart> gridRow = do_QueryInterface(layout, &rv);
00150      if (NS_SUCCEEDED(rv) && gridRow) 
00151      {
00152        if (layout == aRequestor) {
00153           index = count;
00154           break;
00155        }
00156 
00157        PRInt32 c = 0;
00158        gridRow->GetRowCount(c);
00159        count += c;
00160      } else 
00161        count++;
00162 
00163      child->GetNextBox(&child);
00164    }
00165 
00166    // if we didn't find ourselves then the tree isn't properly formed yet
00167    // this could happen during initial construction so lets just
00168    // fail.
00169    if (index == -1) {
00170      *aList = nsnull;
00171      *aIndex = -1;
00172      return NS_OK;
00173    }
00174 
00175    (*aIndex) += index;
00176 
00177    nsCOMPtr<nsIGridPart> parent;
00178    nsIBox* parentBox; // nsIBox is implemented by nsIFrame and is not refcounted.
00179    GetParentGridPart(aBox, &parentBox, getter_AddRefs(parent));
00180 
00181    if (parent)
00182       parent->GetGrid(parentBox, aList, aIndex, this);
00183 
00184    return NS_OK;
00185 }
00186 
00187 NS_IMETHODIMP
00188 nsGridRowLayout::CastToRowGroupLayout(nsGridRowGroupLayout** aRowGroup)
00189 {
00190   (*aRowGroup) = nsnull;
00191   return NS_OK;
00192 }
00193 
00194 NS_IMETHODIMP
00195 nsGridRowLayout::CastToGridLayout(nsGridLayout2** aGridLayout)
00196 {
00197   (*aGridLayout) = nsnull;
00198   return NS_OK;
00199 }
00200 
00201 NS_IMETHODIMP
00202 nsGridRowLayout::GetTotalMargin(nsIBox* aBox, nsMargin& aMargin, PRBool aIsHorizontal)
00203 {
00204   // get our parents margin
00205   nsMargin margin(0,0,0,0);
00206   nsCOMPtr<nsIGridPart> part;
00207   nsIBox* parent = nsnull;
00208   GetParentGridPart(aBox, &parent, getter_AddRefs(part));
00209 
00210   if (part && parent) {
00211     // if we are the first or last child walk upward and add margins.
00212 
00213     nsIBox* next = nsnull;
00214     nsIBox* child = nsnull;
00215 
00216     // make sure we check for a scrollbox
00217     aBox = nsGrid::GetScrollBox(aBox);
00218 
00219     // see if we have a next to see if we are last
00220     aBox->GetNextBox(&next);
00221 
00222     // get the parent first child to see if we are first
00223     parent->GetChildBox(&child);
00224 
00225     part->GetTotalMargin(parent,margin,aIsHorizontal);
00226 
00227     // if first or last
00228     if (child == aBox || next == nsnull) {
00229 
00230        // if its not the first child remove the top margin
00231        // we don't need it.
00232        if (child != aBox)
00233        {
00234           if (aIsHorizontal)
00235               margin.top = 0;
00236           else 
00237               margin.left = 0;
00238        }
00239 
00240        // if its not the last child remove the bottom margin
00241        // we don't need it.
00242        if (next != nsnull)
00243        {
00244           if (aIsHorizontal)
00245               margin.bottom = 0;
00246           else 
00247               margin.right = 0;
00248        }
00249 
00250     }
00251   }
00252     
00253   aMargin = margin;
00254 
00255   // add ours to it.
00256   aBox->GetMargin(margin);
00257   aMargin += margin;
00258 
00259   return NS_OK;
00260 }
00261 
00262 NS_IMPL_ADDREF_INHERITED(nsGridRowLayout, nsBoxLayout)
00263 NS_IMPL_RELEASE_INHERITED(nsGridRowLayout, nsBoxLayout)
00264 
00265 NS_INTERFACE_MAP_BEGIN(nsGridRowLayout)
00266   NS_INTERFACE_MAP_ENTRY(nsIGridPart)
00267   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIGridPart)
00268 NS_INTERFACE_MAP_END_INHERITING(nsBoxLayout)