Back to index

lightning-sunbird  0.9+nobinonly
nsGrippyFrame.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 "nsGrippyFrame.h"
00046 #include "nsScrollbarButtonFrame.h"
00047 #include "nsPresContext.h"
00048 #include "nsIContent.h"
00049 #include "nsCOMPtr.h"
00050 #include "nsUnitConversion.h"
00051 #include "nsINameSpaceManager.h"
00052 #include "nsHTMLAtoms.h"
00053 #include "nsXULAtoms.h"
00054 //#include "nsSliderFrame.h"
00055 #include "nsIDOMElement.h"
00056 #include "nsIDOMDocument.h"
00057 #include "nsIDocument.h"
00058 #include "nsHTMLParts.h"
00059 #include "nsIPresShell.h"
00060 #include "nsIView.h"
00061 #include "nsIViewManager.h"
00062 #include "nsHTMLContainerFrame.h"
00063 
00064 
00065 //
00066 // NS_NewToolbarFrame
00067 //
00068 // Creates a new Toolbar frame and returns it in |aNewFrame|
00069 //
00070 nsresult
00071 NS_NewGrippyFrame ( nsIPresShell* aPresShell, nsIFrame** aNewFrame )
00072 {
00073   NS_PRECONDITION(aNewFrame, "null OUT ptr");
00074   if (nsnull == aNewFrame) {
00075     return NS_ERROR_NULL_POINTER;
00076   }
00077   nsGrippyFrame* it = new (aPresShell) nsGrippyFrame (aPresShell);
00078   if (nsnull == it)
00079     return NS_ERROR_OUT_OF_MEMORY;
00080 
00081   *aNewFrame = it;
00082   return NS_OK;
00083   
00084 } // NS_NewGrippyFrame
00085 
00086 nsGrippyFrame::nsGrippyFrame(nsIPresShell* aShell):nsButtonBoxFrame(aShell),mCollapsed(PR_FALSE)
00087 {
00088 }
00089 
00090 void
00091 nsGrippyFrame::MouseClicked (nsPresContext* aPresContext, nsGUIEvent* aEvent) 
00092 {
00093     // update the splitter first, in case someone's listening on the command event
00094     nsIFrame* splitter;
00095     nsScrollbarButtonFrame::GetParentWithTag(nsXULAtoms::splitter, this, splitter);
00096     if (splitter) {
00097 
00098         // get the splitters content node
00099         nsIContent* content = splitter->GetContent();
00100  
00101         nsAutoString newState(NS_LITERAL_STRING("collapsed"));
00102         nsAutoString oldState;
00103         if (NS_CONTENT_ATTR_HAS_VALUE == content->GetAttr(kNameSpaceID_None, nsXULAtoms::state, oldState))
00104         {
00105             if (oldState.Equals(newState))
00106                 newState.AssignLiteral("open");
00107         }
00108 
00109         content->SetAttr(kNameSpaceID_None, nsXULAtoms::state, newState, PR_TRUE);
00110     }
00111 
00112     nsButtonBoxFrame::MouseClicked(aPresContext, aEvent);
00113 }
00114 
00115 /*
00116 void
00117 nsGrippyFrame::MouseClicked(nsPresContext* aPresContext) 
00118 {
00119 
00120   nsString style;
00121 
00122   if (mCollapsed) {
00123     style = mCollapsedChildStyle;
00124   } else {
00125     // when clicked see if we are in a splitter. 
00126     nsIFrame* splitter;
00127     nsScrollbarButtonFrame::GetParentWithTag(nsXULAtoms::splitter, this, splitter);
00128 
00129     if (splitter == nsnull)
00130        return;
00131 
00132     // get the splitters content node
00133     nsIContent* content = splitter->GetContent();
00134 
00135     // get the collapse attribute. If the attribute is not set collapse
00136     // the child before otherwise collapse the child after
00137     PRBool before = PR_TRUE;
00138     nsString value;
00139     if (NS_CONTENT_ATTR_HAS_VALUE == content->GetAttr(kNameSpaceID_None, nsXULAtoms::collapse, value))
00140     {
00141      if (value=="after")
00142        before = PR_FALSE;
00143     }
00144 
00145     // find the child just in the box just before the splitter. If we are not currently collapsed then
00146     // then get the childs style attribute and store it. Then set the child style attribute to be display none.
00147     // if we are already collapsed then set the child's style back to our stored value.
00148     nsIFrame* child = GetChildBeforeAfter(splitter,before);
00149     if (child == nsnull)
00150       return;
00151 
00152     mCollapsedChild = child->GetContent();
00153 
00154     style = "visibility: collapse";
00155     mCollapsedChildStyle = "";
00156     mCollapsedChild->GetAttr(kNameSpaceID_None, nsHTMLAtoms::style, mCollapsedChildStyle);
00157   }
00158 
00159   mCollapsedChild->SetAttr(kNameSpaceID_None, nsHTMLAtoms::style, style, PR_TRUE);
00160 
00161   mCollapsed = !mCollapsed;
00162 
00163 }
00164 */
00165 
00166 
00167 nsIFrame*
00168 nsGrippyFrame::GetChildBeforeAfter(nsPresContext* aPresContext, nsIFrame* start, PRBool before)
00169 {
00170    nsIFrame* parent = start->GetParent();
00171    PRInt32 index = IndexOf(aPresContext, parent, start);
00172    PRInt32 count = CountFrames(aPresContext, parent);
00173 
00174    if (index == -1) 
00175      return nsnull;
00176 
00177    if (before) {
00178      if (index == 0) {
00179          return nsnull;
00180      }
00181 
00182      return GetChildAt(aPresContext, parent, index-1);
00183    }
00184 
00185 
00186    if (index == count-1)
00187        return nsnull;
00188 
00189    return GetChildAt(aPresContext, parent, index+1);
00190 
00191 }
00192 
00193 PRInt32
00194 nsGrippyFrame::IndexOf(nsPresContext* aPresContext, nsIFrame* parent, nsIFrame* child)
00195 {
00196   PRInt32 count = 0;
00197 
00198   nsIFrame* childFrame = parent->GetFirstChild(nsnull); 
00199   while (nsnull != childFrame) 
00200   {    
00201     if (childFrame == child)
00202        return count;
00203 
00204     childFrame = childFrame->GetNextSibling();
00205     count++;
00206   }
00207 
00208   return -1;
00209 }
00210 
00211 PRInt32
00212 nsGrippyFrame::CountFrames(nsPresContext* aPresContext, nsIFrame* aFrame)
00213 {
00214   PRInt32 count = 0;
00215 
00216   nsIFrame* childFrame = aFrame->GetFirstChild(nsnull);
00217   while (nsnull != childFrame) 
00218   {    
00219     childFrame = childFrame->GetNextSibling();
00220     count++;
00221   }
00222 
00223   return count;
00224 }
00225 
00226 nsIFrame*
00227 nsGrippyFrame::GetChildAt(nsPresContext* aPresContext, nsIFrame* parent, PRInt32 index)
00228 {
00229   PRInt32 count = 0;
00230 
00231   nsIFrame* childFrame = parent->GetFirstChild(nsnull);
00232   while (nsnull != childFrame) 
00233   {    
00234     if (count == index)
00235        return childFrame;
00236 
00237     childFrame = childFrame->GetNextSibling();
00238     count++;
00239   }
00240 
00241   return nsnull;
00242 }
00243 
00244 #ifdef DEBUG
00245 NS_IMETHODIMP
00246 nsGrippyFrame::GetFrameName(nsAString& aResult) const
00247 {
00248   return MakeFrameName(NS_LITERAL_STRING("Grippy"), aResult);
00249 }
00250 #endif