OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #import "ui/views/cocoa/bridged_content_view.h" | 5 #import "ui/views/cocoa/bridged_content_view.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
9 #include "grit/ui_strings.h" | 9 #include "grit/ui_strings.h" |
10 #include "ui/base/ime/text_input_client.h" | 10 #include "ui/base/ime/text_input_client.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 } | 83 } |
84 | 84 |
85 - (void)moveLeft:(id)sender { | 85 - (void)moveLeft:(id)sender { |
86 [self doCommandByID:IDS_MOVE_LEFT]; | 86 [self doCommandByID:IDS_MOVE_LEFT]; |
87 } | 87 } |
88 | 88 |
89 - (void)moveRight:(id)sender { | 89 - (void)moveRight:(id)sender { |
90 [self doCommandByID:IDS_MOVE_RIGHT]; | 90 [self doCommandByID:IDS_MOVE_RIGHT]; |
91 } | 91 } |
92 | 92 |
| 93 - (void)insertText:(id)text { |
| 94 if (textInputClient_) |
| 95 textInputClient_->InsertText(base::SysNSStringToUTF16(text)); |
| 96 } |
| 97 |
| 98 // Support for Services in context menus. |
| 99 // Currently we only support reading and writing plain strings. |
| 100 - (id)validRequestorForSendType:(NSString*)sendType |
| 101 returnType:(NSString*)returnType { |
| 102 BOOL canWrite = [sendType isEqualToString:NSStringPboardType] && |
| 103 [self selectedRange].length > 0; |
| 104 BOOL canRead = [returnType isEqualToString:NSStringPboardType]; |
| 105 // Valid if (sendType, returnType) is either (string, nil), (nil, string), |
| 106 // or (string, string). |
| 107 BOOL valid = textInputClient_ && ((canWrite && (canRead || !returnType)) || |
| 108 (canRead && (canWrite || !sendType))); |
| 109 return valid ? self : [super validRequestorForSendType:sendType |
| 110 returnType:returnType]; |
| 111 } |
| 112 |
| 113 // NSServicesRequests informal protocol. |
| 114 |
| 115 - (BOOL)writeSelectionToPasteboard:(NSPasteboard*)pboard types:(NSArray*)types { |
| 116 DCHECK([types containsObject:NSStringPboardType]); |
| 117 if (!textInputClient_) |
| 118 return NO; |
| 119 |
| 120 gfx::Range selectionRange; |
| 121 if (!textInputClient_->GetSelectionRange(&selectionRange)) |
| 122 return NO; |
| 123 |
| 124 base::string16 text; |
| 125 textInputClient_->GetTextFromRange(selectionRange, &text); |
| 126 return [pboard writeObjects:@[ base::SysUTF16ToNSString(text) ]]; |
| 127 } |
| 128 |
| 129 - (BOOL)readSelectionFromPasteboard:(NSPasteboard*)pboard { |
| 130 NSArray* objects = |
| 131 [pboard readObjectsForClasses:@[ [NSString class] ] options:0]; |
| 132 DCHECK([objects count] == 1); |
| 133 [self insertText:[objects lastObject]]; |
| 134 return YES; |
| 135 } |
| 136 |
93 // NSTextInputClient protocol implementation. | 137 // NSTextInputClient protocol implementation. |
94 | 138 |
95 - (NSAttributedString*) | 139 - (NSAttributedString*) |
96 attributedSubstringForProposedRange:(NSRange)range | 140 attributedSubstringForProposedRange:(NSRange)range |
97 actualRange:(NSRangePointer)actualRange { | 141 actualRange:(NSRangePointer)actualRange { |
98 base::string16 substring; | 142 base::string16 substring; |
99 if (textInputClient_) { | 143 if (textInputClient_) { |
100 gfx::Range textRange; | 144 gfx::Range textRange; |
101 textInputClient_->GetTextRange(&textRange); | 145 textInputClient_->GetTextRange(&textRange); |
102 gfx::Range subrange = textRange.Intersect(gfx::Range(range)); | 146 gfx::Range subrange = textRange.Intersect(gfx::Range(range)); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 - (void)unmarkText { | 219 - (void)unmarkText { |
176 if (textInputClient_) | 220 if (textInputClient_) |
177 textInputClient_->ConfirmCompositionText(); | 221 textInputClient_->ConfirmCompositionText(); |
178 } | 222 } |
179 | 223 |
180 - (NSArray*)validAttributesForMarkedText { | 224 - (NSArray*)validAttributesForMarkedText { |
181 return @[]; | 225 return @[]; |
182 } | 226 } |
183 | 227 |
184 @end | 228 @end |
OLD | NEW |