find_bar_host.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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_HOST_H_
6#define CHROME_BROWSER_UI_VIEWS_FIND_BAR_HOST_H_
7#pragma once
8
9#include "chrome/browser/renderer_host/render_view_host_delegate.h"
10#include "chrome/browser/ui/find_bar/find_bar.h"
11#include "chrome/browser/views/dropdown_bar_host.h"
12#include "gfx/native_widget_types.h"
13#include "gfx/rect.h"
14#include "views/controls/textfield/textfield.h"
15
16class BrowserView;
17class FindBarController;
18class FindBarView;
19class FindNotificationDetails;
20
21////////////////////////////////////////////////////////////////////////////////
22//
23// The FindBarHost implements the container widget for the
24// find-in-page functionality. It uses the appropriate implementation from
25// find_bar_host_win.cc or find_bar_host_gtk.cc to draw its content and is
26// responsible for showing, hiding, closing, and moving the widget if needed,
27// for example if the widget is obscuring the selection results. It also
28// receives notifications about the search results and communicates that to
29// the view.
30//
31// There is one FindBarHost per BrowserView, and its state is updated
32// whenever the selected Tab is changed. The FindBarHost is created when
33// the BrowserView is attached to the frame's Widget for the first time.
34//
35////////////////////////////////////////////////////////////////////////////////
36class FindBarHost : public DropdownBarHost,
37                    public FindBar,
38                    public FindBarTesting {
39 public:
40  explicit FindBarHost(BrowserView* browser_view);
41  virtual ~FindBarHost();
42
43  // Forwards selected keystrokes to the renderer. This is useful to make sure
44  // that arrow keys and PageUp and PageDown result in scrolling, instead of
45  // being eaten because the FindBar has focus. Returns true if the keystroke
46  // was forwarded, false if not.
47  bool MaybeForwardKeystrokeToWebpage(
48      const views::Textfield::Keystroke& key_stroke);
49
50  // FindBar implementation:
51  virtual FindBarController* GetFindBarController() const;
52  virtual void SetFindBarController(FindBarController* find_bar_controller);
53  virtual void Show(bool animate);
54  virtual void Hide(bool animate);
55  virtual void SetFocusAndSelection();
56  virtual void ClearResults(const FindNotificationDetails& results);
57  virtual void StopAnimation();
58  virtual void MoveWindowIfNecessary(const gfx::Rect& selection_rect,
59                                     bool no_redraw);
60  virtual void SetFindText(const string16& find_text);
61  virtual void UpdateUIForFindResult(const FindNotificationDetails& result,
62                                     const string16& find_text);
63  virtual void AudibleAlert();
64  virtual bool IsFindBarVisible();
65  virtual void RestoreSavedFocus();
66  virtual FindBarTesting* GetFindBarTesting();
67
68  // Overridden from views::AcceleratorTarget in DropdownBarHost class:
69  virtual bool AcceleratorPressed(const views::Accelerator& accelerator);
70
71  // FindBarTesting implementation:
72  virtual bool GetFindBarWindowInfo(gfx::Point* position,
73                                    bool* fully_visible);
74  virtual string16 GetFindText();
75  virtual string16 GetFindSelectedText();
76  virtual string16 GetMatchCountText();
77
78  // Overridden from DropdownBarHost:
79  // Returns the rectangle representing where to position the find bar. It uses
80  // GetDialogBounds and positions itself within that, either to the left (if an
81  // InfoBar is present) or to the right (no InfoBar). If
82  // |avoid_overlapping_rect| is specified, the return value will be a rectangle
83  // located immediately to the left of |avoid_overlapping_rect|, as long as
84  // there is enough room for the dialog to draw within the bounds. If not, the
85  // dialog position returned will overlap |avoid_overlapping_rect|.
86  // Note: |avoid_overlapping_rect| is expected to use coordinates relative to
87  // the top of the page area, (it will be converted to coordinates relative to
88  // the top of the browser window, when comparing against the dialog
89  // coordinates). The returned value is relative to the browser window.
90  virtual gfx::Rect GetDialogPosition(gfx::Rect avoid_overlapping_rect);
91  // Moves the dialog window to the provided location, moves it to top in the
92  // z-order (HWND_TOP, not HWND_TOPMOST) and shows the window (if hidden).
93  // It then calls UpdateWindowEdges to make sure we don't overwrite the Chrome
94  // window border. If |no_redraw| is set, the window is getting moved but not
95  // sized, and should not be redrawn to reduce update flicker.
96  virtual void SetDialogPosition(const gfx::Rect& new_pos, bool no_redraw);
97
98  // Retrieves the boundaries that the find bar widget has to work with
99  // within the Chrome frame window. The resulting rectangle will be a
100  // rectangle that overlaps the bottom of the Chrome toolbar by one
101  // pixel (so we can create the illusion that the dropdown widget is
102  // part of the toolbar) and covers the page area, except that we
103  // deflate the rect width by subtracting (from both sides) the width
104  // of the toolbar and some extra pixels to account for the width of
105  // the Chrome window borders. |bounds| is relative to the browser
106  // window. If the function fails to determine the browser
107  // window/client area rectangle or the rectangle for the page area
108  // then |bounds| will be an empty rectangle.
109  virtual void GetWidgetBounds(gfx::Rect* bounds);
110
111  // Additional accelerator handling (on top of what DropDownBarHost does).
112  virtual void RegisterAccelerators();
113  virtual void UnregisterAccelerators();
114
115 private:
116  // Allows implementation to tweak widget position.
117  void GetWidgetPositionNative(gfx::Rect* avoid_overlapping_rect);
118
119  // Allows native implementation to prevent keystrokes from being forwarded.
120  bool ShouldForwardKeystrokeToWebpageNative(
121      const views::Textfield::Keystroke& key_stroke);
122
123  // Returns the FindBarView.
124  FindBarView* find_bar_view();
125
126  // A pointer back to the owning controller.
127  FindBarController* find_bar_controller_;
128
129  DISALLOW_COPY_AND_ASSIGN(FindBarHost);
130};
131
132#endif  // CHROME_BROWSER_UI_VIEWS_FIND_BAR_HOST_H_
133