autocomplete_edit_view_mac.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
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#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/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 16class AutocompleteEditController; 17class AutocompletePopupViewMac; 18class Profile; 19class ToolbarModel; 20 21namespace ui { 22class Clipboard; 23} 24 25// Implements AutocompleteEditView on an AutocompleteTextField. 26 27class 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() { return model_.get(); } 39 virtual const AutocompleteEditModel* model() const { return model_.get(); } 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 virtual string16 GetInstantSuggestion() const; 88 virtual int TextWidth() const; 89 virtual bool IsImeComposing() const; 90 91 // Implement the AutocompleteTextFieldObserver interface. 92 virtual NSRange SelectionRangeForProposedRange(NSRange proposed_range); 93 virtual void OnControlKeyChanged(bool pressed); 94 virtual bool CanCopy(); 95 virtual void CopyToPasteboard(NSPasteboard* pboard); 96 virtual void OnPaste(); 97 virtual bool CanPasteAndGo(); 98 virtual int GetPasteActionStringId(); 99 virtual void OnPasteAndGo(); 100 virtual void OnFrameChanged(); 101 virtual void OnDidBeginEditing(); 102 virtual void OnDidChange(); 103 virtual void OnDidEndEditing(); 104 virtual bool OnDoCommandBySelector(SEL cmd); 105 virtual void OnSetFocus(bool control_down); 106 virtual void OnKillFocus(); 107 108 // Helper for LocationBarViewMac. Optionally selects all in |field_|. 109 void FocusLocation(bool select_all); 110 111 // Helper to get appropriate contents from |clipboard|. Returns 112 // empty string if no appropriate data is found on |clipboard|. 113 static string16 GetClipboardText(ui::Clipboard* clipboard); 114 115 // Helper to get the font to use in the field, exposed for the 116 // popup. 117 static NSFont* GetFieldFont(); 118 119 // If |resource_id| has a PDF image which can be used, return it. 120 // Otherwise return the PNG image from the resource bundle. 121 static NSImage* ImageForResource(int resource_id); 122 123 private: 124 // Called when the user hits backspace in |field_|. Checks whether 125 // keyword search is being terminated. Returns true if the 126 // backspace should be intercepted (not forwarded on to the standard 127 // machinery). 128 bool OnBackspacePressed(); 129 130 // Returns the field's currently selected range. Only valid if the 131 // field has focus. 132 NSRange GetSelectedRange() const; 133 134 // Returns the field's currently marked range. Only valid if the field has 135 // focus. 136 NSRange GetMarkedRange() const; 137 138 // Returns true if |field_| is first-responder in the window. Used 139 // in various DCHECKS to make sure code is running in appropriate 140 // situations. 141 bool IsFirstResponder() const; 142 143 // If |model_| believes it has focus, grab focus if needed and set 144 // the selection to |range|. Otherwise does nothing. 145 void SetSelectedRange(const NSRange range); 146 147 // Update the field with |display_text| and highlight the host and scheme (if 148 // it's an URL or URL-fragment). Resets any suggest text that may be present. 149 void SetText(const string16& display_text); 150 151 // Internal implementation of SetText. Does not reset the suggest text before 152 // setting the display text. Most callers should use |SetText()| instead. 153 void SetTextInternal(const string16& display_text); 154 155 // Update the field with |display_text| and set the selection. 156 void SetTextAndSelectedRange(const string16& display_text, 157 const NSRange range); 158 159 // Returns the non-suggest portion of |field_|'s string value. 160 NSString* GetNonSuggestTextSubstring() const; 161 162 // Returns the suggest portion of |field_|'s string value. 163 NSString* GetSuggestTextSubstring() const; 164 165 // Pass the current content of |field_| to SetText(), maintaining 166 // any selection. Named to be consistent with GTK and Windows, 167 // though here we cannot really do the in-place operation they do. 168 void EmphasizeURLComponents(); 169 170 // Calculates text attributes according to |display_text| and applies them 171 // to the given |as| object. 172 void ApplyTextAttributes(const string16& display_text, 173 NSMutableAttributedString* as); 174 175 // Return the number of UTF-16 units in the current buffer, excluding the 176 // suggested text. 177 NSUInteger GetTextLength() const; 178 179 // Places the caret at the given position. This clears any selection. 180 void PlaceCaretAt(NSUInteger pos); 181 182 // Returns true if the caret is at the end of the content. 183 bool IsCaretAtEnd() const; 184 185 scoped_ptr<AutocompleteEditModel> model_; 186 scoped_ptr<AutocompletePopupViewMac> popup_view_; 187 188 AutocompleteEditController* controller_; 189 ToolbarModel* toolbar_model_; 190 191 // The object that handles additional command functionality exposed on the 192 // edit, such as invoking the keyword editor. 193 CommandUpdater* command_updater_; 194 195 AutocompleteTextField* field_; // owned by tab controller 196 197 // Selection at the point where the user started using the 198 // arrows to move around in the popup. 199 NSRange saved_temporary_selection_; 200 201 // Tracking state before and after a possible change for reporting 202 // to model_. 203 NSRange selection_before_change_; 204 string16 text_before_change_; 205 NSRange marked_range_before_change_; 206 207 // Length of the suggest text. The suggest text always appears at the end of 208 // the field. 209 size_t suggest_text_length_; 210 211 // Was delete pressed? 212 bool delete_was_pressed_; 213 214 // Was the delete key pressed with an empty selection at the end of the edit? 215 bool delete_at_end_pressed_; 216 217 // The maximum/standard line height for the displayed text. 218 CGFloat line_height_; 219 220 DISALLOW_COPY_AND_ASSIGN(AutocompleteEditViewMac); 221}; 222 223#endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_MAC_H_ 224