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
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : //
39 : // Eric Vaughan
40 : // Netscape Communications
41 : //
42 : // See documentation in associated header file
43 : //
44 :
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"
59 : #include "nsContainerFrame.h"
60 :
61 : nsIFrame*
62 0 : NS_NewDeckFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
63 : {
64 0 : return new (aPresShell) nsDeckFrame(aPresShell, aContext);
65 : }
66 :
67 0 : NS_IMPL_FRAMEARENA_HELPERS(nsDeckFrame)
68 :
69 0 : NS_QUERYFRAME_HEAD(nsDeckFrame)
70 0 : NS_QUERYFRAME_ENTRY(nsDeckFrame)
71 0 : NS_QUERYFRAME_TAIL_INHERITING(nsBoxFrame)
72 :
73 :
74 0 : nsDeckFrame::nsDeckFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
75 0 : : nsBoxFrame(aPresShell, aContext), mIndex(0)
76 : {
77 0 : nsCOMPtr<nsBoxLayout> layout;
78 0 : NS_NewStackLayout(aPresShell, layout);
79 0 : SetLayoutManager(layout);
80 0 : }
81 :
82 : nsIAtom*
83 0 : nsDeckFrame::GetType() const
84 : {
85 0 : return nsGkAtoms::deckFrame;
86 : }
87 :
88 : NS_IMETHODIMP
89 0 : nsDeckFrame::AttributeChanged(PRInt32 aNameSpaceID,
90 : nsIAtom* aAttribute,
91 : PRInt32 aModType)
92 : {
93 : nsresult rv = nsBoxFrame::AttributeChanged(aNameSpaceID, aAttribute,
94 0 : aModType);
95 :
96 :
97 : // if the index changed hide the old element and make the new element visible
98 0 : if (aAttribute == nsGkAtoms::selectedIndex) {
99 0 : IndexChanged();
100 : }
101 :
102 0 : return rv;
103 : }
104 :
105 : NS_IMETHODIMP
106 0 : nsDeckFrame::Init(nsIContent* aContent,
107 : nsIFrame* aParent,
108 : nsIFrame* aPrevInFlow)
109 : {
110 0 : nsresult rv = nsBoxFrame::Init(aContent, aParent, aPrevInFlow);
111 :
112 0 : mIndex = GetSelectedIndex();
113 :
114 0 : return rv;
115 : }
116 :
117 : void
118 0 : nsDeckFrame::HideBox(nsIBox* aBox)
119 : {
120 0 : nsIPresShell::ClearMouseCapture(aBox);
121 0 : }
122 :
123 : void
124 0 : nsDeckFrame::IndexChanged()
125 : {
126 : //did the index change?
127 0 : PRInt32 index = GetSelectedIndex();
128 0 : if (index == mIndex)
129 0 : return;
130 :
131 : // redraw
132 0 : InvalidateOverflowRect();
133 :
134 : // hide the currently showing box
135 0 : nsIBox* currentBox = GetSelectedBox();
136 0 : if (currentBox) // only hide if it exists
137 0 : HideBox(currentBox);
138 :
139 0 : mIndex = index;
140 : }
141 :
142 : PRInt32
143 0 : nsDeckFrame::GetSelectedIndex()
144 : {
145 : // default index is 0
146 0 : PRInt32 index = 0;
147 :
148 : // get the index attribute
149 0 : nsAutoString value;
150 0 : if (mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::selectedIndex, value))
151 : {
152 : PRInt32 error;
153 :
154 : // convert it to an integer
155 0 : index = value.ToInteger(&error);
156 : }
157 :
158 0 : return index;
159 : }
160 :
161 : nsIFrame*
162 0 : nsDeckFrame::GetSelectedBox()
163 : {
164 0 : return (mIndex >= 0) ? mFrames.FrameAt(mIndex) : nsnull;
165 : }
166 :
167 : NS_IMETHODIMP
168 0 : nsDeckFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
169 : const nsRect& aDirtyRect,
170 : const nsDisplayListSet& aLists)
171 : {
172 : // if a tab is hidden all its children are too.
173 0 : if (!GetStyleVisibility()->mVisible)
174 0 : return NS_OK;
175 :
176 : // REVIEW: The old code skipped painting of background/borders/outline for this
177 : // frame and painting of debug boxes ... I've put it back.
178 0 : return nsBoxFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);
179 : }
180 :
181 : NS_IMETHODIMP
182 0 : nsDeckFrame::BuildDisplayListForChildren(nsDisplayListBuilder* aBuilder,
183 : const nsRect& aDirtyRect,
184 : const nsDisplayListSet& aLists)
185 : {
186 : // only paint the selected box
187 0 : nsIBox* box = GetSelectedBox();
188 0 : if (!box)
189 0 : return NS_OK;
190 :
191 : // Putting the child in the background list. This is a little weird but
192 : // it matches what we were doing before.
193 0 : nsDisplayListSet set(aLists, aLists.BlockBorderBackgrounds());
194 0 : return BuildDisplayListForChild(aBuilder, box, aDirtyRect, set);
195 : }
196 :
197 : NS_IMETHODIMP
198 0 : nsDeckFrame::DoLayout(nsBoxLayoutState& aState)
199 : {
200 : // Make sure we tweak the state so it does not resize our children.
201 : // We will do that.
202 0 : PRUint32 oldFlags = aState.LayoutFlags();
203 0 : aState.SetLayoutFlags(NS_FRAME_NO_SIZE_VIEW | NS_FRAME_NO_VISIBILITY);
204 :
205 : // do a normal layout
206 0 : nsresult rv = nsBoxFrame::DoLayout(aState);
207 :
208 : // run though each child. Hide all but the selected one
209 0 : nsIBox* box = GetChildBox();
210 :
211 0 : nscoord count = 0;
212 0 : while (box)
213 : {
214 : // make collapsed children not show up
215 0 : if (count != mIndex)
216 0 : HideBox(box);
217 :
218 0 : box = box->GetNextBox();
219 0 : count++;
220 : }
221 :
222 0 : aState.SetLayoutFlags(oldFlags);
223 :
224 0 : return rv;
225 : }
226 :
|