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