Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / find_bar_view.h
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/memory/scoped_ptr.h"
10 #include "base/strings/string16.h"
11 #include "chrome/browser/ui/views/dropdown_bar_view.h"
12 #include "ui/views/controls/button/button.h"
13 #include "ui/views/controls/textfield/textfield.h"
14 #include "ui/views/controls/textfield/textfield_controller.h"
15
16 class FindBarHost;
17 class FindNotificationDetails;
18
19 namespace views {
20 class ImageButton;
21 class Label;
22 class MouseEvent;
23 class Painter;
24 }
25
26 ////////////////////////////////////////////////////////////////////////////////
27 //
28 // The FindBarView is responsible for drawing the UI controls of the
29 // FindBar, the find text box, the 'Find' button and the 'Close'
30 // button. It communicates the user search words to the FindBarHost.
31 //
32 ////////////////////////////////////////////////////////////////////////////////
33 class FindBarView : public DropdownBarView,
34                     public views::ButtonListener,
35                     public views::TextfieldController {
36  public:
37   // A tag denoting which button the user pressed.
38   enum ButtonTag {
39     FIND_PREVIOUS_TAG = 0,  // The Find Previous button.
40     FIND_NEXT_TAG,          // The Find Next button.
41     CLOSE_TAG,              // The Close button (the 'X').
42   };
43
44   explicit FindBarView(FindBarHost* host);
45   ~FindBarView() override;
46
47   // Accessors for the text and selection displayed in the text box.
48   void SetFindTextAndSelectedRange(const base::string16& find_text,
49                                    const gfx::Range& selected_range);
50   base::string16 GetFindText() const;
51   gfx::Range GetSelectedRange() const;
52
53   // Gets the selected text in the text box.
54   base::string16 GetFindSelectedText() const;
55
56   // Gets the match count text displayed in the text box.
57   base::string16 GetMatchCountText() const;
58
59   // Updates the label inside the Find text box that shows the ordinal of the
60   // active item and how many matches were found.
61   void UpdateForResult(const FindNotificationDetails& result,
62                        const base::string16& find_text);
63
64   // Clears the current Match Count value in the Find text box.
65   void ClearMatchCount();
66
67   // Claims focus for the text field and selects its contents.
68   void SetFocusAndSelection(bool select_all) override;
69
70   // views::View:
71   void OnPaint(gfx::Canvas* canvas) override;
72   void Layout() override;
73   gfx::Size GetPreferredSize() const override;
74
75   // views::ButtonListener:
76   void ButtonPressed(views::Button* sender, const ui::Event& event) override;
77
78   // views::TextfieldController:
79   bool HandleKeyEvent(views::Textfield* sender,
80                       const ui::KeyEvent& key_event) override;
81   void OnAfterUserAction(views::Textfield* sender) override;
82   void OnAfterPaste() override;
83
84  private:
85   // Starts finding |search_text|.  If the text is empty, stops finding.
86   void Find(const base::string16& search_text);
87
88   // Updates the appearance for the match count label.
89   void UpdateMatchCountAppearance(bool no_match);
90
91   // views::View:
92   void OnThemeChanged() override;
93
94   // We use a hidden view to grab mouse clicks and bring focus to the find
95   // text box. This is because although the find text box may look like it
96   // extends all the way to the find button, it only goes as far as to the
97   // match_count label. The user, however, expects being able to click anywhere
98   // inside what looks like the find text box (including on or around the
99   // match_count label) and have focus brought to the find box.
100   class FocusForwarderView : public views::View {
101    public:
102     explicit FocusForwarderView(
103         views::Textfield* view_to_focus_on_mousedown)
104       : view_to_focus_on_mousedown_(view_to_focus_on_mousedown) {}
105
106    private:
107     bool OnMousePressed(const ui::MouseEvent& event) override;
108
109     views::Textfield* view_to_focus_on_mousedown_;
110
111     DISALLOW_COPY_AND_ASSIGN(FocusForwarderView);
112   };
113
114   // Returns the OS-specific view for the find bar that acts as an intermediary
115   // between us and the WebContentsView.
116   FindBarHost* find_bar_host() const;
117
118   // Used to detect if the input text, not including the IME composition text,
119   // has changed or not.
120   base::string16 last_searched_text_;
121
122   // The controls in the window.
123   views::Textfield* find_text_;
124   scoped_ptr<views::Painter> find_text_border_;
125   views::Label* match_count_text_;
126   FocusForwarderView* focus_forwarder_view_;
127   views::ImageButton* find_previous_button_;
128   views::ImageButton* find_next_button_;
129   views::ImageButton* close_button_;
130
131   // The preferred height of the find bar.
132   int preferred_height_;
133
134   DISALLOW_COPY_AND_ASSIGN(FindBarView);
135 };
136
137 #endif  // CHROME_BROWSER_UI_VIEWS_FIND_BAR_VIEW_H_