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