Back to index

lightning-sunbird  0.9+nobinonly
nsTreeColFrame.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  *   Dave Hyatt <hyatt@mozilla.org> (Original Author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsCOMPtr.h"
00040 #include "nsTreeColFrame.h"
00041 #include "nsXULAtoms.h"
00042 #include "nsHTMLAtoms.h"
00043 #include "nsIContent.h"
00044 #include "nsStyleContext.h"
00045 #include "nsINameSpaceManager.h" 
00046 #include "nsIDOMNSDocument.h"
00047 #include "nsIDocument.h"
00048 #include "nsTreeBoxObject.h"
00049 #include "nsIBoxObject.h"
00050 #include "nsITreeColumns.h"
00051 #include "nsIDOMElement.h"
00052 #include "nsITreeBoxObject.h"
00053 #include "nsIDOMXULTreeElement.h"
00054 #include "nsINodeInfo.h"
00055 
00056 //
00057 // NS_NewTreeColFrame
00058 //
00059 // Creates a new col frame
00060 //
00061 nsresult
00062 NS_NewTreeColFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame, PRBool aIsRoot, 
00063                    nsIBoxLayout* aLayoutManager)
00064 {
00065   NS_PRECONDITION(aNewFrame, "null OUT ptr");
00066   if (nsnull == aNewFrame) {
00067     return NS_ERROR_NULL_POINTER;
00068   }
00069   nsTreeColFrame* it = new (aPresShell) nsTreeColFrame(aPresShell, aIsRoot, aLayoutManager);
00070   if (!it)
00071     return NS_ERROR_OUT_OF_MEMORY;
00072 
00073   *aNewFrame = it;
00074   return NS_OK;
00075   
00076 } // NS_NewTreeColFrame
00077 
00078 NS_IMETHODIMP_(nsrefcnt) 
00079 nsTreeColFrame::AddRef(void)
00080 {
00081   return NS_OK;
00082 }
00083 
00084 NS_IMETHODIMP_(nsrefcnt)
00085 nsTreeColFrame::Release(void)
00086 {
00087   return NS_OK;
00088 }
00089 
00090 //
00091 // QueryInterface
00092 //
00093 NS_INTERFACE_MAP_BEGIN(nsTreeColFrame)
00094 NS_INTERFACE_MAP_END_INHERITING(nsBoxFrame)
00095 
00096 // Constructor
00097 nsTreeColFrame::nsTreeColFrame(nsIPresShell* aPresShell, PRBool aIsRoot, nsIBoxLayout* aLayoutManager)
00098   : nsBoxFrame(aPresShell, aIsRoot, aLayoutManager) 
00099 {
00100 }
00101 
00102 // Destructor
00103 nsTreeColFrame::~nsTreeColFrame()
00104 {
00105 }
00106 
00107 NS_IMETHODIMP
00108 nsTreeColFrame::Init(nsPresContext*  aPresContext,
00109                      nsIContent*      aContent,
00110                      nsIFrame*        aParent,
00111                      nsStyleContext*  aContext,
00112                      nsIFrame*        aPrevInFlow)
00113 {
00114   nsresult rv = nsBoxFrame::Init(aPresContext, aContent, aParent, aContext, aPrevInFlow);
00115   InvalidateColumns();
00116   return rv;
00117 }
00118 
00119 NS_IMETHODIMP
00120 nsTreeColFrame::Destroy(nsPresContext* aPresContext)
00121 {
00122   InvalidateColumns(PR_FALSE);
00123   return nsBoxFrame::Destroy(aPresContext);
00124 }
00125 
00126 NS_IMETHODIMP
00127 nsTreeColFrame::GetFrameForPoint(const nsPoint& aPoint, 
00128                                      nsFramePaintLayer aWhichLayer,
00129                                      nsIFrame**     aFrame)
00130 {
00131   if (!(mRect.Contains(aPoint) || (mState & NS_FRAME_OUTSIDE_CHILDREN)))
00132     return NS_ERROR_FAILURE;
00133 
00134   // If we are in either the first 2 pixels or the last 2 pixels, we're going to
00135   // do something really strange.  Check for an adjacent splitter.
00136   PRBool left = PR_FALSE;
00137   PRBool right = PR_FALSE;
00138   if (mRect.x + mRect.width - 60 < aPoint.x)
00139     right = PR_TRUE;
00140   else if (mRect.x + 60 > aPoint.x)
00141     left = PR_TRUE;
00142 
00143   if (left || right) {
00144     // We are a header. Look for the correct splitter.
00145     nsFrameList frames(mParent->GetFirstChild(nsnull));
00146     nsIFrame* child;
00147     if (left)
00148       child = frames.GetPrevSiblingFor(this);
00149     else
00150       child = GetNextSibling();
00151 
00152     if (child) {
00153       nsINodeInfo *ni = child->GetContent()->GetNodeInfo();
00154       if (ni && ni->Equals(nsXULAtoms::splitter, kNameSpaceID_XUL)) {
00155         *aFrame = child;
00156         return NS_OK;
00157       }
00158     }
00159   }
00160 
00161   nsresult result = nsBoxFrame::GetFrameForPoint(aPoint, aWhichLayer, aFrame);
00162   if (result == NS_OK) {
00163     nsIContent* content = (*aFrame)->GetContent();
00164     if (content) {
00165       // This allows selective overriding for subcontent.
00166       nsAutoString value;
00167       content->GetAttr(kNameSpaceID_None, nsXULAtoms::allowevents, value);
00168       if (value.EqualsLiteral("true"))
00169         return result;
00170     }
00171   }
00172   if (mRect.Contains(aPoint)) {
00173     if (GetStyleVisibility()->IsVisible()) {
00174       *aFrame = this; // Capture all events.
00175       return NS_OK;
00176     }
00177   }
00178   return NS_ERROR_FAILURE;
00179 }
00180 
00181 NS_IMETHODIMP
00182 nsTreeColFrame::AttributeChanged(nsIContent* aChild,
00183                                  PRInt32 aNameSpaceID,
00184                                  nsIAtom* aAttribute,
00185                                  PRInt32 aModType)
00186 {
00187   nsresult rv = nsBoxFrame::AttributeChanged(aChild, aNameSpaceID,
00188                                              aAttribute, aModType);
00189 
00190   if (aAttribute == nsXULAtoms::ordinal || aAttribute == nsXULAtoms::primary) {
00191     InvalidateColumns();
00192   }
00193 
00194   return rv;
00195 }
00196 
00197 NS_IMETHODIMP
00198 nsTreeColFrame::SetBounds(nsBoxLayoutState& aBoxLayoutState,
00199                           const nsRect& aRect,
00200                           PRBool aRemoveOverflowArea) {
00201   nscoord oldWidth = mRect.width;
00202 
00203   nsresult rv = nsBoxFrame::SetBounds(aBoxLayoutState, aRect,
00204                                       aRemoveOverflowArea);
00205   if (mRect.width != oldWidth) {
00206     nsITreeBoxObject* treeBoxObject = GetTreeBoxObject();
00207     if (treeBoxObject) {
00208       treeBoxObject->Invalidate();
00209     }
00210   }
00211   return rv;
00212 }
00213 
00214 nsITreeBoxObject*
00215 nsTreeColFrame::GetTreeBoxObject()
00216 {
00217   nsITreeBoxObject* result = nsnull;
00218 
00219   nsIContent* parent = mContent->GetParent();
00220   if (parent) {
00221     nsIContent* grandParent = parent->GetParent();
00222     nsCOMPtr<nsIDOMXULElement> treeElement = do_QueryInterface(grandParent);
00223     if (treeElement) {
00224       nsCOMPtr<nsIBoxObject> boxObject;
00225       treeElement->GetBoxObject(getter_AddRefs(boxObject));
00226 
00227       nsCOMPtr<nsITreeBoxObject> treeBoxObject = do_QueryInterface(boxObject);
00228       result = treeBoxObject.get();
00229     }
00230   }
00231   return result;
00232 }
00233 
00234 void
00235 nsTreeColFrame::InvalidateColumns(PRBool aCanWalkFrameTree)
00236 {
00237   nsITreeBoxObject* treeBoxObject = GetTreeBoxObject();
00238   if (treeBoxObject) {
00239     nsCOMPtr<nsITreeColumns> columns;
00240 
00241     if (aCanWalkFrameTree) {
00242       treeBoxObject->GetColumns(getter_AddRefs(columns));
00243     } else {
00244       nsITreeBoxObject* body =
00245         static_cast<nsTreeBoxObject*>(treeBoxObject)->GetCachedTreeBody();
00246       if (body) {
00247         body->GetColumns(getter_AddRefs(columns));
00248       }
00249     }
00250 
00251     if (columns)
00252       columns->InvalidateColumns();
00253   }
00254 }