Home | History | Annotate | Download | only in autocomplete
      1 // Copyright (c) 2010 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 // This file defines the interface class AutocompleteEditView.  Each toolkit
      6 // will implement the edit view differently, so that code is inherently
      7 // platform specific.  However, the AutocompleteEditModel needs to do some
      8 // communication with the view.  Since the model is shared between platforms,
      9 // we need to define an interface that all view implementations will share.
     10 
     11 #ifndef CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_H_
     12 #define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_H_
     13 #pragma once
     14 
     15 #include <string>
     16 
     17 #include "base/string16.h"
     18 #include "content/common/page_transition_types.h"
     19 #include "ui/gfx/native_widget_types.h"
     20 #include "webkit/glue/window_open_disposition.h"
     21 
     22 class AutocompleteEditModel;
     23 class CommandUpdater;
     24 class GURL;
     25 class TabContents;
     26 
     27 #if defined(TOOLKIT_VIEWS)
     28 namespace views {
     29 class DropTargetEvent;
     30 class View;
     31 }  // namespace views
     32 #endif
     33 
     34 class AutocompleteEditView {
     35  public:
     36   // Used by the automation system for getting at the model from the view.
     37   virtual AutocompleteEditModel* model() = 0;
     38   virtual const AutocompleteEditModel* model() const = 0;
     39 
     40   // For use when switching tabs, this saves the current state onto the tab so
     41   // that it can be restored during a later call to Update().
     42   virtual void SaveStateToTab(TabContents* tab) = 0;
     43 
     44   // Called when any LocationBarView state changes. If
     45   // |tab_for_state_restoring| is non-NULL, it points to a TabContents whose
     46   // state we should restore.
     47   virtual void Update(const TabContents* tab_for_state_restoring) = 0;
     48 
     49   // Asks the browser to load the specified URL, which is assumed to be one of
     50   // the popup entries, using the supplied disposition and transition type.
     51   // |alternate_nav_url|, if non-empty, contains the alternate navigation URL
     52   // for |url|.  See comments on AutocompleteResult::GetAlternateNavURL().
     53   //
     54   // |selected_line| is passed to SendOpenNotification(); see comments there.
     55   //
     56   // If the URL was expanded from a keyword, |keyword| is that keyword.
     57   //
     58   // This may close the popup.
     59   virtual void OpenURL(const GURL& url,
     60                        WindowOpenDisposition disposition,
     61                        PageTransition::Type transition,
     62                        const GURL& alternate_nav_url,
     63                        size_t selected_line,
     64                        const string16& keyword) = 0;
     65 
     66   // Returns the current text of the edit control, which could be the
     67   // "temporary" text set by the popup, the "permanent" text set by the
     68   // browser, or just whatever the user has currently typed.
     69   virtual string16 GetText() const = 0;
     70 
     71   // |true| if the user is in the process of editing the field, or if
     72   // the field is empty.
     73   virtual bool IsEditingOrEmpty() const = 0;
     74 
     75   // Returns the resource ID of the icon to show for the current text.
     76   virtual int GetIcon() const = 0;
     77 
     78   // The user text is the text the user has manually keyed in.  When present,
     79   // this is shown in preference to the permanent text; hitting escape will
     80   // revert to the permanent text.
     81   virtual void SetUserText(const string16& text) = 0;
     82   virtual void SetUserText(const string16& text,
     83                            const string16& display_text,
     84                            bool update_popup) = 0;
     85 
     86   // Sets the window text and the caret position.
     87   virtual void SetWindowTextAndCaretPos(const string16& text,
     88                                         size_t caret_pos) = 0;
     89 
     90   // Sets the edit to forced query mode.  Practically speaking, this means that
     91   // if the edit is not in forced query mode, its text is set to "?" with the
     92   // cursor at the end, and if the edit is in forced query mode (its first
     93   // non-whitespace character is '?'), the text after the '?' is selected.
     94   //
     95   // In the future we should display the search engine UI for the default engine
     96   // rather than '?'.
     97   virtual void SetForcedQuery() = 0;
     98 
     99   // Returns true if all text is selected or there is no text at all.
    100   virtual bool IsSelectAll() = 0;
    101 
    102   // Returns true if the user deleted the suggested text.
    103   virtual bool DeleteAtEndPressed() = 0;
    104 
    105   // Fills |start| and |end| with the indexes of the current selection's bounds.
    106   // It is not guaranteed that |*start < *end|, as the selection can be
    107   // directed.  If there is no selection, |start| and |end| will both be equal
    108   // to the current cursor position.
    109   virtual void GetSelectionBounds(string16::size_type* start,
    110                                   string16::size_type* end) = 0;
    111 
    112   // Selects all the text in the edit.  Use this in place of SetSelAll() to
    113   // avoid selecting the "phantom newline" at the end of the edit.
    114   virtual void SelectAll(bool reversed) = 0;
    115 
    116   // Reverts the edit and popup back to their unedited state (permanent text
    117   // showing, popup closed, no user input in progress).
    118   virtual void RevertAll() = 0;
    119 
    120   // Updates the autocomplete popup and other state after the text has been
    121   // changed by the user.
    122   virtual void UpdatePopup() = 0;
    123 
    124   // Closes the autocomplete popup, if it's open.
    125   virtual void ClosePopup() = 0;
    126 
    127   // Sets the focus to the autocomplete view.
    128   virtual void SetFocus() = 0;
    129 
    130   // Called when the temporary text in the model may have changed.
    131   // |display_text| is the new text to show; |save_original_selection| is true
    132   // when there wasn't previously a temporary text and thus we need to save off
    133   // the user's existing selection.
    134   virtual void OnTemporaryTextMaybeChanged(const string16& display_text,
    135                                            bool save_original_selection) = 0;
    136 
    137   // Called when the inline autocomplete text in the model may have changed.
    138   // |display_text| is the new text to show; |user_text_length| is the length of
    139   // the user input portion of that (so, up to but not including the inline
    140   // autocompletion).  Returns whether the display text actually changed.
    141   virtual bool OnInlineAutocompleteTextMaybeChanged(
    142       const string16& display_text, size_t user_text_length) = 0;
    143 
    144   // Called when the temporary text has been reverted by the user.  This will
    145   // reset the user's original selection.
    146   virtual void OnRevertTemporaryText() = 0;
    147 
    148   // Every piece of code that can change the edit should call these functions
    149   // before and after the change.  These functions determine if anything
    150   // meaningful changed, and do any necessary updating and notification.
    151   virtual void OnBeforePossibleChange() = 0;
    152   // OnAfterPossibleChange() returns true if there was a change that caused it
    153   // to call UpdatePopup().
    154   virtual bool OnAfterPossibleChange() = 0;
    155 
    156   // Returns the gfx::NativeView of the edit view.
    157   virtual gfx::NativeView GetNativeView() const = 0;
    158 
    159   // Returns the command updater for this view.
    160   virtual CommandUpdater* GetCommandUpdater() = 0;
    161 
    162   // Shows the instant suggestion text. If |animate_to_complete| is true the
    163   // view should start an animation that when done commits the text.
    164   virtual void SetInstantSuggestion(const string16& input,
    165                                     bool animate_to_complete) = 0;
    166 
    167   // Returns the current instant suggestion text.
    168   virtual string16 GetInstantSuggestion() const = 0;
    169 
    170   // Returns the width in pixels needed to display the current text. The
    171   // returned value includes margins.
    172   virtual int TextWidth() const = 0;
    173 
    174   // Returns true if the user is composing something in an IME.
    175   virtual bool IsImeComposing() const = 0;
    176 
    177 #if defined(TOOLKIT_VIEWS)
    178   // Adds the autocomplete edit view to view hierarchy and
    179   // returns the views::View of the edit view.
    180   virtual views::View* AddToView(views::View* parent) = 0;
    181 
    182   // Performs the drop of a drag and drop operation on the view.
    183   virtual int OnPerformDrop(const views::DropTargetEvent& event) = 0;
    184 #endif
    185 
    186   virtual ~AutocompleteEditView() {}
    187 };
    188 
    189 #endif  // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_H_
    190