Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
      3  * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org)
      4  *           (C) 1999 Antti Koivisto (koivisto (at) kde.org)
      5  *           (C) 2000 Dirk Mueller (mueller (at) kde.org)
      6  * Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011 Apple Inc. All rights reserved.
      7  * Copyright (C) 2010 Google Inc. All rights reserved.
      8  *
      9  * This library is free software; you can redistribute it and/or
     10  * modify it under the terms of the GNU Library General Public
     11  * License as published by the Free Software Foundation; either
     12  * version 2 of the License, or (at your option) any later version.
     13  *
     14  * This library is distributed in the hope that it will be useful,
     15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     17  * Library General Public License for more details.
     18  *
     19  * You should have received a copy of the GNU Library General Public License
     20  * along with this library; see the file COPYING.LIB.  If not, write to
     21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     22  * Boston, MA 02110-1301, USA.
     23  *
     24  */
     25 
     26 #ifndef HTMLSelectElement_h
     27 #define HTMLSelectElement_h
     28 
     29 #include "core/dom/Event.h"
     30 #include "core/html/HTMLFormControlElementWithState.h"
     31 #include "core/html/HTMLOptionsCollection.h"
     32 #include "core/html/TypeAhead.h"
     33 #include "wtf/Vector.h"
     34 
     35 namespace WebCore {
     36 
     37 class ExceptionState;
     38 class HTMLOptionElement;
     39 
     40 class HTMLSelectElement FINAL : public HTMLFormControlElementWithState, public TypeAheadDataSource {
     41 public:
     42     static PassRefPtr<HTMLSelectElement> create(Document*);
     43     static PassRefPtr<HTMLSelectElement> create(const QualifiedName&, Document*, HTMLFormElement*, bool createdByParser);
     44 
     45     int selectedIndex() const;
     46     void setSelectedIndex(int);
     47 
     48     void optionSelectedByUser(int index, bool dispatchChangeEvent, bool allowMultipleSelection = false);
     49 
     50     // For ValidityState
     51     virtual String validationMessage() const OVERRIDE;
     52     virtual bool valueMissing() const OVERRIDE;
     53 
     54     virtual void reset() OVERRIDE;
     55 
     56     unsigned length() const;
     57 
     58     int size() const { return m_size; }
     59     bool multiple() const { return m_multiple; }
     60 
     61     bool usesMenuList() const;
     62 
     63     void add(HTMLElement*, HTMLElement* beforeElement, ExceptionState&);
     64     void remove(int index);
     65     void remove(HTMLOptionElement*);
     66 
     67     String value() const;
     68     void setValue(const String&);
     69 
     70     PassRefPtr<HTMLOptionsCollection> options();
     71     PassRefPtr<HTMLCollection> selectedOptions();
     72 
     73     void optionElementChildrenChanged();
     74 
     75     void setRecalcListItems();
     76     void invalidateSelectedItems();
     77     void updateListItemSelectedStates();
     78 
     79     const Vector<HTMLElement*>& listItems() const;
     80 
     81     virtual void accessKeyAction(bool sendMouseEvents);
     82     void accessKeySetSelectedIndex(int);
     83 
     84     void setMultiple(bool);
     85 
     86     void setSize(int);
     87 
     88     void setOption(unsigned index, HTMLOptionElement*, ExceptionState&);
     89     void setLength(unsigned, ExceptionState&);
     90 
     91     Node* namedItem(const AtomicString& name);
     92     Node* item(unsigned index);
     93 
     94     void scrollToSelection();
     95 
     96     void listBoxSelectItem(int listIndex, bool allowMultiplySelections, bool shift, bool fireOnChangeNow = true);
     97 
     98     bool canSelectAll() const;
     99     void selectAll();
    100     int listToOptionIndex(int listIndex) const;
    101     void listBoxOnChange();
    102     int optionToListIndex(int optionIndex) const;
    103     int activeSelectionStartListIndex() const;
    104     int activeSelectionEndListIndex() const;
    105     void setActiveSelectionAnchorIndex(int);
    106     void setActiveSelectionEndIndex(int);
    107     void updateListBoxSelection(bool deselectOtherOptions);
    108 
    109     // For use in the implementation of HTMLOptionElement.
    110     void optionSelectionStateChanged(HTMLOptionElement*, bool optionIsSelected);
    111     bool isParsingInProgress() const { return m_isParsingInProgress; }
    112     bool anonymousIndexedSetter(unsigned, PassRefPtr<HTMLOptionElement>, ExceptionState&);
    113     bool anonymousIndexedSetterRemove(unsigned, ExceptionState&);
    114 
    115 protected:
    116     HTMLSelectElement(const QualifiedName&, Document*, HTMLFormElement*, bool createdByParser);
    117 
    118 private:
    119     virtual const AtomicString& formControlType() const;
    120 
    121     virtual bool shouldShowFocusRingOnMouseFocus() const OVERRIDE;
    122 
    123     virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusDirection) OVERRIDE;
    124     virtual void dispatchBlurEvent(Element* newFocusedElemnet) OVERRIDE;
    125 
    126     virtual bool canStartSelection() const { return false; }
    127 
    128     virtual bool isEnumeratable() const { return true; }
    129     virtual bool supportLabels() const OVERRIDE { return true; }
    130 
    131     virtual FormControlState saveFormControlState() const OVERRIDE;
    132     virtual void restoreFormControlState(const FormControlState&) OVERRIDE;
    133 
    134     virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
    135     virtual bool isPresentationAttribute(const QualifiedName&) const OVERRIDE;
    136 
    137     virtual bool childShouldCreateRenderer(const NodeRenderingContext&) const OVERRIDE;
    138     virtual RenderObject* createRenderer(RenderStyle *);
    139     virtual bool appendFormData(FormDataList&, bool);
    140 
    141     virtual void defaultEventHandler(Event*);
    142 
    143     void dispatchChangeEventForMenuList();
    144 
    145     void recalcListItems(bool updateSelectedStates = true) const;
    146 
    147     void deselectItems(HTMLOptionElement* excludeElement = 0);
    148     void typeAheadFind(KeyboardEvent*);
    149     void saveLastSelection();
    150 
    151     virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
    152 
    153     virtual bool isOptionalFormControl() const { return !isRequiredFormControl(); }
    154     virtual bool isRequiredFormControl() const;
    155 
    156     bool hasPlaceholderLabelOption() const;
    157 
    158     enum SelectOptionFlag {
    159         DeselectOtherOptions = 1 << 0,
    160         DispatchChangeEvent = 1 << 1,
    161         UserDriven = 1 << 2,
    162     };
    163     typedef unsigned SelectOptionFlags;
    164     void selectOption(int optionIndex, SelectOptionFlags = 0);
    165     void deselectItemsWithoutValidation(HTMLElement* elementToExclude = 0);
    166     void parseMultipleAttribute(const AtomicString&);
    167     int lastSelectedListIndex() const;
    168     void updateSelectedState(int listIndex, bool multi, bool shift);
    169     void menuListDefaultEventHandler(Event*);
    170     bool platformHandleKeydownEvent(KeyboardEvent*);
    171     void listBoxDefaultEventHandler(Event*);
    172     void setOptionsChangedOnRenderer();
    173     size_t searchOptionsForValue(const String&, size_t listIndexStart, size_t listIndexEnd) const;
    174 
    175     enum SkipDirection {
    176         SkipBackwards = -1,
    177         SkipForwards = 1
    178     };
    179     int nextValidIndex(int listIndex, SkipDirection, int skip) const;
    180     int nextSelectableListIndex(int startIndex) const;
    181     int previousSelectableListIndex(int startIndex) const;
    182     int firstSelectableListIndex() const;
    183     int lastSelectableListIndex() const;
    184     int nextSelectableListIndexPageAway(int startIndex, SkipDirection) const;
    185 
    186     virtual void childrenChanged(bool changedByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0);
    187     virtual bool areAuthorShadowsAllowed() const OVERRIDE { return false; }
    188     virtual void finishParsingChildren() OVERRIDE;
    189 
    190     // TypeAheadDataSource functions.
    191     virtual int indexOfSelectedOption() const OVERRIDE;
    192     virtual int optionCount() const OVERRIDE;
    193     virtual String optionAtIndex(int index) const OVERRIDE;
    194 
    195     // m_listItems contains HTMLOptionElement, HTMLOptGroupElement, and HTMLHRElement objects.
    196     mutable Vector<HTMLElement*> m_listItems;
    197     Vector<bool> m_lastOnChangeSelection;
    198     Vector<bool> m_cachedStateForActiveSelection;
    199     TypeAhead m_typeAhead;
    200     int m_size;
    201     int m_lastOnChangeIndex;
    202     int m_activeSelectionAnchorIndex;
    203     int m_activeSelectionEndIndex;
    204     bool m_isProcessingUserDrivenChange;
    205     bool m_multiple;
    206     bool m_activeSelectionState;
    207     mutable bool m_shouldRecalcListItems;
    208     bool m_isParsingInProgress;
    209 };
    210 
    211 inline HTMLSelectElement* toHTMLSelectElement(Node* node)
    212 {
    213     ASSERT_WITH_SECURITY_IMPLICATION(!node || node->hasTagName(HTMLNames::selectTag));
    214     return static_cast<HTMLSelectElement*>(node);
    215 }
    216 
    217 void toHTMLSelectElement(const HTMLSelectElement*); // This overload will catch anyone doing an unnecessary cast.
    218 
    219 } // namespace
    220 
    221 #endif
    222