• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef UI_VIEWS_CONTROLS_TEXTFIELD_NATIVE_TEXTFIELD_WRAPPER_H_
6 #define UI_VIEWS_CONTROLS_TEXTFIELD_NATIVE_TEXTFIELD_WRAPPER_H_
7 
8 #include "base/i18n/rtl.h"
9 #include "base/strings/string16.h"
10 #include "ui/gfx/native_widget_types.h"
11 #include "ui/gfx/range/range.h"
12 #include "ui/gfx/selection_model.h"
13 #include "ui/gfx/text_constants.h"
14 #include "ui/views/views_export.h"
15 
16 namespace gfx {
17 class Insets;
18 class Point;
19 }  // namespace gfx
20 
21 namespace ui {
22 class KeyEvent;
23 class TextInputClient;
24 }  // namespace ui
25 
26 namespace views {
27 
28 class Textfield;
29 class View;
30 
31 // An interface implemented by an object that provides a platform-native
32 // text field.
33 class VIEWS_EXPORT NativeTextfieldWrapper {
34  public:
35   // The Textfield calls this when it is destroyed to clean up the wrapper
36   // object.
~NativeTextfieldWrapper()37   virtual ~NativeTextfieldWrapper() {}
38 
39   // Gets the text displayed in the wrapped native text field.
40   virtual string16 GetText() const = 0;
41 
42   // Updates the text displayed with the text held by the Textfield.
43   virtual void UpdateText() = 0;
44 
45   // Adds the specified text to the text already displayed by the wrapped native
46   // text field.
47   virtual void AppendText(const string16& text) = 0;
48 
49   // Inserts |text| at the current cursor position, replacing any selected text.
50   virtual void InsertOrReplaceText(const string16& text) = 0;
51 
52   // Returns the text direction.
53   virtual base::i18n::TextDirection GetTextDirection() const = 0;
54 
55   // Gets the text that is selected in the wrapped native text field.
56   virtual string16 GetSelectedText() const = 0;
57 
58   // Select the entire text range. If |reversed| is true, the range will end at
59   // the logical beginning of the text; this generally shows the leading portion
60   // of text that overflows its display area.
61   virtual void SelectAll(bool reversed) = 0;
62 
63   // Clears the selection within the edit field and sets the caret to the end.
64   virtual void ClearSelection() = 0;
65 
66   // Updates whether there is a visible border.
67   virtual void UpdateBorder() = 0;
68 
69   // Updates the text color used when painting the native text field.
70   virtual void UpdateTextColor() = 0;
71 
72   // Updates the background color used when painting the native text field.
73   virtual void UpdateBackgroundColor() = 0;
74 
75   // Updates the read-only state of the native text field.
76   virtual void UpdateReadOnly() = 0;
77 
78   // Updates the font used to render text in the native text field.
79   virtual void UpdateFont() = 0;
80 
81   // Updates the visibility of the text in the native text field.
82   virtual void UpdateIsObscured() = 0;
83 
84   // Updates the enabled state of the native text field.
85   virtual void UpdateEnabled() = 0;
86 
87   // Returns the insets for the text field.
88   virtual gfx::Insets CalculateInsets() = 0;
89 
90   // Updates the horizontal margins for the native text field.
91   virtual void UpdateHorizontalMargins() = 0;
92 
93   // Updates the vertical margins for the native text field.
94   virtual void UpdateVerticalMargins() = 0;
95 
96   // Sets the focus to the text field. Returns false if the wrapper
97   // didn't take focus.
98   virtual bool SetFocus() = 0;
99 
100   // Retrieves the views::View that hosts the native control.
101   virtual View* GetView() = 0;
102 
103   // Returns a handle to the underlying native view for testing.
104   virtual gfx::NativeView GetTestingHandle() const = 0;
105 
106   // Returns whether or not an IME is composing text.
107   virtual bool IsIMEComposing() const = 0;
108 
109   // Gets the selected range.
110   virtual gfx::Range GetSelectedRange() const = 0;
111 
112   // Selects the text given by |range|.
113   virtual void SelectRange(const gfx::Range& range) = 0;
114 
115   // Gets the selection model.
116   virtual gfx::SelectionModel GetSelectionModel() const = 0;
117 
118   // Selects the text given by |sel|.
119   virtual void SelectSelectionModel(const gfx::SelectionModel& sel) = 0;
120 
121   // Returns the currnet cursor position.
122   virtual size_t GetCursorPosition() const = 0;
123 
124   // Get or set whether or not the cursor is enabled.
125   virtual bool GetCursorEnabled() const = 0;
126   virtual void SetCursorEnabled(bool enabled) = 0;
127 
128   // Following methods are to forward key/focus related events to the
129   // views wrapper so that TextfieldViews can handle key inputs without
130   // having focus.
131 
132   // Invoked when a key is pressed/release on Textfield.  Subclasser
133   // should return true if the event has been processed and false
134   // otherwise.
135   // See also View::OnKeyPressed/OnKeyReleased.
136   virtual bool HandleKeyPressed(const ui::KeyEvent& e) = 0;
137   virtual bool HandleKeyReleased(const ui::KeyEvent& e) = 0;
138 
139   // Invoked when focus is being moved from or to the Textfield.
140   // See also View::OnFocus/OnBlur.
141   virtual void HandleFocus() = 0;
142   virtual void HandleBlur() = 0;
143 
144   // Returns the View's TextInputClient instance or NULL if the View doesn't
145   // support text input.
146   virtual ui::TextInputClient* GetTextInputClient() = 0;
147 
148   // Set the text colors; see the corresponding Textfield functions for details.
149   virtual void SetColor(SkColor value) = 0;
150   virtual void ApplyColor(SkColor value, const gfx::Range& range) = 0;
151 
152   // Set the text styles; see the corresponding Textfield functions for details.
153   virtual void SetStyle(gfx::TextStyle style, bool value) = 0;
154   virtual void ApplyStyle(gfx::TextStyle style,
155                           bool value,
156                           const gfx::Range& range) = 0;
157 
158   // Clears Edit history.
159   virtual void ClearEditHistory() = 0;
160 
161   // Get the height in pixels of the first font used in this textfield.
162   virtual int GetFontHeight() = 0;
163 
164   // Returns the baseline of the textfield. This should not take into account
165   // any insets.
166   virtual int GetTextfieldBaseline() const = 0;
167 
168   // Returns the width necessary to display the current text, including any
169   // necessary space for the cursor or border/margin.
170   virtual int GetWidthNeededForText() const = 0;
171 
172   // Performs the action associated with the specified command id. Not called
173   // ExecuteCommand to avoid name clash.
174   virtual void ExecuteTextCommand(int command_id) = 0;
175 
176   // Returns whether there is a drag operation originating from the textfield.
177   virtual bool HasTextBeingDragged() = 0;
178 
179   // Returns the location for keyboard-triggered context menus.
180   virtual gfx::Point GetContextMenuLocation() = 0;
181 
182   // Creates an appropriate NativeTextfieldWrapper for the platform.
183   static NativeTextfieldWrapper* CreateWrapper(Textfield* field);
184 };
185 
186 }  // namespace views
187 
188 #endif  // UI_VIEWS_CONTROLS_TEXTFIELD_NATIVE_TEXTFIELD_WRAPPER_H_
189