OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 VIEWS_CONTROLS_TEXTFIELD_NATIVE_TEXTFIELD_VIEWS_H_ |
| 6 #define VIEWS_CONTROLS_TEXTFIELD_NATIVE_TEXTFIELD_VIEWS_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "base/string16.h" |
| 10 #include "base/task.h" |
| 11 #include "gfx/font.h" |
| 12 #include "views/border.h" |
| 13 #include "views/controls/textfield/native_textfield_wrapper.h" |
| 14 #include "views/view.h" |
| 15 |
| 16 namespace gfx { |
| 17 class Canvas; |
| 18 } // namespace |
| 19 |
| 20 namespace views { |
| 21 |
| 22 class KeyEvent; |
| 23 class TextfieldViewsModel; |
| 24 |
| 25 // A views/skia only implementation of NativeTextfieldWrapper. |
| 26 // No platform specific code is used. |
| 27 // Following features are not yet supported. |
| 28 // * BIDI |
| 29 // * Clipboard (Cut & Paste). |
| 30 // * Context Menu. |
| 31 // * IME/i18n support. |
| 32 // * X selection (only if we want to support). |
| 33 // * STYLE_MULTILINE, STYLE_LOWERCASE text. (These are not used in |
| 34 // chromeos, so we may not need them) |
| 35 // * Double click to select word, and triple click to select all. |
| 36 class NativeTextfieldViews : public views::View, |
| 37 public NativeTextfieldWrapper { |
| 38 public: |
| 39 explicit NativeTextfieldViews(Textfield* parent); |
| 40 ~NativeTextfieldViews(); |
| 41 |
| 42 // views::View overrides: |
| 43 virtual bool OnMousePressed(const views::MouseEvent& e); |
| 44 virtual bool OnMouseDragged(const views::MouseEvent& e); |
| 45 virtual void OnMouseReleased(const views::MouseEvent& e, bool canceled); |
| 46 virtual bool OnKeyPressed(const views::KeyEvent& e); |
| 47 virtual bool OnKeyReleased(const views::KeyEvent& e); |
| 48 virtual void Paint(gfx::Canvas* canvas); |
| 49 virtual void WillGainFocus(); |
| 50 virtual void DidGainFocus(); |
| 51 virtual void WillLoseFocus(); |
| 52 virtual void DidChangeBounds(const gfx::Rect& previous, |
| 53 const gfx::Rect& current); |
| 54 |
| 55 // NativeTextfieldWrapper overrides: |
| 56 virtual string16 GetText() const; |
| 57 virtual void UpdateText(); |
| 58 virtual void AppendText(const string16& text); |
| 59 virtual string16 GetSelectedText() const; |
| 60 virtual void SelectAll(); |
| 61 virtual void ClearSelection(); |
| 62 virtual void UpdateBorder(); |
| 63 virtual void UpdateTextColor(); |
| 64 virtual void UpdateBackgroundColor(); |
| 65 virtual void UpdateReadOnly(); |
| 66 virtual void UpdateFont(); |
| 67 virtual void UpdateIsPassword(); |
| 68 virtual void UpdateEnabled(); |
| 69 virtual bool IsPassword(); |
| 70 virtual gfx::Insets CalculateInsets(); |
| 71 virtual void UpdateHorizontalMargins(); |
| 72 virtual void UpdateVerticalMargins(); |
| 73 virtual void SetFocus(); |
| 74 virtual View* GetView(); |
| 75 virtual gfx::NativeView GetTestingHandle() const; |
| 76 virtual bool IsIMEComposing() const; |
| 77 |
| 78 // class name of internal |
| 79 static const char kViewClassName[]; |
| 80 |
| 81 // Returns true when |
| 82 // 1) built with GYP_DEFIENS="touchui=1" |
| 83 // 2) enabled by SetEnableTextfieldViews(true) |
| 84 // 3) enabled by the command line flag "--enable-textfield-view". |
| 85 static bool IsTextfieldViewsEnabled(); |
| 86 // Enable/Disable TextfieldViews implementation for Textfield. |
| 87 static void SetEnableTextfieldViews(bool enabled); |
| 88 |
| 89 private: |
| 90 friend class NativeTextfieldViewsTest; |
| 91 |
| 92 // A Border class to draw focus border for the text field. |
| 93 class TextfieldBorder : public Border { |
| 94 public: |
| 95 TextfieldBorder(); |
| 96 |
| 97 // Border implementation. |
| 98 virtual void Paint(const View& view, gfx::Canvas* canvas) const; |
| 99 virtual void GetInsets(gfx::Insets* insets) const; |
| 100 |
| 101 // Sets the insets of the border. |
| 102 void SetInsets(int top, int left, int bottom, int right); |
| 103 |
| 104 // Sets the focus state. |
| 105 void set_has_focus(bool has_focus) { |
| 106 has_focus_ = has_focus; |
| 107 } |
| 108 |
| 109 private: |
| 110 bool has_focus_; |
| 111 gfx::Insets insets_; |
| 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(TextfieldBorder); |
| 114 }; |
| 115 |
| 116 // Returns the Textfield's font. |
| 117 const gfx::Font& GetFont() const; |
| 118 |
| 119 // Returns the Textfield's text color. |
| 120 SkColor GetTextColor() const; |
| 121 |
| 122 // A callback function to periodically update the cursor state. |
| 123 void UpdateCursor(); |
| 124 |
| 125 // Repaint the cursor. |
| 126 void RepaintCursor(); |
| 127 |
| 128 // Update the cursor_bounds and text_offset. |
| 129 void UpdateCursorBoundsAndTextOffset(); |
| 130 |
| 131 void PaintTextAndCursor(gfx::Canvas* canvas); |
| 132 |
| 133 // Handle the keyevent. |
| 134 bool HandleKeyEvent(const KeyEvent& key_event); |
| 135 |
| 136 // Utility function. Gets the character corresponding to a keyevent. |
| 137 // Returns 0 if the character is not printable. |
| 138 char16 GetPrintableChar(const KeyEvent& key_event); |
| 139 |
| 140 // Find a cusor position for given |point| in this views coordinates. |
| 141 size_t FindCursorPosition(const gfx::Point& point) const; |
| 142 |
| 143 // The parent textfield, the owner of this object. |
| 144 Textfield* textfield_; |
| 145 |
| 146 // The text model. |
| 147 scoped_ptr<TextfieldViewsModel> model_; |
| 148 |
| 149 // The reference to the border class. The object is owned by View::border_. |
| 150 TextfieldBorder* text_border_; |
| 151 |
| 152 // The x offset for the text to be drawn, without insets; |
| 153 int text_offset_; |
| 154 |
| 155 // Cursor's bounds in the textfield's coordinates. |
| 156 gfx::Rect cursor_bounds_; |
| 157 |
| 158 // True if the textfield is in insert mode. |
| 159 bool insert_; |
| 160 |
| 161 // The drawing state of cursor. True to draw. |
| 162 bool is_cursor_visible_; |
| 163 |
| 164 // A runnable method factory for callback to update the cursor. |
| 165 ScopedRunnableMethodFactory<NativeTextfieldViews> cursor_timer_; |
| 166 |
| 167 DISALLOW_COPY_AND_ASSIGN(NativeTextfieldViews); |
| 168 }; |
| 169 |
| 170 } // namespace views |
| 171 |
| 172 #endif // VIEWS_CONTROLS_TEXTFIELD_NATIVE_TEXTFIELD_VIEWS_H_ |
OLD | NEW |