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 CHROME_BROWSER_UI_VIEWS_FIND_BAR_VIEW_H_ 6 #define CHROME_BROWSER_UI_VIEWS_FIND_BAR_VIEW_H_ 7 8 #include "base/compiler_specific.h" 9 #include "base/strings/string16.h" 10 #include "chrome/browser/ui/views/dropdown_bar_view.h" 11 #include "ui/views/controls/button/button.h" 12 #include "ui/views/controls/textfield/textfield.h" 13 #include "ui/views/controls/textfield/textfield_controller.h" 14 15 class FindBarHost; 16 class FindNotificationDetails; 17 18 namespace views { 19 class ImageButton; 20 class Label; 21 class MouseEvent; 22 class View; 23 } 24 25 //////////////////////////////////////////////////////////////////////////////// 26 // 27 // The FindBarView is responsible for drawing the UI controls of the 28 // FindBar, the find text box, the 'Find' button and the 'Close' 29 // button. It communicates the user search words to the FindBarHost. 30 // 31 //////////////////////////////////////////////////////////////////////////////// 32 class FindBarView : public DropdownBarView, 33 public views::ButtonListener, 34 public views::TextfieldController { 35 public: 36 // A tag denoting which button the user pressed. 37 enum ButtonTag { 38 FIND_PREVIOUS_TAG = 0, // The Find Previous button. 39 FIND_NEXT_TAG, // The Find Next button. 40 CLOSE_TAG, // The Close button (the 'X'). 41 }; 42 43 explicit FindBarView(FindBarHost* host); 44 virtual ~FindBarView(); 45 46 // Gets/sets the text displayed in the text box. 47 string16 GetFindText() const; 48 void SetFindText(const string16& find_text); 49 50 // Gets the selected text in the text box. 51 string16 GetFindSelectedText() const; 52 53 // Gets the match count text displayed in the text box. 54 string16 GetMatchCountText() const; 55 56 // Updates the label inside the Find text box that shows the ordinal of the 57 // active item and how many matches were found. 58 void UpdateForResult(const FindNotificationDetails& result, 59 const string16& find_text); 60 61 // Clears the current Match Count value in the Find text box. 62 void ClearMatchCount(); 63 64 // Claims focus for the text field and selects its contents. 65 virtual void SetFocusAndSelection(bool select_all) OVERRIDE; 66 67 // views::View: 68 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; 69 virtual void Layout() OVERRIDE; 70 virtual gfx::Size GetPreferredSize() OVERRIDE; 71 virtual void ViewHierarchyChanged( 72 const ViewHierarchyChangedDetails& details) OVERRIDE; 73 74 // views::ButtonListener: 75 virtual void ButtonPressed(views::Button* sender, 76 const ui::Event& event) OVERRIDE; 77 78 // views::TextfieldController: 79 virtual void ContentsChanged(views::Textfield* sender, 80 const string16& new_contents) OVERRIDE; 81 virtual bool HandleKeyEvent(views::Textfield* sender, 82 const ui::KeyEvent& key_event) OVERRIDE; 83 virtual void OnAfterUserAction(views::Textfield* sender) OVERRIDE; 84 virtual void OnAfterPaste() OVERRIDE; 85 86 private: 87 // Starts finding |search_text|. If the text is empty, stops finding. 88 void Find(const string16& search_text); 89 90 // Updates the appearance for the match count label. 91 void UpdateMatchCountAppearance(bool no_match); 92 93 // views::View: 94 virtual void OnThemeChanged() OVERRIDE; 95 96 // We use a hidden view to grab mouse clicks and bring focus to the find 97 // text box. This is because although the find text box may look like it 98 // extends all the way to the find button, it only goes as far as to the 99 // match_count label. The user, however, expects being able to click anywhere 100 // inside what looks like the find text box (including on or around the 101 // match_count label) and have focus brought to the find box. 102 class FocusForwarderView : public views::View { 103 public: 104 explicit FocusForwarderView( 105 views::Textfield* view_to_focus_on_mousedown) 106 : view_to_focus_on_mousedown_(view_to_focus_on_mousedown) {} 107 108 private: 109 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; 110 111 views::Textfield* view_to_focus_on_mousedown_; 112 113 DISALLOW_COPY_AND_ASSIGN(FocusForwarderView); 114 }; 115 116 // A wrapper of views::TextField that allows us to select all text when we 117 // get focus. Represents the text field where the user enters a search term. 118 class SearchTextfieldView : public views::Textfield { 119 public: 120 SearchTextfieldView(); 121 virtual ~SearchTextfieldView(); 122 123 // views::View: 124 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; 125 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; 126 127 // views::Textfield: 128 virtual void OnFocus() OVERRIDE; 129 130 private: 131 bool select_all_on_focus_; 132 133 DISALLOW_COPY_AND_ASSIGN(SearchTextfieldView); 134 }; 135 136 // Returns the OS-specific view for the find bar that acts as an intermediary 137 // between us and the WebContentsView. 138 FindBarHost* find_bar_host() const; 139 140 // Used to detect if the input text, not including the IME composition text, 141 // has changed or not. 142 string16 last_searched_text_; 143 144 // The controls in the window. 145 SearchTextfieldView* find_text_; 146 views::Label* match_count_text_; 147 FocusForwarderView* focus_forwarder_view_; 148 views::ImageButton* find_previous_button_; 149 views::ImageButton* find_next_button_; 150 views::ImageButton* close_button_; 151 152 // The preferred height of the find bar. 153 int preferred_height_; 154 155 // The background image for the Find text box, which we draw behind the Find 156 // box to provide the Chrome look to the edge of the text box. 157 const gfx::ImageSkia* text_box_background_; 158 159 // The rounded edge on the left side of the Find text box. 160 const gfx::ImageSkia* text_box_background_left_; 161 162 DISALLOW_COPY_AND_ASSIGN(FindBarView); 163 }; 164 165 #endif // CHROME_BROWSER_UI_VIEWS_FIND_BAR_VIEW_H_ 166