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