Back to index

lightning-sunbird  0.9+nobinonly
nsMathMLmactionFrame.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla MathML Project.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * The University Of Queensland.
00018  * Portions created by the Initial Developer are Copyright (C) 1999
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Roger B. Sidje <rbs@maths.uq.edu.au>
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 #include "nsCOMPtr.h"
00039 #include "nsFrame.h"
00040 #include "nsPresContext.h"
00041 #include "nsUnitConversion.h"
00042 #include "nsStyleContext.h"
00043 #include "nsStyleConsts.h"
00044 #include "nsINameSpaceManager.h"
00045 #include "nsIRenderingContext.h"
00046 #include "nsIFontMetrics.h"
00047 
00048 #include "nsCSSRendering.h"
00049 #include "prprf.h"         // For PR_snprintf()
00050 
00051 #include "nsIDocShellTreeItem.h"
00052 #include "nsIDocShellTreeOwner.h"
00053 #include "nsIWebBrowserChrome.h"
00054 #include "nsIInterfaceRequestor.h"
00055 #include "nsIInterfaceRequestorUtils.h"
00056 #include "nsIDOMElement.h"
00057 
00058 #include "nsIDOMEventReceiver.h"
00059 #include "nsIDOMMouseListener.h"
00060 
00061 #include "nsMathMLmactionFrame.h"
00062 #include "nsAutoPtr.h"
00063 #include "nsStyleSet.h"
00064 
00065 //
00066 // <maction> -- bind actions to a subexpression - implementation
00067 //
00068 
00069 #define NS_MATHML_ACTION_TYPE_NONE         0
00070 #define NS_MATHML_ACTION_TYPE_TOGGLE       1
00071 #define NS_MATHML_ACTION_TYPE_STATUSLINE   2
00072 #define NS_MATHML_ACTION_TYPE_TOOLTIP      3 // unsupported
00073 #define NS_MATHML_ACTION_TYPE_RESTYLE      4
00074 
00075 NS_IMPL_ADDREF_INHERITED(nsMathMLmactionFrame, nsMathMLContainerFrame)
00076 NS_IMPL_RELEASE_INHERITED(nsMathMLmactionFrame, nsMathMLContainerFrame)
00077 NS_IMPL_QUERY_INTERFACE_INHERITED1(nsMathMLmactionFrame, nsMathMLContainerFrame, nsIDOMMouseListener)
00078 
00079 nsresult
00080 NS_NewMathMLmactionFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame)
00081 {
00082   NS_PRECONDITION(aNewFrame, "null OUT ptr");
00083   if (nsnull == aNewFrame) {
00084     return NS_ERROR_NULL_POINTER;
00085   }
00086   nsMathMLmactionFrame* it = new (aPresShell) nsMathMLmactionFrame;
00087   if (nsnull == it) {
00088     return NS_ERROR_OUT_OF_MEMORY;
00089   }
00090   *aNewFrame = it;
00091   return NS_OK;
00092 }
00093 
00094 nsMathMLmactionFrame::nsMathMLmactionFrame()
00095 {
00096 }
00097 
00098 nsMathMLmactionFrame::~nsMathMLmactionFrame()
00099 {
00100   // unregister us as a mouse event listener ...
00101 //  printf("maction:%p unregistering as mouse event listener ...\n", this);
00102   nsCOMPtr<nsIDOMEventReceiver> receiver(do_QueryInterface(mContent));
00103   receiver->RemoveEventListenerByIID(this, NS_GET_IID(nsIDOMMouseListener));
00104 }
00105 
00106 NS_IMETHODIMP
00107 nsMathMLmactionFrame::Init(nsPresContext*  aPresContext,
00108                            nsIContent*      aContent,
00109                            nsIFrame*        aParent,
00110                            nsStyleContext*  aContext,
00111                            nsIFrame*        aPrevInFlow)
00112 {
00113   nsAutoString value, prefix;
00114 
00115   // Init our local attributes
00116 
00117   mPresContext = aPresContext;
00118  
00119   mWasRestyled = PR_FALSE;
00120   mChildCount = -1; // these will be updated in GetSelectedFrame()
00121   mSelection = 0;
00122   mSelectedFrame = nsnull;
00123   nsRefPtr<nsStyleContext> newStyleContext;
00124 
00125   mActionType = NS_MATHML_ACTION_TYPE_NONE;
00126   if (NS_CONTENT_ATTR_HAS_VALUE == aContent->GetAttr(kNameSpaceID_None, 
00127                    nsMathMLAtoms::actiontype_, value)) {
00128     if (value.EqualsLiteral("toggle"))
00129       mActionType = NS_MATHML_ACTION_TYPE_TOGGLE;
00130 
00131     // XXX use goto to jump out of these if?
00132 
00133     if (NS_MATHML_ACTION_TYPE_NONE == mActionType) {
00134       // expected tooltip prefix (8ch)...
00135       if (8 < value.Length() && 0 == value.Find("tooltip#"))
00136         mActionType = NS_MATHML_ACTION_TYPE_TOOLTIP;
00137     }
00138 
00139     if (NS_MATHML_ACTION_TYPE_NONE == mActionType) {
00140       // expected statusline prefix (11ch)...
00141       if (11 < value.Length() && 0 == value.Find("statusline#"))
00142         mActionType = NS_MATHML_ACTION_TYPE_STATUSLINE;
00143     }
00144 
00145     if (NS_MATHML_ACTION_TYPE_NONE == mActionType) {
00146       // expected restyle prefix (8ch)...
00147       if (8 < value.Length() && 0 == value.Find("restyle#")) {
00148         mActionType = NS_MATHML_ACTION_TYPE_RESTYLE;
00149         mRestyle = value;
00150 
00151         // Here is the situation:
00152         // When the attribute [actiontype="restyle#id"] is set, the Style System has
00153         // given us the associated style. But we want to start with our default style.
00154 
00155         // So... first, remove the attribute actiontype="restyle#id"
00156         PRBool notify = PR_FALSE; // don't trigger a reflow yet!
00157         aContent->UnsetAttr(kNameSpaceID_None, nsMathMLAtoms::actiontype_, notify);
00158 
00159         // then, re-resolve our style
00160         nsStyleContext* parentStyleContext = aParent->GetStyleContext();
00161         newStyleContext = aPresContext->StyleSet()->
00162           ResolveStyleFor(aContent, parentStyleContext);
00163 
00164         if (!newStyleContext) 
00165           mRestyle.Truncate();
00166         else {
00167           if (newStyleContext != aContext)
00168             aContext = newStyleContext;
00169           else
00170             mRestyle.Truncate();
00171         }
00172       }
00173     }
00174   }
00175 
00176   // Let the base class do the rest
00177   return nsMathMLContainerFrame::Init(aPresContext, aContent, aParent, aContext, aPrevInFlow);
00178 }
00179 
00180 // return the frame whose number is given by the attribute selection="number"
00181 nsIFrame* 
00182 nsMathMLmactionFrame::GetSelectedFrame()
00183 {
00184   nsAutoString value;
00185   PRInt32 selection; 
00186 
00187   if (NS_CONTENT_ATTR_HAS_VALUE == mContent->GetAttr(kNameSpaceID_None, 
00188                    nsMathMLAtoms::selection_, value)) {
00189     PRInt32 errorCode;
00190     selection = value.ToInteger(&errorCode);
00191     if (NS_FAILED(errorCode)) 
00192       selection = 1;
00193   }
00194   else selection = 1; // default is first frame
00195 
00196   if (-1 != mChildCount) { // we have been in this function before...
00197     // cater for invalid user-supplied selection
00198     if (selection > mChildCount || selection < 1) 
00199       selection = 1;
00200     // quick return if it is identical with our cache
00201     if (selection == mSelection) 
00202       return mSelectedFrame;
00203   }
00204 
00205   // get the selected child and cache new values...
00206   PRInt32 count = 0;
00207   nsIFrame* childFrame = mFrames.FirstChild();
00208   while (childFrame) {
00209     if (!mSelectedFrame) 
00210       mSelectedFrame = childFrame; // default is first child
00211     if (++count == selection) 
00212       mSelectedFrame = childFrame;
00213 
00214     childFrame = childFrame->GetNextSibling();
00215   }
00216   // cater for invalid user-supplied selection
00217   if (selection > count || selection < 1) 
00218     selection = 1;
00219 
00220   mChildCount = count;
00221   mSelection = selection;
00222 
00223   // if the selected child is an embellished operator,
00224   // we become embellished as well
00225   mPresentationData.baseFrame = mSelectedFrame;
00226   GetEmbellishDataFrom(mSelectedFrame, mEmbellishData);
00227 
00228   return mSelectedFrame;
00229 }
00230 
00231 NS_IMETHODIMP
00232 nsMathMLmactionFrame::SetInitialChildList(nsPresContext* aPresContext,
00233                                           nsIAtom*        aListName,
00234                                           nsIFrame*       aChildList)
00235 {
00236   nsresult rv = nsMathMLContainerFrame::SetInitialChildList(aPresContext, aListName, aChildList);
00237 
00238   // This very first call to GetSelectedFrame() will cause us to be marked as an
00239   // embellished operator if the selected child is an embellished operator
00240   if (!GetSelectedFrame()) {
00241     mActionType = NS_MATHML_ACTION_TYPE_NONE;
00242   }
00243   else {
00244     // register us as a mouse event listener ...
00245     // printf("maction:%p registering as mouse event listener ...\n", this);
00246     nsCOMPtr<nsIDOMEventReceiver> receiver(do_QueryInterface(mContent));
00247     receiver->AddEventListenerByIID(this, NS_GET_IID(nsIDOMMouseListener));
00248   }
00249   return rv;
00250 }
00251 
00252 // Return the selected frame ...
00253 NS_IMETHODIMP
00254 nsMathMLmactionFrame::GetFrameForPoint(const nsPoint&    aPoint,
00255                                        nsFramePaintLayer aWhichLayer,
00256                                        nsIFrame**        aFrame)
00257 {
00258   nsIFrame* childFrame = GetSelectedFrame();
00259   if (childFrame) {
00260     nsPoint pt(aPoint.x - mRect.x, aPoint.y - mRect.y);
00261     return childFrame->GetFrameForPoint(pt, aWhichLayer, aFrame);
00262   }
00263   return nsFrame::GetFrameForPoint(aPoint, aWhichLayer, aFrame);
00264 }
00265 
00266 //  Only paint the selected child...
00267 NS_IMETHODIMP
00268 nsMathMLmactionFrame::Paint(nsPresContext*      aPresContext,
00269                             nsIRenderingContext& aRenderingContext,
00270                             const nsRect&        aDirtyRect,
00271                             nsFramePaintLayer    aWhichLayer,
00272                             PRUint32             aFlags)
00273 {
00274   if (NS_FRAME_PAINT_LAYER_BACKGROUND == aWhichLayer) {
00275     PaintSelf(aPresContext, aRenderingContext, aDirtyRect);
00276   }
00277 
00278   nsIFrame* childFrame = GetSelectedFrame();
00279   if (childFrame)
00280     PaintChild(aPresContext, aRenderingContext, aDirtyRect, childFrame, aWhichLayer);
00281 
00282 #if defined(NS_DEBUG) && defined(SHOW_BOUNDING_BOX)
00283   // visual debug
00284   if (NS_FRAME_PAINT_LAYER_FOREGROUND == aWhichLayer &&
00285       NS_MATHML_PAINT_BOUNDING_METRICS(mPresentationData.flags)) {
00286     aRenderingContext.SetColor(NS_RGB(0,0,255));
00287 
00288     nscoord x = mReference.x + mBoundingMetrics.leftBearing;
00289     nscoord y = mReference.y - mBoundingMetrics.ascent;
00290     nscoord w = mBoundingMetrics.rightBearing - mBoundingMetrics.leftBearing;
00291     nscoord h = mBoundingMetrics.ascent + mBoundingMetrics.descent;
00292 
00293     aRenderingContext.DrawRect(x,y,w,h);
00294   }
00295 #endif
00296   return NS_OK;
00297 }
00298 
00299 // Only reflow the selected child ...
00300 NS_IMETHODIMP
00301 nsMathMLmactionFrame::Reflow(nsPresContext*          aPresContext,
00302                              nsHTMLReflowMetrics&     aDesiredSize,
00303                              const nsHTMLReflowState& aReflowState,
00304                              nsReflowStatus&          aStatus)
00305 {
00306   nsresult rv = NS_OK;
00307   aDesiredSize.width = aDesiredSize.height = 0;
00308   aDesiredSize.ascent = aDesiredSize.descent = 0;
00309   mBoundingMetrics.Clear();
00310   nsIFrame* childFrame = GetSelectedFrame();
00311   if (childFrame) {
00312     nsReflowReason reason = aReflowState.reason;
00313     if (childFrame->GetStateBits() & NS_FRAME_FIRST_REFLOW)
00314       reason = eReflowReason_Initial;
00315     else if (mWasRestyled) {
00316       mWasRestyled = PR_FALSE;
00317       // If we have just been restyled, make sure to reflow our
00318       // selected child with a StyleChange reflow reason so that
00319       // it doesn't over-optimize its reflow. In principle we shouldn't
00320       // need to do this because we posted a style changed reflow (see
00321       // MouseClick() below). But that reason can be (and usually, it is)
00322       // changed in the reflow chain and we don't have much control other
00323       // than making sure that the right value is reset here.
00324       reason = eReflowReason_StyleChange;
00325     }
00326 
00327     nsSize availSize(aReflowState.mComputedWidth, aReflowState.mComputedHeight);
00328     nsHTMLReflowState childReflowState(aPresContext, aReflowState,
00329                                        childFrame, availSize, reason);
00330     rv = ReflowChild(childFrame, aPresContext, aDesiredSize,
00331                      childReflowState, aStatus);
00332     childFrame->SetRect(nsRect(aDesiredSize.descent,aDesiredSize.ascent,
00333                         aDesiredSize.width,aDesiredSize.height));
00334     mBoundingMetrics = aDesiredSize.mBoundingMetrics;
00335     FinalizeReflow(*aReflowState.rendContext, aDesiredSize);
00336   }
00337   NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize);
00338   return rv;
00339 }
00340 
00341 // Only place the selected child ...
00342 NS_IMETHODIMP
00343 nsMathMLmactionFrame::Place(nsIRenderingContext& aRenderingContext,
00344                             PRBool               aPlaceOrigin,
00345                             nsHTMLReflowMetrics& aDesiredSize)
00346 {
00347   aDesiredSize.width = aDesiredSize.height = 0;
00348   aDesiredSize.ascent = aDesiredSize.descent = 0;
00349   mBoundingMetrics.Clear();
00350   nsIFrame* childFrame = GetSelectedFrame();
00351   if (childFrame) {
00352     GetReflowAndBoundingMetricsFor(childFrame, aDesiredSize, mBoundingMetrics);
00353     if (aPlaceOrigin) {
00354       FinishReflowChild(childFrame, GetPresContext(), nsnull, aDesiredSize, 0, 0, 0);
00355     }
00356     mReference.x = 0;
00357     mReference.y = aDesiredSize.ascent;
00358   }
00359   aDesiredSize.mBoundingMetrics = mBoundingMetrics;
00360   return NS_OK;
00361 }
00362 
00363 // ################################################################
00364 // Event handlers 
00365 // ################################################################
00366 
00367 // helper to show a msg on the status bar
00368 // curled from nsObjectFrame.cpp ...
00369 nsresult
00370 nsMathMLmactionFrame::ShowStatus(nsPresContext* aPresContext,
00371                                  nsString&       aStatusMsg)
00372 {
00373   nsCOMPtr<nsISupports> cont = aPresContext->GetContainer();
00374   if (cont) {
00375     nsCOMPtr<nsIDocShellTreeItem> docShellItem(do_QueryInterface(cont));
00376     if (docShellItem) {
00377       nsCOMPtr<nsIDocShellTreeOwner> treeOwner;
00378       docShellItem->GetTreeOwner(getter_AddRefs(treeOwner));
00379       if (treeOwner) {
00380         nsCOMPtr<nsIWebBrowserChrome> browserChrome(do_GetInterface(treeOwner));
00381         if (browserChrome) {
00382           browserChrome->SetStatus(nsIWebBrowserChrome::STATUS_LINK, aStatusMsg.get());
00383         }
00384       }
00385     }
00386   }
00387   return NS_OK;
00388 }
00389 
00390 NS_IMETHODIMP
00391 nsMathMLmactionFrame::MouseOver(nsIDOMEvent* aMouseEvent) 
00392 {
00393   // see if we should display a status message
00394   if (NS_MATHML_ACTION_TYPE_STATUSLINE == mActionType) {
00395     nsAutoString value;
00396     if (NS_CONTENT_ATTR_HAS_VALUE == mContent->GetAttr(kNameSpaceID_None, 
00397                      nsMathMLAtoms::actiontype_, value)) {
00398       // expected statusline prefix (11ch)...
00399       if (11 < value.Length() && 0 == value.Find("statusline#")) {
00400         value.Cut(0, 11);
00401         ShowStatus(mPresContext, value);
00402       }
00403     }
00404   }
00405   return NS_OK;
00406 }
00407 
00408 NS_IMETHODIMP
00409 nsMathMLmactionFrame::MouseOut(nsIDOMEvent* aMouseEvent) 
00410 { 
00411   // see if we should remove the status message
00412   if (NS_MATHML_ACTION_TYPE_STATUSLINE == mActionType) {
00413     nsAutoString value;
00414     value.SetLength(0);
00415     ShowStatus(mPresContext, value);
00416   }
00417   return NS_OK;
00418 }
00419 
00420 NS_IMETHODIMP
00421 nsMathMLmactionFrame::MouseClick(nsIDOMEvent* aMouseEvent)
00422 {
00423   nsAutoString value;
00424   if (NS_MATHML_ACTION_TYPE_TOGGLE == mActionType) {
00425     if (mChildCount > 1) {
00426       PRInt32 selection = (mSelection == mChildCount)? 1 : mSelection + 1;
00427       char cbuf[10];
00428       PR_snprintf(cbuf, sizeof(cbuf), "%d", selection);
00429       value.AssignASCII(cbuf);
00430       PRBool notify = PR_FALSE; // don't yet notify the document
00431       mContent->SetAttr(kNameSpaceID_None, nsMathMLAtoms::selection_, value, notify);
00432 
00433       // Now trigger a content-changed reflow...
00434       ReflowDirtyChild(mPresContext->PresShell(), mSelectedFrame);
00435     }
00436   }
00437   else if (NS_MATHML_ACTION_TYPE_RESTYLE == mActionType) {
00438     if (!mRestyle.IsEmpty()) {
00439       nsCOMPtr<nsIDOMElement> node( do_QueryInterface(mContent) );
00440       if (node.get()) {
00441         if (NS_CONTENT_ATTR_HAS_VALUE == mContent->GetAttr(kNameSpaceID_None, 
00442                          nsMathMLAtoms::actiontype_, value))
00443           node->RemoveAttribute(NS_LITERAL_STRING("actiontype"));
00444         else
00445           node->SetAttribute(NS_LITERAL_STRING("actiontype"), mRestyle);
00446 
00447         // At this stage, our style sub-tree has been re-resolved
00448         mWasRestyled = PR_TRUE;
00449 
00450         // Cancel the reflow command that the change of attribute has
00451         // caused, and post a style changed reflow request that is instead
00452         // targeted at our selected frame
00453         nsIPresShell *presShell = mPresContext->PresShell();
00454         presShell->CancelReflowCommand(this, nsnull);
00455         presShell->AppendReflowCommand(mSelectedFrame,
00456                                    eReflowType_StyleChanged,
00457                                    nsnull);
00458       }
00459     }
00460   }
00461   return NS_OK;
00462 }