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