Home | History | Annotate | Download | only in omnibox
      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_OMNIBOX_OMNIBOX_VIEW_VIEWS_H_
      6 #define CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_VIEW_VIEWS_H_
      7 
      8 #include <set>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "chrome/browser/ui/omnibox/omnibox_view.h"
     15 #include "chrome/browser/ui/toolbar/toolbar_model.h"
     16 #include "ui/base/window_open_disposition.h"
     17 #include "ui/gfx/range/range.h"
     18 #include "ui/views/controls/textfield/textfield.h"
     19 #include "ui/views/controls/textfield/textfield_controller.h"
     20 
     21 #if defined(OS_CHROMEOS)
     22 #include "chromeos/ime/input_method_manager.h"
     23 #endif
     24 
     25 class LocationBarView;
     26 class OmniboxPopupView;
     27 class Profile;
     28 
     29 namespace gfx {
     30 class RenderText;
     31 }
     32 
     33 namespace ui {
     34 class OSExchangeData;
     35 }  // namespace ui
     36 
     37 // Views-implementation of OmniboxView.
     38 class OmniboxViewViews
     39     : public OmniboxView,
     40       public views::Textfield,
     41 #if defined(OS_CHROMEOS)
     42       public
     43           chromeos::input_method::InputMethodManager::CandidateWindowObserver,
     44 #endif
     45       public views::TextfieldController {
     46  public:
     47   // The internal view class name.
     48   static const char kViewClassName[];
     49 
     50   OmniboxViewViews(OmniboxEditController* controller,
     51                    Profile* profile,
     52                    CommandUpdater* command_updater,
     53                    bool popup_window_mode,
     54                    LocationBarView* location_bar,
     55                    const gfx::FontList& font_list);
     56   virtual ~OmniboxViewViews();
     57 
     58   // Initialize, create the underlying views, etc.
     59   void Init();
     60 
     61   // Exposes the RenderText for tests.
     62 #if defined(UNIT_TEST)
     63   gfx::RenderText* GetRenderText() {
     64     return views::Textfield::GetRenderText();
     65   }
     66 #endif
     67 
     68   // OmniboxView:
     69   virtual void SaveStateToTab(content::WebContents* tab) OVERRIDE;
     70   virtual void OnTabChanged(const content::WebContents* web_contents) OVERRIDE;
     71   virtual void Update() OVERRIDE;
     72   virtual void UpdatePlaceholderText() OVERRIDE;
     73   virtual base::string16 GetText() const OVERRIDE;
     74   virtual void SetUserText(const base::string16& text,
     75                            const base::string16& display_text,
     76                            bool update_popup) OVERRIDE;
     77   virtual void SetForcedQuery() OVERRIDE;
     78   virtual void GetSelectionBounds(
     79       base::string16::size_type* start,
     80       base::string16::size_type* end) const OVERRIDE;
     81   virtual void SelectAll(bool reversed) OVERRIDE;
     82   virtual void RevertAll() OVERRIDE;
     83   virtual void SetFocus() OVERRIDE;
     84   virtual int GetTextWidth() const OVERRIDE;
     85   virtual bool IsImeComposing() const OVERRIDE;
     86 
     87   // views::Textfield:
     88   virtual gfx::Size GetMinimumSize() const OVERRIDE;
     89   virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE;
     90   virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
     91 
     92  private:
     93   FRIEND_TEST_ALL_PREFIXES(OmniboxViewViewsTest, CloseOmniboxPopupOnTextDrag);
     94 
     95   // Update the field with |text| and set the selection.
     96   void SetTextAndSelectedRange(const base::string16& text,
     97                                const gfx::Range& range);
     98 
     99   // Returns the selected text.
    100   base::string16 GetSelectedText() const;
    101 
    102   // Paste text from the clipboard into the omnibox.
    103   // Textfields implementation of Paste() pastes the contents of the clipboard
    104   // as is. We want to strip whitespace and other things (see GetClipboardText()
    105   // for details).
    106   // It is assumed this is invoked after a call to OnBeforePossibleChange() and
    107   // that after invoking this OnAfterPossibleChange() is invoked.
    108   void OnPaste();
    109 
    110   // Handle keyword hint tab-to-search and tabbing through dropdown results.
    111   bool HandleEarlyTabActions(const ui::KeyEvent& event);
    112 
    113   // Handles a request to change the value of this text field from software
    114   // using an accessibility API (typically automation software, screen readers
    115   // don't normally use this). Sets the value and clears the selection.
    116   void AccessibilitySetValue(const base::string16& new_value);
    117 
    118   // OmniboxView:
    119   virtual void SetWindowTextAndCaretPos(const base::string16& text,
    120                                         size_t caret_pos,
    121                                         bool update_popup,
    122                                         bool notify_text_changed) OVERRIDE;
    123   virtual bool IsSelectAll() const OVERRIDE;
    124   virtual bool DeleteAtEndPressed() OVERRIDE;
    125   virtual void UpdatePopup() OVERRIDE;
    126   virtual void ApplyCaretVisibility() OVERRIDE;
    127   virtual void OnTemporaryTextMaybeChanged(
    128       const base::string16& display_text,
    129       bool save_original_selection,
    130       bool notify_text_changed) OVERRIDE;
    131   virtual bool OnInlineAutocompleteTextMaybeChanged(
    132       const base::string16& display_text, size_t user_text_length) OVERRIDE;
    133   virtual void OnInlineAutocompleteTextCleared() OVERRIDE;
    134   virtual void OnRevertTemporaryText() OVERRIDE;
    135   virtual void OnBeforePossibleChange() OVERRIDE;
    136   virtual bool OnAfterPossibleChange() OVERRIDE;
    137   virtual gfx::NativeView GetNativeView() const OVERRIDE;
    138   virtual gfx::NativeView GetRelativeWindowForPopup() const OVERRIDE;
    139   virtual void SetGrayTextAutocompletion(const base::string16& input) OVERRIDE;
    140   virtual base::string16 GetGrayTextAutocompletion() const OVERRIDE;
    141   virtual int GetWidth() const OVERRIDE;
    142   virtual bool IsImeShowingPopup() const OVERRIDE;
    143   virtual void ShowImeIfNeeded() OVERRIDE;
    144   virtual void OnMatchOpened(const AutocompleteMatch& match,
    145                              content::WebContents* web_contents) OVERRIDE;
    146   virtual int GetOmniboxTextLength() const OVERRIDE;
    147   virtual void EmphasizeURLComponents() OVERRIDE;
    148 
    149   // views::Textfield:
    150   virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE;
    151   virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE;
    152   virtual base::string16 GetLabelForCommandId(int command_id) const OVERRIDE;
    153   virtual const char* GetClassName() const OVERRIDE;
    154   virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
    155   virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
    156   virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
    157   virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE;
    158   virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
    159   virtual void AboutToRequestFocusFromTabTraversal(bool reverse) OVERRIDE;
    160   virtual bool SkipDefaultKeyEventProcessing(
    161       const ui::KeyEvent& event) OVERRIDE;
    162   virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
    163   virtual void OnFocus() OVERRIDE;
    164   virtual void OnBlur() OVERRIDE;
    165   virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
    166   virtual base::string16 GetSelectionClipboardText() const OVERRIDE;
    167 
    168   // chromeos::input_method::InputMethodManager::CandidateWindowObserver:
    169 #if defined(OS_CHROMEOS)
    170   virtual void CandidateWindowOpened(
    171       chromeos::input_method::InputMethodManager* manager) OVERRIDE;
    172   virtual void CandidateWindowClosed(
    173       chromeos::input_method::InputMethodManager* manager) OVERRIDE;
    174 #endif
    175 
    176   // views::TextfieldController:
    177   virtual void ContentsChanged(views::Textfield* sender,
    178                                const base::string16& new_contents) OVERRIDE;
    179   virtual bool HandleKeyEvent(views::Textfield* sender,
    180                               const ui::KeyEvent& key_event) OVERRIDE;
    181   virtual void OnBeforeUserAction(views::Textfield* sender) OVERRIDE;
    182   virtual void OnAfterUserAction(views::Textfield* sender) OVERRIDE;
    183   virtual void OnAfterCutOrCopy(ui::ClipboardType clipboard_type) OVERRIDE;
    184   virtual void OnWriteDragData(ui::OSExchangeData* data) OVERRIDE;
    185   virtual void OnGetDragOperationsForTextfield(int* drag_operations) OVERRIDE;
    186   virtual void AppendDropFormats(
    187       int* formats,
    188       std::set<ui::OSExchangeData::CustomFormat>* custom_formats) OVERRIDE;
    189   virtual int OnDrop(const ui::OSExchangeData& data) OVERRIDE;
    190   virtual void UpdateContextMenu(ui::SimpleMenuModel* menu_contents) OVERRIDE;
    191 
    192   // When true, the location bar view is read only and also is has a slightly
    193   // different presentation (smaller font size). This is used for popups.
    194   bool popup_window_mode_;
    195 
    196   scoped_ptr<OmniboxPopupView> popup_view_;
    197 
    198   ToolbarModel::SecurityLevel security_level_;
    199 
    200   // Selection persisted across temporary text changes, like popup suggestions.
    201   gfx::Range saved_temporary_selection_;
    202 
    203   // Holds the user's selection across focus changes.  There is only a saved
    204   // selection if this range IsValid().
    205   gfx::Range saved_selection_for_focus_change_;
    206 
    207   // Tracking state before and after a possible change.
    208   base::string16 text_before_change_;
    209   gfx::Range sel_before_change_;
    210   bool ime_composing_before_change_;
    211 
    212   // Was the delete key pressed with an empty selection at the end of the edit?
    213   bool delete_at_end_pressed_;
    214   LocationBarView* location_bar_view_;
    215 
    216   // True if the IME candidate window is open. When this is true, we want to
    217   // avoid showing the popup. So far, the candidate window is detected only
    218   // on Chrome OS.
    219   bool ime_candidate_window_open_;
    220 
    221   // Should we select all the text when we see the mouse button get released?
    222   // We select in response to a click that focuses the omnibox, but we defer
    223   // until release, setting this variable back to false if we saw a drag, to
    224   // allow the user to select just a portion of the text.
    225   bool select_all_on_mouse_release_;
    226 
    227   // Indicates if we want to select all text in the omnibox when we get a
    228   // GESTURE_TAP. We want to select all only when the textfield is not in focus
    229   // and gets a tap. So we use this variable to remember focus state before tap.
    230   bool select_all_on_gesture_tap_;
    231 
    232   // Used to bind callback functions to this object.
    233   base::WeakPtrFactory<OmniboxViewViews> weak_ptr_factory_;
    234 
    235   DISALLOW_COPY_AND_ASSIGN(OmniboxViewViews);
    236 };
    237 
    238 #endif  // CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_VIEW_VIEWS_H_
    239