Home | History | Annotate | Download | only in views
      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 "ui/gfx/native_widget_types.h"
     12 #include "ui/gfx/rect.h"
     13 #include "ui/views/controls/textfield/textfield.h"
     14 
     15 class BrowserView;
     16 class FindBarController;
     17 class FindBarView;
     18 class 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 ////////////////////////////////////////////////////////////////////////////////
     35 class 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 SetFindText(const string16& find_text) OVERRIDE;
     60   virtual void UpdateUIForFindResult(const FindNotificationDetails& result,
     61                                      const string16& find_text) OVERRIDE;
     62   virtual void AudibleAlert() OVERRIDE;
     63   virtual bool IsFindBarVisible() OVERRIDE;
     64   virtual void RestoreSavedFocus() OVERRIDE;
     65   virtual bool HasGlobalFindPasteboard() OVERRIDE;
     66   virtual void UpdateFindBarForChangedWebContents() OVERRIDE;
     67   virtual FindBarTesting* GetFindBarTesting() OVERRIDE;
     68 
     69   // Overridden from ui::AcceleratorTarget in DropdownBarHost class:
     70   virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
     71   virtual bool CanHandleAccelerators() const OVERRIDE;
     72 
     73   // FindBarTesting implementation:
     74   virtual bool GetFindBarWindowInfo(gfx::Point* position,
     75                                     bool* fully_visible) OVERRIDE;
     76   virtual string16 GetFindText() OVERRIDE;
     77   virtual string16 GetFindSelectedText() OVERRIDE;
     78   virtual string16 GetMatchCountText() OVERRIDE;
     79   virtual int GetWidth() OVERRIDE;
     80 
     81   // Overridden from DropdownBarHost:
     82   // Returns the rectangle representing where to position the find bar. It uses
     83   // GetDialogBounds and positions itself within that, either to the left (if an
     84   // InfoBar is present) or to the right (no InfoBar). If
     85   // |avoid_overlapping_rect| is specified, the return value will be a rectangle
     86   // located immediately to the left of |avoid_overlapping_rect|, as long as
     87   // there is enough room for the dialog to draw within the bounds. If not, the
     88   // dialog position returned will overlap |avoid_overlapping_rect|.
     89   // Note: |avoid_overlapping_rect| is expected to use coordinates relative to
     90   // the top of the page area, (it will be converted to coordinates relative to
     91   // the top of the browser window, when comparing against the dialog
     92   // coordinates). The returned value is relative to the browser window.
     93   virtual gfx::Rect GetDialogPosition(
     94       gfx::Rect avoid_overlapping_rect) OVERRIDE;
     95   // Moves the dialog window to the provided location, moves it to top in the
     96   // z-order (HWND_TOP, not HWND_TOPMOST) and shows the window (if hidden).
     97   // It then calls UpdateWindowEdges to make sure we don't overwrite the Chrome
     98   // window border. If |no_redraw| is set, the window is getting moved but not
     99   // sized, and should not be redrawn to reduce update flicker.
    100   virtual void SetDialogPosition(const gfx::Rect& new_pos,
    101                                  bool no_redraw) OVERRIDE;
    102 
    103   // Retrieves the boundaries that the find bar widget has to work with
    104   // within the Chrome frame window. The resulting rectangle will be a
    105   // rectangle that overlaps the bottom of the Chrome toolbar by one
    106   // pixel (so we can create the illusion that the dropdown widget is
    107   // part of the toolbar) and covers the page area, except that we
    108   // deflate the rect width by subtracting (from both sides) the width
    109   // of the toolbar and some extra pixels to account for the width of
    110   // the Chrome window borders. |bounds| is relative to the browser
    111   // window. If the function fails to determine the browser
    112   // window/client area rectangle or the rectangle for the page area
    113   // then |bounds| will be an empty rectangle.
    114   virtual void GetWidgetBounds(gfx::Rect* bounds) OVERRIDE;
    115 
    116   // Additional accelerator handling (on top of what DropDownBarHost does).
    117   virtual void RegisterAccelerators() OVERRIDE;
    118   virtual void UnregisterAccelerators() OVERRIDE;
    119 
    120  protected:
    121   // Overridden from DropdownBarHost:
    122   virtual void OnVisibilityChanged() OVERRIDE;
    123 
    124  private:
    125   // Allows implementation to tweak widget position.
    126   void GetWidgetPositionNative(gfx::Rect* avoid_overlapping_rect);
    127 
    128   // Allows native implementation to prevent key events from being forwarded.
    129   bool ShouldForwardKeyEventToWebpageNative(const ui::KeyEvent& key_event);
    130 
    131   // Returns the FindBarView.
    132   FindBarView* find_bar_view();
    133 
    134   // A pointer back to the owning controller.
    135   FindBarController* find_bar_controller_;
    136 
    137   DISALLOW_COPY_AND_ASSIGN(FindBarHost);
    138 };
    139 
    140 #endif  // CHROME_BROWSER_UI_VIEWS_FIND_BAR_HOST_H_
    141