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_COCOA_OMNIBOX_OMNIBOX_VIEW_MAC_H_
      6 #define CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_VIEW_MAC_H_
      7 
      8 #import <Cocoa/Cocoa.h>
      9 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/strings/string16.h"
     12 #include "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h"
     13 #include "chrome/browser/ui/omnibox/omnibox_view.h"
     14 
     15 class OmniboxPopupView;
     16 
     17 namespace ui {
     18 class Clipboard;
     19 }
     20 
     21 // Implements OmniboxView on an AutocompleteTextField.
     22 class OmniboxViewMac : public OmniboxView,
     23                        public AutocompleteTextFieldObserver {
     24  public:
     25   OmniboxViewMac(OmniboxEditController* controller,
     26                  Profile* profile,
     27                  CommandUpdater* command_updater,
     28                  AutocompleteTextField* field);
     29   virtual ~OmniboxViewMac();
     30 
     31   // OmniboxView:
     32   virtual void SaveStateToTab(content::WebContents* tab) OVERRIDE;
     33   virtual void OnTabChanged(const content::WebContents* web_contents) OVERRIDE;
     34   virtual void Update() OVERRIDE;
     35   virtual void OpenMatch(const AutocompleteMatch& match,
     36                          WindowOpenDisposition disposition,
     37                          const GURL& alternate_nav_url,
     38                          const base::string16& pasted_text,
     39                          size_t selected_line) OVERRIDE;
     40   virtual base::string16 GetText() const OVERRIDE;
     41   virtual void SetWindowTextAndCaretPos(const base::string16& text,
     42                                         size_t caret_pos,
     43                                         bool update_popup,
     44                                         bool notify_text_changed) OVERRIDE;
     45   virtual void SetForcedQuery() OVERRIDE;
     46   virtual bool IsSelectAll() const OVERRIDE;
     47   virtual bool DeleteAtEndPressed() OVERRIDE;
     48   virtual void GetSelectionBounds(
     49       base::string16::size_type* start,
     50       base::string16::size_type* end) const OVERRIDE;
     51   virtual void SelectAll(bool reversed) OVERRIDE;
     52   virtual void RevertAll() OVERRIDE;
     53   virtual void UpdatePopup() OVERRIDE;
     54   virtual void CloseOmniboxPopup() OVERRIDE;
     55   virtual void SetFocus() OVERRIDE;
     56   virtual void ApplyCaretVisibility() OVERRIDE;
     57   virtual void OnTemporaryTextMaybeChanged(
     58       const base::string16& display_text,
     59       bool save_original_selection,
     60       bool notify_text_changed) OVERRIDE;
     61   virtual bool OnInlineAutocompleteTextMaybeChanged(
     62       const base::string16& display_text, size_t user_text_length) OVERRIDE;
     63   virtual void OnInlineAutocompleteTextCleared() OVERRIDE;
     64   virtual void OnRevertTemporaryText() OVERRIDE;
     65   virtual void OnBeforePossibleChange() OVERRIDE;
     66   virtual bool OnAfterPossibleChange() OVERRIDE;
     67   virtual gfx::NativeView GetNativeView() const OVERRIDE;
     68   virtual gfx::NativeView GetRelativeWindowForPopup() const OVERRIDE;
     69   virtual void SetGrayTextAutocompletion(const base::string16& input) OVERRIDE;
     70   virtual base::string16 GetGrayTextAutocompletion() const OVERRIDE;
     71   virtual int GetTextWidth() const OVERRIDE;
     72   virtual int GetWidth() const OVERRIDE;
     73   virtual bool IsImeComposing() const OVERRIDE;
     74 
     75   // Implement the AutocompleteTextFieldObserver interface.
     76   virtual NSRange SelectionRangeForProposedRange(
     77       NSRange proposed_range) OVERRIDE;
     78   virtual void OnControlKeyChanged(bool pressed) OVERRIDE;
     79   virtual bool CanCopy() OVERRIDE;
     80   virtual void CopyToPasteboard(NSPasteboard* pboard) OVERRIDE;
     81   virtual bool ShouldEnableShowURL() OVERRIDE;
     82   virtual void ShowURL() OVERRIDE;
     83   virtual void OnPaste() OVERRIDE;
     84   virtual bool CanPasteAndGo() OVERRIDE;
     85   virtual int GetPasteActionStringId() OVERRIDE;
     86   virtual void OnPasteAndGo() OVERRIDE;
     87   virtual void OnFrameChanged() OVERRIDE;
     88   virtual void ClosePopup() OVERRIDE;
     89   virtual void OnDidBeginEditing() OVERRIDE;
     90   virtual void OnBeforeChange() OVERRIDE;
     91   virtual void OnDidChange() OVERRIDE;
     92   virtual void OnDidEndEditing() OVERRIDE;
     93   virtual bool OnDoCommandBySelector(SEL cmd) OVERRIDE;
     94   virtual void OnSetFocus(bool control_down) OVERRIDE;
     95   virtual void OnKillFocus() OVERRIDE;
     96   virtual void OnMouseDown(NSInteger button_number) OVERRIDE;
     97   virtual bool ShouldSelectAllOnMouseDown() OVERRIDE;
     98 
     99   // Helper for LocationBarViewMac.  Optionally selects all in |field_|.
    100   void FocusLocation(bool select_all);
    101 
    102   // Helper to get the font to use in the field, exposed for the
    103   // popup.
    104   // The style parameter specifies the new style for the font, and is a
    105   // bitmask of the values: BOLD, ITALIC and UNDERLINE (see ui/gfx/font.h).
    106   static NSFont* GetFieldFont(int style);
    107 
    108   // If |resource_id| has a PDF image which can be used, return it.
    109   // Otherwise return the PNG image from the resource bundle.
    110   static NSImage* ImageForResource(int resource_id);
    111 
    112   // Color used to draw suggest text.
    113   static NSColor* SuggestTextColor();
    114 
    115   AutocompleteTextField* field() const { return field_; }
    116 
    117  private:
    118   // Called when the user hits backspace in |field_|.  Checks whether
    119   // keyword search is being terminated.  Returns true if the
    120   // backspace should be intercepted (not forwarded on to the standard
    121   // machinery).
    122   bool OnBackspacePressed();
    123 
    124   // Returns the field's currently selected range.  Only valid if the
    125   // field has focus.
    126   NSRange GetSelectedRange() const;
    127 
    128   // Returns the field's currently marked range. Only valid if the field has
    129   // focus.
    130   NSRange GetMarkedRange() const;
    131 
    132   // Returns true if |field_| is first-responder in the window.  Used
    133   // in various DCHECKS to make sure code is running in appropriate
    134   // situations.
    135   bool IsFirstResponder() const;
    136 
    137   // If |model_| believes it has focus, grab focus if needed and set
    138   // the selection to |range|.  Otherwise does nothing.
    139   void SetSelectedRange(const NSRange range);
    140 
    141   // Update the field with |display_text| and highlight the host and scheme (if
    142   // it's an URL or URL-fragment).  Resets any suggest text that may be present.
    143   void SetText(const base::string16& display_text);
    144 
    145   // Internal implementation of SetText.  Does not reset the suggest text before
    146   // setting the display text.  Most callers should use |SetText()| instead.
    147   void SetTextInternal(const base::string16& display_text);
    148 
    149   // Update the field with |display_text| and set the selection.
    150   void SetTextAndSelectedRange(const base::string16& display_text,
    151                                const NSRange range);
    152 
    153   // Pass the current content of |field_| to SetText(), maintaining
    154   // any selection.  Named to be consistent with GTK and Windows,
    155   // though here we cannot really do the in-place operation they do.
    156   virtual void EmphasizeURLComponents() OVERRIDE;
    157 
    158   // Calculates text attributes according to |display_text| and applies them
    159   // to the given |as| object.
    160   void ApplyTextAttributes(const base::string16& display_text,
    161                            NSMutableAttributedString* as);
    162 
    163   // Return the number of UTF-16 units in the current buffer, excluding the
    164   // suggested text.
    165   virtual int GetOmniboxTextLength() const OVERRIDE;
    166   NSUInteger GetTextLength() const;
    167 
    168   // Returns true if the caret is at the end of the content.
    169   bool IsCaretAtEnd() const;
    170 
    171   scoped_ptr<OmniboxPopupView> popup_view_;
    172 
    173   AutocompleteTextField* field_;  // owned by tab controller
    174 
    175   // Selection at the point where the user started using the
    176   // arrows to move around in the popup.
    177   NSRange saved_temporary_selection_;
    178 
    179   // Tracking state before and after a possible change for reporting
    180   // to model_.
    181   NSRange selection_before_change_;
    182   base::string16 text_before_change_;
    183   NSRange marked_range_before_change_;
    184 
    185   // Was delete pressed?
    186   bool delete_was_pressed_;
    187 
    188   // Was the delete key pressed with an empty selection at the end of the edit?
    189   bool delete_at_end_pressed_;
    190 
    191   base::string16 suggest_text_;
    192 
    193   // State used to coalesce changes to text and selection to avoid drawing
    194   // transient state.
    195   bool in_coalesced_update_block_;
    196   bool do_coalesced_text_update_;
    197   base::string16 coalesced_text_update_;
    198   bool do_coalesced_range_update_;
    199   NSRange coalesced_range_update_;
    200 
    201   DISALLOW_COPY_AND_ASSIGN(OmniboxViewMac);
    202 };
    203 
    204 #endif  // CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_VIEW_MAC_H_
    205