fix logic
[personal-kdelibs.git] / khtml / html / html_formimpl.h
blob2652dc4dbb204f159c46556a292b2f3f02d91516
1 /*
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 * (C) 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
25 #ifndef HTML_FORMIMPL_H
26 #define HTML_FORMIMPL_H
28 #include "html/html_elementimpl.h"
29 #include "html/html_imageimpl.h"
30 #include "dom/html_element.h"
32 class QTextCodec;
34 namespace khtml
36 class RenderFormElement;
37 class RenderTextArea;
38 class RenderSelect;
39 class RenderLineEdit;
40 class RenderRadioButton;
41 class RenderFileButton;
43 typedef QList<QByteArray> encodingList;
46 namespace KWallet {
47 class Wallet;
50 namespace DOM {
52 class HTMLFormElement;
53 class DOMString;
54 class HTMLGenericFormElementImpl;
55 class HTMLOptionElementImpl;
56 class HTMLCollectionImpl;
58 // -------------------------------------------------------------------------
60 class HTMLFormElementImpl : public HTMLElementImpl
62 public:
63 HTMLFormElementImpl(DocumentImpl *doc, bool implicit);
64 virtual ~HTMLFormElementImpl();
66 virtual Id id() const;
68 virtual void insertedIntoDocument();
69 virtual void removedFromDocument();
70 virtual void addId(const QString& id);
71 virtual void removeId(const QString& id);
74 long length() const;
76 QByteArray formData(bool& ok);
78 DOMString enctype() const { return m_enctype; }
79 void setEnctype( const DOMString & );
81 DOMString target() const;
82 DOMString action() const;
83 HTMLCollectionImpl* elements();
85 bool autoComplete() const { return m_autocomplete; }
86 void doAutoFill();
87 void walletOpened(KWallet::Wallet *w);
89 virtual void parseAttribute(AttributeImpl *attr);
91 void radioClicked( HTMLGenericFormElementImpl *caller );
93 void registerFormElement(HTMLGenericFormElementImpl *);
94 void removeFormElement(HTMLGenericFormElementImpl *);
95 void registerImgElement(HTMLImageElementImpl *);
96 void removeImgElement(HTMLImageElementImpl *);
98 void submitFromKeyboard();
99 bool prepareSubmit();
100 void submit();
101 void reset();
103 void setMalformed(bool malformed) { m_malformed = malformed; }
104 bool isMalformed() const { return m_malformed; }
106 friend class HTMLFormElement;
107 friend class HTMLFormCollectionImpl;
109 private:
110 void gatherWalletData();
111 QList<HTMLGenericFormElementImpl*> formElements;
112 QList<HTMLImageElementImpl*> imgElements;
113 DOMString m_target;
114 DOMString m_enctype;
115 QString m_boundary;
116 DOMString m_acceptcharset;
117 QString m_encCharset;
118 bool m_post : 1;
119 bool m_multipart : 1;
120 bool m_autocomplete : 1;
121 bool m_insubmit : 1;
122 bool m_doingsubmit : 1;
123 bool m_inreset : 1;
124 bool m_malformed : 1;
125 bool m_haveTextarea : 1; // for wallet storage
126 bool m_havePassword : 1; // for wallet storage
127 DOMString m_name; // our name
128 QMap<QString, QString> m_walletMap; // for wallet storage
131 // -------------------------------------------------------------------------
133 class HTMLGenericFormElementImpl : public HTMLElementImpl
135 friend class HTMLFormElementImpl;
136 friend class khtml::RenderFormElement;
138 public:
139 HTMLGenericFormElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = 0);
140 virtual ~HTMLGenericFormElementImpl();
142 HTMLFormElementImpl *form() const { return m_form; }
144 virtual void parseAttribute(AttributeImpl *attr);
145 virtual void attach();
146 virtual void reset() {}
148 virtual void insertedIntoDocument();
149 virtual void removedFromDocument();
151 void onSelect();
152 void onChange();
154 bool disabled() const { return m_disabled; }
155 void setDisabled(bool _disabled);
157 virtual bool isFocusable() const;
158 virtual bool isEnumeratable() const { return false; }
160 virtual bool isDefault() const { return false; }
162 bool readOnly() const { return m_readOnly; }
163 void setReadOnly(bool _readOnly) { m_readOnly = _readOnly; }
165 DOMString name() const;
166 void setName(const DOMString& name);
168 virtual bool isGenericFormElement() const { return true; }
171 * override in derived classes to get the encoded name=value pair
172 * for submitting
173 * return true for a successful control (see HTML4-17.13.2)
175 virtual bool encoding(const QTextCodec*, khtml::encodingList&, bool) { return false; }
177 virtual void defaultEventHandler(EventImpl *evt);
178 virtual bool isEditable();
180 virtual bool unsubmittedFormChanges() const { return false; }
182 protected:
183 HTMLFormElementImpl *getForm() const;
185 DOMStringImpl* m_name;
186 HTMLFormElementImpl *m_form;
187 bool m_disabled, m_readOnly;
190 // -------------------------------------------------------------------------
192 class HTMLButtonElementImpl : public HTMLGenericFormElementImpl
194 public:
195 HTMLButtonElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = 0);
197 virtual ~HTMLButtonElementImpl();
199 enum typeEnum {
200 SUBMIT,
201 RESET,
202 BUTTON
205 virtual Id id() const;
206 virtual bool isEnumeratable() const { return true; }
208 DOMString type() const;
209 typeEnum buttonType() const { return KDE_CAST_BF_ENUM(typeEnum, m_type); }
210 virtual void parseAttribute(AttributeImpl *attr);
211 virtual void defaultEventHandler(EventImpl *evt);
212 virtual bool encoding(const QTextCodec*, khtml::encodingList&, bool);
213 void activate();
214 virtual void attach();
215 void click();
217 protected:
218 DOMString m_value;
219 QString m_currValue;
220 KDE_BF_ENUM(typeEnum) m_type : 2;
221 bool m_dirty : 1;
222 bool m_clicked : 1;
223 bool m_activeSubmit : 1;
226 // -------------------------------------------------------------------------
228 class HTMLFieldSetElementImpl : public HTMLGenericFormElementImpl
230 public:
231 HTMLFieldSetElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = 0);
233 virtual ~HTMLFieldSetElementImpl();
235 virtual Id id() const;
236 virtual void attach();
237 virtual void parseAttribute(AttributeImpl *attr);
241 // -------------------------------------------------------------------------
243 class HTMLInputElementImpl : public HTMLGenericFormElementImpl
245 friend class khtml::RenderLineEdit;
246 friend class khtml::RenderRadioButton;
247 friend class khtml::RenderFileButton;
249 public:
250 // do not change the order!
251 enum typeEnum {
252 TEXT,
253 PASSWORD,
254 ISINDEX,
255 CHECKBOX,
256 RADIO,
257 SUBMIT,
258 RESET,
259 FILE,
260 HIDDEN,
261 IMAGE,
262 BUTTON
265 HTMLInputElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = 0);
266 virtual ~HTMLInputElementImpl();
268 virtual Id id() const;
270 virtual bool isEnumeratable() const { return inputType() != IMAGE; }
271 virtual bool isDefault() const { return m_defaultChecked; }
273 bool autoComplete() const { return m_autocomplete; }
275 bool checked() const { return m_useDefaultChecked ? m_defaultChecked : m_checked; }
276 void setChecked(bool);
277 bool indeterminate() const { return m_indeterminate; }
278 void setIndeterminate(bool);
279 long maxLength() const { return m_maxLen; }
280 int size() const { return m_size; }
281 DOMString type() const;
282 void setType(const DOMString& t);
284 DOMString value() const;
285 void setValue(DOMString val);
287 DOMString valueWithDefault() const;
289 virtual bool maintainsState() { return true; }
290 virtual QString state();
291 virtual void restoreState(const QString &state);
293 void select();
294 void click();
296 virtual void parseAttribute(AttributeImpl *attr);
298 virtual void copyNonAttributeProperties(const ElementImpl* source);
300 virtual void attach();
301 virtual bool encoding(const QTextCodec*, khtml::encodingList&, bool);
303 typeEnum inputType() const { return KDE_CAST_BF_ENUM(typeEnum, m_type); }
304 virtual void reset();
306 // used in case input type=image was clicked.
307 int clickX() const { return xPos; }
308 int clickY() const { return yPos; }
310 virtual void defaultEventHandler(EventImpl *evt);
311 virtual bool isEditable();
313 DOMString altText() const;
314 void activate();
316 void setUnsubmittedFormChange(bool unsubmitted) { m_unsubmittedFormChange = unsubmitted; }
317 virtual bool unsubmittedFormChanges() const { return m_unsubmittedFormChange; }
319 //Mozilla extensions.
320 long selectionStart();
321 long selectionEnd();
322 void setSelectionStart(long pos);
323 void setSelectionEnd (long pos);
324 void setSelectionRange(long start, long end);
325 protected:
326 void parseType(const DOMString& t);
328 DOMString m_value;
329 int xPos;
330 short m_maxLen;
331 short m_size;
332 short yPos;
334 KDE_BF_ENUM(typeEnum) m_type : 4;
335 bool m_clicked : 1 ;
336 bool m_checked : 1;
337 bool m_defaultChecked : 1; // could do without by checking ATTR_CHECKED
338 bool m_useDefaultChecked : 1;
339 bool m_indeterminate : 1;
340 bool m_haveType : 1;
341 bool m_activeSubmit : 1;
342 bool m_autocomplete : 1;
343 bool m_inited : 1;
344 bool m_unsubmittedFormChange : 1;
347 // -------------------------------------------------------------------------
349 class HTMLLabelElementImpl : public HTMLGenericFormElementImpl
351 public:
352 HTMLLabelElementImpl(DocumentImpl *doc);
353 virtual ~HTMLLabelElementImpl();
355 virtual Id id() const;
356 virtual void attach();
357 virtual void defaultEventHandler(EventImpl *evt);
358 virtual bool isFocusable() const { return true; }
359 virtual bool isTabFocusable() const { return false; }
360 NodeImpl* getFormElement();
362 private:
363 DOMString m_formElementID;
366 // -------------------------------------------------------------------------
368 class HTMLLegendElementImpl : public HTMLGenericFormElementImpl
370 public:
371 HTMLLegendElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = 0);
372 virtual ~HTMLLegendElementImpl();
374 virtual Id id() const;
375 virtual void attach();
376 virtual void parseAttribute(AttributeImpl *attr);
380 // -------------------------------------------------------------------------
382 class HTMLSelectElementImpl : public HTMLGenericFormElementImpl
384 friend class khtml::RenderSelect;
386 public:
387 HTMLSelectElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = 0);
388 ~HTMLSelectElementImpl();
390 virtual Id id() const;
392 DOMString type() const;
394 HTMLCollectionImpl* options();
396 long selectedIndex() const;
397 void setSelectedIndex( long index );
399 virtual bool isEnumeratable() const { return true; }
401 long length() const;
403 long minWidth() const { return m_minwidth; }
405 long size() const { return m_size; }
407 bool multiple() const { return m_multiple; }
409 void add ( HTMLElementImpl* element, HTMLElementImpl* before, int& exceptioncode );
410 void remove ( long index );
412 DOMString value() const;
413 void setValue(DOMStringImpl* value);
415 virtual bool maintainsState() { return true; }
416 virtual QString state();
417 virtual void restoreState(const QString &state);
419 virtual NodeImpl *insertBefore ( NodeImpl *newChild, NodeImpl *refChild, int &exceptioncode );
420 virtual void replaceChild ( NodeImpl *newChild, NodeImpl *oldChild, int &exceptioncode );
421 virtual void removeChild ( NodeImpl *oldChild, int &exceptioncode );
422 virtual NodeImpl *appendChild ( NodeImpl *newChild, int &exceptioncode );
423 virtual NodeImpl *addChild( NodeImpl* newChild );
425 virtual void childrenChanged();
427 virtual void parseAttribute(AttributeImpl *attr);
429 virtual void attach();
430 virtual bool encoding(const QTextCodec*, khtml::encodingList&, bool);
432 // get the actual listbox index of the optionIndexth option
433 int optionToListIndex(int optionIndex) const;
434 // reverse of optionToListIndex - get optionIndex from listboxIndex
435 int listToOptionIndex(int listIndex) const;
437 void setRecalcListItems();
439 QVector<HTMLGenericFormElementImpl*> listItems() const
441 if (m_recalcListItems) const_cast<HTMLSelectElementImpl*>(this)->recalcListItems();
442 return m_listItems;
444 virtual void reset();
445 void notifyOptionSelected(HTMLOptionElementImpl *selectedOption, bool selected);
447 private:
448 void recalcListItems() const;
450 protected:
451 mutable QVector<HTMLGenericFormElementImpl*> m_listItems;
452 short m_minwidth;
453 signed short m_size : 15;
454 bool m_multiple : 1;
455 mutable bool m_recalcListItems :1;
456 mutable unsigned int m_length:31;
459 // -------------------------------------------------------------------------
461 class HTMLKeygenElementImpl : public HTMLSelectElementImpl
463 public:
464 HTMLKeygenElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = 0);
466 virtual Id id() const;
468 DOMString type() const;
470 long selectedIndex() const;
471 void setSelectedIndex( long index );
473 // ### this is just a rough guess
474 virtual bool isEnumeratable() const { return false; }
476 virtual void parseAttribute(AttributeImpl *attr);
477 virtual bool encoding(const QTextCodec*, khtml::encodingList&, bool);
481 // -------------------------------------------------------------------------
483 class HTMLOptGroupElementImpl : public HTMLGenericFormElementImpl
485 public:
486 HTMLOptGroupElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = 0)
487 : HTMLGenericFormElementImpl(doc, f) {}
489 virtual Id id() const;
493 // ---------------------------------------------------------------------------
495 class HTMLOptionElementImpl : public HTMLGenericFormElementImpl
497 friend class khtml::RenderSelect;
498 friend class DOM::HTMLSelectElementImpl;
500 public:
501 HTMLOptionElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = 0);
503 virtual Id id() const;
505 DOMString text() const;
507 long index() const;
508 void setIndex( long );
509 virtual void parseAttribute(AttributeImpl *attr);
510 DOMString value() const;
511 void setValue(DOMStringImpl* value);
513 virtual bool isDefault() const { return m_defaultSelected; }
515 // For internal use --- just returns the bit
516 bool selectedBit() const { return m_selected; }
518 // DOM use --- may recompute information of a parent <select>
519 bool selected() const;
520 void setSelected(bool _selected);
521 void setDefaultSelected( bool _defaultSelected );
523 HTMLSelectElementImpl *getSelect() const;
525 protected:
526 DOMString m_value;
527 bool m_selected;
528 bool m_defaultSelected;
532 // -------------------------------------------------------------------------
534 class HTMLTextAreaElementImpl : public HTMLGenericFormElementImpl
536 friend class khtml::RenderTextArea;
538 public:
539 enum WrapMethod {
540 ta_NoWrap,
541 ta_Virtual,
542 ta_Physical
545 HTMLTextAreaElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = 0);
546 ~HTMLTextAreaElementImpl();
548 virtual Id id() const;
549 virtual void childrenChanged();
551 long cols() const { return m_cols; }
553 long rows() const { return m_rows; }
555 WrapMethod wrap() const { return m_wrap; }
557 virtual bool isEnumeratable() const { return true; }
559 DOMString type() const;
561 virtual bool maintainsState() { return true; }
562 virtual QString state();
563 virtual void restoreState(const QString &state);
565 void select ( );
567 virtual void parseAttribute(AttributeImpl *attr);
568 virtual void attach();
569 virtual bool encoding(const QTextCodec*, khtml::encodingList&, bool);
570 virtual void reset();
571 DOMString value();
572 void setValue(DOMString _value);
573 DOMString defaultValue();
574 void setDefaultValue(DOMString _defaultValue);
576 virtual bool isEditable();
577 void setUnsubmittedFormChange(bool unsubmitted) { m_unsubmittedFormChange = unsubmitted; }
578 virtual bool unsubmittedFormChanges() const { return m_unsubmittedFormChange; }
580 //Mozilla extensions.
581 long selectionStart();
582 long selectionEnd();
583 void setSelectionStart(long pos);
584 void setSelectionEnd (long pos);
585 void setSelectionRange(long start, long end);
586 long textLength();
587 protected:
588 int m_rows;
589 int m_cols;
590 WrapMethod m_wrap;
591 QString m_value;
592 bool m_changed: 1; //States whether the contents has been editted
593 bool m_unsubmittedFormChange: 1;
594 bool m_initialized: 1;
597 // -------------------------------------------------------------------------
599 class HTMLIsIndexElementImpl : public HTMLInputElementImpl
601 public:
602 HTMLIsIndexElementImpl(DocumentImpl *doc, HTMLFormElementImpl *f = 0);
603 ~HTMLIsIndexElementImpl();
605 virtual Id id() const;
606 virtual void parseAttribute(AttributeImpl *attr);
608 DOMString prompt() const;
609 void setPrompt(const DOMString& _value);
613 } //namespace
615 #endif