find_bar_view.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2011 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#pragma once
8
9#include "base/string16.h"
10#include "chrome/browser/ui/find_bar/find_notification_details.h"
11#include "chrome/browser/ui/views/dropdown_bar_view.h"
12#include "ui/gfx/size.h"
13#include "views/controls/button/button.h"
14#include "views/controls/textfield/textfield.h"
15
16class FindBarHost;
17
18namespace views {
19class ImageButton;
20class Label;
21class MouseEvent;
22class 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////////////////////////////////////////////////////////////////////////////////
32class FindBarView : public DropdownBarView,
33                    public views::ButtonListener,
34                    public views::Textfield::Controller {
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);
66
67  // Overridden from views::View:
68  virtual void Paint(gfx::Canvas* canvas);
69  virtual void Layout();
70  virtual gfx::Size GetPreferredSize();
71  virtual void ViewHierarchyChanged(bool is_add,
72                                    views::View* parent,
73                                    views::View* child);
74
75  // Overridden from views::ButtonListener:
76  virtual void ButtonPressed(views::Button* sender, const views::Event& event);
77
78  // Overridden from views::Textfield::Controller:
79  virtual void ContentsChanged(views::Textfield* sender,
80                               const string16& new_contents);
81  virtual bool HandleKeyEvent(views::Textfield* sender,
82                              const views::KeyEvent& key_event);
83
84 private:
85  // Update the appearance for the match count label.
86  void UpdateMatchCountAppearance(bool no_match);
87
88  // Overridden from views::View.
89  virtual void OnThemeChanged();
90
91  // We use a hidden view to grab mouse clicks and bring focus to the find
92  // text box. This is because although the find text box may look like it
93  // extends all the way to the find button, it only goes as far as to the
94  // match_count label. The user, however, expects being able to click anywhere
95  // inside what looks like the find text box (including on or around the
96  // match_count label) and have focus brought to the find box.
97  class FocusForwarderView : public views::View {
98   public:
99    explicit FocusForwarderView(
100        views::Textfield* view_to_focus_on_mousedown)
101      : view_to_focus_on_mousedown_(view_to_focus_on_mousedown) {}
102
103   private:
104    virtual bool OnMousePressed(const views::MouseEvent& event);
105
106    views::Textfield* view_to_focus_on_mousedown_;
107
108    DISALLOW_COPY_AND_ASSIGN(FocusForwarderView);
109  };
110
111  // A wrapper of views::TextField that allows us to select all text when we
112  // get focus. Represents the text field where the user enters a search term.
113  class SearchTextfieldView : public views::Textfield {
114   public:
115     SearchTextfieldView();
116     virtual ~SearchTextfieldView();
117
118     virtual void RequestFocus();
119
120   private:
121     DISALLOW_COPY_AND_ASSIGN(SearchTextfieldView);
122  };
123
124  // Returns the OS-specific view for the find bar that acts as an intermediary
125  // between us and the TabContentsView.
126  FindBarHost* find_bar_host() const;
127
128#if defined(OS_LINUX)
129  // In GTK we get changed signals if we programmatically set the text. If we
130  // don't ignore them we run into problems. For example, switching tabs back
131  // to one with the find bar visible will cause a search to the next found
132  // text. Also if the find bar had been visible and then hidden and the user
133  // switches back, found text will be highlighted again.
134  bool ignore_contents_changed_;
135#endif
136
137  // The controls in the window.
138  SearchTextfieldView* find_text_;
139  views::Label* match_count_text_;
140  FocusForwarderView* focus_forwarder_view_;
141  views::ImageButton* find_previous_button_;
142  views::ImageButton* find_next_button_;
143  views::ImageButton* close_button_;
144
145  DISALLOW_COPY_AND_ASSIGN(FindBarView);
146};
147
148#endif  // CHROME_BROWSER_UI_VIEWS_FIND_BAR_VIEW_H_
149