Home | History | Annotate | Download | only in views
      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_VIEW_H_
      6 #define CHROME_BROWSER_UI_VIEWS_FIND_BAR_VIEW_H_
      7 #pragma once
      8 
      9 #include "base/string16.h"
     10 #include "chrome/browser/ui/find_bar/find_notification_details.h"
     11 #include "chrome/browser/ui/views/dropdown_bar_view.h"
     12 #include "ui/gfx/size.h"
     13 #include "views/controls/button/button.h"
     14 #include "views/controls/textfield/textfield.h"
     15 #include "views/controls/textfield/textfield_controller.h"
     16 
     17 class FindBarHost;
     18 
     19 namespace views {
     20 class ImageButton;
     21 class Label;
     22 class MouseEvent;
     23 class View;
     24 }
     25 
     26 ////////////////////////////////////////////////////////////////////////////////
     27 //
     28 // The FindBarView is responsible for drawing the UI controls of the
     29 // FindBar, the find text box, the 'Find' button and the 'Close'
     30 // button. It communicates the user search words to the FindBarHost.
     31 //
     32 ////////////////////////////////////////////////////////////////////////////////
     33 class FindBarView : public DropdownBarView,
     34                     public views::ButtonListener,
     35                     public views::TextfieldController {
     36  public:
     37   // A tag denoting which button the user pressed.
     38   enum ButtonTag {
     39     FIND_PREVIOUS_TAG = 0,  // The Find Previous button.
     40     FIND_NEXT_TAG,          // The Find Next button.
     41     CLOSE_TAG,              // The Close button (the 'X').
     42   };
     43 
     44   explicit FindBarView(FindBarHost* host);
     45   virtual ~FindBarView();
     46 
     47   // Gets/sets the text displayed in the text box.
     48   string16 GetFindText() const;
     49   void SetFindText(const string16& find_text);
     50 
     51   // Gets the selected text in the text box.
     52   string16 GetFindSelectedText() const;
     53 
     54   // Gets the match count text displayed in the text box.
     55   string16 GetMatchCountText() const;
     56 
     57   // Updates the label inside the Find text box that shows the ordinal of the
     58   // active item and how many matches were found.
     59   void UpdateForResult(const FindNotificationDetails& result,
     60                        const string16& find_text);
     61 
     62   // Clears the current Match Count value in the Find text box.
     63   void ClearMatchCount();
     64 
     65   // Claims focus for the text field and selects its contents.
     66   virtual void SetFocusAndSelection(bool select_all);
     67 
     68   // views::View:
     69   virtual void OnPaint(gfx::Canvas* canvas);
     70   virtual void Layout();
     71   virtual gfx::Size GetPreferredSize();
     72   virtual void ViewHierarchyChanged(bool is_add,
     73                                     views::View* parent,
     74                                     views::View* child);
     75 
     76   // views::ButtonListener:
     77   virtual void ButtonPressed(views::Button* sender, const views::Event& event);
     78 
     79   // views::TextfieldController:
     80   virtual void ContentsChanged(views::Textfield* sender,
     81                                const string16& new_contents);
     82   virtual bool HandleKeyEvent(views::Textfield* sender,
     83                               const views::KeyEvent& key_event);
     84 
     85  private:
     86   // Update the appearance for the match count label.
     87   void UpdateMatchCountAppearance(bool no_match);
     88 
     89   // Overridden from views::View.
     90   virtual void OnThemeChanged();
     91 
     92   // We use a hidden view to grab mouse clicks and bring focus to the find
     93   // text box. This is because although the find text box may look like it
     94   // extends all the way to the find button, it only goes as far as to the
     95   // match_count label. The user, however, expects being able to click anywhere
     96   // inside what looks like the find text box (including on or around the
     97   // match_count label) and have focus brought to the find box.
     98   class FocusForwarderView : public views::View {
     99    public:
    100     explicit FocusForwarderView(
    101         views::Textfield* view_to_focus_on_mousedown)
    102       : view_to_focus_on_mousedown_(view_to_focus_on_mousedown) {}
    103 
    104    private:
    105     virtual bool OnMousePressed(const views::MouseEvent& event);
    106 
    107     views::Textfield* view_to_focus_on_mousedown_;
    108 
    109     DISALLOW_COPY_AND_ASSIGN(FocusForwarderView);
    110   };
    111 
    112   // A wrapper of views::TextField that allows us to select all text when we
    113   // get focus. Represents the text field where the user enters a search term.
    114   class SearchTextfieldView : public views::Textfield {
    115    public:
    116      SearchTextfieldView();
    117      virtual ~SearchTextfieldView();
    118 
    119      virtual void RequestFocus();
    120 
    121    private:
    122      DISALLOW_COPY_AND_ASSIGN(SearchTextfieldView);
    123   };
    124 
    125   // Returns the OS-specific view for the find bar that acts as an intermediary
    126   // between us and the TabContentsView.
    127   FindBarHost* find_bar_host() const;
    128 
    129   // The controls in the window.
    130   SearchTextfieldView* find_text_;
    131   views::Label* match_count_text_;
    132   FocusForwarderView* focus_forwarder_view_;
    133   views::ImageButton* find_previous_button_;
    134   views::ImageButton* find_next_button_;
    135   views::ImageButton* close_button_;
    136 
    137   DISALLOW_COPY_AND_ASSIGN(FindBarView);
    138 };
    139 
    140 #endif  // CHROME_BROWSER_UI_VIEWS_FIND_BAR_VIEW_H_
    141