Bug 473390 part 10. Move various XUL nsIBoxLayout stuff out of the frame constructor...
[mozilla-central.git] / layout / xul / base / src / nsDeckFrame.cpp
blobd7b27d9fcb60515f8efb0406bc20b4d7cebf69ca
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 // Eric Vaughan
40 // Netscape Communications
42 // See documentation in associated header file
45 #include "nsDeckFrame.h"
46 #include "nsStyleContext.h"
47 #include "nsPresContext.h"
48 #include "nsIContent.h"
49 #include "nsCOMPtr.h"
50 #include "nsINameSpaceManager.h"
51 #include "nsGkAtoms.h"
52 #include "nsHTMLParts.h"
53 #include "nsIPresShell.h"
54 #include "nsCSSRendering.h"
55 #include "nsIViewManager.h"
56 #include "nsBoxLayoutState.h"
57 #include "nsStackLayout.h"
58 #include "nsDisplayList.h"
60 nsIFrame*
61 NS_NewDeckFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
63 return new (aPresShell) nsDeckFrame(aPresShell, aContext);
64 } // NS_NewDeckFrame
67 nsDeckFrame::nsDeckFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
68 : nsBoxFrame(aPresShell, aContext), mIndex(0)
70 nsCOMPtr<nsIBoxLayout> layout;
71 NS_NewStackLayout(aPresShell, layout);
72 SetLayoutManager(layout);
75 nsIAtom*
76 nsDeckFrame::GetType() const
78 return nsGkAtoms::deckFrame;
81 NS_IMETHODIMP
82 nsDeckFrame::AttributeChanged(PRInt32 aNameSpaceID,
83 nsIAtom* aAttribute,
84 PRInt32 aModType)
86 nsresult rv = nsBoxFrame::AttributeChanged(aNameSpaceID, aAttribute,
87 aModType);
90 // if the index changed hide the old element and make the new element visible
91 if (aAttribute == nsGkAtoms::selectedIndex) {
92 IndexChanged(PresContext());
95 return rv;
98 NS_IMETHODIMP
99 nsDeckFrame::Init(nsIContent* aContent,
100 nsIFrame* aParent,
101 nsIFrame* aPrevInFlow)
103 nsresult rv = nsBoxFrame::Init(aContent, aParent, aPrevInFlow);
105 mIndex = GetSelectedIndex();
107 return rv;
110 void
111 nsDeckFrame::HideBox(nsPresContext* aPresContext, nsIBox* aBox)
113 nsIView* view = aBox->GetView();
115 if (view) {
116 nsIViewManager* viewManager = view->GetViewManager();
117 viewManager->SetViewVisibility(view, nsViewVisibility_kHide);
118 viewManager->ResizeView(view, nsRect(0, 0, 0, 0));
122 void
123 nsDeckFrame::ShowBox(nsPresContext* aPresContext, nsIBox* aBox)
125 nsRect rect = aBox->GetRect();
126 nsIView* view = aBox->GetView();
127 if (view) {
128 nsIViewManager* viewManager = view->GetViewManager();
129 rect.x = rect.y = 0;
130 viewManager->ResizeView(view, rect);
131 viewManager->SetViewVisibility(view, nsViewVisibility_kShow);
135 void
136 nsDeckFrame::IndexChanged(nsPresContext* aPresContext)
138 //did the index change?
139 PRInt32 index = GetSelectedIndex();
140 if (index == mIndex)
141 return;
143 // redraw
144 nsBoxLayoutState state(aPresContext);
145 Redraw(state);
147 // hide the currently showing box
148 nsIBox* currentBox = GetSelectedBox();
149 if (currentBox) // only hide if it exists
150 HideBox(aPresContext, currentBox);
152 mIndex = index;
154 // show the new box
155 nsIBox* newBox = GetSelectedBox();
156 if (newBox) // only show if it exists
157 ShowBox(aPresContext, newBox);
160 PRInt32
161 nsDeckFrame::GetSelectedIndex()
163 // default index is 0
164 PRInt32 index = 0;
166 // get the index attribute
167 nsAutoString value;
168 if (mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::selectedIndex, value))
170 PRInt32 error;
172 // convert it to an integer
173 index = value.ToInteger(&error);
176 return index;
179 nsIBox*
180 nsDeckFrame::GetSelectedBox()
182 return (mIndex >= 0) ? mFrames.FrameAt(mIndex) : nsnull;
185 NS_IMETHODIMP
186 nsDeckFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
187 const nsRect& aDirtyRect,
188 const nsDisplayListSet& aLists)
190 // if a tab is hidden all its children are too.
191 if (!GetStyleVisibility()->mVisible)
192 return NS_OK;
194 // REVIEW: The old code skipped painting of background/borders/outline for this
195 // frame and painting of debug boxes ... I've put it back.
196 return nsBoxFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);
199 NS_IMETHODIMP
200 nsDeckFrame::BuildDisplayListForChildren(nsDisplayListBuilder* aBuilder,
201 const nsRect& aDirtyRect,
202 const nsDisplayListSet& aLists)
204 // only paint the selected box
205 nsIBox* box = GetSelectedBox();
206 if (!box)
207 return NS_OK;
209 // Putting the child in the background list. This is a little weird but
210 // it matches what we were doing before.
211 nsDisplayListSet set(aLists, aLists.BlockBorderBackgrounds());
212 return BuildDisplayListForChild(aBuilder, box, aDirtyRect, set);
215 NS_IMETHODIMP
216 nsDeckFrame::DoLayout(nsBoxLayoutState& aState)
218 // Make sure we tweak the state so it does not resize our children.
219 // We will do that.
220 PRUint32 oldFlags = aState.LayoutFlags();
221 aState.SetLayoutFlags(NS_FRAME_NO_SIZE_VIEW | NS_FRAME_NO_VISIBILITY);
223 // do a normal layout
224 nsresult rv = nsBoxFrame::DoLayout(aState);
226 // run though each child. Hide all but the selected one
227 nsIBox* box = GetChildBox();
229 nscoord count = 0;
230 while (box)
232 // make collapsed children not show up
233 if (count == mIndex)
234 ShowBox(aState.PresContext(), box);
235 else
236 HideBox(aState.PresContext(), box);
238 box = box->GetNextBox();
239 count++;
242 aState.SetLayoutFlags(oldFlags);
244 return rv;