Home | History | Annotate | Download | only in dom
      1 /*
      2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
      3  * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http//www.torchmobile.com/)
      4  *
      5  * This library is free software; you can redistribute it and/or
      6  * modify it under the terms of the GNU Library General Public
      7  * License as published by the Free Software Foundation; either
      8  * version 2 of the License, or (at your option) any later version.
      9  *
     10  * This library is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13  * Library General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU Library General Public License
     16  * along with this library; see the file COPYING.LIB.  If not, write to
     17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18  * Boston, MA 02110-1301, USA.
     19  *
     20  */
     21 
     22 #ifndef SelectElement_h
     23 #define SelectElement_h
     24 
     25 #include "Event.h"
     26 #include <wtf/Forward.h>
     27 #include <wtf/Vector.h>
     28 
     29 namespace WebCore {
     30 
     31 class Attribute;
     32 class Element;
     33 class Event;
     34 class FormDataList;
     35 class HTMLFormElement;
     36 class KeyboardEvent;
     37 class SelectElementData;
     38 
     39 class SelectElement {
     40 public:
     41     virtual bool multiple() const = 0;
     42 
     43     virtual int size() const = 0;
     44     virtual const Vector<Element*>& listItems() const = 0;
     45 
     46     virtual void listBoxOnChange() = 0;
     47     virtual void updateListBoxSelection(bool deselectOtherOptions) = 0;
     48 
     49     virtual void menuListOnChange() = 0;
     50 
     51     virtual int activeSelectionStartListIndex() const = 0;
     52     virtual int activeSelectionEndListIndex() const = 0;
     53 
     54     virtual void setActiveSelectionAnchorIndex(int index) = 0;
     55     virtual void setActiveSelectionEndIndex(int index) = 0;
     56 
     57     virtual int listToOptionIndex(int listIndex) const = 0;
     58     virtual int optionToListIndex(int optionIndex) const = 0;
     59 
     60     virtual int selectedIndex() const = 0;
     61     virtual void setSelectedIndex(int index, bool deselect = true) = 0;
     62     virtual void setSelectedIndexByUser(int index, bool deselect = true, bool fireOnChangeNow = false, bool allowMultipleSelection = false) = 0;
     63 
     64     virtual void listBoxSelectItem(int listIndex, bool allowMultiplySelections, bool shift, bool fireOnChangeNow = true) = 0;
     65 
     66     virtual void updateValidity() = 0;
     67 
     68 protected:
     69     virtual ~SelectElement() { }
     70 
     71     friend class SelectElementData;
     72 
     73     static void selectAll(SelectElementData&, Element*);
     74     static void saveLastSelection(SelectElementData&, Element*);
     75     static void setActiveSelectionAnchorIndex(SelectElementData&, Element*, int index);
     76     static void setActiveSelectionEndIndex(SelectElementData&, int index);
     77     static void updateListBoxSelection(SelectElementData&, Element*, bool deselectOtherOptions);
     78     static void listBoxOnChange(SelectElementData&, Element*);
     79     static void menuListOnChange(SelectElementData&, Element*);
     80     static void scrollToSelection(SelectElementData&, Element*);
     81     static void setRecalcListItems(SelectElementData&, Element*);
     82     static void recalcListItems(SelectElementData&, const Element*, bool updateSelectedStates = true);
     83     static int selectedIndex(const SelectElementData&, const Element*);
     84     static void setSelectedIndex(SelectElementData&, Element*, int optionIndex, bool deselect = true, bool fireOnChangeNow = false, bool userDrivenChange = true);
     85     static int optionToListIndex(const SelectElementData&, const Element*, int optionIndex);
     86     static int listToOptionIndex(const SelectElementData&, const Element*, int listIndex);
     87     static void dispatchFocusEvent(SelectElementData&, Element*);
     88     static void dispatchBlurEvent(SelectElementData&, Element*);
     89     static void deselectItems(SelectElementData&, Element*, Element* excludeElement = 0);
     90     static bool saveFormControlState(const SelectElementData&, const Element*, String& state);
     91     static void restoreFormControlState(SelectElementData&, Element*, const String& state);
     92     static void parseMultipleAttribute(SelectElementData&, Element*, Attribute*);
     93     static bool appendFormData(SelectElementData&, Element*, FormDataList&);
     94     static void reset(SelectElementData&, Element*);
     95     static void defaultEventHandler(SelectElementData&, Element*, Event*, HTMLFormElement*);
     96     static int lastSelectedListIndex(const SelectElementData&, const Element*);
     97     static void typeAheadFind(SelectElementData&, Element*, KeyboardEvent*);
     98     static void insertedIntoTree(SelectElementData&, Element*);
     99     static void accessKeySetSelectedIndex(SelectElementData&, Element*, int index);
    100     static unsigned optionCount(const SelectElementData&, const Element*);
    101 
    102     static void updateSelectedState(SelectElementData& data, Element* element, int listIndex,
    103                                     bool multi, bool shift);
    104 
    105 private:
    106     static void menuListDefaultEventHandler(SelectElementData&, Element*, Event*, HTMLFormElement*);
    107     static void listBoxDefaultEventHandler(SelectElementData&, Element*, Event*, HTMLFormElement*);
    108     static void setOptionsChangedOnRenderer(SelectElementData&, Element*);
    109 };
    110 
    111 // HTML/WMLSelectElement hold this struct as member variable
    112 // and pass it to the static helper functions in SelectElement
    113 class SelectElementData {
    114 public:
    115     SelectElementData();
    116     ~SelectElementData();
    117 
    118     bool multiple() const { return m_multiple; }
    119     void setMultiple(bool value) { m_multiple = value; }
    120 
    121     int size() const { return m_size; }
    122     void setSize(int value) { m_size = value; }
    123 
    124     bool usesMenuList() const
    125     {
    126 #if ENABLE(NO_LISTBOX_RENDERING)
    127         return true;
    128 #else
    129         return !m_multiple && m_size <= 1;
    130 #endif
    131     }
    132 
    133     int lastOnChangeIndex() const { return m_lastOnChangeIndex; }
    134     void setLastOnChangeIndex(int value) { m_lastOnChangeIndex = value; }
    135 
    136     bool userDrivenChange() const { return m_userDrivenChange; }
    137     void setUserDrivenChange(bool value) { m_userDrivenChange = value; }
    138 
    139     Vector<bool>& lastOnChangeSelection() { return m_lastOnChangeSelection; }
    140 
    141     bool activeSelectionState() const { return m_activeSelectionState; }
    142     void setActiveSelectionState(bool value) { m_activeSelectionState = value; }
    143 
    144     int activeSelectionAnchorIndex() const { return m_activeSelectionAnchorIndex; }
    145     void setActiveSelectionAnchorIndex(int value) { m_activeSelectionAnchorIndex = value; }
    146 
    147     int activeSelectionEndIndex() const { return m_activeSelectionEndIndex; }
    148     void setActiveSelectionEndIndex(int value) { m_activeSelectionEndIndex = value; }
    149 
    150     Vector<bool>& cachedStateForActiveSelection() { return m_cachedStateForActiveSelection; }
    151 
    152     bool shouldRecalcListItems() const { return m_recalcListItems; }
    153     void setShouldRecalcListItems(bool value) { m_recalcListItems = value; }
    154 
    155     Vector<Element*>& rawListItems() { return m_listItems; }
    156     Vector<Element*>& listItems(const Element*);
    157     const Vector<Element*>& listItems(const Element*) const;
    158 
    159     UChar repeatingChar() const { return m_repeatingChar; }
    160     void setRepeatingChar(const UChar& value) { m_repeatingChar = value; }
    161 
    162     DOMTimeStamp lastCharTime() const { return m_lastCharTime; }
    163     void setLastCharTime(const DOMTimeStamp& value) { m_lastCharTime = value; }
    164 
    165     String& typedString() { return m_typedString; }
    166     void setTypedString(const String& value) { m_typedString = value; }
    167 
    168 private:
    169     void checkListItems(const Element*) const;
    170 
    171     bool m_multiple;
    172     int m_size;
    173 
    174     int m_lastOnChangeIndex;
    175     Vector<bool> m_lastOnChangeSelection;
    176     bool m_userDrivenChange;
    177 
    178     bool m_activeSelectionState;
    179     int m_activeSelectionAnchorIndex;
    180     int m_activeSelectionEndIndex;
    181     Vector<bool> m_cachedStateForActiveSelection;
    182 
    183     bool m_recalcListItems;
    184     Vector<Element*> m_listItems;
    185 
    186     // Instance variables for type-ahead find
    187     UChar m_repeatingChar;
    188     DOMTimeStamp m_lastCharTime;
    189     String m_typedString;
    190 };
    191 
    192 SelectElement* toSelectElement(Element*);
    193 
    194 }
    195 
    196 #endif
    197