2 * This file is part of the DOM implementation for KDE.
4 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
5 * (C) 1999 Antti Koivisto (koivisto@kde.org)
6 * (C) 2000 Dirk Mueller (mueller@kde.org)
7 * Copyright (C) 2004 Apple Computer, Inc.
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.
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.
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., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
25 #ifndef HTML_FORMIMPL_H
26 #define HTML_FORMIMPL_H
28 #include "html/html_elementimpl.h"
29 #include "dom/html_element.h"
31 #include <qptrvector.h>
32 #include <qmemarray.h>
40 class RenderFormElement;
44 class RenderRadioButton;
45 class RenderFileButton;
55 class HTMLFormElement;
56 class HTMLGenericFormElementImpl;
57 class HTMLImageElementImpl;
58 class HTMLImageLoader;
59 class HTMLOptionElementImpl;
60 class HTMLOptionsCollectionImpl;
62 // -------------------------------------------------------------------------
64 class HTMLFormElementImpl : public HTMLElementImpl
67 HTMLFormElementImpl(DocumentPtr *doc);
68 virtual ~HTMLFormElementImpl();
70 virtual Id id() const;
72 virtual void attach();
73 virtual void detach();
77 DOMString enctype() const { return m_enctype; }
78 void setEnctype( const DOMString & );
80 DOMString boundary() const { return m_boundary; }
81 void setBoundary( const DOMString & );
83 bool autoComplete() const { return m_autocomplete; }
85 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
87 void radioClicked( HTMLGenericFormElementImpl *caller );
89 void registerFormElement(HTMLGenericFormElementImpl *);
90 void removeFormElement(HTMLGenericFormElementImpl *);
91 void makeFormElementDormant(HTMLGenericFormElementImpl *);
92 void registerImgElement(HTMLImageElementImpl *);
93 void removeImgElement(HTMLImageElementImpl *);
96 void submit(bool activateSubmitButton);
99 void setMalformed(bool malformed) { m_malformed = malformed; }
100 virtual bool isMalformed() { return m_malformed; }
102 virtual bool isURLAttribute(AttributeImpl *attr) const;
106 bool formWouldHaveSecureSubmission(const DOMString &url);
109 static void i18nData();
111 friend class HTMLFormElement;
112 friend class HTMLFormCollectionImpl;
114 QPtrVector<HTMLGenericFormElementImpl> formElements;
115 QPtrVector<HTMLGenericFormElementImpl> dormantFormElements;
116 QPtrVector<HTMLImageElementImpl> imgElements;
120 DOMString m_boundary;
121 DOMString m_acceptcharset;
123 bool m_multipart : 1;
124 bool m_autocomplete : 1;
126 bool m_doingsubmit : 1;
128 bool m_malformed : 1;
131 bool formData(khtml::FormData &) const;
137 // -------------------------------------------------------------------------
139 class HTMLGenericFormElementImpl : public HTMLElementImpl
141 friend class HTMLFormElementImpl;
142 friend class khtml::RenderFormElement;
145 HTMLGenericFormElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
146 virtual ~HTMLGenericFormElementImpl();
148 HTMLFormElementImpl *form() { return m_form; }
150 virtual DOMString type() const = 0;
152 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
153 virtual void attach();
154 virtual void insertedIntoDocument();
155 virtual void removedFromDocument();
157 virtual void reset() {}
162 virtual bool disabled() const;
163 void setDisabled(bool _disabled);
165 virtual bool isFocusable() const;
166 virtual bool isKeyboardFocusable() const;
167 virtual bool isMouseFocusable() const;
168 virtual bool isEnumeratable() const { return false; }
170 bool readOnly() const { return m_readOnly; }
171 void setReadOnly(bool _readOnly) { m_readOnly = _readOnly; }
173 virtual void recalcStyle( StyleChange );
175 DOMString name() const;
176 void setName(const DOMString& name);
178 virtual bool isGenericFormElement() const { return true; }
181 * override in derived classes to get the encoded name=value pair
183 * return true for a successful control (see HTML4-17.13.2)
185 virtual bool appendFormData(FormDataList&, bool) { return false; }
187 virtual void defaultEventHandler(EventImpl *evt);
188 virtual bool isEditable();
190 virtual QString state();
191 QString findMatchingState(QStringList &states);
193 virtual bool isSuccessfulSubmitButton() const { return false; }
194 virtual bool isActivatedSubmit() const { return false; }
195 virtual void setActivatedSubmit(bool flag) { }
198 HTMLFormElementImpl *getForm() const;
200 DOMStringImpl* m_name;
201 HTMLFormElementImpl *m_form;
202 bool m_disabled, m_readOnly;
208 // -------------------------------------------------------------------------
210 class HTMLButtonElementImpl : public HTMLGenericFormElementImpl
213 HTMLButtonElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
215 virtual ~HTMLButtonElementImpl();
223 virtual Id id() const;
224 DOMString type() const;
226 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
227 virtual void defaultEventHandler(EventImpl *evt);
228 virtual bool appendFormData(FormDataList&, bool);
230 virtual bool isSuccessfulSubmitButton() const;
231 virtual bool isActivatedSubmit() const;
232 virtual void setActivatedSubmit(bool flag);
234 virtual void click();
235 virtual void accessKeyAction();
239 DOMString m_currValue;
242 bool m_activeSubmit : 1;
245 // -------------------------------------------------------------------------
247 class HTMLFieldSetElementImpl : public HTMLGenericFormElementImpl
250 HTMLFieldSetElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
252 virtual ~HTMLFieldSetElementImpl();
254 virtual Id id() const;
256 virtual bool isFocusable() const;
258 virtual khtml::RenderObject *createRenderer(RenderArena *, khtml::RenderStyle *);
260 virtual DOMString type() const;
263 // -------------------------------------------------------------------------
265 class HTMLInputElementImpl : public HTMLGenericFormElementImpl
267 friend class khtml::RenderLineEdit;
268 friend class khtml::RenderRadioButton;
269 friend class khtml::RenderFileButton;
272 friend class HTMLSelectElementImpl;
273 friend class khtml::RenderSlider;
277 // do not change the order!
296 HTMLInputElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
297 virtual ~HTMLInputElementImpl();
299 virtual Id id() const;
301 virtual bool isEnumeratable() const { return inputType() != IMAGE; }
303 bool autoComplete() const { return m_autocomplete; }
305 bool checked() const { return m_checked; }
306 void setChecked(bool);
307 long maxLength() const { return m_maxLen; }
308 int size() const { return m_size; }
309 DOMString type() const;
310 void setType(const DOMString& t);
312 DOMString value() const;
313 void setValue(const DOMString &);
318 virtual bool maintainsState() { return m_type != PASSWORD; }
319 virtual QString state();
320 virtual void restoreState(QStringList &);
324 virtual void click();
325 virtual void accessKeyAction();
327 virtual bool mapToEntry(NodeImpl::Id attr, MappedAttributeEntry& result) const;
328 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
330 virtual void attach();
331 virtual bool rendererIsNeeded(khtml::RenderStyle *);
332 virtual khtml::RenderObject *createRenderer(RenderArena *, khtml::RenderStyle *);
333 virtual bool appendFormData(FormDataList&, bool);
335 virtual bool isSuccessfulSubmitButton() const;
336 virtual bool isActivatedSubmit() const;
337 virtual void setActivatedSubmit(bool flag);
339 typeEnum inputType() const { return m_type; }
340 virtual void reset();
342 // used in case input type=image was clicked.
343 int clickX() const { return xPos; }
344 int clickY() const { return yPos; }
346 virtual void defaultEventHandler(EventImpl *evt);
347 virtual bool isEditable();
349 DOMString altText() const;
351 virtual bool isURLAttribute(AttributeImpl *attr) const;
354 long maxResults() const { return m_maxResults; }
358 bool storesValueSeparateFromAttribute() const;
370 HTMLImageLoader* m_imageLoader;
374 bool m_defaultChecked : 1;
375 bool m_useDefaultChecked : 1;
377 bool m_activeSubmit : 1;
378 bool m_autocomplete : 1;
381 // -------------------------------------------------------------------------
383 class HTMLLabelElementImpl : public HTMLElementImpl
386 HTMLLabelElementImpl(DocumentPtr *doc);
387 virtual ~HTMLLabelElementImpl();
389 virtual bool isFocusable() const;
391 virtual Id id() const;
393 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
395 virtual void accessKeyAction();
398 * the form element this label is associated to.
400 ElementImpl *formElement();
403 DOMString m_formElementID;
406 // -------------------------------------------------------------------------
408 class HTMLLegendElementImpl : public HTMLGenericFormElementImpl
411 HTMLLegendElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
412 virtual ~HTMLLegendElementImpl();
414 virtual bool isFocusable() const;
416 virtual Id id() const;
417 virtual khtml::RenderObject *createRenderer(RenderArena *, khtml::RenderStyle *);
419 virtual DOMString type() const;
422 // -------------------------------------------------------------------------
424 class HTMLSelectElementImpl : public HTMLGenericFormElementImpl
426 friend class khtml::RenderSelect;
429 HTMLSelectElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
430 ~HTMLSelectElementImpl();
432 virtual Id id() const;
433 DOMString type() const;
435 virtual void recalcStyle( StyleChange );
437 long selectedIndex() const;
438 void setSelectedIndex( long index );
440 virtual bool isEnumeratable() const { return true; }
444 long minWidth() const { return m_minwidth; }
446 long size() const { return m_size; }
448 bool multiple() const { return m_multiple; }
450 void add ( HTMLElementImpl *element, HTMLElementImpl *before );
451 void remove ( long index );
456 void setValue(DOMStringImpl* value);
458 HTMLOptionsCollectionImpl *options();
460 virtual bool maintainsState() { return true; }
461 virtual QString state();
462 virtual void restoreState(QStringList &);
464 virtual NodeImpl *insertBefore ( NodeImpl *newChild, NodeImpl *refChild, int &exceptioncode );
465 virtual NodeImpl *replaceChild ( NodeImpl *newChild, NodeImpl *oldChild, int &exceptioncode );
466 virtual NodeImpl *removeChild ( NodeImpl *oldChild, int &exceptioncode );
467 virtual NodeImpl *appendChild ( NodeImpl *newChild, int &exceptioncode );
468 virtual NodeImpl *addChild( NodeImpl* newChild );
470 virtual void childrenChanged();
472 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
474 virtual khtml::RenderObject *createRenderer(RenderArena *, khtml::RenderStyle *);
475 virtual bool appendFormData(FormDataList&, bool);
477 // get the actual listbox index of the optionIndexth option
478 int optionToListIndex(int optionIndex) const;
479 // reverse of optionToListIndex - get optionIndex from listboxIndex
480 int listToOptionIndex(int listIndex) const;
482 void setRecalcListItems();
484 QMemArray<HTMLGenericFormElementImpl*> listItems() const
486 if (m_recalcListItems) const_cast<HTMLSelectElementImpl*>(this)->recalcListItems();
489 virtual void reset();
490 void notifyOptionSelected(HTMLOptionElementImpl *selectedOption, bool selected);
493 virtual void defaultEventHandler(EventImpl *evt);
496 virtual void accessKeyAction();
499 void recalcListItems();
502 mutable QMemArray<HTMLGenericFormElementImpl*> m_listItems;
503 HTMLOptionsCollectionImpl *m_options;
507 bool m_recalcListItems;
510 // -------------------------------------------------------------------------
512 class HTMLKeygenElementImpl : public HTMLSelectElementImpl
515 HTMLKeygenElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
517 virtual Id id() const;
519 DOMString type() const;
521 // ### this is just a rough guess
522 virtual bool isEnumeratable() const { return false; }
524 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
525 virtual bool appendFormData(FormDataList&, bool);
527 AtomicString m_challenge;
528 AtomicString m_keyType;
531 // -------------------------------------------------------------------------
533 class HTMLOptGroupElementImpl : public HTMLGenericFormElementImpl
536 HTMLOptGroupElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
537 virtual ~HTMLOptGroupElementImpl();
539 virtual Id id() const;
540 DOMString type() const;
542 virtual bool isFocusable() const;
544 virtual NodeImpl *insertBefore ( NodeImpl *newChild, NodeImpl *refChild, int &exceptioncode );
545 virtual NodeImpl *replaceChild ( NodeImpl *newChild, NodeImpl *oldChild, int &exceptioncode );
546 virtual NodeImpl *removeChild ( NodeImpl *oldChild, int &exceptioncode );
547 virtual NodeImpl *appendChild ( NodeImpl *newChild, int &exceptioncode );
548 virtual NodeImpl *addChild( NodeImpl* newChild );
549 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
550 void recalcSelectOptions();
555 // ---------------------------------------------------------------------------
557 class HTMLOptionElementImpl : public HTMLGenericFormElementImpl
559 friend class khtml::RenderSelect;
560 friend class DOM::HTMLSelectElementImpl;
563 HTMLOptionElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
565 virtual bool isFocusable() const;
567 virtual Id id() const;
568 DOMString type() const;
570 DOMString text() const;
573 void setIndex( long );
574 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
575 DOMString value() const;
576 void setValue(DOMStringImpl* value);
578 bool selected() const { return m_selected; }
579 void setSelected(bool _selected);
581 HTMLSelectElementImpl *getSelect() const;
583 virtual void childrenChanged();
591 // -------------------------------------------------------------------------
593 class HTMLTextAreaElementImpl : public HTMLGenericFormElementImpl
595 friend class khtml::RenderTextArea;
604 HTMLTextAreaElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
605 ~HTMLTextAreaElementImpl();
607 virtual Id id() const;
609 long cols() const { return m_cols; }
611 long rows() const { return m_rows; }
613 WrapMethod wrap() const { return m_wrap; }
615 virtual bool isEnumeratable() const { return true; }
617 DOMString type() const;
619 virtual bool maintainsState() { return true; }
620 virtual QString state();
621 virtual void restoreState(QStringList &);
625 virtual void childrenChanged();
626 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
627 virtual khtml::RenderObject *createRenderer(RenderArena *, khtml::RenderStyle *);
628 virtual bool appendFormData(FormDataList&, bool);
629 virtual void reset();
631 void setValue(DOMString _value);
632 DOMString defaultValue();
633 void setDefaultValue(DOMString _defaultValue);
637 virtual bool isEditable();
639 virtual void accessKeyAction();
649 // -------------------------------------------------------------------------
651 class HTMLIsIndexElementImpl : public HTMLInputElementImpl
654 HTMLIsIndexElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f = 0);
656 virtual Id id() const;
657 virtual void parseHTMLAttribute(HTMLAttributeImpl *attr);
663 // -------------------------------------------------------------------------
665 class HTMLOptionsCollectionImpl : public khtml::Shared<HTMLOptionsCollectionImpl>
668 HTMLOptionsCollectionImpl(HTMLSelectElementImpl *impl) : m_select(impl) { }
670 unsigned long length() const;
671 void setLength(unsigned long);
672 NodeImpl *item(unsigned long index) const;
673 NodeImpl *namedItem(const DOMString &name) const;
675 void detach() { m_select = 0; }
678 HTMLSelectElementImpl *m_select;