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_AUTOCOMPLETE_TEXT_FIELD_H_ 6 #define CHROME_BROWSER_UI_COCOA_AUTOCOMPLETE_TEXT_FIELD_H_ 7 8 #import <Cocoa/Cocoa.h> 9 10 #include "base/mac/scoped_nsobject.h" 11 #import "chrome/browser/ui/cocoa/styled_text_field.h" 12 #import "chrome/browser/ui/cocoa/url_drop_target.h" 13 14 @class AutocompleteTextFieldCell; 15 class LocationBarDecoration; 16 17 // AutocompleteTextField intercepts UI actions for forwarding to 18 // OmniboxViewMac (*), and provides a custom look. It works 19 // together with AutocompleteTextFieldEditor (mostly for intercepting 20 // user actions) and AutocompleteTextFieldCell (mostly for custom 21 // drawing). 22 // 23 // For historical reasons, chrome/browser/autocomplete is the core 24 // implementation of the Omnibox. Chrome code seems to vary between 25 // autocomplete and Omnibox in describing this. 26 // 27 // (*) OmniboxViewMac is a view in the MVC sense for the 28 // Chrome internals, though it's really more of a mish-mash of model, 29 // view, and controller. 30 31 // Provides a hook so that we can call directly down to 32 // OmniboxViewMac rather than traversing the delegate chain. 33 class AutocompleteTextFieldObserver { 34 public: 35 // Called before changing the selected range of the field. 36 virtual NSRange SelectionRangeForProposedRange(NSRange proposed_range) = 0; 37 38 // Called when the control-key state changes while the field is 39 // first responder. 40 virtual void OnControlKeyChanged(bool pressed) = 0; 41 42 // Called when the user pastes into the field. 43 virtual void OnPaste() = 0; 44 45 // Return |true| if there is a selection to copy. 46 virtual bool CanCopy() = 0; 47 48 // Clears the |pboard| and adds the field's current selection. 49 // Called when the user does a copy or drag. 50 virtual void CopyToPasteboard(NSPasteboard* pboard) = 0; 51 52 // Returns true if the Show URL option should be available. 53 virtual bool ShouldEnableShowURL() = 0; 54 55 // Shows the underlying URL. See OmniboxView::ShowURL(). 56 virtual void ShowURL() = 0; 57 58 // Returns true if the current clipboard text supports paste and go 59 // (or paste and search). 60 virtual bool CanPasteAndGo() = 0; 61 62 // Returns the appropriate "Paste and Go" or "Paste and Search" 63 // context menu string, depending on what is currently in the 64 // clipboard. Must not be called unless CanPasteAndGo() returns 65 // true. 66 virtual int GetPasteActionStringId() = 0; 67 68 // Called when the user initiates a "paste and go" or "paste and 69 // search" into the field. 70 virtual void OnPasteAndGo() = 0; 71 72 // Called when the field's frame changes. 73 virtual void OnFrameChanged() = 0; 74 75 // Called when the popup is no longer appropriate, such as when the 76 // field's window loses focus or a page action is clicked. 77 virtual void ClosePopup() = 0; 78 79 // Called when the user begins editing the field, for every edit, 80 // and when the user is done editing the field. 81 virtual void OnDidBeginEditing() = 0; 82 virtual void OnBeforeChange() = 0; 83 virtual void OnDidChange() = 0; 84 virtual void OnDidEndEditing() = 0; 85 86 // NSResponder translates certain keyboard actions into selectors 87 // passed to -doCommandBySelector:. The selector is forwarded here, 88 // return true if |cmd| is handled, false if the caller should 89 // handle it. 90 // TODO(shess): For now, I think having the code which makes these 91 // decisions closer to the other autocomplete code is worthwhile, 92 // since it calls a wide variety of methods which otherwise aren't 93 // clearly relevent to expose here. But consider pulling more of 94 // the OmniboxViewMac calls up to here. 95 virtual bool OnDoCommandBySelector(SEL cmd) = 0; 96 97 // Called whenever the autocomplete text field gets focused. 98 virtual void OnSetFocus(bool control_down) = 0; 99 100 // Called whenever the autocomplete text field is losing focus. 101 virtual void OnKillFocus() = 0; 102 103 // Called before the text field handles a mouse down event. 104 virtual void OnMouseDown(NSInteger button_number) = 0; 105 106 // Returns true if mouse down should select all. 107 virtual bool ShouldSelectAllOnMouseDown() = 0; 108 109 protected: 110 virtual ~AutocompleteTextFieldObserver() {} 111 }; 112 113 @interface AutocompleteTextField : StyledTextField<NSTextViewDelegate, 114 URLDropTarget> { 115 @private 116 // Undo manager for this text field. We use a specific instance rather than 117 // the standard undo manager in order to let us clear the undo stack at will. 118 base::scoped_nsobject<NSUndoManager> undoManager_; 119 120 AutocompleteTextFieldObserver* observer_; // weak, owned by location bar. 121 122 // Handles being a drag-and-drop target. 123 base::scoped_nsobject<URLDropTargetHandler> dropHandler_; 124 125 // Holds current tooltip strings, to keep them from being dealloced. 126 base::scoped_nsobject<NSMutableArray> currentToolTips_; 127 128 base::scoped_nsobject<NSString> suggestText_; 129 base::scoped_nsobject<NSColor> suggestColor_; 130 } 131 132 @property(nonatomic) AutocompleteTextFieldObserver* observer; 133 134 // Convenience method to return the cell, casted appropriately. 135 - (AutocompleteTextFieldCell*)cell; 136 137 // Superclass aborts editing before changing the string, which causes 138 // problems for undo. This version modifies the field editor's 139 // contents if the control is already being edited. 140 - (void)setAttributedStringValue:(NSAttributedString*)aString; 141 142 // Clears the undo chain for this text field. 143 - (void)clearUndoChain; 144 145 // Updates cursor and tooltip rects depending on the contents of the text field 146 // e.g. the security icon should have a default pointer shown on hover instead 147 // of an I-beam. 148 - (void)updateMouseTracking; 149 150 // Return the appropriate menu for any decoration under |event|. 151 - (NSMenu*)decorationMenuForEvent:(NSEvent*)event; 152 153 // Retains |tooltip| (in |currentToolTips_|) and adds this tooltip 154 // via -[NSView addToolTipRect:owner:userData:]. 155 - (void)addToolTip:(NSString*)tooltip forRect:(NSRect)aRect; 156 157 // Sets the suggest text that shows at the end of the field's normal text. 158 // This can't be simply appended to the field's text storage because that 159 // will end any pending IME session. 160 - (void)setGrayTextAutocompletion:(NSString*)suggestText 161 textColor:(NSColor*)suggestColor; 162 163 - (NSString*)suggestText; 164 - (NSColor*)suggestColor; 165 166 // Obtain the bubble anchor point for |decoration|. In window coordinates. 167 - (NSPoint)bubblePointForDecoration:(LocationBarDecoration*)decoration; 168 169 @end 170 171 namespace autocomplete_text_field { 172 173 // Draw gray text suggestion in |controlView|. 174 void DrawGrayTextAutocompletion(NSAttributedString* mainText, 175 NSString* suggestText, 176 NSColor* suggestColor, 177 NSView* controlView, 178 NSRect frame); 179 180 } // namespace autocomplete_text_field 181 182 #endif // CHROME_BROWSER_UI_COCOA_AUTOCOMPLETE_TEXT_FIELD_H_ 183