/*
 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
 * Copyright (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de)
 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#ifndef Event_h
#define Event_h

#include "Clipboard.h"
#include "DOMTimeStamp.h"
#include "EventInit.h"
#include "EventInterfaces.h"
#include "ScriptWrappable.h"
#include <wtf/RefCounted.h>
#include <wtf/text/AtomicString.h>

namespace WebCore {

class EventPath;
class EventTarget;

enum EventInterface {

#define DOM_EVENT_INTERFACE_DECLARE(name) name##InterfaceType,
DOM_EVENT_INTERFACES_FOR_EACH(DOM_EVENT_INTERFACE_DECLARE)
#undef DOM_EVENT_INTERFACE_DECLARE

};

class Event : public ScriptWrappable, public RefCounted<Event> {
public:
	enum PhaseType {
		NONE                = 0,
		CAPTURING_PHASE     = 1,
		AT_TARGET           = 2,
		BUBBLING_PHASE      = 3
	};

	enum EventType {
		MOUSEDOWN           = 1,
		MOUSEUP             = 2,
		MOUSEOVER           = 4,
		MOUSEOUT            = 8,
		MOUSEMOVE           = 16,
		MOUSEDRAG           = 32,
		CLICK               = 64,
		DBLCLICK            = 128,
		KEYDOWN             = 256,
		KEYUP               = 512,
		KEYPRESS            = 1024,
		DRAGDROP            = 2048,
		FOCUS               = 4096,
		BLUR                = 8192,
		SELECT              = 16384,
		CHANGE              = 32768
	};

	static ALWAYS_INLINE PassRefPtr<Event> create()
	{
		return adoptRef(new Event);
	}
	static ALWAYS_INLINE PassRefPtr<Event> create(const AtomicString& type, bool canBubble, bool cancelable)
	{
		return adoptRef(new Event(type, canBubble, cancelable));
	}

	static ALWAYS_INLINE PassRefPtr<Event> create(const AtomicString& type, const EventInit& initializer)
	{
		return adoptRef(new Event(type, initializer));
	}

	virtual ~Event();

	void initEvent(const AtomicString& type, bool canBubble, bool cancelable);
    bool isInitialized() const { return m_isInitialized; }

	ALWAYS_INLINE const AtomicString& type() const { return m_type; }
	void setType(const AtomicString& type) { m_type = type; }

	ALWAYS_INLINE EventTarget* target() const { return m_target.get(); }
	void setTarget(PassRefPtr<EventTarget>);

	ALWAYS_INLINE EventTarget* currentTarget() const { return m_currentTarget; }
	void setCurrentTarget(EventTarget* currentTarget) { m_currentTarget = currentTarget; }

	ALWAYS_INLINE unsigned short eventPhase() const { return m_eventPhase; }
	void setEventPhase(unsigned short eventPhase) { m_eventPhase = eventPhase; }

	ALWAYS_INLINE bool bubbles() const { return m_canBubble; }
	ALWAYS_INLINE bool cancelable() const { return m_cancelable; }
    bool composed() const;

	ALWAYS_INLINE DOMTimeStamp timeStamp() const { return m_createTime; }

    void setEventPath(const EventPath& path) { m_eventPath = &path; }
    void clearEventPath() { m_eventPath = 0; }
    Vector<EventTarget*> composedPath() const;

	void stopPropagation() { m_propagationStopped = true; }
	void stopImmediatePropagation() { m_immediatePropagationStopped = true; }

    bool isTrusted() const { return m_isTrusted; }
    void setUntrusted() { m_isTrusted = false; }

	// IE Extensions
	ALWAYS_INLINE EventTarget* srcElement() const { return target(); } // MSIE extension - "the object that fired the event"

	ALWAYS_INLINE bool returnValue() const { return !defaultPrevented(); }
	void setReturnValue(bool returnValue) { setDefaultPrevented(!returnValue); }

    virtual EventInterface eventInterface() const;

	// These events are general classes of events.
	virtual bool isUIEvent() const;
	virtual bool isMouseEvent() const;
    virtual bool isFocusEvent() const;
	virtual bool isKeyboardEvent() const;
    virtual bool isCompositionEvent() const;
	virtual bool isTouchEvent() const;

#if ENABLE(CEHTML)
	virtual bool isKeyEvent() const;
#endif

	// Drag events are a subset of mouse events.
	virtual bool isDragEvent() const;

	// These events lack a DOM interface.
	virtual bool isClipboardEvent() const;
	virtual bool isBeforeTextInsertedEvent() const;

#if ENABLE(RMAI)
	virtual bool isMOMEvent() const;
	virtual bool isGalioConfigurationEvent() const;
	void clearState() {
		m_propagationStopped = false;
		m_immediatePropagationStopped = false;
		m_cancelBubble = false;
		m_defaultPrevented = false;
		m_defaultHandled = false;
	}
#endif

	ALWAYS_INLINE bool propagationStopped() const { return m_propagationStopped || m_immediatePropagationStopped; }
	ALWAYS_INLINE bool immediatePropagationStopped() const { return m_immediatePropagationStopped; }

    void resetPropagationFlags();

	ALWAYS_INLINE bool defaultPrevented() const { return m_defaultPrevented; }
	void preventDefault() { if (m_cancelable) m_defaultPrevented = true; }
	void setDefaultPrevented(bool defaultPrevented) { m_defaultPrevented = defaultPrevented; }

	ALWAYS_INLINE bool defaultHandled() const { return m_defaultHandled; }
	void setDefaultHandled() { m_defaultHandled = true; }

	ALWAYS_INLINE bool cancelBubble() const { return m_cancelBubble; }
	void setCancelBubble(bool cancel) { m_cancelBubble = cancel; }

	ALWAYS_INLINE Event* underlyingEvent() const { return m_underlyingEvent.get(); }
	void setUnderlyingEvent(PassRefPtr<Event>);

	virtual bool storesResultAsString() const;
	virtual void storeResult(const String&);

	ALWAYS_INLINE bool isBeingDispatched() const { return eventPhase(); }

    virtual EventTarget* relatedTarget() const { return 0; }

protected:
	Event();
	Event(const AtomicString& type, bool canBubble, bool cancelable);
	Event(const AtomicString& type, const EventInit&);

	virtual void receivedTarget();
	ALWAYS_INLINE bool dispatched() const { return m_target; }

private:
	AtomicString m_type;

    bool m_isInitialized;
	bool m_canBubble;
	bool m_cancelable;
    bool m_composed;

	bool m_propagationStopped;
	bool m_immediatePropagationStopped;
	bool m_defaultPrevented;
	bool m_defaultHandled;
	bool m_cancelBubble;
    bool m_isTrusted;

	unsigned short m_eventPhase;
	EventTarget* m_currentTarget;
    const EventPath* m_eventPath;
	RefPtr<EventTarget> m_target;
	DOMTimeStamp m_createTime;

	RefPtr<Event> m_underlyingEvent;
};

inline void Event::resetPropagationFlags()
{
    m_propagationStopped = false;
    m_immediatePropagationStopped = false;
}

} // namespace WebCore

#endif // Event_h
