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
15 * The Original Code is Mozilla MathML Project.
17 * The Initial Developer of the Original Code is
18 * The University Of Queensland.
19 * Portions created by the Initial Developer are Copyright (C) 1999
20 * the Initial Developer. All Rights Reserved.
23 * Roger B. Sidje <rbs@maths.uq.edu.au>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
41 #include "nsPresContext.h"
42 #include "nsStyleContext.h"
43 #include "nsStyleConsts.h"
44 #include "nsINameSpaceManager.h"
45 #include "nsIRenderingContext.h"
46 #include "nsIFontMetrics.h"
48 #include "nsCSSRendering.h"
49 #include "prprf.h" // For PR_snprintf()
51 #include "nsIDocShellTreeItem.h"
52 #include "nsIDocShellTreeOwner.h"
53 #include "nsIWebBrowserChrome.h"
54 #include "nsIInterfaceRequestor.h"
55 #include "nsIInterfaceRequestorUtils.h"
56 #include "nsIDOMElement.h"
58 #include "nsIDOMEventTarget.h"
59 #include "nsIDOMMouseListener.h"
61 #include "nsMathMLmactionFrame.h"
62 #include "nsAutoPtr.h"
63 #include "nsStyleSet.h"
64 #include "nsDisplayList.h"
65 #include "nsContentUtils.h"
68 // <maction> -- bind actions to a subexpression - implementation
71 #define NS_MATHML_ACTION_TYPE_NONE 0
72 #define NS_MATHML_ACTION_TYPE_TOGGLE 1
73 #define NS_MATHML_ACTION_TYPE_STATUSLINE 2
74 #define NS_MATHML_ACTION_TYPE_TOOLTIP 3 // unsupported
75 #define NS_MATHML_ACTION_TYPE_RESTYLE 4
77 NS_IMPL_ADDREF_INHERITED(nsMathMLmactionFrame
, nsMathMLContainerFrame
)
78 NS_IMPL_RELEASE_INHERITED(nsMathMLmactionFrame
, nsMathMLContainerFrame
)
79 NS_IMPL_QUERY_INTERFACE_INHERITED2(nsMathMLmactionFrame
,
80 nsMathMLContainerFrame
,
85 NS_NewMathMLmactionFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
)
87 return new (aPresShell
) nsMathMLmactionFrame(aContext
);
90 nsMathMLmactionFrame::~nsMathMLmactionFrame()
92 // unregister us as a mouse event listener ...
93 // printf("maction:%p unregistering as mouse event listener ...\n", this);
94 mContent
->RemoveEventListenerByIID(this, NS_GET_IID(nsIDOMMouseListener
));
98 nsMathMLmactionFrame::Init(nsIContent
* aContent
,
100 nsIFrame
* aPrevInFlow
)
102 nsAutoString value
, prefix
;
104 // Init our local attributes
106 mChildCount
= -1; // these will be updated in GetSelectedFrame()
108 mSelectedFrame
= nsnull
;
109 nsRefPtr
<nsStyleContext
> newStyleContext
;
111 mActionType
= NS_MATHML_ACTION_TYPE_NONE
;
112 aContent
->GetAttr(kNameSpaceID_None
, nsGkAtoms::actiontype_
, value
);
113 if (!value
.IsEmpty()) {
114 if (value
.EqualsLiteral("toggle"))
115 mActionType
= NS_MATHML_ACTION_TYPE_TOGGLE
;
117 // XXX use goto to jump out of these if?
119 if (NS_MATHML_ACTION_TYPE_NONE
== mActionType
) {
120 // expected tooltip prefix (8ch)...
121 if (8 < value
.Length() && 0 == value
.Find("tooltip#"))
122 mActionType
= NS_MATHML_ACTION_TYPE_TOOLTIP
;
125 if (NS_MATHML_ACTION_TYPE_NONE
== mActionType
) {
126 // expected statusline prefix (11ch)...
127 if (11 < value
.Length() && 0 == value
.Find("statusline#"))
128 mActionType
= NS_MATHML_ACTION_TYPE_STATUSLINE
;
131 if (NS_MATHML_ACTION_TYPE_NONE
== mActionType
) {
132 // expected restyle prefix (8ch)...
133 if (8 < value
.Length() && 0 == value
.Find("restyle#")) {
134 mActionType
= NS_MATHML_ACTION_TYPE_RESTYLE
;
137 // Here is the situation:
138 // When the attribute [actiontype="restyle#id"] is set, the Style System has
139 // given us the associated style. But we want to start with our default style.
141 // So... first, remove the attribute actiontype="restyle#id"
142 // XXXbz this is pretty messed up, since this can change whether we
143 // should have a frame at all. This really needs a better solution.
144 PRBool notify
= PR_FALSE
; // don't trigger a reflow yet!
145 aContent
->UnsetAttr(kNameSpaceID_None
, nsGkAtoms::actiontype_
, notify
);
147 // then, re-resolve our style
148 nsStyleContext
* parentStyleContext
= GetStyleContext()->GetParent();
149 newStyleContext
= PresContext()->StyleSet()->
150 ResolveStyleFor(aContent
, parentStyleContext
);
152 if (!newStyleContext
)
155 if (newStyleContext
!= GetStyleContext())
156 SetStyleContextWithoutNotification(newStyleContext
);
164 // Let the base class do the rest
165 return nsMathMLContainerFrame::Init(aContent
, aParent
, aPrevInFlow
);
169 nsMathMLmactionFrame::ChildListChanged(PRInt32 aModType
)
171 // update cached values
174 mSelectedFrame
= nsnull
;
177 return nsMathMLContainerFrame::ChildListChanged(aModType
);
180 // return the frame whose number is given by the attribute selection="number"
182 nsMathMLmactionFrame::GetSelectedFrame()
187 mContent
->GetAttr(kNameSpaceID_None
, nsGkAtoms::selection_
, value
);
188 if (!value
.IsEmpty()) {
190 selection
= value
.ToInteger(&errorCode
);
191 if (NS_FAILED(errorCode
))
194 else selection
= 1; // default is first frame
196 if (-1 != mChildCount
) { // we have been in this function before...
197 // cater for invalid user-supplied selection
198 if (selection
> mChildCount
|| selection
< 1)
200 // quick return if it is identical with our cache
201 if (selection
== mSelection
)
202 return mSelectedFrame
;
205 // get the selected child and cache new values...
207 nsIFrame
* childFrame
= mFrames
.FirstChild();
210 mSelectedFrame
= childFrame
; // default is first child
211 if (++count
== selection
)
212 mSelectedFrame
= childFrame
;
214 childFrame
= childFrame
->GetNextSibling();
216 // cater for invalid user-supplied selection
217 if (selection
> count
|| selection
< 1)
221 mSelection
= selection
;
223 // if the selected child is an embellished operator,
224 // we become embellished as well
225 mPresentationData
.baseFrame
= mSelectedFrame
;
226 GetEmbellishDataFrom(mSelectedFrame
, mEmbellishData
);
228 return mSelectedFrame
;
232 nsMathMLmactionFrame::SetInitialChildList(nsIAtom
* aListName
,
233 nsIFrame
* aChildList
)
235 nsresult rv
= nsMathMLContainerFrame::SetInitialChildList(aListName
, aChildList
);
237 // This very first call to GetSelectedFrame() will cause us to be marked as an
238 // embellished operator if the selected child is an embellished operator
239 if (!GetSelectedFrame()) {
240 mActionType
= NS_MATHML_ACTION_TYPE_NONE
;
243 // register us as a mouse event listener ...
244 // printf("maction:%p registering as mouse event listener ...\n", this);
245 mContent
->AddEventListenerByIID(this, NS_GET_IID(nsIDOMMouseListener
));
250 // Only paint the selected child...
252 nsMathMLmactionFrame::BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
253 const nsRect
& aDirtyRect
,
254 const nsDisplayListSet
& aLists
)
256 nsresult rv
= DisplayBorderBackgroundOutline(aBuilder
, aLists
);
257 NS_ENSURE_SUCCESS(rv
, rv
);
259 nsIFrame
* childFrame
= GetSelectedFrame();
261 // Put the child's background directly onto the content list
262 nsDisplayListSet
set(aLists
, aLists
.Content());
263 // The children should be in content order
264 rv
= BuildDisplayListForChild(aBuilder
, childFrame
, aDirtyRect
, set
);
265 NS_ENSURE_SUCCESS(rv
, rv
);
268 #if defined(NS_DEBUG) && defined(SHOW_BOUNDING_BOX)
270 rv
= DisplayBoundingMetrics(aBuilder
, this, mReference
, mBoundingMetrics
, aLists
);
275 // Only reflow the selected child ...
277 nsMathMLmactionFrame::Reflow(nsPresContext
* aPresContext
,
278 nsHTMLReflowMetrics
& aDesiredSize
,
279 const nsHTMLReflowState
& aReflowState
,
280 nsReflowStatus
& aStatus
)
283 aStatus
= NS_FRAME_COMPLETE
;
284 aDesiredSize
.width
= aDesiredSize
.height
= 0;
285 aDesiredSize
.ascent
= 0;
286 mBoundingMetrics
.Clear();
287 nsIFrame
* childFrame
= GetSelectedFrame();
289 nsSize
availSize(aReflowState
.ComputedWidth(), NS_UNCONSTRAINEDSIZE
);
290 nsHTMLReflowState
childReflowState(aPresContext
, aReflowState
,
291 childFrame
, availSize
);
292 rv
= ReflowChild(childFrame
, aPresContext
, aDesiredSize
,
293 childReflowState
, aStatus
);
294 SaveReflowAndBoundingMetricsFor(childFrame
, aDesiredSize
,
295 aDesiredSize
.mBoundingMetrics
);
296 mBoundingMetrics
= aDesiredSize
.mBoundingMetrics
;
298 FinalizeReflow(*aReflowState
.rendContext
, aDesiredSize
);
299 NS_FRAME_SET_TRUNCATION(aStatus
, aReflowState
, aDesiredSize
);
303 // Only place the selected child ...
304 /* virtual */ nsresult
305 nsMathMLmactionFrame::Place(nsIRenderingContext
& aRenderingContext
,
307 nsHTMLReflowMetrics
& aDesiredSize
)
309 aDesiredSize
.width
= aDesiredSize
.height
= 0;
310 aDesiredSize
.ascent
= 0;
311 mBoundingMetrics
.Clear();
312 nsIFrame
* childFrame
= GetSelectedFrame();
314 GetReflowAndBoundingMetricsFor(childFrame
, aDesiredSize
, mBoundingMetrics
);
316 FinishReflowChild(childFrame
, PresContext(), nsnull
, aDesiredSize
, 0, 0, 0);
319 mReference
.y
= aDesiredSize
.ascent
;
321 aDesiredSize
.mBoundingMetrics
= mBoundingMetrics
;
325 // ################################################################
327 // ################################################################
329 // helper to show a msg on the status bar
330 // curled from nsObjectFrame.cpp ...
332 nsMathMLmactionFrame::ShowStatus(nsPresContext
* aPresContext
,
333 nsString
& aStatusMsg
)
335 nsCOMPtr
<nsISupports
> cont
= aPresContext
->GetContainer();
337 nsCOMPtr
<nsIDocShellTreeItem
> docShellItem(do_QueryInterface(cont
));
339 nsCOMPtr
<nsIDocShellTreeOwner
> treeOwner
;
340 docShellItem
->GetTreeOwner(getter_AddRefs(treeOwner
));
342 nsCOMPtr
<nsIWebBrowserChrome
> browserChrome(do_GetInterface(treeOwner
));
344 browserChrome
->SetStatus(nsIWebBrowserChrome::STATUS_LINK
, aStatusMsg
.get());
353 nsMathMLmactionFrame::MouseOver(nsIDOMEvent
* aMouseEvent
)
355 // see if we should display a status message
356 if (NS_MATHML_ACTION_TYPE_STATUSLINE
== mActionType
) {
358 mContent
->GetAttr(kNameSpaceID_None
, nsGkAtoms::actiontype_
, value
);
359 // expected statusline prefix (11ch)...
360 if (11 < value
.Length() && 0 == value
.Find("statusline#")) {
362 ShowStatus(PresContext(), value
);
369 nsMathMLmactionFrame::MouseOut(nsIDOMEvent
* aMouseEvent
)
371 // see if we should remove the status message
372 if (NS_MATHML_ACTION_TYPE_STATUSLINE
== mActionType
) {
375 ShowStatus(PresContext(), value
);
381 nsMathMLmactionFrame::MouseClick(nsIDOMEvent
* aMouseEvent
)
383 if (NS_MATHML_ACTION_TYPE_TOGGLE
== mActionType
) {
384 if (mChildCount
> 1) {
385 PRInt32 selection
= (mSelection
== mChildCount
)? 1 : mSelection
+ 1;
388 PR_snprintf(cbuf
, sizeof(cbuf
), "%d", selection
);
389 value
.AssignASCII(cbuf
);
390 PRBool notify
= PR_FALSE
; // don't yet notify the document
391 mContent
->SetAttr(kNameSpaceID_None
, nsGkAtoms::selection_
, value
, notify
);
393 // Now trigger a content-changed reflow...
394 PresContext()->PresShell()->
395 FrameNeedsReflow(mSelectedFrame
, nsIPresShell::eTreeChange
,
399 else if (NS_MATHML_ACTION_TYPE_RESTYLE
== mActionType
) {
400 if (!mRestyle
.IsEmpty()) {
401 nsCOMPtr
<nsIDOMElement
> node( do_QueryInterface(mContent
) );
403 if (nsContentUtils::HasNonEmptyAttr(mContent
, kNameSpaceID_None
,
404 nsGkAtoms::actiontype_
))
405 node
->RemoveAttribute(NS_LITERAL_STRING("actiontype"));
407 node
->SetAttribute(NS_LITERAL_STRING("actiontype"), mRestyle
);
409 // Trigger a style change reflow
410 PresContext()->PresShell()->
411 FrameNeedsReflow(mSelectedFrame
, nsIPresShell::eStyleChange
,