Back to index

lightning-sunbird  0.9+nobinonly
nsGridRowGroupLayout.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 
00046 /*
00047  * The nsGridRowGroupLayout implements the <rows> or <columns> tag in a grid. 
00048  */
00049 
00050 #include "nsGridRowGroupLayout.h"
00051 #include "nsCOMPtr.h"
00052 #include "nsIScrollableFrame.h"
00053 #include "nsBoxLayoutState.h"
00054 #include "nsGridLayout2.h"
00055 #include "nsGridRow.h"
00056 
00057 nsresult
00058 NS_NewGridRowGroupLayout( nsIPresShell* aPresShell, nsIBoxLayout** aNewLayout)
00059 {
00060   *aNewLayout = new nsGridRowGroupLayout(aPresShell);
00061   NS_IF_ADDREF(*aNewLayout);
00062 
00063   return NS_OK;
00064   
00065 } 
00066 
00067 nsGridRowGroupLayout::nsGridRowGroupLayout(nsIPresShell* aPresShell):nsGridRowLayout(aPresShell), mRowCount(0)
00068 {
00069 }
00070 
00071 nsGridRowGroupLayout::~nsGridRowGroupLayout()
00072 {
00073 }
00074 
00075 NS_IMETHODIMP
00076 nsGridRowGroupLayout::ChildAddedOrRemoved(nsIBox* aBox, nsBoxLayoutState& aState)
00077 {
00078   nsGrid* grid = nsnull;
00079   PRInt32 index = 0;
00080   GetGrid(aBox, &grid, &index);
00081   PRInt32 isHorizontal = IsHorizontal(aBox);
00082 
00083   if (grid)
00084     grid->RowAddedOrRemoved(aState, index, isHorizontal);
00085 
00086   return NS_OK;
00087 }
00088 
00089 void
00090 nsGridRowGroupLayout::AddWidth(nsSize& aSize, nscoord aSize2, PRBool aIsHorizontal)
00091 {
00092   nscoord& size = GET_WIDTH(aSize, aIsHorizontal);
00093 
00094   if (size == NS_INTRINSICSIZE || aSize2 == NS_INTRINSICSIZE)
00095     size = NS_INTRINSICSIZE;
00096   else
00097     size += aSize2;
00098 }
00099 
00100 NS_IMETHODIMP
00101 nsGridRowGroupLayout::GetPrefSize(nsIBox* aBox, nsBoxLayoutState& aState, nsSize& aSize)
00102 { 
00103   nsresult rv = nsGridRowLayout::GetPrefSize(aBox, aState, aSize); 
00104 
00105 
00106  /* It is possible that we could have some extra columns. This is when less columns in XUL were 
00107   * defined that needed. And example might be a grid with 3 defined columns but a row with 4 cells in 
00108   * it. We would need an extra column to make the grid work. But because that extra column does not 
00109   * have a box associated with it we must add its size in manually. Remember we could have extra rows
00110   * as well.
00111   */
00112 
00113   nsGrid* grid = nsnull;
00114   PRInt32 index = 0;
00115   GetGrid(aBox, &grid, &index);
00116 
00117   if (grid) 
00118   {
00119     // make sure we add in extra columns sizes as well
00120     PRInt32 isHorizontal = IsHorizontal(aBox);
00121     PRInt32 extraColumns = grid->GetExtraColumnCount(isHorizontal);
00122     PRInt32 start = grid->GetColumnCount(isHorizontal) - grid->GetExtraColumnCount(isHorizontal);
00123     for (PRInt32 i=0; i < extraColumns; i++)
00124     {
00125       nscoord size = 0;
00126       grid->GetPrefRowHeight(aState, i+start, size, !isHorizontal); // GetPrefColumnWidth
00127 
00128       AddWidth(aSize, size, isHorizontal);
00129     }
00130   }
00131 
00132   return rv;
00133 }
00134 
00135 NS_IMETHODIMP
00136 nsGridRowGroupLayout::GetMaxSize(nsIBox* aBox, nsBoxLayoutState& aState, nsSize& aSize)
00137 {
00138  nsresult rv = nsGridRowLayout::GetMaxSize(aBox, aState, aSize); 
00139 
00140   nsGrid* grid = nsnull;
00141   PRInt32 index = 0;
00142   GetGrid(aBox, &grid, &index);
00143 
00144   if (grid) 
00145   {
00146     // make sure we add in extra columns sizes as well
00147     PRInt32 isHorizontal = IsHorizontal(aBox);
00148     PRInt32 extraColumns = grid->GetExtraColumnCount(isHorizontal);
00149     PRInt32 start = grid->GetColumnCount(isHorizontal) - grid->GetExtraColumnCount(isHorizontal);
00150     for (PRInt32 i=0; i < extraColumns; i++)
00151     {
00152       nscoord size = 0;
00153       grid->GetMaxRowHeight(aState, i+start, size, !isHorizontal); // GetMaxColumnWidth
00154 
00155       AddWidth(aSize, size, isHorizontal);
00156     }
00157   }
00158 
00159 
00160   return rv;
00161 }
00162 
00163 NS_IMETHODIMP
00164 nsGridRowGroupLayout::GetMinSize(nsIBox* aBox, nsBoxLayoutState& aState, nsSize& aSize)
00165 {
00166  nsresult rv = nsGridRowLayout::GetMinSize(aBox, aState, aSize); 
00167 
00168   nsGrid* grid = nsnull;
00169   PRInt32 index = 0;
00170   GetGrid(aBox, &grid, &index);
00171 
00172   if (grid) 
00173   {
00174     // make sure we add in extra columns sizes as well
00175     PRInt32 isHorizontal = IsHorizontal(aBox);
00176     PRInt32 extraColumns = grid->GetExtraColumnCount(isHorizontal);
00177     PRInt32 start = grid->GetColumnCount(isHorizontal) - grid->GetExtraColumnCount(isHorizontal);
00178     for (PRInt32 i=0; i < extraColumns; i++)
00179     {
00180       nscoord size = 0;
00181       grid->GetMinRowHeight(aState, i+start, size, !isHorizontal); // GetMinColumnWidth
00182 
00183       AddWidth(aSize, size, isHorizontal);
00184     }
00185   }
00186 
00187   return rv;
00188 }
00189 
00190 /*
00191  * Run down through our children dirtying them recursively.
00192  */
00193 NS_IMETHODIMP
00194 nsGridRowGroupLayout::DirtyRows(nsIBox* aBox, nsBoxLayoutState& aState)
00195 {
00196   if (aBox) {
00197     // mark us dirty
00198     aBox->MarkDirty(aState);
00199     nsIBox* child = nsnull;
00200     aBox->GetChildBox(&child); 
00201 
00202     while(child) {
00203 
00204       // walk into scrollframes
00205       nsIBox* deepChild = nsGrid::GetScrolledBox(child);
00206 
00207       // walk into other monuments
00208       nsCOMPtr<nsIBoxLayout> layout;
00209       // deepChild might be null if child is a scrollframe around a non-box.
00210       // But in that case there's nothing to do here, really.
00211       if (deepChild) {
00212         deepChild->GetLayoutManager(getter_AddRefs(layout));
00213       }
00214       if (layout) {
00215         nsCOMPtr<nsIGridPart> monument( do_QueryInterface(layout) );
00216         if (monument) 
00217           monument->DirtyRows(deepChild, aState);
00218       }
00219 
00220       child->GetNextBox(&child);
00221     }
00222   }
00223 
00224   return NS_OK;
00225 }
00226 
00227 
00228 NS_IMETHODIMP
00229 nsGridRowGroupLayout::CountRowsColumns(nsIBox* aBox, PRInt32& aRowCount, PRInt32& aComputedColumnCount)
00230 {
00231   if (aBox) {
00232     PRInt32 startCount = aRowCount;
00233 
00234     nsIBox* child = nsnull;
00235     aBox->GetChildBox(&child); 
00236 
00237     while(child) {
00238       
00239       // first see if it is a scrollframe. If so walk down into it and get the scrolled child
00240       nsIBox* deepChild = nsGrid::GetScrolledBox(child);
00241 
00242       nsCOMPtr<nsIBoxLayout> layout;
00243       // deepChild might be null if child is a scrollframe around a non-box.
00244       // But in that case I guess we can count this as a single grid row.  Or
00245       // something.
00246       if (deepChild) {
00247         deepChild->GetLayoutManager(getter_AddRefs(layout));
00248       }
00249       if (layout) {
00250         nsCOMPtr<nsIGridPart> monument( do_QueryInterface(layout) );
00251         if (monument) {
00252           monument->CountRowsColumns(deepChild, aRowCount, aComputedColumnCount);
00253           child->GetNextBox(&child);
00254           deepChild = child;
00255           continue;
00256         }
00257       }
00258 
00259       child->GetNextBox(&child);
00260 
00261       // if not a monument. Then count it. It will be a bogus row
00262       aRowCount++;
00263     }
00264 
00265     mRowCount = aRowCount - startCount;
00266   }
00267 
00268   return NS_OK;
00269 }
00270 
00271 NS_IMETHODIMP
00272 nsGridRowGroupLayout::GetRowCount(PRInt32& aRowCount)
00273 {
00274   aRowCount = mRowCount;
00275   return NS_OK;
00276 }
00277 
00278 
00282 NS_IMETHODIMP
00283 nsGridRowGroupLayout::BuildRows(nsIBox* aBox, nsGridRow* aRows, PRInt32* aCount)
00284 { 
00285   PRInt32 rowCount = 0;
00286 
00287   if (aBox) {
00288     nsIBox* child = nsnull;
00289     aBox->GetChildBox(&child); 
00290 
00291     while(child) {
00292       
00293       // first see if it is a scrollframe. If so walk down into it and get the scrolled child
00294       nsIBox* deepChild = nsGrid::GetScrolledBox(child);
00295 
00296       nsCOMPtr<nsIBoxLayout> layout;
00297       // deepChild might be null if child is a scrollframe around a non-box.
00298       // But in that case there's nothing special that needs doing there.
00299       if (deepChild) {
00300         deepChild->GetLayoutManager(getter_AddRefs(layout));
00301       }
00302       if (layout) {
00303         nsCOMPtr<nsIGridPart> monument( do_QueryInterface(layout) );
00304         if (monument) {
00305           PRInt32 count = 0;
00306           monument->BuildRows(deepChild, &aRows[rowCount], &count);
00307           rowCount += count;
00308           child->GetNextBox(&child);
00309           deepChild = child;
00310           continue;
00311         }
00312       }
00313 
00314       aRows[rowCount].Init(child, PR_TRUE);
00315 
00316       child->GetNextBox(&child);
00317 
00318       // if not a monument. Then count it. It will be a bogus row
00319       rowCount++;
00320     }
00321   }
00322 
00323   *aCount = rowCount;
00324 
00325   return NS_OK;
00326 }
00327 
00328 NS_IMETHODIMP
00329 nsGridRowGroupLayout::CastToRowGroupLayout(nsGridRowGroupLayout** aRowGroup)
00330 {
00331   (*aRowGroup) = this;
00332   return NS_OK;
00333 }
00334 
00335 NS_IMETHODIMP
00336 nsGridRowGroupLayout::GetTotalMargin(nsIBox* aBox, nsMargin& aMargin, PRBool aIsHorizontal)
00337 {
00338   // group have border and padding added to the total margin
00339 
00340   nsresult rv = nsGridRowLayout::GetTotalMargin(aBox, aMargin, aIsHorizontal);
00341   
00342   // make sure we have the scrollframe on the outside if it has one.
00343   // thats where the border it.
00344   aBox = nsGrid::GetScrollBox(aBox);
00345 
00346   // add our border/padding to it
00347   nsMargin borderPadding(0,0,0,0);
00348   aBox->GetBorderAndPadding(borderPadding);
00349 
00350   aMargin += borderPadding;
00351   aBox->GetInset(borderPadding);
00352 
00353   aMargin += borderPadding;
00354 
00355   return rv;
00356 }
00357 
00358