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_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_MAC_H_ 6 #define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_MAC_H_ 7 #pragma once 8 9 #import <Cocoa/Cocoa.h> 10 11 #include "base/memory/scoped_ptr.h" 12 #include "base/string16.h" 13 #include "chrome/browser/autocomplete/autocomplete_edit_view.h" 14 #include "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h" 15 16 class AutocompleteEditController; 17 class AutocompletePopupViewMac; 18 class Profile; 19 class ToolbarModel; 20 21 namespace ui { 22 class Clipboard; 23 } 24 25 // Implements AutocompleteEditView on an AutocompleteTextField. 26 27 class AutocompleteEditViewMac : public AutocompleteEditView, 28 public AutocompleteTextFieldObserver { 29 public: 30 AutocompleteEditViewMac(AutocompleteEditController* controller, 31 ToolbarModel* toolbar_model, 32 Profile* profile, 33 CommandUpdater* command_updater, 34 AutocompleteTextField* field); 35 virtual ~AutocompleteEditViewMac(); 36 37 // Implement the AutocompleteEditView interface. 38 virtual AutocompleteEditModel* model(); 39 virtual const AutocompleteEditModel* model() const; 40 41 virtual void SaveStateToTab(TabContents* tab); 42 virtual void Update(const TabContents* tab_for_state_restoring); 43 44 virtual void OpenURL(const GURL& url, 45 WindowOpenDisposition disposition, 46 PageTransition::Type transition, 47 const GURL& alternate_nav_url, 48 size_t selected_line, 49 const string16& keyword); 50 51 virtual string16 GetText() const; 52 53 virtual bool IsEditingOrEmpty() const; 54 virtual int GetIcon() const; 55 56 virtual void SetUserText(const string16& text); 57 virtual void SetUserText(const string16& text, 58 const string16& display_text, 59 bool update_popup); 60 61 virtual void SetWindowTextAndCaretPos(const string16& text, 62 size_t caret_pos); 63 64 virtual void SetForcedQuery(); 65 66 virtual bool IsSelectAll(); 67 virtual bool DeleteAtEndPressed(); 68 virtual void GetSelectionBounds(string16::size_type* start, 69 string16::size_type* end); 70 71 virtual void SelectAll(bool reversed); 72 virtual void RevertAll(); 73 virtual void UpdatePopup(); 74 virtual void ClosePopup(); 75 virtual void SetFocus(); 76 virtual void OnTemporaryTextMaybeChanged(const string16& display_text, 77 bool save_original_selection); 78 virtual bool OnInlineAutocompleteTextMaybeChanged( 79 const string16& display_text, size_t user_text_length); 80 virtual void OnStartingIME(); 81 virtual void OnRevertTemporaryText(); 82 virtual void OnBeforePossibleChange(); 83 virtual bool OnAfterPossibleChange(); 84 virtual gfx::NativeView GetNativeView() const; 85 virtual CommandUpdater* GetCommandUpdater(); 86 virtual void SetInstantSuggestion(const string16& input, 87 bool animate_to_complete); 88 virtual string16 GetInstantSuggestion() const; 89 virtual int TextWidth() const; 90 virtual bool IsImeComposing() const; 91 92 // Implement the AutocompleteTextFieldObserver interface. 93 virtual NSRange SelectionRangeForProposedRange(NSRange proposed_range); 94 virtual void OnControlKeyChanged(bool pressed); 95 virtual bool CanCopy(); 96 virtual void CopyToPasteboard(NSPasteboard* pboard); 97 virtual void OnPaste(); 98 virtual bool CanPasteAndGo(); 99 virtual int GetPasteActionStringId(); 100 virtual void OnPasteAndGo(); 101 virtual void OnFrameChanged(); 102 virtual void OnDidBeginEditing(); 103 virtual void OnBeforeChange(); 104 virtual void OnDidChange(); 105 virtual void OnDidEndEditing(); 106 virtual bool OnDoCommandBySelector(SEL cmd); 107 virtual void OnSetFocus(bool control_down); 108 virtual void OnKillFocus(); 109 110 // Helper for LocationBarViewMac. Optionally selects all in |field_|. 111 void FocusLocation(bool select_all); 112 113 // Helper to get appropriate contents from |clipboard|. Returns 114 // empty string if no appropriate data is found on |clipboard|. 115 static string16 GetClipboardText(ui::Clipboard* clipboard); 116 117 // Helper to get the font to use in the field, exposed for the 118 // popup. 119 static NSFont* GetFieldFont(); 120 121 // If |resource_id| has a PDF image which can be used, return it. 122 // Otherwise return the PNG image from the resource bundle. 123 static NSImage* ImageForResource(int resource_id); 124 125 private: 126 // Called when the user hits backspace in |field_|. Checks whether 127 // keyword search is being terminated. Returns true if the 128 // backspace should be intercepted (not forwarded on to the standard 129 // machinery). 130 bool OnBackspacePressed(); 131 132 // Returns the field's currently selected range. Only valid if the 133 // field has focus. 134 NSRange GetSelectedRange() const; 135 136 // Returns the field's currently marked range. Only valid if the field has 137 // focus. 138 NSRange GetMarkedRange() const; 139 140 // Returns true if |field_| is first-responder in the window. Used 141 // in various DCHECKS to make sure code is running in appropriate 142 // situations. 143 bool IsFirstResponder() const; 144 145 // If |model_| believes it has focus, grab focus if needed and set 146 // the selection to |range|. Otherwise does nothing. 147 void SetSelectedRange(const NSRange range); 148 149 // Update the field with |display_text| and highlight the host and scheme (if 150 // it's an URL or URL-fragment). Resets any suggest text that may be present. 151 void SetText(const string16& display_text); 152 153 // Internal implementation of SetText. Does not reset the suggest text before 154 // setting the display text. Most callers should use |SetText()| instead. 155 void SetTextInternal(const string16& display_text); 156 157 // Update the field with |display_text| and set the selection. 158 void SetTextAndSelectedRange(const string16& display_text, 159 const NSRange range); 160 161 // Returns the non-suggest portion of |field_|'s string value. 162 NSString* GetNonSuggestTextSubstring() const; 163 164 // Returns the suggest portion of |field_|'s string value. 165 NSString* GetSuggestTextSubstring() const; 166 167 // Pass the current content of |field_| to SetText(), maintaining 168 // any selection. Named to be consistent with GTK and Windows, 169 // though here we cannot really do the in-place operation they do. 170 void EmphasizeURLComponents(); 171 172 // Calculates text attributes according to |display_text| and applies them 173 // to the given |as| object. 174 void ApplyTextAttributes(const string16& display_text, 175 NSMutableAttributedString* as); 176 177 // Return the number of UTF-16 units in the current buffer, excluding the 178 // suggested text. 179 NSUInteger GetTextLength() const; 180 181 // Places the caret at the given position. This clears any selection. 182 void PlaceCaretAt(NSUInteger pos); 183 184 // Returns true if the caret is at the end of the content. 185 bool IsCaretAtEnd() const; 186 187 scoped_ptr<AutocompleteEditModel> model_; 188 scoped_ptr<AutocompletePopupViewMac> popup_view_; 189 190 AutocompleteEditController* controller_; 191 ToolbarModel* toolbar_model_; 192 193 // The object that handles additional command functionality exposed on the 194 // edit, such as invoking the keyword editor. 195 CommandUpdater* command_updater_; 196 197 AutocompleteTextField* field_; // owned by tab controller 198 199 // Selection at the point where the user started using the 200 // arrows to move around in the popup. 201 NSRange saved_temporary_selection_; 202 203 // Tracking state before and after a possible change for reporting 204 // to model_. 205 NSRange selection_before_change_; 206 string16 text_before_change_; 207 NSRange marked_range_before_change_; 208 209 // Length of the suggest text. The suggest text always appears at the end of 210 // the field. 211 size_t suggest_text_length_; 212 213 // Was delete pressed? 214 bool delete_was_pressed_; 215 216 // Was the delete key pressed with an empty selection at the end of the edit? 217 bool delete_at_end_pressed_; 218 219 // The maximum/standard line height for the displayed text. 220 CGFloat line_height_; 221 222 DISALLOW_COPY_AND_ASSIGN(AutocompleteEditViewMac); 223 }; 224 225 #endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_MAC_H_ 226