15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_UI_VIEWS_FIND_BAR_HOST_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_UI_VIEWS_FIND_BAR_HOST_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/find_bar/find_bar.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/views/dropdown_bar_host.h"
114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/browser/ui/views/find_bar_view.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/native_widget_types.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/rect.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/views/controls/textfield/textfield.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BrowserView;
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FindBarController;
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FindNotificationDetails;
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The FindBarHost implements the container widget for the
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// find-in-page functionality. It uses the implementation from
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// find_bar_host_aura.cc to draw its content and is responsible for showing,
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// hiding, closing, and moving the widget if needed, for example if the widget
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// is obscuring the selection results. It also receives notifications about the
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// search results and communicates that to the view.
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// There is one FindBarHost per BrowserView, and its state is updated
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// whenever the selected Tab is changed. The FindBarHost is created when
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the BrowserView is attached to the frame's Widget for the first time.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FindBarHost : public DropdownBarHost,
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    public FindBar,
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    public FindBarTesting {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit FindBarHost(BrowserView* browser_view);
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~FindBarHost();
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Forwards selected key events to the renderer. This is useful to make sure
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that arrow keys and PageUp and PageDown result in scrolling, instead of
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // being eaten because the FindBar has focus. Returns true if the keystroke
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // was forwarded, false if not.
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool MaybeForwardKeyEventToWebpage(const ui::KeyEvent& key_event);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // FindBar implementation:
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual FindBarController* GetFindBarController() const OVERRIDE;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetFindBarController(
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      FindBarController* find_bar_controller) OVERRIDE;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Show(bool animate) OVERRIDE;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Hide(bool animate) OVERRIDE;
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetFocusAndSelection() OVERRIDE;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void ClearResults(const FindNotificationDetails& results) OVERRIDE;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void StopAnimation() OVERRIDE;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void MoveWindowIfNecessary(const gfx::Rect& selection_rect,
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     bool no_redraw) OVERRIDE;
584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  virtual void SetFindTextAndSelectedRange(
59a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      const base::string16& find_text,
604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      const gfx::Range& selected_range) OVERRIDE;
61a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual base::string16 GetFindText() OVERRIDE;
624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  virtual gfx::Range GetSelectedRange() OVERRIDE;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void UpdateUIForFindResult(const FindNotificationDetails& result,
64a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                     const base::string16& find_text) OVERRIDE;
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void AudibleAlert() OVERRIDE;
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsFindBarVisible() OVERRIDE;
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void RestoreSavedFocus() OVERRIDE;
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool HasGlobalFindPasteboard() OVERRIDE;
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void UpdateFindBarForChangedWebContents() OVERRIDE;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual FindBarTesting* GetFindBarTesting() OVERRIDE;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from ui::AcceleratorTarget in DropdownBarHost class:
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool CanHandleAccelerators() const OVERRIDE;
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // FindBarTesting implementation:
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetFindBarWindowInfo(gfx::Point* position,
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    bool* fully_visible) OVERRIDE;
79a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual base::string16 GetFindSelectedText() OVERRIDE;
80a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual base::string16 GetMatchCountText() OVERRIDE;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int GetWidth() OVERRIDE;
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from DropdownBarHost:
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the rectangle representing where to position the find bar. It uses
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // GetDialogBounds and positions itself within that, either to the left (if an
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // InfoBar is present) or to the right (no InfoBar). If
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |avoid_overlapping_rect| is specified, the return value will be a rectangle
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // located immediately to the left of |avoid_overlapping_rect|, as long as
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // there is enough room for the dialog to draw within the bounds. If not, the
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // dialog position returned will overlap |avoid_overlapping_rect|.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note: |avoid_overlapping_rect| is expected to use coordinates relative to
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the top of the page area, (it will be converted to coordinates relative to
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the top of the browser window, when comparing against the dialog
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // coordinates). The returned value is relative to the browser window.
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual gfx::Rect GetDialogPosition(
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      gfx::Rect avoid_overlapping_rect) OVERRIDE;
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Moves the dialog window to the provided location, moves it to top in the
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // z-order (HWND_TOP, not HWND_TOPMOST) and shows the window (if hidden).
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It then calls UpdateWindowEdges to make sure we don't overwrite the Chrome
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // window border. If |no_redraw| is set, the window is getting moved but not
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // sized, and should not be redrawn to reduce update flicker.
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetDialogPosition(const gfx::Rect& new_pos,
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 bool no_redraw) OVERRIDE;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Retrieves the boundaries that the find bar widget has to work with
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // within the Chrome frame window. The resulting rectangle will be a
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // rectangle that overlaps the bottom of the Chrome toolbar by one
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // pixel (so we can create the illusion that the dropdown widget is
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // part of the toolbar) and covers the page area, except that we
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // deflate the rect width by subtracting (from both sides) the width
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of the toolbar and some extra pixels to account for the width of
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the Chrome window borders. |bounds| is relative to the browser
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // window. If the function fails to determine the browser
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // window/client area rectangle or the rectangle for the page area
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // then |bounds| will be an empty rectangle.
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void GetWidgetBounds(gfx::Rect* bounds) OVERRIDE;
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Additional accelerator handling (on top of what DropDownBarHost does).
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void RegisterAccelerators() OVERRIDE;
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void UnregisterAccelerators() OVERRIDE;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) protected:
1237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Overridden from DropdownBarHost:
1247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void OnVisibilityChanged() OVERRIDE;
1257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Allows implementation to tweak widget position.
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void GetWidgetPositionNative(gfx::Rect* avoid_overlapping_rect);
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Allows native implementation to prevent key events from being forwarded.
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ShouldForwardKeyEventToWebpageNative(const ui::KeyEvent& key_event);
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the FindBarView.
1344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  FindBarView* find_bar_view() { return static_cast<FindBarView*>(view()); }
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A pointer back to the owning controller.
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FindBarController* find_bar_controller_;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(FindBarHost);
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_UI_VIEWS_FIND_BAR_HOST_H_
143