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