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