2013-07-10 02:25:27 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "nsColorControlFrame.h"
|
|
|
|
|
|
|
|
#include "nsContentCreatorFunctions.h"
|
|
|
|
#include "nsContentList.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsFormControlFrame.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsIDOMHTMLInputElement.h"
|
|
|
|
#include "nsIDOMNode.h"
|
2013-12-02 22:39:32 +04:00
|
|
|
#include "nsIFormControl.h"
|
2013-07-10 02:25:27 +04:00
|
|
|
#include "nsStyleSet.h"
|
|
|
|
|
2013-12-03 08:49:03 +04:00
|
|
|
using mozilla::dom::Element;
|
|
|
|
|
2013-07-10 02:25:27 +04:00
|
|
|
nsColorControlFrame::nsColorControlFrame(nsStyleContext* aContext):
|
2013-10-22 19:29:20 +04:00
|
|
|
nsColorControlFrameSuper(aContext)
|
2013-07-10 02:25:27 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame*
|
|
|
|
NS_NewColorControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
|
|
|
|
{
|
|
|
|
return new (aPresShell) nsColorControlFrame(aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsColorControlFrame)
|
|
|
|
|
|
|
|
NS_QUERYFRAME_HEAD(nsColorControlFrame)
|
2014-02-28 23:52:42 +04:00
|
|
|
NS_QUERYFRAME_ENTRY(nsColorControlFrame)
|
2013-07-10 02:25:27 +04:00
|
|
|
NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
|
2013-10-22 19:29:20 +04:00
|
|
|
NS_QUERYFRAME_TAIL_INHERITING(nsColorControlFrameSuper)
|
2013-07-10 02:25:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
void nsColorControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
|
|
|
|
{
|
|
|
|
nsFormControlFrame::RegUnRegAccessKey(static_cast<nsIFrame*>(this), false);
|
|
|
|
nsContentUtils::DestroyAnonymousContent(&mColorContent);
|
2013-10-22 19:29:20 +04:00
|
|
|
nsColorControlFrameSuper::DestroyFrom(aDestructRoot);
|
2013-07-10 02:25:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIAtom*
|
|
|
|
nsColorControlFrame::GetType() const
|
|
|
|
{
|
|
|
|
return nsGkAtoms::colorControlFrame;
|
|
|
|
}
|
|
|
|
|
2014-01-06 03:31:14 +04:00
|
|
|
#ifdef DEBUG_FRAME_DUMP
|
2014-02-18 11:47:48 +04:00
|
|
|
nsresult
|
2013-07-10 02:25:27 +04:00
|
|
|
nsColorControlFrame::GetFrameName(nsAString& aResult) const
|
|
|
|
{
|
|
|
|
return MakeFrameName(NS_LITERAL_STRING("ColorControl"), aResult);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Create the color area for the button.
|
|
|
|
// The frame will be generated by the frame constructor.
|
|
|
|
nsresult
|
|
|
|
nsColorControlFrame::CreateAnonymousContent(nsTArray<ContentInfo>& aElements)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocument> doc = mContent->GetCurrentDoc();
|
2013-12-03 08:49:03 +04:00
|
|
|
mColorContent = doc->CreateHTMLElement(nsGkAtoms::div);
|
2013-07-10 02:25:27 +04:00
|
|
|
|
|
|
|
// Mark the element to be native anonymous before setting any attributes.
|
|
|
|
mColorContent->SetIsNativeAnonymousRoot();
|
|
|
|
|
2013-12-03 08:49:03 +04:00
|
|
|
nsresult rv = UpdateColor();
|
2013-07-10 02:25:27 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCSSPseudoElements::Type pseudoType = nsCSSPseudoElements::ePseudo_mozColorSwatch;
|
|
|
|
nsRefPtr<nsStyleContext> newStyleContext = PresContext()->StyleSet()->
|
|
|
|
ResolvePseudoElementStyle(mContent->AsElement(), pseudoType,
|
|
|
|
StyleContext(), mColorContent->AsElement());
|
|
|
|
if (!aElements.AppendElement(ContentInfo(mColorContent, newStyleContext))) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsColorControlFrame::AppendAnonymousContentTo(nsBaseContentList& aElements,
|
|
|
|
uint32_t aFilter)
|
|
|
|
{
|
|
|
|
aElements.MaybeAppendElement(mColorContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsColorControlFrame::UpdateColor()
|
|
|
|
{
|
|
|
|
// Get the color from the "value" property of our content; it will return the
|
|
|
|
// default color (through the sanitization algorithm) if there is none.
|
|
|
|
nsAutoString color;
|
|
|
|
nsCOMPtr<nsIDOMHTMLInputElement> elt = do_QueryInterface(mContent);
|
|
|
|
elt->GetValue(color);
|
|
|
|
MOZ_ASSERT(!color.IsEmpty(),
|
|
|
|
"Content node's GetValue() should return a valid color string "
|
|
|
|
"(the default color, in case no valid color is set)");
|
|
|
|
|
|
|
|
// Set the background-color style property of the swatch element to this color
|
|
|
|
return mColorContent->SetAttr(kNameSpaceID_None, nsGkAtoms::style,
|
|
|
|
NS_LITERAL_STRING("background-color:") + color, true);
|
|
|
|
}
|
|
|
|
|
2014-02-18 11:47:48 +04:00
|
|
|
nsresult
|
2013-07-10 02:25:27 +04:00
|
|
|
nsColorControlFrame::AttributeChanged(int32_t aNameSpaceID,
|
|
|
|
nsIAtom* aAttribute,
|
|
|
|
int32_t aModType)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mColorContent, "The color div must exist");
|
|
|
|
|
2013-12-02 22:39:32 +04:00
|
|
|
// If the value attribute is set, update the color box, but only if we're
|
|
|
|
// still a color control, which might not be the case if the type attribute
|
|
|
|
// was removed/changed.
|
|
|
|
nsCOMPtr<nsIFormControl> fctrl = do_QueryInterface(GetContent());
|
|
|
|
if (fctrl->GetType() == NS_FORM_INPUT_COLOR &&
|
|
|
|
aNameSpaceID == kNameSpaceID_None && nsGkAtoms::value == aAttribute) {
|
2013-07-10 02:25:27 +04:00
|
|
|
UpdateColor();
|
|
|
|
}
|
2013-10-22 19:29:20 +04:00
|
|
|
return nsColorControlFrameSuper::AttributeChanged(aNameSpaceID, aAttribute,
|
|
|
|
aModType);
|
2013-07-10 02:25:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame*
|
|
|
|
nsColorControlFrame::GetContentInsertionFrame()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
2013-12-03 03:57:50 +04:00
|
|
|
|
2013-12-03 08:49:03 +04:00
|
|
|
Element*
|
|
|
|
nsColorControlFrame::GetPseudoElement(nsCSSPseudoElements::Type aType)
|
2013-12-03 03:57:50 +04:00
|
|
|
{
|
|
|
|
if (aType == nsCSSPseudoElements::ePseudo_mozColorSwatch) {
|
|
|
|
return mColorContent;
|
|
|
|
}
|
|
|
|
|
2013-12-03 08:49:03 +04:00
|
|
|
return nsContainerFrame::GetPseudoElement(aType);
|
2013-12-03 03:57:50 +04:00
|
|
|
}
|