| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/autocomplete/autocomplete_edit_view_mac.h" | 5 #include "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" |
| 6 | 6 |
| 7 #include <Carbon/Carbon.h> // kVK_Return | 7 #include <Carbon/Carbon.h> // kVK_Return |
| 8 | 8 |
| 9 #include "app/mac/nsimage_cache.h" | 9 #include "app/mac/nsimage_cache.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 } | 225 } |
| 226 | 226 |
| 227 void AutocompleteEditViewMac::Update( | 227 void AutocompleteEditViewMac::Update( |
| 228 const TabContents* tab_for_state_restoring) { | 228 const TabContents* tab_for_state_restoring) { |
| 229 // TODO(shess): It seems like if the tab is non-NULL, then this code | 229 // TODO(shess): It seems like if the tab is non-NULL, then this code |
| 230 // shouldn't need to be called at all. When coded that way, I find | 230 // shouldn't need to be called at all. When coded that way, I find |
| 231 // that the field isn't always updated correctly. Figure out why | 231 // that the field isn't always updated correctly. Figure out why |
| 232 // this is. Maybe this method should be refactored into more | 232 // this is. Maybe this method should be refactored into more |
| 233 // specific cases. | 233 // specific cases. |
| 234 const bool user_visible = | 234 const bool user_visible = |
| 235 model_->UpdatePermanentText(WideToUTF16Hack(toolbar_model_->GetText())); | 235 model_->UpdatePermanentText(toolbar_model_->GetText()); |
| 236 | 236 |
| 237 if (tab_for_state_restoring) { | 237 if (tab_for_state_restoring) { |
| 238 RevertAll(); | 238 RevertAll(); |
| 239 | 239 |
| 240 const AutocompleteEditViewMacState* state = | 240 const AutocompleteEditViewMacState* state = |
| 241 GetStateFromTab(tab_for_state_restoring); | 241 GetStateFromTab(tab_for_state_restoring); |
| 242 if (state) { | 242 if (state) { |
| 243 // Should restore the user's text via SetUserText(). | 243 // Should restore the user's text via SetUserText(). |
| 244 model_->RestoreState(state->model_state); | 244 model_->RestoreState(state->model_state); |
| 245 | 245 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 272 // that should go away. | 272 // that should go away. |
| 273 EmphasizeURLComponents(); | 273 EmphasizeURLComponents(); |
| 274 } | 274 } |
| 275 } | 275 } |
| 276 | 276 |
| 277 void AutocompleteEditViewMac::OpenURL(const GURL& url, | 277 void AutocompleteEditViewMac::OpenURL(const GURL& url, |
| 278 WindowOpenDisposition disposition, | 278 WindowOpenDisposition disposition, |
| 279 PageTransition::Type transition, | 279 PageTransition::Type transition, |
| 280 const GURL& alternate_nav_url, | 280 const GURL& alternate_nav_url, |
| 281 size_t selected_line, | 281 size_t selected_line, |
| 282 const string16& keyword) { | 282 const std::wstring& keyword) { |
| 283 // TODO(shess): Why is the caller passing an invalid url in the | 283 // TODO(shess): Why is the caller passing an invalid url in the |
| 284 // first place? Make sure that case isn't being dropped on the | 284 // first place? Make sure that case isn't being dropped on the |
| 285 // floor. | 285 // floor. |
| 286 if (!url.is_valid()) { | 286 if (!url.is_valid()) { |
| 287 return; | 287 return; |
| 288 } | 288 } |
| 289 | 289 |
| 290 model_->OpenURL(url, disposition, transition, alternate_nav_url, | 290 model_->OpenURL(url, disposition, transition, alternate_nav_url, |
| 291 selected_line, keyword); | 291 selected_line, keyword); |
| 292 } | 292 } |
| 293 | 293 |
| 294 string16 AutocompleteEditViewMac::GetText() const { | 294 std::wstring AutocompleteEditViewMac::GetText() const { |
| 295 return base::SysNSStringToUTF16(GetNonSuggestTextSubstring()); | 295 return base::SysNSStringToWide(GetNonSuggestTextSubstring()); |
| 296 } | 296 } |
| 297 | 297 |
| 298 bool AutocompleteEditViewMac::IsEditingOrEmpty() const { | 298 bool AutocompleteEditViewMac::IsEditingOrEmpty() const { |
| 299 return model_->user_input_in_progress() || | 299 return model_->user_input_in_progress() || |
| 300 ([[field_ stringValue] length] == 0); | 300 ([[field_ stringValue] length] == 0); |
| 301 } | 301 } |
| 302 | 302 |
| 303 int AutocompleteEditViewMac::GetIcon() const { | 303 int AutocompleteEditViewMac::GetIcon() const { |
| 304 return IsEditingOrEmpty() ? | 304 return IsEditingOrEmpty() ? |
| 305 AutocompleteMatch::TypeToIcon(model_->CurrentTextType()) : | 305 AutocompleteMatch::TypeToIcon(model_->CurrentTextType()) : |
| 306 toolbar_model_->GetIcon(); | 306 toolbar_model_->GetIcon(); |
| 307 } | 307 } |
| 308 | 308 |
| 309 void AutocompleteEditViewMac::SetUserText(const string16& text) { | 309 void AutocompleteEditViewMac::SetUserText(const std::wstring& text) { |
| 310 SetUserText(text, text, true); | 310 SetUserText(text, text, true); |
| 311 } | 311 } |
| 312 | 312 |
| 313 void AutocompleteEditViewMac::SetUserText(const string16& text, | 313 void AutocompleteEditViewMac::SetUserText(const std::wstring& text, |
| 314 const string16& display_text, | 314 const std::wstring& display_text, |
| 315 bool update_popup) { | 315 bool update_popup) { |
| 316 model_->SetUserText(text); | 316 model_->SetUserText(text); |
| 317 // TODO(shess): TODO below from gtk. | 317 // TODO(shess): TODO below from gtk. |
| 318 // TODO(deanm): something about selection / focus change here. | 318 // TODO(deanm): something about selection / focus change here. |
| 319 SetText(display_text); | 319 SetText(display_text); |
| 320 if (update_popup) { | 320 if (update_popup) { |
| 321 UpdatePopup(); | 321 UpdatePopup(); |
| 322 } | 322 } |
| 323 controller_->OnChanged(); | 323 controller_->OnChanged(); |
| 324 } | 324 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 344 } | 344 } |
| 345 | 345 |
| 346 // TODO(shess): What if it didn't get first responder, and there is | 346 // TODO(shess): What if it didn't get first responder, and there is |
| 347 // no field editor? This will do nothing. Well, at least it won't | 347 // no field editor? This will do nothing. Well, at least it won't |
| 348 // crash. Think of something more productive to do, or prove that | 348 // crash. Think of something more productive to do, or prove that |
| 349 // it cannot occur and DCHECK appropriately. | 349 // it cannot occur and DCHECK appropriately. |
| 350 [[field_ currentEditor] setSelectedRange:range]; | 350 [[field_ currentEditor] setSelectedRange:range]; |
| 351 } | 351 } |
| 352 } | 352 } |
| 353 | 353 |
| 354 void AutocompleteEditViewMac::SetWindowTextAndCaretPos(const string16& text, | 354 void AutocompleteEditViewMac::SetWindowTextAndCaretPos(const std::wstring& text, |
| 355 size_t caret_pos) { | 355 size_t caret_pos) { |
| 356 DCHECK_LE(caret_pos, text.size()); | 356 DCHECK_LE(caret_pos, text.size()); |
| 357 SetTextAndSelectedRange(text, NSMakeRange(caret_pos, caret_pos)); | 357 SetTextAndSelectedRange(text, NSMakeRange(caret_pos, caret_pos)); |
| 358 } | 358 } |
| 359 | 359 |
| 360 void AutocompleteEditViewMac::SetForcedQuery() { | 360 void AutocompleteEditViewMac::SetForcedQuery() { |
| 361 // We need to do this first, else |SetSelectedRange()| won't work. | 361 // We need to do this first, else |SetSelectedRange()| won't work. |
| 362 FocusLocation(true); | 362 FocusLocation(true); |
| 363 | 363 |
| 364 const string16 current_text(GetText()); | 364 const std::wstring current_text(GetText()); |
| 365 const size_t start = current_text.find_first_not_of(kWhitespaceUTF16); | 365 const size_t start = current_text.find_first_not_of(kWhitespaceWide); |
| 366 if (start == string16::npos || (current_text[start] != '?')) { | 366 if (start == std::wstring::npos || (current_text[start] != '?')) { |
| 367 SetUserText(ASCIIToUTF16("?")); | 367 SetUserText(L"?"); |
| 368 } else { | 368 } else { |
| 369 NSRange range = NSMakeRange(start + 1, current_text.size() - start - 1); | 369 NSRange range = NSMakeRange(start + 1, current_text.size() - start - 1); |
| 370 [[field_ currentEditor] setSelectedRange:range]; | 370 [[field_ currentEditor] setSelectedRange:range]; |
| 371 } | 371 } |
| 372 } | 372 } |
| 373 | 373 |
| 374 bool AutocompleteEditViewMac::IsSelectAll() { | 374 bool AutocompleteEditViewMac::IsSelectAll() { |
| 375 if (![field_ currentEditor]) | 375 if (![field_ currentEditor]) |
| 376 return true; | 376 return true; |
| 377 const NSRange all_range = NSMakeRange(0, GetText().length()); | 377 const NSRange all_range = NSMakeRange(0, GetText().length()); |
| 378 return NSEqualRanges(all_range, GetSelectedRange()); | 378 return NSEqualRanges(all_range, GetSelectedRange()); |
| 379 } | 379 } |
| 380 | 380 |
| 381 bool AutocompleteEditViewMac::DeleteAtEndPressed() { | 381 bool AutocompleteEditViewMac::DeleteAtEndPressed() { |
| 382 return delete_at_end_pressed_; | 382 return delete_at_end_pressed_; |
| 383 } | 383 } |
| 384 | 384 |
| 385 void AutocompleteEditViewMac::GetSelectionBounds(string16::size_type* start, | 385 void AutocompleteEditViewMac::GetSelectionBounds(std::wstring::size_type* start, |
| 386 string16::size_type* end) { | 386 std::wstring::size_type* end) { |
| 387 if (![field_ currentEditor]) { | 387 if (![field_ currentEditor]) { |
| 388 *start = *end = 0; | 388 *start = *end = 0; |
| 389 return; | 389 return; |
| 390 } | 390 } |
| 391 | 391 |
| 392 const NSRange selected_range = GetSelectedRange(); | 392 const NSRange selected_range = GetSelectedRange(); |
| 393 *start = static_cast<size_t>(selected_range.location); | 393 *start = static_cast<size_t>(selected_range.location); |
| 394 *end = static_cast<size_t>(NSMaxRange(selected_range)); | 394 *end = static_cast<size_t>(NSMaxRange(selected_range)); |
| 395 } | 395 } |
| 396 | 396 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 popup_view_->GetModel()->StopAutocomplete(); | 441 popup_view_->GetModel()->StopAutocomplete(); |
| 442 } | 442 } |
| 443 | 443 |
| 444 void AutocompleteEditViewMac::SetFocus() { | 444 void AutocompleteEditViewMac::SetFocus() { |
| 445 } | 445 } |
| 446 | 446 |
| 447 bool AutocompleteEditViewMac::CommitSuggestText() { | 447 bool AutocompleteEditViewMac::CommitSuggestText() { |
| 448 if (suggest_text_length_ == 0) | 448 if (suggest_text_length_ == 0) |
| 449 return false; | 449 return false; |
| 450 | 450 |
| 451 string16 input_text(GetText()); | 451 std::wstring input_text(GetText()); |
| 452 suggest_text_length_ = 0; | 452 suggest_text_length_ = 0; |
| 453 string16 text(GetText()); | 453 std::wstring text(GetText()); |
| 454 // Call SetText() to force a redraw and move the cursor to the end. | 454 // Call SetText() to force a redraw and move the cursor to the end. |
| 455 SetText(text); | 455 SetText(text); |
| 456 model()->FinalizeInstantQuery(input_text, text.substr(input_text.size())); | 456 model()->FinalizeInstantQuery(input_text, text.substr(input_text.size())); |
| 457 return true; | 457 return true; |
| 458 } | 458 } |
| 459 | 459 |
| 460 void AutocompleteEditViewMac::SetText(const string16& display_text) { | 460 void AutocompleteEditViewMac::SetText(const std::wstring& display_text) { |
| 461 // If we are setting the text directly, there cannot be any suggest text. | 461 // If we are setting the text directly, there cannot be any suggest text. |
| 462 suggest_text_length_ = 0; | 462 suggest_text_length_ = 0; |
| 463 SetTextInternal(display_text); | 463 SetTextInternal(display_text); |
| 464 } | 464 } |
| 465 | 465 |
| 466 void AutocompleteEditViewMac::SetTextInternal( | 466 void AutocompleteEditViewMac::SetTextInternal( |
| 467 const string16& display_text) { | 467 const std::wstring& display_text) { |
| 468 NSString* ss = base::SysUTF16ToNSString(display_text); | 468 NSString* ss = base::SysWideToNSString(display_text); |
| 469 NSMutableAttributedString* as = | 469 NSMutableAttributedString* as = |
| 470 [[[NSMutableAttributedString alloc] initWithString:ss] autorelease]; | 470 [[[NSMutableAttributedString alloc] initWithString:ss] autorelease]; |
| 471 | 471 |
| 472 ApplyTextAttributes(display_text, as); | 472 ApplyTextAttributes(display_text, as); |
| 473 | 473 |
| 474 [field_ setAttributedStringValue:as]; | 474 [field_ setAttributedStringValue:as]; |
| 475 | 475 |
| 476 // TODO(shess): This may be an appropriate place to call: | 476 // TODO(shess): This may be an appropriate place to call: |
| 477 // controller_->OnChanged(); | 477 // controller_->OnChanged(); |
| 478 // In the current implementation, this tells LocationBarViewMac to | 478 // In the current implementation, this tells LocationBarViewMac to |
| 479 // mess around with |model_| and update |field_|. Unfortunately, | 479 // mess around with |model_| and update |field_|. Unfortunately, |
| 480 // when I look at our peer implementations, it's not entirely clear | 480 // when I look at our peer implementations, it's not entirely clear |
| 481 // to me if this is safe. SetTextInternal() is sort of an utility method, | 481 // to me if this is safe. SetTextInternal() is sort of an utility method, |
| 482 // and different callers sometimes have different needs. Research | 482 // and different callers sometimes have different needs. Research |
| 483 // this issue so that it can be added safely. | 483 // this issue so that it can be added safely. |
| 484 | 484 |
| 485 // TODO(shess): Also, consider whether this code couldn't just | 485 // TODO(shess): Also, consider whether this code couldn't just |
| 486 // manage things directly. Windows uses a series of overlaid view | 486 // manage things directly. Windows uses a series of overlaid view |
| 487 // objects to accomplish the hinting stuff that OnChanged() does, so | 487 // objects to accomplish the hinting stuff that OnChanged() does, so |
| 488 // it makes sense to have it in the controller that lays those | 488 // it makes sense to have it in the controller that lays those |
| 489 // things out. Mac instead pushes the support into a custom | 489 // things out. Mac instead pushes the support into a custom |
| 490 // text-field implementation. | 490 // text-field implementation. |
| 491 } | 491 } |
| 492 | 492 |
| 493 void AutocompleteEditViewMac::SetTextAndSelectedRange( | 493 void AutocompleteEditViewMac::SetTextAndSelectedRange( |
| 494 const string16& display_text, const NSRange range) { | 494 const std::wstring& display_text, const NSRange range) { |
| 495 SetText(display_text); | 495 SetText(display_text); |
| 496 SetSelectedRange(range); | 496 SetSelectedRange(range); |
| 497 } | 497 } |
| 498 | 498 |
| 499 NSString* AutocompleteEditViewMac::GetNonSuggestTextSubstring() const { | 499 NSString* AutocompleteEditViewMac::GetNonSuggestTextSubstring() const { |
| 500 NSString* text = [field_ stringValue]; | 500 NSString* text = [field_ stringValue]; |
| 501 if (suggest_text_length_ > 0) { | 501 if (suggest_text_length_ > 0) { |
| 502 NSUInteger length = [text length]; | 502 NSUInteger length = [text length]; |
| 503 | 503 |
| 504 DCHECK_LE(suggest_text_length_, length); | 504 DCHECK_LE(suggest_text_length_, length); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 521 range:NSMakeRange(0, [storage length])]; | 521 range:NSMakeRange(0, [storage length])]; |
| 522 ApplyTextAttributes(GetText(), storage); | 522 ApplyTextAttributes(GetText(), storage); |
| 523 | 523 |
| 524 [storage endEditing]; | 524 [storage endEditing]; |
| 525 } else { | 525 } else { |
| 526 SetText(GetText()); | 526 SetText(GetText()); |
| 527 } | 527 } |
| 528 } | 528 } |
| 529 | 529 |
| 530 void AutocompleteEditViewMac::ApplyTextAttributes( | 530 void AutocompleteEditViewMac::ApplyTextAttributes( |
| 531 const string16& display_text, NSMutableAttributedString* as) { | 531 const std::wstring& display_text, NSMutableAttributedString* as) { |
| 532 [as addAttribute:NSFontAttributeName value:GetFieldFont() | 532 [as addAttribute:NSFontAttributeName value:GetFieldFont() |
| 533 range:NSMakeRange(0, [as length])]; | 533 range:NSMakeRange(0, [as length])]; |
| 534 | 534 |
| 535 // Make a paragraph style locking in the standard line height as the maximum, | 535 // Make a paragraph style locking in the standard line height as the maximum, |
| 536 // otherwise the baseline may shift "downwards". | 536 // otherwise the baseline may shift "downwards". |
| 537 scoped_nsobject<NSMutableParagraphStyle> | 537 scoped_nsobject<NSMutableParagraphStyle> |
| 538 paragraph_style([[NSMutableParagraphStyle alloc] init]); | 538 paragraph_style([[NSMutableParagraphStyle alloc] init]); |
| 539 [paragraph_style setMaximumLineHeight:line_height_]; | 539 [paragraph_style setMaximumLineHeight:line_height_]; |
| 540 [as addAttribute:NSParagraphStyleAttributeName value:paragraph_style | 540 [as addAttribute:NSParagraphStyleAttributeName value:paragraph_style |
| 541 range:NSMakeRange(0, [as length])]; | 541 range:NSMakeRange(0, [as length])]; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 } else { | 581 } else { |
| 582 NOTREACHED(); | 582 NOTREACHED(); |
| 583 color = BaseTextColor(); | 583 color = BaseTextColor(); |
| 584 } | 584 } |
| 585 [as addAttribute:NSForegroundColorAttributeName value:color | 585 [as addAttribute:NSForegroundColorAttributeName value:color |
| 586 range:ComponentToNSRange(scheme)]; | 586 range:ComponentToNSRange(scheme)]; |
| 587 } | 587 } |
| 588 } | 588 } |
| 589 | 589 |
| 590 void AutocompleteEditViewMac::OnTemporaryTextMaybeChanged( | 590 void AutocompleteEditViewMac::OnTemporaryTextMaybeChanged( |
| 591 const string16& display_text, bool save_original_selection) { | 591 const std::wstring& display_text, bool save_original_selection) { |
| 592 if (save_original_selection) | 592 if (save_original_selection) |
| 593 saved_temporary_selection_ = GetSelectedRange(); | 593 saved_temporary_selection_ = GetSelectedRange(); |
| 594 | 594 |
| 595 suggest_text_length_ = 0; | 595 suggest_text_length_ = 0; |
| 596 SetWindowTextAndCaretPos(display_text, display_text.size()); | 596 SetWindowTextAndCaretPos(display_text, display_text.size()); |
| 597 controller_->OnChanged(); | 597 controller_->OnChanged(); |
| 598 [field_ clearUndoChain]; | 598 [field_ clearUndoChain]; |
| 599 } | 599 } |
| 600 | 600 |
| 601 void AutocompleteEditViewMac::OnStartingIME() { | 601 void AutocompleteEditViewMac::OnStartingIME() { |
| 602 // Reset the suggest text just before starting an IME composition session, | 602 // Reset the suggest text just before starting an IME composition session, |
| 603 // otherwise the IME composition may be interrupted when the suggest text | 603 // otherwise the IME composition may be interrupted when the suggest text |
| 604 // gets reset by the IME composition change. | 604 // gets reset by the IME composition change. |
| 605 SetInstantSuggestion(string16()); | 605 SetInstantSuggestion(string16()); |
| 606 } | 606 } |
| 607 | 607 |
| 608 bool AutocompleteEditViewMac::OnInlineAutocompleteTextMaybeChanged( | 608 bool AutocompleteEditViewMac::OnInlineAutocompleteTextMaybeChanged( |
| 609 const string16& display_text, size_t user_text_length) { | 609 const std::wstring& display_text, size_t user_text_length) { |
| 610 // TODO(shess): Make sure that this actually works. The round trip | 610 // TODO(shess): Make sure that this actually works. The round trip |
| 611 // to native form and back may mean that it's the same but not the | 611 // to native form and back may mean that it's the same but not the |
| 612 // same. | 612 // same. |
| 613 if (display_text == GetText()) | 613 if (display_text == GetText()) |
| 614 return false; | 614 return false; |
| 615 | 615 |
| 616 DCHECK_LE(user_text_length, display_text.size()); | 616 DCHECK_LE(user_text_length, display_text.size()); |
| 617 const NSRange range = | 617 const NSRange range = |
| 618 NSMakeRange(user_text_length, display_text.size() - user_text_length); | 618 NSMakeRange(user_text_length, display_text.size() - user_text_length); |
| 619 SetTextAndSelectedRange(display_text, range); | 619 SetTextAndSelectedRange(display_text, range); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 638 selection_before_change_ = GetSelectedRange(); | 638 selection_before_change_ = GetSelectedRange(); |
| 639 text_before_change_ = GetText(); | 639 text_before_change_ = GetText(); |
| 640 marked_range_before_change_ = GetMarkedRange(); | 640 marked_range_before_change_ = GetMarkedRange(); |
| 641 } | 641 } |
| 642 | 642 |
| 643 bool AutocompleteEditViewMac::OnAfterPossibleChange() { | 643 bool AutocompleteEditViewMac::OnAfterPossibleChange() { |
| 644 // We should only arrive here when the field is focussed. | 644 // We should only arrive here when the field is focussed. |
| 645 DCHECK(IsFirstResponder()); | 645 DCHECK(IsFirstResponder()); |
| 646 | 646 |
| 647 const NSRange new_selection(GetSelectedRange()); | 647 const NSRange new_selection(GetSelectedRange()); |
| 648 const string16 new_text(GetText()); | 648 const std::wstring new_text(GetText()); |
| 649 const size_t length = new_text.length(); | 649 const size_t length = new_text.length(); |
| 650 | 650 |
| 651 const bool selection_differs = | 651 const bool selection_differs = |
| 652 (new_selection.length || selection_before_change_.length) && | 652 (new_selection.length || selection_before_change_.length) && |
| 653 !NSEqualRanges(new_selection, selection_before_change_); | 653 !NSEqualRanges(new_selection, selection_before_change_); |
| 654 const bool at_end_of_edit = (length == new_selection.location); | 654 const bool at_end_of_edit = (length == new_selection.location); |
| 655 const bool text_differs = (new_text != text_before_change_) || | 655 const bool text_differs = (new_text != text_before_change_) || |
| 656 !NSEqualRanges(marked_range_before_change_, GetMarkedRange()); | 656 !NSEqualRanges(marked_range_before_change_, GetMarkedRange()); |
| 657 | 657 |
| 658 // When the user has deleted text, we don't allow inline | 658 // When the user has deleted text, we don't allow inline |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 708 // Append the new suggest text. | 708 // Append the new suggest text. |
| 709 suggest_text_length_ = suggest_text.length(); | 709 suggest_text_length_ = suggest_text.length(); |
| 710 if (suggest_text_length_ > 0) { | 710 if (suggest_text_length_ > 0) { |
| 711 text = [text stringByAppendingString:base::SysUTF16ToNSString( | 711 text = [text stringByAppendingString:base::SysUTF16ToNSString( |
| 712 suggest_text)]; | 712 suggest_text)]; |
| 713 needs_update = true; | 713 needs_update = true; |
| 714 } | 714 } |
| 715 | 715 |
| 716 if (needs_update) { | 716 if (needs_update) { |
| 717 NSRange current_range = GetSelectedRange(); | 717 NSRange current_range = GetSelectedRange(); |
| 718 SetTextInternal(base::SysNSStringToUTF16(text)); | 718 SetTextInternal(base::SysNSStringToWide(text)); |
| 719 if (NSMaxRange(current_range) <= [text length] - suggest_text_length_) | 719 if (NSMaxRange(current_range) <= [text length] - suggest_text_length_) |
| 720 SetSelectedRange(current_range); | 720 SetSelectedRange(current_range); |
| 721 else | 721 else |
| 722 SetSelectedRange(NSMakeRange([text length] - suggest_text_length_, 0)); | 722 SetSelectedRange(NSMakeRange([text length] - suggest_text_length_, 0)); |
| 723 } | 723 } |
| 724 } | 724 } |
| 725 | 725 |
| 726 int AutocompleteEditViewMac::TextWidth() const { | 726 int AutocompleteEditViewMac::TextWidth() const { |
| 727 // Not used on mac. | 727 // Not used on mac. |
| 728 NOTREACHED(); | 728 NOTREACHED(); |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 894 | 894 |
| 895 bool AutocompleteEditViewMac::CanCopy() { | 895 bool AutocompleteEditViewMac::CanCopy() { |
| 896 const NSRange selection = GetSelectedRange(); | 896 const NSRange selection = GetSelectedRange(); |
| 897 return selection.length > 0; | 897 return selection.length > 0; |
| 898 } | 898 } |
| 899 | 899 |
| 900 void AutocompleteEditViewMac::CopyToPasteboard(NSPasteboard* pb) { | 900 void AutocompleteEditViewMac::CopyToPasteboard(NSPasteboard* pb) { |
| 901 DCHECK(CanCopy()); | 901 DCHECK(CanCopy()); |
| 902 | 902 |
| 903 const NSRange selection = GetSelectedRange(); | 903 const NSRange selection = GetSelectedRange(); |
| 904 string16 text = base::SysNSStringToUTF16( | 904 std::wstring text = base::SysNSStringToWide( |
| 905 [[field_ stringValue] substringWithRange:selection]); | 905 [[field_ stringValue] substringWithRange:selection]); |
| 906 | 906 |
| 907 GURL url; | 907 GURL url; |
| 908 bool write_url = false; | 908 bool write_url = false; |
| 909 model_->AdjustTextForCopy(selection.location, IsSelectAll(), &text, &url, | 909 model_->AdjustTextForCopy(selection.location, IsSelectAll(), &text, &url, |
| 910 &write_url); | 910 &write_url); |
| 911 | 911 |
| 912 NSString* nstext = base::SysUTF16ToNSString(text); | 912 NSString* nstext = base::SysWideToNSString(text); |
| 913 [pb declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; | 913 [pb declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; |
| 914 [pb setString:nstext forType:NSStringPboardType]; | 914 [pb setString:nstext forType:NSStringPboardType]; |
| 915 | 915 |
| 916 if (write_url) { | 916 if (write_url) { |
| 917 [pb declareURLPasteboardWithAdditionalTypes:[NSArray array] owner:nil]; | 917 [pb declareURLPasteboardWithAdditionalTypes:[NSArray array] owner:nil]; |
| 918 [pb setDataForURL:base::SysUTF8ToNSString(url.spec()) title:nstext]; | 918 [pb setDataForURL:base::SysUTF8ToNSString(url.spec()) title:nstext]; |
| 919 } | 919 } |
| 920 } | 920 } |
| 921 | 921 |
| 922 void AutocompleteEditViewMac::OnPaste() { | 922 void AutocompleteEditViewMac::OnPaste() { |
| 923 // This code currently expects |field_| to be focussed. | 923 // This code currently expects |field_| to be focussed. |
| 924 DCHECK([field_ currentEditor]); | 924 DCHECK([field_ currentEditor]); |
| 925 | 925 |
| 926 string16 text = GetClipboardText(g_browser_process->clipboard()); | 926 std::wstring text = GetClipboardText(g_browser_process->clipboard()); |
| 927 if (text.empty()) { | 927 if (text.empty()) { |
| 928 return; | 928 return; |
| 929 } | 929 } |
| 930 NSString* s = base::SysUTF16ToNSString(text); | 930 NSString* s = base::SysWideToNSString(text); |
| 931 | 931 |
| 932 // -shouldChangeTextInRange:* and -didChangeText are documented in | 932 // -shouldChangeTextInRange:* and -didChangeText are documented in |
| 933 // NSTextView as things you need to do if you write additional | 933 // NSTextView as things you need to do if you write additional |
| 934 // user-initiated editing functions. They cause the appropriate | 934 // user-initiated editing functions. They cause the appropriate |
| 935 // delegate methods to be called. | 935 // delegate methods to be called. |
| 936 // TODO(shess): It would be nice to separate the Cocoa-specific code | 936 // TODO(shess): It would be nice to separate the Cocoa-specific code |
| 937 // from the Chrome-specific code. | 937 // from the Chrome-specific code. |
| 938 NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]); | 938 NSTextView* editor = static_cast<NSTextView*>([field_ currentEditor]); |
| 939 const NSRange selectedRange = GetSelectedRange(); | 939 const NSRange selectedRange = GetSelectedRange(); |
| 940 if ([editor shouldChangeTextInRange:selectedRange replacementString:s]) { | 940 if ([editor shouldChangeTextInRange:selectedRange replacementString:s]) { |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1041 // that it's already focused. makeFirstResponder: will select all, so only | 1041 // that it's already focused. makeFirstResponder: will select all, so only |
| 1042 // call it if this behavior is desired. | 1042 // call it if this behavior is desired. |
| 1043 if (select_all || ![field_ currentEditor]) | 1043 if (select_all || ![field_ currentEditor]) |
| 1044 [[field_ window] makeFirstResponder:field_]; | 1044 [[field_ window] makeFirstResponder:field_]; |
| 1045 DCHECK_EQ([field_ currentEditor], [[field_ window] firstResponder]); | 1045 DCHECK_EQ([field_ currentEditor], [[field_ window] firstResponder]); |
| 1046 } | 1046 } |
| 1047 } | 1047 } |
| 1048 | 1048 |
| 1049 // TODO(shess): Copied from autocomplete_edit_view_win.cc. Could this | 1049 // TODO(shess): Copied from autocomplete_edit_view_win.cc. Could this |
| 1050 // be pushed into the model? | 1050 // be pushed into the model? |
| 1051 string16 AutocompleteEditViewMac::GetClipboardText( | 1051 std::wstring AutocompleteEditViewMac::GetClipboardText( |
| 1052 ui::Clipboard* clipboard) { | 1052 ui::Clipboard* clipboard) { |
| 1053 // autocomplete_edit_view_win.cc assumes this can never happen, we | 1053 // autocomplete_edit_view_win.cc assumes this can never happen, we |
| 1054 // will too. | 1054 // will too. |
| 1055 DCHECK(clipboard); | 1055 DCHECK(clipboard); |
| 1056 | 1056 |
| 1057 if (clipboard->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), | 1057 if (clipboard->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), |
| 1058 ui::Clipboard::BUFFER_STANDARD)) { | 1058 ui::Clipboard::BUFFER_STANDARD)) { |
| 1059 string16 text16; | 1059 string16 text16; |
| 1060 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &text16); | 1060 clipboard->ReadText(ui::Clipboard::BUFFER_STANDARD, &text16); |
| 1061 | 1061 |
| 1062 // Note: Unlike in the find popup and textfield view, here we completely | 1062 // Note: Unlike in the find popup and textfield view, here we completely |
| 1063 // remove whitespace strings containing newlines. We assume users are | 1063 // remove whitespace strings containing newlines. We assume users are |
| 1064 // most likely pasting in URLs that may have been split into multiple | 1064 // most likely pasting in URLs that may have been split into multiple |
| 1065 // lines in terminals, email programs, etc., and so linebreaks indicate | 1065 // lines in terminals, email programs, etc., and so linebreaks indicate |
| 1066 // completely bogus whitespace that would just cause the input to be | 1066 // completely bogus whitespace that would just cause the input to be |
| 1067 // invalid. | 1067 // invalid. |
| 1068 return CollapseWhitespace(text16, true); | 1068 return CollapseWhitespace(UTF16ToWide(text16), true); |
| 1069 } | 1069 } |
| 1070 | 1070 |
| 1071 // Try bookmark format. | 1071 // Try bookmark format. |
| 1072 // | 1072 // |
| 1073 // It is tempting to try bookmark format first, but the URL we get out of a | 1073 // It is tempting to try bookmark format first, but the URL we get out of a |
| 1074 // bookmark has been cannonicalized via GURL. This means if a user copies | 1074 // bookmark has been cannonicalized via GURL. This means if a user copies |
| 1075 // and pastes from the URL bar to itself, the text will get fixed up and | 1075 // and pastes from the URL bar to itself, the text will get fixed up and |
| 1076 // cannonicalized, which is not what the user expects. By pasting in this | 1076 // cannonicalized, which is not what the user expects. By pasting in this |
| 1077 // order, we are sure to paste what the user copied. | 1077 // order, we are sure to paste what the user copied. |
| 1078 if (clipboard->IsFormatAvailable(ui::Clipboard::GetUrlWFormatType(), | 1078 if (clipboard->IsFormatAvailable(ui::Clipboard::GetUrlWFormatType(), |
| 1079 ui::Clipboard::BUFFER_STANDARD)) { | 1079 ui::Clipboard::BUFFER_STANDARD)) { |
| 1080 std::string url_str; | 1080 std::string url_str; |
| 1081 clipboard->ReadBookmark(NULL, &url_str); | 1081 clipboard->ReadBookmark(NULL, &url_str); |
| 1082 // pass resulting url string through GURL to normalize | 1082 // pass resulting url string through GURL to normalize |
| 1083 GURL url(url_str); | 1083 GURL url(url_str); |
| 1084 if (url.is_valid()) { | 1084 if (url.is_valid()) { |
| 1085 return UTF8ToUTF16(url.spec()); | 1085 return UTF8ToWide(url.spec()); |
| 1086 } | 1086 } |
| 1087 } | 1087 } |
| 1088 | 1088 |
| 1089 return string16(); | 1089 return std::wstring(); |
| 1090 } | 1090 } |
| 1091 | 1091 |
| 1092 // static | 1092 // static |
| 1093 NSFont* AutocompleteEditViewMac::GetFieldFont() { | 1093 NSFont* AutocompleteEditViewMac::GetFieldFont() { |
| 1094 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 1094 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 1095 return rb.GetFont(ResourceBundle::BaseFont).GetNativeFont(); | 1095 return rb.GetFont(ResourceBundle::BaseFont).GetNativeFont(); |
| 1096 } | 1096 } |
| OLD | NEW |