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_HOST_H_ 6 #define CHROME_BROWSER_UI_VIEWS_FIND_BAR_HOST_H_ 7 #pragma once 8 9 #include "chrome/browser/ui/find_bar/find_bar.h" 10 #include "chrome/browser/ui/views/dropdown_bar_host.h" 11 #include "content/browser/renderer_host/render_view_host_delegate.h" 12 #include "ui/gfx/native_widget_types.h" 13 #include "ui/gfx/rect.h" 14 #include "views/controls/textfield/textfield.h" 15 16 class BrowserView; 17 class FindBarController; 18 class FindBarView; 19 class 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 //////////////////////////////////////////////////////////////////////////////// 36 class FindBarHost : public DropdownBarHost, 37 public FindBar, 38 public FindBarTesting { 39 public: 40 explicit FindBarHost(BrowserView* browser_view); 41 virtual ~FindBarHost(); 42 43 // Forwards selected key events 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 MaybeForwardKeyEventToWebpage(const views::KeyEvent& key_event); 48 49 // FindBar implementation: 50 virtual FindBarController* GetFindBarController() const; 51 virtual void SetFindBarController(FindBarController* find_bar_controller); 52 virtual void Show(bool animate); 53 virtual void Hide(bool animate); 54 virtual void SetFocusAndSelection(); 55 virtual void ClearResults(const FindNotificationDetails& results); 56 virtual void StopAnimation(); 57 virtual void MoveWindowIfNecessary(const gfx::Rect& selection_rect, 58 bool no_redraw); 59 virtual void SetFindText(const string16& find_text); 60 virtual void UpdateUIForFindResult(const FindNotificationDetails& result, 61 const string16& find_text); 62 virtual void AudibleAlert(); 63 virtual bool IsFindBarVisible(); 64 virtual void RestoreSavedFocus(); 65 virtual FindBarTesting* GetFindBarTesting(); 66 67 // Overridden from views::AcceleratorTarget in DropdownBarHost class: 68 virtual bool AcceleratorPressed(const views::Accelerator& accelerator); 69 70 // FindBarTesting implementation: 71 virtual bool GetFindBarWindowInfo(gfx::Point* position, 72 bool* fully_visible); 73 virtual string16 GetFindText(); 74 virtual string16 GetFindSelectedText(); 75 virtual string16 GetMatchCountText(); 76 77 // Overridden from DropdownBarHost: 78 // Returns the rectangle representing where to position the find bar. It uses 79 // GetDialogBounds and positions itself within that, either to the left (if an 80 // InfoBar is present) or to the right (no InfoBar). If 81 // |avoid_overlapping_rect| is specified, the return value will be a rectangle 82 // located immediately to the left of |avoid_overlapping_rect|, as long as 83 // there is enough room for the dialog to draw within the bounds. If not, the 84 // dialog position returned will overlap |avoid_overlapping_rect|. 85 // Note: |avoid_overlapping_rect| is expected to use coordinates relative to 86 // the top of the page area, (it will be converted to coordinates relative to 87 // the top of the browser window, when comparing against the dialog 88 // coordinates). The returned value is relative to the browser window. 89 virtual gfx::Rect GetDialogPosition(gfx::Rect avoid_overlapping_rect); 90 // Moves the dialog window to the provided location, moves it to top in the 91 // z-order (HWND_TOP, not HWND_TOPMOST) and shows the window (if hidden). 92 // It then calls UpdateWindowEdges to make sure we don't overwrite the Chrome 93 // window border. If |no_redraw| is set, the window is getting moved but not 94 // sized, and should not be redrawn to reduce update flicker. 95 virtual void SetDialogPosition(const gfx::Rect& new_pos, bool no_redraw); 96 97 // Retrieves the boundaries that the find bar widget has to work with 98 // within the Chrome frame window. The resulting rectangle will be a 99 // rectangle that overlaps the bottom of the Chrome toolbar by one 100 // pixel (so we can create the illusion that the dropdown widget is 101 // part of the toolbar) and covers the page area, except that we 102 // deflate the rect width by subtracting (from both sides) the width 103 // of the toolbar and some extra pixels to account for the width of 104 // the Chrome window borders. |bounds| is relative to the browser 105 // window. If the function fails to determine the browser 106 // window/client area rectangle or the rectangle for the page area 107 // then |bounds| will be an empty rectangle. 108 virtual void GetWidgetBounds(gfx::Rect* bounds); 109 110 // Additional accelerator handling (on top of what DropDownBarHost does). 111 virtual void RegisterAccelerators(); 112 virtual void UnregisterAccelerators(); 113 114 private: 115 // Allows implementation to tweak widget position. 116 void GetWidgetPositionNative(gfx::Rect* avoid_overlapping_rect); 117 118 // Allows native implementation to prevent key events from being forwarded. 119 bool ShouldForwardKeyEventToWebpageNative( 120 const views::KeyEvent& key_event); 121 122 // Returns the FindBarView. 123 FindBarView* find_bar_view(); 124 125 // A pointer back to the owning controller. 126 FindBarController* find_bar_controller_; 127 128 DISALLOW_COPY_AND_ASSIGN(FindBarHost); 129 }; 130 131 #endif // CHROME_BROWSER_UI_VIEWS_FIND_BAR_HOST_H_ 132