1 /*
2  * Copyright (C) 2006, 2007, 2009 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
4  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef RenderTextControlSingleLine_h
24 #define RenderTextControlSingleLine_h
25 
26 #include "PopupMenuClient.h"
27 #include "RenderTextControl.h"
28 #include "SearchPopupMenu.h"
29 #include "Timer.h"
30 
31 namespace WebCore {
32 
33 class InputElement;
34 class InputFieldSpeechButtonElement;
35 class SearchFieldCancelButtonElement;
36 class SearchFieldResultsButtonElement;
37 class SpinButtonElement;
38 class TextControlInnerElement;
39 
40 class RenderTextControlSingleLine : public RenderTextControl, private PopupMenuClient {
41 public:
42     RenderTextControlSingleLine(Node*, bool);
43     virtual ~RenderTextControlSingleLine();
44 
placeholderIsVisible()45     bool placeholderIsVisible() const { return m_placeholderVisible; }
46     bool placeholderShouldBeVisible() const;
47 
48     void addSearchResult();
49     void stopSearchEventTimer();
50 
popupIsVisible()51     bool popupIsVisible() const { return m_searchPopupIsVisible; }
52     void showPopup();
53     void hidePopup();
54 
55     void forwardEvent(Event*);
56 
57     void capsLockStateMayHaveChanged();
58 
59     // Decoration width outside of the text field.
60     int decorationWidthRight() const;
61 
62 private:
63     int preferredDecorationWidthRight() const;
64     virtual bool hasControlClip() const;
65     virtual IntRect controlClipRect(int tx, int ty) const;
isTextField()66     virtual bool isTextField() const { return true; }
67 
68     virtual void subtreeHasChanged();
69     virtual void paint(PaintInfo&, int tx, int ty);
70     virtual void paintBoxDecorations(PaintInfo&, int tx, int ty);
71     virtual void addFocusRingRects(Vector<IntRect>&, int tx, int ty);
72     virtual void layout();
73 
74     virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, int x, int y, int tx, int ty, HitTestAction);
75 
76     virtual void autoscroll();
77 
78     // Subclassed to forward to our inner div.
79     virtual int scrollLeft() const;
80     virtual int scrollTop() const;
81     virtual int scrollWidth() const;
82     virtual int scrollHeight() const;
83     virtual void setScrollLeft(int);
84     virtual void setScrollTop(int);
85     virtual bool scroll(ScrollDirection, ScrollGranularity, float multiplier = 1, Node** stopNode = 0);
86     virtual bool logicalScroll(ScrollLogicalDirection, ScrollGranularity, float multiplier = 1, Node** stopNode = 0);
87 
88     int textBlockWidth() const;
89     virtual float getAvgCharWidth(AtomicString family);
90     virtual int preferredContentWidth(float charWidth) const;
91     virtual void adjustControlHeightBasedOnLineHeight(int lineHeight);
92 
93     void createSubtreeIfNeeded();
94     virtual void updateFromElement();
95     virtual void cacheSelection(int start, int end);
96     virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
97 
98     virtual RenderStyle* textBaseStyle() const;
99     virtual PassRefPtr<RenderStyle> createInnerTextStyle(const RenderStyle* startStyle) const;
100     PassRefPtr<RenderStyle> createInnerBlockStyle(const RenderStyle* startStyle) const;
101     PassRefPtr<RenderStyle> createResultsButtonStyle(const RenderStyle* startStyle) const;
102     PassRefPtr<RenderStyle> createCancelButtonStyle(const RenderStyle* startStyle) const;
103     PassRefPtr<RenderStyle> createInnerSpinButtonStyle() const;
104     PassRefPtr<RenderStyle> createOuterSpinButtonStyle() const;
105 #if ENABLE(INPUT_SPEECH)
106     PassRefPtr<RenderStyle> createSpeechButtonStyle() const;
107 #endif
108 
109     void updateCancelButtonVisibility() const;
110     EVisibility visibilityForCancelButton() const;
111     const AtomicString& autosaveName() const;
112 
113     void startSearchEventTimer();
114     void searchEventTimerFired(Timer<RenderTextControlSingleLine>*);
115 
116     // PopupMenuClient methods
117     virtual void valueChanged(unsigned listIndex, bool fireEvents = true);
selectionChanged(unsigned,bool)118     virtual void selectionChanged(unsigned, bool) {}
selectionCleared()119     virtual void selectionCleared() {}
120     virtual String itemText(unsigned listIndex) const;
121     virtual String itemLabel(unsigned listIndex) const;
122     virtual String itemIcon(unsigned listIndex) const;
itemToolTip(unsigned)123     virtual String itemToolTip(unsigned) const { return String(); }
itemAccessibilityText(unsigned)124     virtual String itemAccessibilityText(unsigned) const { return String(); }
125     virtual bool itemIsEnabled(unsigned listIndex) const;
126     virtual PopupMenuStyle itemStyle(unsigned listIndex) const;
127     virtual PopupMenuStyle menuStyle() const;
128     virtual int clientInsetLeft() const;
129     virtual int clientInsetRight() const;
130     virtual int clientPaddingLeft() const;
131     virtual int clientPaddingRight() const;
132     virtual int listSize() const;
133     virtual int selectedIndex() const;
134     virtual void popupDidHide();
135     virtual bool itemIsSeparator(unsigned listIndex) const;
136     virtual bool itemIsLabel(unsigned listIndex) const;
137     virtual bool itemIsSelected(unsigned listIndex) const;
shouldPopOver()138     virtual bool shouldPopOver() const { return false; }
valueShouldChangeOnHotTrack()139     virtual bool valueShouldChangeOnHotTrack() const { return false; }
140     virtual void setTextFromItem(unsigned listIndex);
141     virtual FontSelector* fontSelector() const;
142     virtual HostWindow* hostWindow() const;
143     virtual PassRefPtr<Scrollbar> createScrollbar(ScrollableArea*, ScrollbarOrientation, ScrollbarControlSize);
144 
145     InputElement* inputElement() const;
146 
147     virtual int textBlockInsetLeft() const;
148     virtual int textBlockInsetRight() const;
149     virtual int textBlockInsetTop() const;
150 
151     bool m_searchPopupIsVisible;
152     bool m_shouldDrawCapsLockIndicator;
153 
154     RefPtr<TextControlInnerElement> m_innerBlock;
155     RefPtr<SearchFieldResultsButtonElement> m_resultsButton;
156     RefPtr<SearchFieldCancelButtonElement> m_cancelButton;
157     RefPtr<TextControlInnerElement> m_innerSpinButton;
158     RefPtr<TextControlInnerElement> m_outerSpinButton;
159 #if ENABLE(INPUT_SPEECH)
160     RefPtr<InputFieldSpeechButtonElement> m_speechButton;
161 #endif
162 
163     Timer<RenderTextControlSingleLine> m_searchEventTimer;
164     RefPtr<SearchPopupMenu> m_searchPopup;
165     Vector<String> m_recentSearches;
166 };
167 
toRenderTextControlSingleLine(RenderObject * object)168 inline RenderTextControlSingleLine* toRenderTextControlSingleLine(RenderObject* object)
169 {
170     ASSERT(!object || object->isTextField());
171     return static_cast<RenderTextControlSingleLine*>(object);
172 }
173 
174 // This will catch anyone doing an unnecessary cast.
175 void toRenderTextControlSingleLine(const RenderTextControlSingleLine*);
176 
177 // ----------------------------
178 
179 class RenderTextControlInnerBlock : public RenderBlock {
180 public:
RenderTextControlInnerBlock(Node * node,bool isMultiLine)181     RenderTextControlInnerBlock(Node* node, bool isMultiLine) : RenderBlock(node), m_multiLine(isMultiLine) { }
182 
183 private:
hasLineIfEmpty()184     virtual bool hasLineIfEmpty() const { return true; }
185     virtual VisiblePosition positionForPoint(const IntPoint&);
186 
187     bool m_multiLine;
188 };
189 
190 }
191 
192 #endif
193