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.
5 #ifndef CHROME_BROWSER_UI_COCOA_AUTOCOMPLETE_TEXT_FIELD_H_
6 #define CHROME_BROWSER_UI_COCOA_AUTOCOMPLETE_TEXT_FIELD_H_
8 #import <Cocoa/Cocoa.h>
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"
14 @
class AutocompleteTextFieldCell
;
15 class LocationBarDecoration
;
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
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.
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.
31 // Provides a hook so that we can call directly down to
32 // OmniboxViewMac rather than traversing the delegate chain.
33 class AutocompleteTextFieldObserver
{
35 // Called before changing the selected range of the field.
36 virtual NSRange
SelectionRangeForProposedRange(NSRange proposed_range
) = 0;
38 // Called when the control-key state changes while the field is
40 virtual void OnControlKeyChanged(bool pressed
) = 0;
42 // Called when the user pastes into the field.
43 virtual void OnPaste() = 0;
45 // Return |true| if there is a selection to copy.
46 virtual bool CanCopy() = 0;
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;
52 // Returns true if the Show URL option should be available.
53 virtual bool ShouldEnableShowURL() = 0;
55 // Shows the underlying URL. See OmniboxView::ShowURL().
56 virtual void ShowURL() = 0;
58 // Returns true if the current clipboard text supports paste and go
59 // (or paste and search).
60 virtual bool CanPasteAndGo() = 0;
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
66 virtual int GetPasteActionStringId() = 0;
68 // Called when the user initiates a "paste and go" or "paste and
69 // search" into the field.
70 virtual void OnPasteAndGo() = 0;
72 // Called when the field's frame changes.
73 virtual void OnFrameChanged() = 0;
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;
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;
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
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;
97 // Called whenever the autocomplete text field gets focused.
98 virtual void OnSetFocus(bool control_down
) = 0;
100 // Called whenever the autocomplete text field is losing focus.
101 virtual void OnKillFocus() = 0;
103 // Called before the text field handles a mouse down event.
104 virtual void OnMouseDown(NSInteger button_number
) = 0;
106 // Returns true if mouse down should select all.
107 virtual bool ShouldSelectAllOnMouseDown() = 0;
110 virtual ~AutocompleteTextFieldObserver() {}
113 @interface AutocompleteTextField
: StyledTextField
<NSTextViewDelegate
,
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_
;
120 AutocompleteTextFieldObserver
* observer_
; // weak, owned by location bar.
122 // Handles being a drag-and-drop target.
123 base::scoped_nsobject
<URLDropTargetHandler
> dropHandler_
;
125 // Holds current tooltip strings, to keep them from being dealloced.
126 base::scoped_nsobject
<NSMutableArray
> currentToolTips_
;
128 base::scoped_nsobject
<NSString
> suggestText_
;
129 base::scoped_nsobject
<NSColor
> suggestColor_
;
132 @
property(nonatomic
) AutocompleteTextFieldObserver
* observer
;
134 // Convenience method to return the cell, casted appropriately.
135 - (AutocompleteTextFieldCell
*)cell
;
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
;
142 // Clears the undo chain for this text field.
143 - (void)clearUndoChain
;
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
148 - (void)updateMouseTracking
;
150 // Return the appropriate menu for any decoration under |event|.
151 - (NSMenu
*)decorationMenuForEvent
:(NSEvent
*)event
;
153 // Retains |tooltip| (in |currentToolTips_|) and adds this tooltip
154 // via -[NSView addToolTipRect:owner:userData:].
155 - (void)addToolTip
:(NSString
*)tooltip forRect
:(NSRect
)aRect
;
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
;
163 - (NSString
*)suggestText
;
164 - (NSColor
*)suggestColor
;
166 // Obtain the bubble anchor point for |decoration|. In window coordinates.
167 - (NSPoint
)bubblePointForDecoration
:(LocationBarDecoration
*)decoration
;
171 namespace autocomplete_text_field
{
173 // Draw gray text suggestion in |controlView|.
174 void DrawGrayTextAutocompletion(NSAttributedString
* mainText
,
175 NSString
* suggestText
,
176 NSColor
* suggestColor
,
180 } // namespace autocomplete_text_field
182 #endif // CHROME_BROWSER_UI_COCOA_AUTOCOMPLETE_TEXT_FIELD_H_