This source file includes following definitions.
- m_isInHoverState
- create
- hostInput
- updateImage
- customStyleForRenderer
- createRenderer
- attach
- imageForNormalState
- imageForHoverState
- defaultEventHandler
- willRespondToMouseMoveEvents
- willRespondToMouseClickEvents
#include "config.h"
#include "core/html/shadow/PasswordGeneratorButtonElement.h"
#include "core/dom/NodeRenderStyle.h"
#include "core/events/Event.h"
#include "core/fetch/ImageResource.h"
#include "core/frame/FrameHost.h"
#include "core/html/HTMLInputElement.h"
#include "core/html/shadow/ShadowElementNames.h"
#include "core/page/Chrome.h"
#include "core/page/ChromeClient.h"
#include "core/rendering/RenderImage.h"
#include "platform/graphics/Image.h"
namespace WebCore {
using namespace HTMLNames;
PasswordGeneratorButtonElement::PasswordGeneratorButtonElement(Document& document)
: HTMLDivElement(document)
, m_isInHoverState(false)
{
setHasCustomStyleCallbacks();
}
PassRefPtr<PasswordGeneratorButtonElement> PasswordGeneratorButtonElement::create(Document& document)
{
RefPtr<PasswordGeneratorButtonElement> element = adoptRef(new PasswordGeneratorButtonElement(document));
element->setAttribute(idAttr, ShadowElementNames::passwordGenerator());
return element.release();
}
inline HTMLInputElement* PasswordGeneratorButtonElement::hostInput()
{
return toHTMLInputElement(shadowHost());
}
void PasswordGeneratorButtonElement::updateImage()
{
if (!renderer() || !renderer()->isImage())
return;
RenderImageResource* resource = toRenderImage(renderer())->imageResource();
ImageResource* image = m_isInHoverState ? imageForHoverState() : imageForNormalState();
ASSERT(image);
resource->setImageResource(image);
}
PassRefPtr<RenderStyle> PasswordGeneratorButtonElement::customStyleForRenderer()
{
RefPtr<RenderStyle> originalStyle = originalStyleForRenderer();
RefPtr<RenderStyle> style = RenderStyle::clone(originalStyle.get());
RenderStyle* inputStyle = hostInput()->renderStyle();
ASSERT(inputStyle);
style->setWidth(Length(inputStyle->fontSize(), Fixed));
style->setHeight(Length(inputStyle->fontSize(), Fixed));
style->setUnique();
updateImage();
return style.release();
}
RenderObject* PasswordGeneratorButtonElement::createRenderer(RenderStyle*)
{
RenderImage* image = new RenderImage(this);
image->setImageResource(RenderImageResource::create());
return image;
}
void PasswordGeneratorButtonElement::attach(const AttachContext& context)
{
HTMLDivElement::attach(context);
updateImage();
}
ImageResource* PasswordGeneratorButtonElement::imageForNormalState()
{
if (!m_cachedImageForNormalState) {
RefPtr<Image> image = Image::loadPlatformResource("generatePassword");
m_cachedImageForNormalState = new ImageResource(image.get());
}
return m_cachedImageForNormalState.get();
}
ImageResource* PasswordGeneratorButtonElement::imageForHoverState()
{
if (!m_cachedImageForHoverState) {
RefPtr<Image> image = Image::loadPlatformResource("generatePasswordHover");
m_cachedImageForHoverState = new ImageResource(image.get());
}
return m_cachedImageForHoverState.get();
}
void PasswordGeneratorButtonElement::defaultEventHandler(Event* event)
{
ASSERT(document().isActive());
RefPtr<HTMLInputElement> input = hostInput();
if (!input || input->isDisabledOrReadOnly() || !event->isMouseEvent()) {
if (!event->defaultHandled())
HTMLDivElement::defaultEventHandler(event);
return;
}
RefPtr<PasswordGeneratorButtonElement> protector(this);
if (event->type() == EventTypeNames::click) {
document().frameHost()->chrome().client().openPasswordGenerator(input.get());
event->setDefaultHandled();
}
if (event->type() == EventTypeNames::mouseover) {
m_isInHoverState = true;
updateImage();
}
if (event->type() == EventTypeNames::mouseout) {
m_isInHoverState = false;
updateImage();
}
if (!event->defaultHandled())
HTMLDivElement::defaultEventHandler(event);
}
bool PasswordGeneratorButtonElement::willRespondToMouseMoveEvents()
{
const HTMLInputElement* input = hostInput();
if (!input->isDisabledOrReadOnly())
return true;
return HTMLDivElement::willRespondToMouseMoveEvents();
}
bool PasswordGeneratorButtonElement::willRespondToMouseClickEvents()
{
const HTMLInputElement* input = hostInput();
if (!input->isDisabledOrReadOnly())
return true;
return HTMLDivElement::willRespondToMouseClickEvents();
}
}