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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 } | 151 } |
152 | 152 |
153 - (void)moveLeft:(id)sender { | 153 - (void)moveLeft:(id)sender { |
154 [self doCommandByID:IDS_MOVE_LEFT]; | 154 [self doCommandByID:IDS_MOVE_LEFT]; |
155 } | 155 } |
156 | 156 |
157 - (void)moveRight:(id)sender { | 157 - (void)moveRight:(id)sender { |
158 [self doCommandByID:IDS_MOVE_RIGHT]; | 158 [self doCommandByID:IDS_MOVE_RIGHT]; |
159 } | 159 } |
160 | 160 |
| 161 - (void)insertText:(id)text { |
| 162 if (textInputClient_) |
| 163 textInputClient_->InsertText(base::SysNSStringToUTF16(text)); |
| 164 } |
| 165 |
| 166 // Support for Services in context menus. |
| 167 // Currently we only support reading and writing plain strings. |
| 168 - (id)validRequestorForSendType:(NSString*)sendType |
| 169 returnType:(NSString*)returnType { |
| 170 BOOL canWrite = [sendType isEqualToString:NSStringPboardType] && |
| 171 [self selectedRange].length > 0; |
| 172 BOOL canRead = [returnType isEqualToString:NSStringPboardType]; |
| 173 // Valid if (sendType, returnType) is either (string, nil), (nil, string), |
| 174 // or (string, string). |
| 175 BOOL valid = textInputClient_ && ((canWrite && (canRead || !returnType)) || |
| 176 (canRead && (canWrite || !sendType))); |
| 177 return valid ? self : [super validRequestorForSendType:sendType |
| 178 returnType:returnType]; |
| 179 } |
| 180 |
| 181 // NSServicesRequests informal protocol. |
| 182 |
| 183 - (BOOL)writeSelectionToPasteboard:(NSPasteboard*)pboard types:(NSArray*)types { |
| 184 DCHECK([types containsObject:NSStringPboardType]); |
| 185 if (!textInputClient_) |
| 186 return NO; |
| 187 |
| 188 gfx::Range selectionRange; |
| 189 if (!textInputClient_->GetSelectionRange(&selectionRange)) |
| 190 return NO; |
| 191 |
| 192 base::string16 text; |
| 193 textInputClient_->GetTextFromRange(selectionRange, &text); |
| 194 return [pboard writeObjects:@[ base::SysUTF16ToNSString(text) ]]; |
| 195 } |
| 196 |
| 197 - (BOOL)readSelectionFromPasteboard:(NSPasteboard*)pboard { |
| 198 NSArray* objects = |
| 199 [pboard readObjectsForClasses:@[ [NSString class] ] options:0]; |
| 200 DCHECK([objects count] == 1); |
| 201 [self insertText:[objects lastObject]]; |
| 202 return YES; |
| 203 } |
| 204 |
161 // NSTextInputClient protocol implementation. | 205 // NSTextInputClient protocol implementation. |
162 | 206 |
163 - (NSAttributedString*) | 207 - (NSAttributedString*) |
164 attributedSubstringForProposedRange:(NSRange)range | 208 attributedSubstringForProposedRange:(NSRange)range |
165 actualRange:(NSRangePointer)actualRange { | 209 actualRange:(NSRangePointer)actualRange { |
166 base::string16 substring; | 210 base::string16 substring; |
167 if (textInputClient_) { | 211 if (textInputClient_) { |
168 gfx::Range textRange; | 212 gfx::Range textRange; |
169 textInputClient_->GetTextRange(&textRange); | 213 textInputClient_->GetTextRange(&textRange); |
170 gfx::Range subrange = textRange.Intersect(gfx::Range(range)); | 214 gfx::Range subrange = textRange.Intersect(gfx::Range(range)); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 - (void)unmarkText { | 287 - (void)unmarkText { |
244 if (textInputClient_) | 288 if (textInputClient_) |
245 textInputClient_->ConfirmCompositionText(); | 289 textInputClient_->ConfirmCompositionText(); |
246 } | 290 } |
247 | 291 |
248 - (NSArray*)validAttributesForMarkedText { | 292 - (NSArray*)validAttributesForMarkedText { |
249 return @[]; | 293 return @[]; |
250 } | 294 } |
251 | 295 |
252 @end | 296 @end |
OLD | NEW |