Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsMathMLmactionFrame.h" |
michael@0 | 7 | #include "nsCOMPtr.h" |
michael@0 | 8 | #include "nsPresContext.h" |
michael@0 | 9 | #include "nsNameSpaceManager.h" |
michael@0 | 10 | #include "prprf.h" // For PR_snprintf() |
michael@0 | 11 | #include "nsIDocShell.h" |
michael@0 | 12 | #include "nsIDocShellTreeOwner.h" |
michael@0 | 13 | #include "nsIWebBrowserChrome.h" |
michael@0 | 14 | #include "nsIInterfaceRequestorUtils.h" |
michael@0 | 15 | #include "nsTextFragment.h" |
michael@0 | 16 | #include "nsIDOMEvent.h" |
michael@0 | 17 | #include "mozilla/gfx/2D.h" |
michael@0 | 18 | |
michael@0 | 19 | // |
michael@0 | 20 | // <maction> -- bind actions to a subexpression - implementation |
michael@0 | 21 | // |
michael@0 | 22 | |
michael@0 | 23 | enum nsMactionActionTypes { |
michael@0 | 24 | NS_MATHML_ACTION_TYPE_CLASS_ERROR = 0x10, |
michael@0 | 25 | NS_MATHML_ACTION_TYPE_CLASS_USE_SELECTION = 0x20, |
michael@0 | 26 | NS_MATHML_ACTION_TYPE_CLASS_IGNORE_SELECTION = 0x40, |
michael@0 | 27 | NS_MATHML_ACTION_TYPE_CLASS_BITMASK = 0xF0, |
michael@0 | 28 | |
michael@0 | 29 | NS_MATHML_ACTION_TYPE_NONE = NS_MATHML_ACTION_TYPE_CLASS_ERROR|0x01, |
michael@0 | 30 | |
michael@0 | 31 | NS_MATHML_ACTION_TYPE_TOGGLE = NS_MATHML_ACTION_TYPE_CLASS_USE_SELECTION|0x01, |
michael@0 | 32 | NS_MATHML_ACTION_TYPE_UNKNOWN = NS_MATHML_ACTION_TYPE_CLASS_USE_SELECTION|0x02, |
michael@0 | 33 | |
michael@0 | 34 | NS_MATHML_ACTION_TYPE_STATUSLINE = NS_MATHML_ACTION_TYPE_CLASS_IGNORE_SELECTION|0x01, |
michael@0 | 35 | NS_MATHML_ACTION_TYPE_TOOLTIP = NS_MATHML_ACTION_TYPE_CLASS_IGNORE_SELECTION|0x02 |
michael@0 | 36 | }; |
michael@0 | 37 | |
michael@0 | 38 | |
michael@0 | 39 | // helper function to parse actiontype attribute |
michael@0 | 40 | static int32_t |
michael@0 | 41 | GetActionType(nsIContent* aContent) |
michael@0 | 42 | { |
michael@0 | 43 | nsAutoString value; |
michael@0 | 44 | |
michael@0 | 45 | if (aContent) { |
michael@0 | 46 | if (!aContent->GetAttr(kNameSpaceID_None, nsGkAtoms::actiontype_, value)) |
michael@0 | 47 | return NS_MATHML_ACTION_TYPE_NONE; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | if (value.EqualsLiteral("toggle")) |
michael@0 | 51 | return NS_MATHML_ACTION_TYPE_TOGGLE; |
michael@0 | 52 | if (value.EqualsLiteral("statusline")) |
michael@0 | 53 | return NS_MATHML_ACTION_TYPE_STATUSLINE; |
michael@0 | 54 | if (value.EqualsLiteral("tooltip")) |
michael@0 | 55 | return NS_MATHML_ACTION_TYPE_TOOLTIP; |
michael@0 | 56 | |
michael@0 | 57 | return NS_MATHML_ACTION_TYPE_UNKNOWN; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | nsIFrame* |
michael@0 | 61 | NS_NewMathMLmactionFrame(nsIPresShell* aPresShell, nsStyleContext* aContext) |
michael@0 | 62 | { |
michael@0 | 63 | return new (aPresShell) nsMathMLmactionFrame(aContext); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | NS_IMPL_FRAMEARENA_HELPERS(nsMathMLmactionFrame) |
michael@0 | 67 | |
michael@0 | 68 | nsMathMLmactionFrame::~nsMathMLmactionFrame() |
michael@0 | 69 | { |
michael@0 | 70 | // unregister us as a mouse event listener ... |
michael@0 | 71 | // printf("maction:%p unregistering as mouse event listener ...\n", this); |
michael@0 | 72 | if (mListener) { |
michael@0 | 73 | mContent->RemoveSystemEventListener(NS_LITERAL_STRING("click"), mListener, |
michael@0 | 74 | false); |
michael@0 | 75 | mContent->RemoveSystemEventListener(NS_LITERAL_STRING("mouseover"), mListener, |
michael@0 | 76 | false); |
michael@0 | 77 | mContent->RemoveSystemEventListener(NS_LITERAL_STRING("mouseout"), mListener, |
michael@0 | 78 | false); |
michael@0 | 79 | } |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | void |
michael@0 | 83 | nsMathMLmactionFrame::Init(nsIContent* aContent, |
michael@0 | 84 | nsIFrame* aParent, |
michael@0 | 85 | nsIFrame* aPrevInFlow) |
michael@0 | 86 | { |
michael@0 | 87 | // Init our local attributes |
michael@0 | 88 | |
michael@0 | 89 | mChildCount = -1; // these will be updated in GetSelectedFrame() |
michael@0 | 90 | mActionType = GetActionType(aContent); |
michael@0 | 91 | |
michael@0 | 92 | // Let the base class do the rest |
michael@0 | 93 | return nsMathMLSelectedFrame::Init(aContent, aParent, aPrevInFlow); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | nsresult |
michael@0 | 97 | nsMathMLmactionFrame::ChildListChanged(int32_t aModType) |
michael@0 | 98 | { |
michael@0 | 99 | // update cached values |
michael@0 | 100 | mChildCount = -1; |
michael@0 | 101 | mSelectedFrame = nullptr; |
michael@0 | 102 | |
michael@0 | 103 | return nsMathMLSelectedFrame::ChildListChanged(aModType); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | // return the frame whose number is given by the attribute selection="number" |
michael@0 | 107 | nsIFrame* |
michael@0 | 108 | nsMathMLmactionFrame::GetSelectedFrame() |
michael@0 | 109 | { |
michael@0 | 110 | nsAutoString value; |
michael@0 | 111 | int32_t selection; |
michael@0 | 112 | |
michael@0 | 113 | if ((mActionType & NS_MATHML_ACTION_TYPE_CLASS_BITMASK) == |
michael@0 | 114 | NS_MATHML_ACTION_TYPE_CLASS_ERROR) { |
michael@0 | 115 | mSelection = -1; |
michael@0 | 116 | mInvalidMarkup = true; |
michael@0 | 117 | mSelectedFrame = nullptr; |
michael@0 | 118 | return mSelectedFrame; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | // Selection is not applied to tooltip and statusline. |
michael@0 | 122 | // Thereby return the first child. |
michael@0 | 123 | if ((mActionType & NS_MATHML_ACTION_TYPE_CLASS_BITMASK) == |
michael@0 | 124 | NS_MATHML_ACTION_TYPE_CLASS_IGNORE_SELECTION) { |
michael@0 | 125 | // We don't touch mChildCount here. It's incorrect to assign it 1, |
michael@0 | 126 | // and it's inefficient to count the children. It's fine to leave |
michael@0 | 127 | // it be equal -1 because it's not used with other actiontypes. |
michael@0 | 128 | mSelection = 1; |
michael@0 | 129 | mInvalidMarkup = false; |
michael@0 | 130 | mSelectedFrame = mFrames.FirstChild(); |
michael@0 | 131 | return mSelectedFrame; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::selection_, value); |
michael@0 | 135 | if (!value.IsEmpty()) { |
michael@0 | 136 | nsresult errorCode; |
michael@0 | 137 | selection = value.ToInteger(&errorCode); |
michael@0 | 138 | if (NS_FAILED(errorCode)) |
michael@0 | 139 | selection = 1; |
michael@0 | 140 | } |
michael@0 | 141 | else selection = 1; // default is first frame |
michael@0 | 142 | |
michael@0 | 143 | if (-1 != mChildCount) { // we have been in this function before... |
michael@0 | 144 | // cater for invalid user-supplied selection |
michael@0 | 145 | if (selection > mChildCount || selection < 1) |
michael@0 | 146 | selection = -1; |
michael@0 | 147 | // quick return if it is identical with our cache |
michael@0 | 148 | if (selection == mSelection) |
michael@0 | 149 | return mSelectedFrame; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | // get the selected child and cache new values... |
michael@0 | 153 | int32_t count = 0; |
michael@0 | 154 | nsIFrame* childFrame = mFrames.FirstChild(); |
michael@0 | 155 | while (childFrame) { |
michael@0 | 156 | if (!mSelectedFrame) |
michael@0 | 157 | mSelectedFrame = childFrame; // default is first child |
michael@0 | 158 | if (++count == selection) |
michael@0 | 159 | mSelectedFrame = childFrame; |
michael@0 | 160 | |
michael@0 | 161 | childFrame = childFrame->GetNextSibling(); |
michael@0 | 162 | } |
michael@0 | 163 | // cater for invalid user-supplied selection |
michael@0 | 164 | if (selection > count || selection < 1) |
michael@0 | 165 | selection = -1; |
michael@0 | 166 | |
michael@0 | 167 | mChildCount = count; |
michael@0 | 168 | mSelection = selection; |
michael@0 | 169 | mInvalidMarkup = (mSelection == -1); |
michael@0 | 170 | TransmitAutomaticData(); |
michael@0 | 171 | |
michael@0 | 172 | return mSelectedFrame; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | nsresult |
michael@0 | 176 | nsMathMLmactionFrame::SetInitialChildList(ChildListID aListID, |
michael@0 | 177 | nsFrameList& aChildList) |
michael@0 | 178 | { |
michael@0 | 179 | nsresult rv = nsMathMLSelectedFrame::SetInitialChildList(aListID, aChildList); |
michael@0 | 180 | |
michael@0 | 181 | if (!mSelectedFrame) { |
michael@0 | 182 | mActionType = NS_MATHML_ACTION_TYPE_NONE; |
michael@0 | 183 | } |
michael@0 | 184 | else { |
michael@0 | 185 | // create mouse event listener and register it |
michael@0 | 186 | mListener = new nsMathMLmactionFrame::MouseListener(this); |
michael@0 | 187 | // printf("maction:%p registering as mouse event listener ...\n", this); |
michael@0 | 188 | mContent->AddSystemEventListener(NS_LITERAL_STRING("click"), mListener, |
michael@0 | 189 | false, false); |
michael@0 | 190 | mContent->AddSystemEventListener(NS_LITERAL_STRING("mouseover"), mListener, |
michael@0 | 191 | false, false); |
michael@0 | 192 | mContent->AddSystemEventListener(NS_LITERAL_STRING("mouseout"), mListener, |
michael@0 | 193 | false, false); |
michael@0 | 194 | } |
michael@0 | 195 | return rv; |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | nsresult |
michael@0 | 199 | nsMathMLmactionFrame::AttributeChanged(int32_t aNameSpaceID, |
michael@0 | 200 | nsIAtom* aAttribute, |
michael@0 | 201 | int32_t aModType) |
michael@0 | 202 | { |
michael@0 | 203 | bool needsReflow = false; |
michael@0 | 204 | |
michael@0 | 205 | if (aAttribute == nsGkAtoms::actiontype_) { |
michael@0 | 206 | // updating mActionType ... |
michael@0 | 207 | int32_t oldActionType = mActionType; |
michael@0 | 208 | mActionType = GetActionType(mContent); |
michael@0 | 209 | |
michael@0 | 210 | // Initiate a reflow when actiontype classes are different. |
michael@0 | 211 | if ((oldActionType & NS_MATHML_ACTION_TYPE_CLASS_BITMASK) != |
michael@0 | 212 | (mActionType & NS_MATHML_ACTION_TYPE_CLASS_BITMASK)) { |
michael@0 | 213 | needsReflow = true; |
michael@0 | 214 | } |
michael@0 | 215 | } else if (aAttribute == nsGkAtoms::selection_) { |
michael@0 | 216 | if ((mActionType & NS_MATHML_ACTION_TYPE_CLASS_BITMASK) == |
michael@0 | 217 | NS_MATHML_ACTION_TYPE_CLASS_USE_SELECTION) { |
michael@0 | 218 | needsReflow = true; |
michael@0 | 219 | } |
michael@0 | 220 | } else { |
michael@0 | 221 | // let the base class handle other attribute changes |
michael@0 | 222 | return |
michael@0 | 223 | nsMathMLContainerFrame::AttributeChanged(aNameSpaceID, |
michael@0 | 224 | aAttribute, aModType); |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | if (needsReflow) { |
michael@0 | 228 | PresContext()->PresShell()-> |
michael@0 | 229 | FrameNeedsReflow(this, nsIPresShell::eTreeChange, NS_FRAME_IS_DIRTY); |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | return NS_OK; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | // ################################################################ |
michael@0 | 236 | // Event handlers |
michael@0 | 237 | // ################################################################ |
michael@0 | 238 | |
michael@0 | 239 | NS_IMPL_ISUPPORTS(nsMathMLmactionFrame::MouseListener, |
michael@0 | 240 | nsIDOMEventListener) |
michael@0 | 241 | |
michael@0 | 242 | |
michael@0 | 243 | // helper to show a msg on the status bar |
michael@0 | 244 | // curled from nsObjectFrame.cpp ... |
michael@0 | 245 | void |
michael@0 | 246 | ShowStatus(nsPresContext* aPresContext, nsString& aStatusMsg) |
michael@0 | 247 | { |
michael@0 | 248 | nsCOMPtr<nsIDocShellTreeItem> docShellItem(aPresContext->GetDocShell()); |
michael@0 | 249 | if (docShellItem) { |
michael@0 | 250 | nsCOMPtr<nsIDocShellTreeOwner> treeOwner; |
michael@0 | 251 | docShellItem->GetTreeOwner(getter_AddRefs(treeOwner)); |
michael@0 | 252 | if (treeOwner) { |
michael@0 | 253 | nsCOMPtr<nsIWebBrowserChrome> browserChrome(do_GetInterface(treeOwner)); |
michael@0 | 254 | if (browserChrome) { |
michael@0 | 255 | browserChrome->SetStatus(nsIWebBrowserChrome::STATUS_LINK, aStatusMsg.get()); |
michael@0 | 256 | } |
michael@0 | 257 | } |
michael@0 | 258 | } |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | NS_IMETHODIMP |
michael@0 | 262 | nsMathMLmactionFrame::MouseListener::HandleEvent(nsIDOMEvent* aEvent) |
michael@0 | 263 | { |
michael@0 | 264 | nsAutoString eventType; |
michael@0 | 265 | aEvent->GetType(eventType); |
michael@0 | 266 | if (eventType.EqualsLiteral("mouseover")) { |
michael@0 | 267 | mOwner->MouseOver(); |
michael@0 | 268 | } |
michael@0 | 269 | else if (eventType.EqualsLiteral("click")) { |
michael@0 | 270 | mOwner->MouseClick(); |
michael@0 | 271 | } |
michael@0 | 272 | else if (eventType.EqualsLiteral("mouseout")) { |
michael@0 | 273 | mOwner->MouseOut(); |
michael@0 | 274 | } |
michael@0 | 275 | else { |
michael@0 | 276 | NS_ABORT(); |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | return NS_OK; |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | void |
michael@0 | 283 | nsMathMLmactionFrame::MouseOver() |
michael@0 | 284 | { |
michael@0 | 285 | // see if we should display a status message |
michael@0 | 286 | if (NS_MATHML_ACTION_TYPE_STATUSLINE == mActionType) { |
michael@0 | 287 | // retrieve content from a second child if it exists |
michael@0 | 288 | nsIFrame* childFrame = mFrames.FrameAt(1); |
michael@0 | 289 | if (!childFrame) return; |
michael@0 | 290 | |
michael@0 | 291 | nsIContent* content = childFrame->GetContent(); |
michael@0 | 292 | if (!content) return; |
michael@0 | 293 | |
michael@0 | 294 | // check whether the content is mtext or not |
michael@0 | 295 | if (content->GetNameSpaceID() == kNameSpaceID_MathML && |
michael@0 | 296 | content->Tag() == nsGkAtoms::mtext_) { |
michael@0 | 297 | // get the text to be displayed |
michael@0 | 298 | content = content->GetFirstChild(); |
michael@0 | 299 | if (!content) return; |
michael@0 | 300 | |
michael@0 | 301 | const nsTextFragment* textFrg = content->GetText(); |
michael@0 | 302 | if (!textFrg) return; |
michael@0 | 303 | |
michael@0 | 304 | nsAutoString text; |
michael@0 | 305 | textFrg->AppendTo(text); |
michael@0 | 306 | // collapse whitespaces as listed in REC, section 3.2.6.1 |
michael@0 | 307 | text.CompressWhitespace(); |
michael@0 | 308 | ShowStatus(PresContext(), text); |
michael@0 | 309 | } |
michael@0 | 310 | } |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | void |
michael@0 | 314 | nsMathMLmactionFrame::MouseOut() |
michael@0 | 315 | { |
michael@0 | 316 | // see if we should remove the status message |
michael@0 | 317 | if (NS_MATHML_ACTION_TYPE_STATUSLINE == mActionType) { |
michael@0 | 318 | nsAutoString value; |
michael@0 | 319 | value.SetLength(0); |
michael@0 | 320 | ShowStatus(PresContext(), value); |
michael@0 | 321 | } |
michael@0 | 322 | } |
michael@0 | 323 | |
michael@0 | 324 | void |
michael@0 | 325 | nsMathMLmactionFrame::MouseClick() |
michael@0 | 326 | { |
michael@0 | 327 | if (NS_MATHML_ACTION_TYPE_TOGGLE == mActionType) { |
michael@0 | 328 | if (mChildCount > 1) { |
michael@0 | 329 | int32_t selection = (mSelection == mChildCount)? 1 : mSelection + 1; |
michael@0 | 330 | nsAutoString value; |
michael@0 | 331 | char cbuf[10]; |
michael@0 | 332 | PR_snprintf(cbuf, sizeof(cbuf), "%d", selection); |
michael@0 | 333 | value.AssignASCII(cbuf); |
michael@0 | 334 | bool notify = false; // don't yet notify the document |
michael@0 | 335 | mContent->SetAttr(kNameSpaceID_None, nsGkAtoms::selection_, value, notify); |
michael@0 | 336 | |
michael@0 | 337 | // Now trigger a content-changed reflow... |
michael@0 | 338 | PresContext()->PresShell()-> |
michael@0 | 339 | FrameNeedsReflow(mSelectedFrame, nsIPresShell::eTreeChange, |
michael@0 | 340 | NS_FRAME_IS_DIRTY); |
michael@0 | 341 | } |
michael@0 | 342 | } |
michael@0 | 343 | } |