OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/cocoa/autocomplete_text_field.h" | 5 #include "chrome/browser/cocoa/autocomplete_text_field.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "chrome/browser/cocoa/autocomplete_text_field_cell.h" | 8 #include "chrome/browser/cocoa/autocomplete_text_field_cell.h" |
9 | 9 |
10 @implementation AutocompleteTextField | 10 @implementation AutocompleteTextField |
(...skipping 15 matching lines...) Expand all Loading... |
26 } | 26 } |
27 | 27 |
28 - (void)flagsChanged:(NSEvent*)theEvent { | 28 - (void)flagsChanged:(NSEvent*)theEvent { |
29 id delegate = [self delegate]; | 29 id delegate = [self delegate]; |
30 if ([delegate respondsToSelector:@selector(control:flagsChanged:)]) { | 30 if ([delegate respondsToSelector:@selector(control:flagsChanged:)]) { |
31 [delegate control:self flagsChanged:theEvent]; | 31 [delegate control:self flagsChanged:theEvent]; |
32 } | 32 } |
33 [super flagsChanged:theEvent]; | 33 [super flagsChanged:theEvent]; |
34 } | 34 } |
35 | 35 |
| 36 - (AutocompleteTextFieldCell*)autocompleteTextFieldCell { |
| 37 DCHECK([[self cell] isKindOfClass:[AutocompleteTextFieldCell class]]); |
| 38 return static_cast<AutocompleteTextFieldCell*>([self cell]); |
| 39 } |
| 40 |
| 41 // TODO(shess): An alternate implementation of this would be to pick |
| 42 // out the field's subview (which may be a clip view around the field |
| 43 // editor) and manually resize it to the textFrame returned from the |
| 44 // cell's -splitFrame:*. That doesn't mess with the editing state of |
| 45 // the field editor system, but does make other assumptions about how |
| 46 // field editors are placed. |
| 47 - (void)resetFieldEditorFrameIfNeeded { |
| 48 AutocompleteTextFieldCell* cell = [self cell]; |
| 49 if ([cell fieldEditorNeedsReset]) { |
| 50 NSTextView* editor = (id)[self currentEditor]; |
| 51 if (editor) { |
| 52 // Clear the delegate so that it doesn't see |
| 53 // -control:textShouldEndEditing: (closes autocomplete popup). |
| 54 id delegate = [self delegate]; |
| 55 [self setDelegate:nil]; |
| 56 |
| 57 // -makeFirstResponder: will select-all, restore selection. |
| 58 NSRange sel = [editor selectedRange]; |
| 59 [[self window] makeFirstResponder:self]; |
| 60 [editor setSelectedRange:sel]; |
| 61 |
| 62 [self setDelegate:delegate]; |
| 63 |
| 64 // Now provoke call to delegate's -controlTextDidBeginEditing:. |
| 65 // This is necessary to make sure that we'll send the |
| 66 // appropriate -control:textShouldEndEditing: call when we lose |
| 67 // focus. |
| 68 // TODO(shess): Would be better to only restore this state if |
| 69 // -controlTextDidBeginEditing: had already been sent. |
| 70 // Unfortunately, that's hard to detect. Could either check |
| 71 // popup IsOpen() or model has_focus()? |
| 72 [editor shouldChangeTextInRange:sel replacementString:@""]; |
| 73 } |
| 74 [cell setFieldEditorNeedsReset:NO]; |
| 75 } |
| 76 } |
| 77 |
| 78 // Clicks in the frame outside the field editor will attempt to make |
| 79 // us first-responder, which will select-all. So decline |
| 80 // first-responder when the field editor is active. |
| 81 - (BOOL)acceptsFirstResponder { |
| 82 if ([self currentEditor]) { |
| 83 return NO; |
| 84 } |
| 85 return [super acceptsFirstResponder]; |
| 86 } |
| 87 |
| 88 // Reroute events for the decoration area to the field editor. This |
| 89 // will cause the cursor to be moved as close to the edge where the |
| 90 // event was seen as possible. |
| 91 // |
| 92 // TODO(shess) Check this in light of the -acceptsFirstResponder |
| 93 // change. It may no longer be needed. |
| 94 - (void)mouseDown:(NSEvent *)theEvent { |
| 95 NSText* editor = [self currentEditor]; |
| 96 if (editor) { |
| 97 [editor mouseDown:theEvent]; |
| 98 } else { |
| 99 [super mouseDown:theEvent]; |
| 100 } |
| 101 } |
| 102 |
36 @end | 103 @end |
OLD | NEW |