Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / location_bar / autocomplete_text_field.h
blob9c302858d9a97000fee92170850bad3e401e560a
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
21 // drawing).
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 {
34 public:
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
39 // first responder.
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
65 // true.
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 // Called when -insertText: is invoked on the editor.
87 virtual void OnInsertText() = 0;
89 // Called after the completion of a -drawRect: operation.
90 virtual void OnDidDrawRect() = 0;
92 // NSResponder translates certain keyboard actions into selectors
93 // passed to -doCommandBySelector:. The selector is forwarded here,
94 // return true if |cmd| is handled, false if the caller should
95 // handle it.
96 // TODO(shess): For now, I think having the code which makes these
97 // decisions closer to the other autocomplete code is worthwhile,
98 // since it calls a wide variety of methods which otherwise aren't
99 // clearly relevent to expose here. But consider pulling more of
100 // the OmniboxViewMac calls up to here.
101 virtual bool OnDoCommandBySelector(SEL cmd) = 0;
103 // Called whenever the autocomplete text field gets focused.
104 virtual void OnSetFocus(bool control_down) = 0;
106 // Called whenever the autocomplete text field is losing focus.
107 virtual void OnKillFocus() = 0;
109 // Called before the text field handles a mouse down event.
110 virtual void OnMouseDown(NSInteger button_number) = 0;
112 // Returns true if mouse down should select all.
113 virtual bool ShouldSelectAllOnMouseDown() = 0;
115 protected:
116 virtual ~AutocompleteTextFieldObserver() {}
119 @interface AutocompleteTextField : StyledTextField<NSTextViewDelegate,
120 URLDropTarget> {
121 @private
122 // Undo manager for this text field. We use a specific instance rather than
123 // the standard undo manager in order to let us clear the undo stack at will.
124 base::scoped_nsobject<NSUndoManager> undoManager_;
126 AutocompleteTextFieldObserver* observer_; // weak, owned by location bar.
128 // Handles being a drag-and-drop target.
129 base::scoped_nsobject<URLDropTargetHandler> dropHandler_;
131 // Holds current tooltip strings, to keep them from being dealloced.
132 base::scoped_nsobject<NSMutableArray> currentToolTips_;
134 // Animation object used for resizing the autocomplete field.
135 base::scoped_nsobject<NSViewAnimation> resizeAnimation_;
137 base::scoped_nsobject<NSString> suggestText_;
138 base::scoped_nsobject<NSColor> suggestColor_;
141 @property(nonatomic) AutocompleteTextFieldObserver* observer;
143 // Convenience method to return the cell, casted appropriately.
144 - (AutocompleteTextFieldCell*)cell;
146 // Superclass aborts editing before changing the string, which causes
147 // problems for undo. This version modifies the field editor's
148 // contents if the control is already being edited.
149 - (void)setAttributedStringValue:(NSAttributedString*)aString;
151 // Clears the undo chain for this text field.
152 - (void)clearUndoChain;
154 // Animates the text field to the given |frame|.
155 - (void)animateToFrame:(NSRect)frame;
157 // Stops the current animation, if any. The frame will be set to the current
158 // (mid-animation) frame.
159 - (void)stopAnimation;
161 // Updates cursor and tooltip rects depending on the contents of the text field
162 // e.g. the security icon should have a default pointer shown on hover instead
163 // of an I-beam.
164 - (void)updateMouseTracking;
166 // Return the appropriate menu for any decoration under |event|.
167 - (NSMenu*)decorationMenuForEvent:(NSEvent*)event;
169 // Retains |tooltip| (in |currentToolTips_|) and adds this tooltip
170 // via -[NSView addToolTipRect:owner:userData:].
171 - (void)addToolTip:(NSString*)tooltip forRect:(NSRect)aRect;
173 // Sets the suggest text that shows at the end of the field's normal text.
174 // This can't be simply appended to the field's text storage because that
175 // will end any pending IME session.
176 - (void)setGrayTextAutocompletion:(NSString*)suggestText
177 textColor:(NSColor*)suggestColor;
179 - (NSString*)suggestText;
180 - (NSColor*)suggestColor;
182 // Obtain the bubble anchor point for |decoration|. In window coordinates.
183 - (NSPoint)bubblePointForDecoration:(LocationBarDecoration*)decoration;
185 @end
187 namespace autocomplete_text_field {
189 // Draw gray text suggestion in |controlView|.
190 void DrawGrayTextAutocompletion(NSAttributedString* mainText,
191 NSString* suggestText,
192 NSColor* suggestColor,
193 NSView* controlView,
194 NSRect frame);
196 } // namespace autocomplete_text_field
198 #endif // CHROME_BROWSER_UI_COCOA_AUTOCOMPLETE_TEXT_FIELD_H_