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