decorated_textfield.h revision f2477e01787aa58f445919b809d89e252beef54f
1// Copyright 2013 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 CHROME_BROWSER_UI_VIEWS_AUTOFILL_DECORATED_TEXTFIELD_H_ 6#define CHROME_BROWSER_UI_VIEWS_AUTOFILL_DECORATED_TEXTFIELD_H_ 7 8#include "base/memory/scoped_ptr.h" 9#include "base/strings/string16.h" 10#include "ui/gfx/image/image.h" 11#include "ui/views/controls/textfield/textfield.h" 12#include "ui/views/controls/textfield/textfield.h" 13 14namespace views { 15class FocusableBorder; 16class TextfieldController; 17} 18 19namespace autofill { 20 21// A class which holds a textfield and draws extra stuff on top, like 22// invalid content indications. 23class DecoratedTextfield : public views::Textfield { 24 public: 25 static const char kViewClassName[]; 26 27 DecoratedTextfield(const base::string16& default_value, 28 const base::string16& placeholder, 29 views::TextfieldController* controller); 30 virtual ~DecoratedTextfield(); 31 32 // Sets whether to indicate the textfield has invalid content. 33 void SetInvalid(bool invalid); 34 bool invalid() const { return invalid_; } 35 36 // See docs for |editable_|. 37 void SetEditable(bool editable); 38 bool editable() const { return editable_; } 39 40 // Sets the icon to be displayed inside the textfield at the end of the 41 // text. 42 void SetIcon(const gfx::Image& icon); 43 44 // Sets a tooltip for this field. This will override the icon set with 45 // SetIcon(), if any, and will be overridden by future calls to SetIcon(). 46 void SetTooltipIcon(const base::string16& text); 47 48 // views::Textfield implementation. 49 virtual base::string16 GetPlaceholderText() const OVERRIDE; 50 51 // views::View implementation. 52 virtual const char* GetClassName() const OVERRIDE; 53 virtual gfx::Size GetPreferredSize() OVERRIDE; 54 virtual void Layout() OVERRIDE; 55 virtual views::View* GetEventHandlerForRect(const gfx::Rect& rect) OVERRIDE; 56 virtual void OnFocus() OVERRIDE; 57 virtual void OnBlur() OVERRIDE; 58 59 private: 60 FRIEND_TEST_ALL_PREFIXES(DecoratedTextfieldTest, HeightMatchesButton); 61 62 // Updates the background of |this| after it may have changed. This is 63 // necessary for the sake of the padding around the native textfield. 64 void UpdateBackground(); 65 66 // Called to update the layout after SetIcon or SetTooltipIcon has been 67 // called. 68 void IconChanged(); 69 70 // This number corresponds to the number of pixels in the images that 71 // are used to draw a views button which are above or below the actual border. 72 // This number is encoded in the button assets themselves, so there's no other 73 // way to get it than to hardcode it here. 74 static const int kMagicInsetNumber; 75 76 // We draw the border. 77 views::FocusableBorder* border_; // Weak. 78 79 // The view that holds the icon at the end of the textfield. 80 scoped_ptr<views::ImageView> icon_view_; 81 82 // Whether the text contents are "invalid" (i.e. should special markers be 83 // shown to indicate invalidness). 84 bool invalid_; 85 86 // Whether the user can edit the field. When not editable, many of the 87 // pieces of the textfield disappear (border, background, icon, placeholder 88 // text) and it can't receive focus. 89 bool editable_; 90 91 DISALLOW_COPY_AND_ASSIGN(DecoratedTextfield); 92}; 93 94} // namespace autofill 95 96#endif // CHROME_BROWSER_UI_VIEWS_AUTOFILL_DECORATED_TEXTFIELD_H_ 97