| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "views/controls/textfield/native_textfield_views.h" | 5 #include "views/controls/textfield/native_textfield_views.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "grit/app_strings.h" | 13 #include "grit/app_strings.h" |
| 14 #include "ui/base/clipboard/clipboard.h" | 14 #include "ui/base/clipboard/clipboard.h" |
| 15 #include "ui/base/range/range.h" | 15 #include "ui/base/range/range.h" |
| 16 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
| 17 #include "ui/gfx/canvas_skia.h" | 17 #include "ui/gfx/canvas_skia.h" |
| 18 #include "ui/gfx/insets.h" | 18 #include "ui/gfx/insets.h" |
| 19 #include "views/background.h" | 19 #include "views/background.h" |
| 20 #include "views/border.h" | 20 #include "views/border.h" |
| 21 #include "views/controls/menu/menu_2.h" | 21 #include "views/controls/menu/menu_2.h" |
| 22 #include "views/controls/textfield/textfield.h" | 22 #include "views/controls/textfield/textfield.h" |
| 23 #include "views/controls/textfield/textfield_controller.h" | 23 #include "views/controls/textfield/textfield_controller.h" |
| 24 #include "views/controls/textfield/textfield_views_model.h" | 24 #include "views/controls/textfield/textfield_views_model.h" |
| 25 #include "views/events/event.h" | 25 #include "views/events/event.h" |
| 26 #include "views/ime/input_method.h" |
| 26 #include "views/metrics.h" | 27 #include "views/metrics.h" |
| 27 #include "views/views_delegate.h" | 28 #include "views/views_delegate.h" |
| 28 | 29 |
| 29 #if defined(OS_LINUX) | 30 #if defined(OS_LINUX) |
| 30 #include "ui/gfx/gtk_util.h" | 31 #include "ui/gfx/gtk_util.h" |
| 31 #endif | 32 #endif |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 // A global flag to switch the Textfield wrapper to TextfieldViews. | 36 // A global flag to switch the Textfield wrapper to TextfieldViews. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 54 const char kEnableViewsBasedTextfieldSwitch[] = "enable-textfield-views"; | 55 const char kEnableViewsBasedTextfieldSwitch[] = "enable-textfield-views"; |
| 55 } // namespace | 56 } // namespace |
| 56 | 57 |
| 57 namespace views { | 58 namespace views { |
| 58 | 59 |
| 59 const char NativeTextfieldViews::kViewClassName[] = | 60 const char NativeTextfieldViews::kViewClassName[] = |
| 60 "views/NativeTextfieldViews"; | 61 "views/NativeTextfieldViews"; |
| 61 | 62 |
| 62 NativeTextfieldViews::NativeTextfieldViews(Textfield* parent) | 63 NativeTextfieldViews::NativeTextfieldViews(Textfield* parent) |
| 63 : textfield_(parent), | 64 : textfield_(parent), |
| 64 model_(new TextfieldViewsModel()), | 65 ALLOW_THIS_IN_INITIALIZER_LIST(model_(new TextfieldViewsModel(this))), |
| 65 text_border_(new TextfieldBorder()), | 66 text_border_(new TextfieldBorder()), |
| 66 text_offset_(0), | 67 text_offset_(0), |
| 67 insert_(true), | 68 insert_(true), |
| 68 is_cursor_visible_(false), | 69 is_cursor_visible_(false), |
| 70 skip_input_method_cancel_composition_(false), |
| 69 ALLOW_THIS_IN_INITIALIZER_LIST(cursor_timer_(this)), | 71 ALLOW_THIS_IN_INITIALIZER_LIST(cursor_timer_(this)), |
| 70 last_mouse_press_time_(base::Time::FromInternalValue(0)), | 72 last_mouse_press_time_(base::Time::FromInternalValue(0)), |
| 71 click_state_(NONE) { | 73 click_state_(NONE) { |
| 72 set_border(text_border_); | 74 set_border(text_border_); |
| 73 | 75 |
| 74 // Multiline is not supported. | 76 // Multiline is not supported. |
| 75 DCHECK_NE(parent->style(), Textfield::STYLE_MULTILINE); | 77 DCHECK_NE(parent->style(), Textfield::STYLE_MULTILINE); |
| 76 // Lowercase is not supported. | 78 // Lowercase is not supported. |
| 77 DCHECK_NE(parent->style(), Textfield::STYLE_LOWERCASE); | 79 DCHECK_NE(parent->style(), Textfield::STYLE_LOWERCASE); |
| 78 | 80 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 } | 159 } |
| 158 | 160 |
| 159 ///////////////////////////////////////////////////////////////// | 161 ///////////////////////////////////////////////////////////////// |
| 160 // NativeTextfieldViews, NativeTextifieldWrapper overrides: | 162 // NativeTextfieldViews, NativeTextifieldWrapper overrides: |
| 161 | 163 |
| 162 string16 NativeTextfieldViews::GetText() const { | 164 string16 NativeTextfieldViews::GetText() const { |
| 163 return model_->text(); | 165 return model_->text(); |
| 164 } | 166 } |
| 165 | 167 |
| 166 void NativeTextfieldViews::UpdateText() { | 168 void NativeTextfieldViews::UpdateText() { |
| 167 bool changed = model_->SetText(textfield_->text()); | 169 model_->SetText(textfield_->text()); |
| 168 UpdateCursorBoundsAndTextOffset(); | 170 UpdateCursorBoundsAndTextOffset(); |
| 169 SchedulePaint(); | 171 SchedulePaint(); |
| 170 if (changed) { | |
| 171 TextfieldController* controller = textfield_->GetController(); | |
| 172 if (controller) | |
| 173 controller->ContentsChanged(textfield_, GetText()); | |
| 174 } | |
| 175 } | 172 } |
| 176 | 173 |
| 177 void NativeTextfieldViews::AppendText(const string16& text) { | 174 void NativeTextfieldViews::AppendText(const string16& text) { |
| 178 if (text.empty()) | 175 if (text.empty()) |
| 179 return; | 176 return; |
| 180 model_->Append(text); | 177 model_->Append(text); |
| 181 UpdateCursorBoundsAndTextOffset(); | 178 UpdateCursorBoundsAndTextOffset(); |
| 182 SchedulePaint(); | 179 SchedulePaint(); |
| 183 | |
| 184 TextfieldController* controller = textfield_->GetController(); | |
| 185 if (controller) | |
| 186 controller->ContentsChanged(textfield_, GetText()); | |
| 187 } | 180 } |
| 188 | 181 |
| 189 string16 NativeTextfieldViews::GetSelectedText() const { | 182 string16 NativeTextfieldViews::GetSelectedText() const { |
| 190 return model_->GetSelectedText(); | 183 return model_->GetSelectedText(); |
| 191 } | 184 } |
| 192 | 185 |
| 193 void NativeTextfieldViews::SelectAll() { | 186 void NativeTextfieldViews::SelectAll() { |
| 194 model_->SelectAll(); | 187 model_->SelectAll(); |
| 195 SchedulePaint(); | 188 SchedulePaint(); |
| 196 } | 189 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 217 | 210 |
| 218 void NativeTextfieldViews::UpdateBackgroundColor() { | 211 void NativeTextfieldViews::UpdateBackgroundColor() { |
| 219 // TODO(oshima): Background has to match the border's shape. | 212 // TODO(oshima): Background has to match the border's shape. |
| 220 set_background( | 213 set_background( |
| 221 Background::CreateSolidBackground(textfield_->background_color())); | 214 Background::CreateSolidBackground(textfield_->background_color())); |
| 222 SchedulePaint(); | 215 SchedulePaint(); |
| 223 } | 216 } |
| 224 | 217 |
| 225 void NativeTextfieldViews::UpdateReadOnly() { | 218 void NativeTextfieldViews::UpdateReadOnly() { |
| 226 SchedulePaint(); | 219 SchedulePaint(); |
| 220 OnTextInputTypeChanged(); |
| 227 } | 221 } |
| 228 | 222 |
| 229 void NativeTextfieldViews::UpdateFont() { | 223 void NativeTextfieldViews::UpdateFont() { |
| 230 UpdateCursorBoundsAndTextOffset(); | 224 UpdateCursorBoundsAndTextOffset(); |
| 231 } | 225 } |
| 232 | 226 |
| 233 void NativeTextfieldViews::UpdateIsPassword() { | 227 void NativeTextfieldViews::UpdateIsPassword() { |
| 234 model_->set_is_password(textfield_->IsPassword()); | 228 model_->set_is_password(textfield_->IsPassword()); |
| 235 UpdateCursorBoundsAndTextOffset(); | 229 UpdateCursorBoundsAndTextOffset(); |
| 236 SchedulePaint(); | 230 SchedulePaint(); |
| 231 OnTextInputTypeChanged(); |
| 237 } | 232 } |
| 238 | 233 |
| 239 void NativeTextfieldViews::UpdateEnabled() { | 234 void NativeTextfieldViews::UpdateEnabled() { |
| 240 SetEnabled(textfield_->IsEnabled()); | 235 SetEnabled(textfield_->IsEnabled()); |
| 241 SchedulePaint(); | 236 SchedulePaint(); |
| 237 OnTextInputTypeChanged(); |
| 242 } | 238 } |
| 243 | 239 |
| 244 gfx::Insets NativeTextfieldViews::CalculateInsets() { | 240 gfx::Insets NativeTextfieldViews::CalculateInsets() { |
| 245 return GetInsets(); | 241 return GetInsets(); |
| 246 } | 242 } |
| 247 | 243 |
| 248 void NativeTextfieldViews::UpdateHorizontalMargins() { | 244 void NativeTextfieldViews::UpdateHorizontalMargins() { |
| 249 int left, right; | 245 int left, right; |
| 250 if (!textfield_->GetHorizontalMargins(&left, &right)) | 246 if (!textfield_->GetHorizontalMargins(&left, &right)) |
| 251 return; | 247 return; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 272 View* NativeTextfieldViews::GetView() { | 268 View* NativeTextfieldViews::GetView() { |
| 273 return this; | 269 return this; |
| 274 } | 270 } |
| 275 | 271 |
| 276 gfx::NativeView NativeTextfieldViews::GetTestingHandle() const { | 272 gfx::NativeView NativeTextfieldViews::GetTestingHandle() const { |
| 277 NOTREACHED(); | 273 NOTREACHED(); |
| 278 return NULL; | 274 return NULL; |
| 279 } | 275 } |
| 280 | 276 |
| 281 bool NativeTextfieldViews::IsIMEComposing() const { | 277 bool NativeTextfieldViews::IsIMEComposing() const { |
| 282 return false; | 278 return model_->HasCompositionText(); |
| 283 } | 279 } |
| 284 | 280 |
| 285 void NativeTextfieldViews::GetSelectedRange(ui::Range* range) const { | 281 void NativeTextfieldViews::GetSelectedRange(ui::Range* range) const { |
| 286 model_->GetSelectedRange(range); | 282 model_->GetSelectedRange(range); |
| 287 } | 283 } |
| 288 | 284 |
| 289 void NativeTextfieldViews::SelectRange(const ui::Range& range) { | 285 void NativeTextfieldViews::SelectRange(const ui::Range& range) { |
| 290 model_->SelectRange(range); | 286 model_->SelectRange(range); |
| 291 UpdateCursorBoundsAndTextOffset(); | 287 UpdateCursorBoundsAndTextOffset(); |
| 292 SchedulePaint(); | 288 SchedulePaint(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 304 return handled || HandleKeyEvent(e); | 300 return handled || HandleKeyEvent(e); |
| 305 } | 301 } |
| 306 | 302 |
| 307 bool NativeTextfieldViews::HandleKeyReleased(const views::KeyEvent& e) { | 303 bool NativeTextfieldViews::HandleKeyReleased(const views::KeyEvent& e) { |
| 308 return true; | 304 return true; |
| 309 } | 305 } |
| 310 | 306 |
| 311 void NativeTextfieldViews::HandleFocus() { | 307 void NativeTextfieldViews::HandleFocus() { |
| 312 is_cursor_visible_ = true; | 308 is_cursor_visible_ = true; |
| 313 SchedulePaint(); | 309 SchedulePaint(); |
| 310 OnCaretBoundsChanged(); |
| 314 // Start blinking cursor. | 311 // Start blinking cursor. |
| 315 MessageLoop::current()->PostDelayedTask( | 312 MessageLoop::current()->PostDelayedTask( |
| 316 FROM_HERE, | 313 FROM_HERE, |
| 317 cursor_timer_.NewRunnableMethod(&NativeTextfieldViews::UpdateCursor), | 314 cursor_timer_.NewRunnableMethod(&NativeTextfieldViews::UpdateCursor), |
| 318 kCursorVisibleTimeMs); | 315 kCursorVisibleTimeMs); |
| 319 } | 316 } |
| 320 | 317 |
| 321 void NativeTextfieldViews::HandleBlur() { | 318 void NativeTextfieldViews::HandleBlur() { |
| 322 // Stop blinking cursor. | 319 // Stop blinking cursor. |
| 323 cursor_timer_.RevokeAll(); | 320 cursor_timer_.RevokeAll(); |
| 324 if (is_cursor_visible_) { | 321 if (is_cursor_visible_) { |
| 325 is_cursor_visible_ = false; | 322 is_cursor_visible_ = false; |
| 326 RepaintCursor(); | 323 RepaintCursor(); |
| 327 } | 324 } |
| 328 } | 325 } |
| 329 | 326 |
| 327 TextInputClient* NativeTextfieldViews::GetTextInputClient() { |
| 328 return textfield_->read_only() ? NULL : this; |
| 329 } |
| 330 |
| 330 ///////////////////////////////////////////////////////////////// | 331 ///////////////////////////////////////////////////////////////// |
| 331 // NativeTextfieldViews, ui::SimpleMenuModel::Delegate overrides: | 332 // NativeTextfieldViews, ui::SimpleMenuModel::Delegate overrides: |
| 332 | 333 |
| 333 bool NativeTextfieldViews::IsCommandIdChecked(int command_id) const { | 334 bool NativeTextfieldViews::IsCommandIdChecked(int command_id) const { |
| 334 return true; | 335 return true; |
| 335 } | 336 } |
| 336 | 337 |
| 337 bool NativeTextfieldViews::IsCommandIdEnabled(int command_id) const { | 338 bool NativeTextfieldViews::IsCommandIdEnabled(int command_id) const { |
| 338 bool editable = !textfield_->read_only(); | 339 bool editable = !textfield_->read_only(); |
| 339 string16 result; | 340 string16 result; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 void NativeTextfieldViews::SetEnableTextfieldViews(bool enabled) { | 410 void NativeTextfieldViews::SetEnableTextfieldViews(bool enabled) { |
| 410 textfield_view_enabled = enabled; | 411 textfield_view_enabled = enabled; |
| 411 } | 412 } |
| 412 | 413 |
| 413 void NativeTextfieldViews::OnBoundsChanged(const gfx::Rect& previous_bounds) { | 414 void NativeTextfieldViews::OnBoundsChanged(const gfx::Rect& previous_bounds) { |
| 414 UpdateCursorBoundsAndTextOffset(); | 415 UpdateCursorBoundsAndTextOffset(); |
| 415 } | 416 } |
| 416 | 417 |
| 417 | 418 |
| 418 /////////////////////////////////////////////////////////////////////////////// | 419 /////////////////////////////////////////////////////////////////////////////// |
| 419 // NativeTextfieldViews private: | 420 // NativeTextfieldViews, TextInputClient implementation, private: |
| 421 |
| 422 void NativeTextfieldViews::SetCompositionText( |
| 423 const ui::CompositionText& composition) { |
| 424 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) |
| 425 return; |
| 426 |
| 427 OnBeforeUserAction(); |
| 428 skip_input_method_cancel_composition_ = true; |
| 429 model_->SetCompositionText(composition); |
| 430 skip_input_method_cancel_composition_ = false; |
| 431 UpdateAfterChange(true, true); |
| 432 OnAfterUserAction(); |
| 433 } |
| 434 |
| 435 void NativeTextfieldViews::ConfirmCompositionText() { |
| 436 if (!model_->HasCompositionText()) |
| 437 return; |
| 438 |
| 439 OnBeforeUserAction(); |
| 440 skip_input_method_cancel_composition_ = true; |
| 441 model_->ConfirmCompositionText(); |
| 442 skip_input_method_cancel_composition_ = false; |
| 443 UpdateAfterChange(true, true); |
| 444 OnAfterUserAction(); |
| 445 } |
| 446 |
| 447 void NativeTextfieldViews::ClearCompositionText() { |
| 448 if (!model_->HasCompositionText()) |
| 449 return; |
| 450 |
| 451 OnBeforeUserAction(); |
| 452 skip_input_method_cancel_composition_ = true; |
| 453 model_->ClearCompositionText(); |
| 454 skip_input_method_cancel_composition_ = false; |
| 455 UpdateAfterChange(true, true); |
| 456 OnAfterUserAction(); |
| 457 } |
| 458 |
| 459 void NativeTextfieldViews::InsertText(const string16& text) { |
| 460 // TODO(suzhe): Filter invalid characters. |
| 461 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || text.empty()) |
| 462 return; |
| 463 |
| 464 OnBeforeUserAction(); |
| 465 skip_input_method_cancel_composition_ = true; |
| 466 if (insert_) |
| 467 model_->InsertText(text); |
| 468 else |
| 469 model_->ReplaceText(text); |
| 470 skip_input_method_cancel_composition_ = false; |
| 471 UpdateAfterChange(true, true); |
| 472 OnAfterUserAction(); |
| 473 } |
| 474 |
| 475 void NativeTextfieldViews::InsertChar(char16 ch, int flags) { |
| 476 if (GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE || |
| 477 !ShouldInsertChar(ch, flags)) { |
| 478 return; |
| 479 } |
| 480 |
| 481 OnBeforeUserAction(); |
| 482 skip_input_method_cancel_composition_ = true; |
| 483 if (insert_) |
| 484 model_->InsertChar(ch); |
| 485 else |
| 486 model_->ReplaceChar(ch); |
| 487 skip_input_method_cancel_composition_ = false; |
| 488 UpdateAfterChange(true, true); |
| 489 OnAfterUserAction(); |
| 490 } |
| 491 |
| 492 ui::TextInputType NativeTextfieldViews::GetTextInputType() { |
| 493 if (textfield_->read_only() || !textfield_->IsEnabled()) |
| 494 return ui::TEXT_INPUT_TYPE_NONE; |
| 495 else if (textfield_->IsPassword()) |
| 496 return ui::TEXT_INPUT_TYPE_PASSWORD; |
| 497 return ui::TEXT_INPUT_TYPE_TEXT; |
| 498 } |
| 499 |
| 500 gfx::Rect NativeTextfieldViews::GetCaretBounds() { |
| 501 return cursor_bounds_; |
| 502 } |
| 503 |
| 504 bool NativeTextfieldViews::HasCompositionText() { |
| 505 return model_->HasCompositionText(); |
| 506 } |
| 507 |
| 508 bool NativeTextfieldViews::GetTextRange(ui::Range* range) { |
| 509 // We don't allow the input method to retrieve or delete content from a |
| 510 // password box. |
| 511 if (GetTextInputType() != ui::TEXT_INPUT_TYPE_TEXT) |
| 512 return false; |
| 513 |
| 514 model_->GetTextRange(range); |
| 515 return true; |
| 516 } |
| 517 |
| 518 bool NativeTextfieldViews::GetCompositionTextRange(ui::Range* range) { |
| 519 if (GetTextInputType() != ui::TEXT_INPUT_TYPE_TEXT) |
| 520 return false; |
| 521 |
| 522 model_->GetCompositionTextRange(range); |
| 523 return true; |
| 524 } |
| 525 |
| 526 bool NativeTextfieldViews::GetSelectionRange(ui::Range* range) { |
| 527 if (GetTextInputType() != ui::TEXT_INPUT_TYPE_TEXT) |
| 528 return false; |
| 529 |
| 530 model_->GetSelectedRange(range); |
| 531 return true; |
| 532 } |
| 533 |
| 534 bool NativeTextfieldViews::SetSelectionRange(const ui::Range& range) { |
| 535 if (GetTextInputType() != ui::TEXT_INPUT_TYPE_TEXT || !range.IsValid()) |
| 536 return false; |
| 537 |
| 538 OnBeforeUserAction(); |
| 539 SelectRange(range); |
| 540 OnAfterUserAction(); |
| 541 return true; |
| 542 } |
| 543 |
| 544 bool NativeTextfieldViews::DeleteRange(const ui::Range& range) { |
| 545 if (GetTextInputType() != ui::TEXT_INPUT_TYPE_TEXT || range.is_empty()) |
| 546 return false; |
| 547 |
| 548 OnBeforeUserAction(); |
| 549 model_->SelectRange(range); |
| 550 if (model_->HasSelection()) { |
| 551 model_->DeleteSelection(); |
| 552 UpdateAfterChange(true, true); |
| 553 } |
| 554 OnAfterUserAction(); |
| 555 return true; |
| 556 } |
| 557 |
| 558 bool NativeTextfieldViews::GetTextFromRange( |
| 559 const ui::Range& range, |
| 560 const base::Callback<void(const string16&)>& callback) { |
| 561 if (GetTextInputType() != ui::TEXT_INPUT_TYPE_TEXT || range.is_empty()) |
| 562 return false; |
| 563 |
| 564 callback.Run(model_->GetTextFromRange(range)); |
| 565 return true; |
| 566 } |
| 567 |
| 568 void NativeTextfieldViews::OnInputMethodChanged() { |
| 569 NOTIMPLEMENTED(); |
| 570 } |
| 571 |
| 572 bool NativeTextfieldViews::ChangeTextDirectionAndLayoutAlignment( |
| 573 base::i18n::TextDirection direction) { |
| 574 NOTIMPLEMENTED(); |
| 575 return false; |
| 576 } |
| 577 |
| 578 void NativeTextfieldViews::OnCompositionTextConfirmedOrCleared() { |
| 579 if (skip_input_method_cancel_composition_ || !textfield_->HasFocus()) |
| 580 return; |
| 581 |
| 582 InputMethod* input_method = textfield_->GetInputMethod(); |
| 583 if (input_method) |
| 584 input_method->CancelComposition(textfield_); |
| 585 } |
| 420 | 586 |
| 421 const gfx::Font& NativeTextfieldViews::GetFont() const { | 587 const gfx::Font& NativeTextfieldViews::GetFont() const { |
| 422 return textfield_->font(); | 588 return textfield_->font(); |
| 423 } | 589 } |
| 424 | 590 |
| 425 SkColor NativeTextfieldViews::GetTextColor() const { | 591 SkColor NativeTextfieldViews::GetTextColor() const { |
| 426 return textfield_->text_color(); | 592 return textfield_->text_color(); |
| 427 } | 593 } |
| 428 | 594 |
| 429 void NativeTextfieldViews::UpdateCursor() { | 595 void NativeTextfieldViews::UpdateCursor() { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 text_offset_ = -cursor_bounds_.x(); | 635 text_offset_ = -cursor_bounds_.x(); |
| 470 } else if(full_width > width && text_offset_ + full_width < width) { | 636 } else if(full_width > width && text_offset_ + full_width < width) { |
| 471 // when the cursor moves within the textfield with the text | 637 // when the cursor moves within the textfield with the text |
| 472 // longer than the field. | 638 // longer than the field. |
| 473 text_offset_ = width - full_width; | 639 text_offset_ = width - full_width; |
| 474 } else { | 640 } else { |
| 475 // move cursor freely. | 641 // move cursor freely. |
| 476 } | 642 } |
| 477 // shift cursor bounds to fit insets. | 643 // shift cursor bounds to fit insets. |
| 478 cursor_bounds_.set_x(cursor_bounds_.x() + text_offset_ + insets.left()); | 644 cursor_bounds_.set_x(cursor_bounds_.x() + text_offset_ + insets.left()); |
| 645 |
| 646 OnCaretBoundsChanged(); |
| 479 } | 647 } |
| 480 | 648 |
| 481 void NativeTextfieldViews::PaintTextAndCursor(gfx::Canvas* canvas) { | 649 void NativeTextfieldViews::PaintTextAndCursor(gfx::Canvas* canvas) { |
| 482 gfx::Insets insets = GetInsets(); | 650 gfx::Insets insets = GetInsets(); |
| 483 | 651 |
| 484 canvas->Save(); | 652 canvas->Save(); |
| 485 canvas->ClipRectInt(insets.left(), insets.top(), | 653 canvas->ClipRectInt(insets.left(), insets.top(), |
| 486 width() - insets.width(), height() - insets.height()); | 654 width() - insets.width(), height() - insets.height()); |
| 487 | 655 |
| 488 // TODO(oshima): bidi support | 656 // TODO(oshima): bidi support |
| 489 // TODO(varunjain): re-implement this so only that dirty text is painted. | 657 // TODO(varunjain): re-implement this so only that dirty text is painted. |
| 490 TextfieldViewsModel::TextFragments fragments; | 658 TextfieldViewsModel::TextFragments fragments; |
| 491 model_->GetFragments(&fragments); | 659 model_->GetFragments(&fragments); |
| 492 int x_offset = text_offset_ + insets.left(); | 660 int x_offset = text_offset_ + insets.left(); |
| 493 int y = insets.top(); | 661 int y = insets.top(); |
| 494 int text_height = height() - insets.height(); | 662 int text_height = height() - insets.height(); |
| 495 SkColor selection_color = | 663 SkColor selection_color = |
| 496 textfield_->HasFocus() ? | 664 textfield_->HasFocus() ? |
| 497 kFocusedSelectionColor : kUnfocusedSelectionColor; | 665 kFocusedSelectionColor : kUnfocusedSelectionColor; |
| 498 SkColor text_color = | 666 SkColor text_color = |
| 499 textfield_->read_only() ? kReadonlyTextColor : GetTextColor(); | 667 textfield_->read_only() ? kReadonlyTextColor : GetTextColor(); |
| 500 | 668 |
| 501 for (TextfieldViewsModel::TextFragments::const_iterator iter = | 669 for (TextfieldViewsModel::TextFragments::const_iterator iter = |
| 502 fragments.begin(); | 670 fragments.begin(); |
| 503 iter != fragments.end(); | 671 iter != fragments.end(); |
| 504 iter++) { | 672 iter++) { |
| 505 string16 text = model_->GetVisibleText((*iter).begin, (*iter).end); | 673 string16 text = model_->GetVisibleText(iter->start, iter->end); |
| 674 |
| 675 gfx::Font font = GetFont(); |
| 676 if (iter->underline) |
| 677 font = font.DeriveFont(0, font.GetStyle() | gfx::Font::UNDERLINED); |
| 678 |
| 506 // TODO(oshima): This does not give the accurate position due to | 679 // TODO(oshima): This does not give the accurate position due to |
| 507 // kerning. Figure out how webkit does this with skia. | 680 // kerning. Figure out how webkit does this with skia. |
| 508 int width = GetFont().GetStringWidth(text); | 681 int width = font.GetStringWidth(text); |
| 509 | 682 |
| 510 if ((*iter).selected) { | 683 if (iter->selected) { |
| 511 canvas->FillRectInt(selection_color, x_offset, y, width, text_height); | 684 canvas->FillRectInt(selection_color, x_offset, y, width, text_height); |
| 512 canvas->DrawStringInt(text, GetFont(), kSelectedTextColor, | 685 canvas->DrawStringInt(text, font, kSelectedTextColor, |
| 513 x_offset, y, width, text_height); | 686 x_offset, y, width, text_height); |
| 514 } else { | 687 } else { |
| 515 canvas->DrawStringInt(text, GetFont(), text_color, | 688 canvas->DrawStringInt(text, font, text_color, |
| 516 x_offset, y, width, text_height); | 689 x_offset, y, width, text_height); |
| 517 } | 690 } |
| 518 x_offset += width; | 691 x_offset += width; |
| 519 } | 692 } |
| 520 canvas->Restore(); | 693 canvas->Restore(); |
| 521 | 694 |
| 522 if (textfield_->IsEnabled() && is_cursor_visible_ && | 695 if (textfield_->IsEnabled() && is_cursor_visible_ && |
| 523 !model_->HasSelection()) { | 696 !model_->HasSelection()) { |
| 524 // Paint Cursor. Replace cursor is drawn as rectangle for now. | 697 // Paint Cursor. Replace cursor is drawn as rectangle for now. |
| 525 canvas->DrawRectInt(kCursorColor, | 698 canvas->DrawRectInt(kCursorColor, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 571 case ui::VKEY_LEFT: | 744 case ui::VKEY_LEFT: |
| 572 control ? model_->MoveCursorToPreviousWord(selection) | 745 control ? model_->MoveCursorToPreviousWord(selection) |
| 573 : model_->MoveCursorLeft(selection); | 746 : model_->MoveCursorLeft(selection); |
| 574 cursor_changed = true; | 747 cursor_changed = true; |
| 575 break; | 748 break; |
| 576 case ui::VKEY_END: | 749 case ui::VKEY_END: |
| 577 model_->MoveCursorToEnd(selection); | 750 model_->MoveCursorToEnd(selection); |
| 578 cursor_changed = true; | 751 cursor_changed = true; |
| 579 break; | 752 break; |
| 580 case ui::VKEY_HOME: | 753 case ui::VKEY_HOME: |
| 581 model_->MoveCursorToStart(selection); | 754 model_->MoveCursorToHome(selection); |
| 582 cursor_changed = true; | 755 cursor_changed = true; |
| 583 break; | 756 break; |
| 584 case ui::VKEY_BACK: | 757 case ui::VKEY_BACK: |
| 585 if (!editable) | 758 if (!editable) |
| 586 break; | 759 break; |
| 587 if (!model_->HasSelection()) { | 760 if (!model_->HasSelection()) { |
| 588 if (selection && control) { | 761 if (selection && control) { |
| 589 // If both shift and control are pressed, then erase upto the | 762 // If both shift and control are pressed, then erase upto the |
| 590 // beginning of the buffer in ChromeOS. In windows, do nothing. | 763 // beginning of the buffer in ChromeOS. In windows, do nothing. |
| 591 #if defined(OS_WIN) | 764 #if defined(OS_WIN) |
| 592 break; | 765 break; |
| 593 #else | 766 #else |
| 594 model_->MoveCursorToStart(true); | 767 model_->MoveCursorToHome(true); |
| 595 #endif | 768 #endif |
| 596 } else if (control) { | 769 } else if (control) { |
| 597 // If only control is pressed, then erase the previous word. | 770 // If only control is pressed, then erase the previous word. |
| 598 model_->MoveCursorToPreviousWord(true); | 771 model_->MoveCursorToPreviousWord(true); |
| 599 } | 772 } |
| 600 } | 773 } |
| 601 text_changed = model_->Backspace(); | 774 text_changed = model_->Backspace(); |
| 602 cursor_changed = true; | 775 cursor_changed = true; |
| 603 break; | 776 break; |
| 604 case ui::VKEY_DELETE: | 777 case ui::VKEY_DELETE: |
| (...skipping 15 matching lines...) Expand all Loading... |
| 620 } | 793 } |
| 621 cursor_changed = text_changed = model_->Delete(); | 794 cursor_changed = text_changed = model_->Delete(); |
| 622 break; | 795 break; |
| 623 case ui::VKEY_INSERT: | 796 case ui::VKEY_INSERT: |
| 624 insert_ = !insert_; | 797 insert_ = !insert_; |
| 625 cursor_changed = true; | 798 cursor_changed = true; |
| 626 break; | 799 break; |
| 627 default: | 800 default: |
| 628 break; | 801 break; |
| 629 } | 802 } |
| 630 char16 ch = key_event.GetCharacter(); | 803 |
| 631 if (editable && ShouldInsertChar(ch, key_event.flags())) { | 804 // Only handle text input by ourselves if there is no input method. |
| 632 if (insert_) | 805 if (!textfield_->GetInputMethod()) { |
| 633 model_->Insert(ch); | 806 char16 ch = key_event.GetCharacter(); |
| 634 else | 807 if (editable && ShouldInsertChar(ch, key_event.flags())) { |
| 635 model_->Replace(ch); | 808 if (insert_) |
| 636 text_changed = true; | 809 model_->InsertChar(ch); |
| 810 else |
| 811 model_->ReplaceChar(ch); |
| 812 text_changed = true; |
| 813 } |
| 637 } | 814 } |
| 638 | 815 |
| 639 UpdateAfterChange(text_changed, cursor_changed); | 816 UpdateAfterChange(text_changed, cursor_changed); |
| 640 OnAfterUserAction(); | 817 OnAfterUserAction(); |
| 641 return (text_changed || cursor_changed); | 818 return (text_changed || cursor_changed); |
| 642 } | 819 } |
| 643 return false; | 820 return false; |
| 644 } | 821 } |
| 645 | 822 |
| 646 size_t NativeTextfieldViews::FindCursorPosition(const gfx::Point& point) const { | 823 size_t NativeTextfieldViews::FindCursorPosition(const gfx::Point& point) const { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 711 | 888 |
| 712 void NativeTextfieldViews::SetCursorForMouseClick(const views::MouseEvent& e) { | 889 void NativeTextfieldViews::SetCursorForMouseClick(const views::MouseEvent& e) { |
| 713 size_t pos = FindCursorPosition(e.location()); | 890 size_t pos = FindCursorPosition(e.location()); |
| 714 if (model_->MoveCursorTo(pos, false)) { | 891 if (model_->MoveCursorTo(pos, false)) { |
| 715 UpdateCursorBoundsAndTextOffset(); | 892 UpdateCursorBoundsAndTextOffset(); |
| 716 } | 893 } |
| 717 } | 894 } |
| 718 | 895 |
| 719 void NativeTextfieldViews::PropagateTextChange() { | 896 void NativeTextfieldViews::PropagateTextChange() { |
| 720 textfield_->SyncText(); | 897 textfield_->SyncText(); |
| 721 TextfieldController* controller = textfield_->GetController(); | |
| 722 if (controller) | |
| 723 controller->ContentsChanged(textfield_, GetText()); | |
| 724 } | 898 } |
| 725 | 899 |
| 726 void NativeTextfieldViews::UpdateAfterChange(bool text_changed, | 900 void NativeTextfieldViews::UpdateAfterChange(bool text_changed, |
| 727 bool cursor_changed) { | 901 bool cursor_changed) { |
| 728 if (text_changed) | 902 if (text_changed) |
| 729 PropagateTextChange(); | 903 PropagateTextChange(); |
| 730 if (cursor_changed) { | 904 if (cursor_changed) { |
| 731 is_cursor_visible_ = true; | 905 is_cursor_visible_ = true; |
| 732 RepaintCursor(); | 906 RepaintCursor(); |
| 733 } | 907 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 744 context_menu_contents_->AddItemWithStringId(IDS_APP_CUT, IDS_APP_CUT); | 918 context_menu_contents_->AddItemWithStringId(IDS_APP_CUT, IDS_APP_CUT); |
| 745 context_menu_contents_->AddItemWithStringId(IDS_APP_COPY, IDS_APP_COPY); | 919 context_menu_contents_->AddItemWithStringId(IDS_APP_COPY, IDS_APP_COPY); |
| 746 context_menu_contents_->AddItemWithStringId(IDS_APP_PASTE, IDS_APP_PASTE); | 920 context_menu_contents_->AddItemWithStringId(IDS_APP_PASTE, IDS_APP_PASTE); |
| 747 context_menu_contents_->AddItemWithStringId(IDS_APP_DELETE, IDS_APP_DELETE); | 921 context_menu_contents_->AddItemWithStringId(IDS_APP_DELETE, IDS_APP_DELETE); |
| 748 context_menu_contents_->AddSeparator(); | 922 context_menu_contents_->AddSeparator(); |
| 749 context_menu_contents_->AddItemWithStringId(IDS_APP_SELECT_ALL, | 923 context_menu_contents_->AddItemWithStringId(IDS_APP_SELECT_ALL, |
| 750 IDS_APP_SELECT_ALL); | 924 IDS_APP_SELECT_ALL); |
| 751 context_menu_menu_.reset(new Menu2(context_menu_contents_.get())); | 925 context_menu_menu_.reset(new Menu2(context_menu_contents_.get())); |
| 752 } | 926 } |
| 753 | 927 |
| 928 void NativeTextfieldViews::OnTextInputTypeChanged() { |
| 929 if (!textfield_->HasFocus()) |
| 930 return; |
| 931 |
| 932 InputMethod* input_method = textfield_->GetInputMethod(); |
| 933 if (input_method) |
| 934 input_method->OnTextInputTypeChanged(textfield_); |
| 935 } |
| 936 |
| 937 void NativeTextfieldViews::OnCaretBoundsChanged() { |
| 938 if (!textfield_->HasFocus()) |
| 939 return; |
| 940 |
| 941 InputMethod* input_method = textfield_->GetInputMethod(); |
| 942 if (input_method) |
| 943 input_method->OnCaretBoundsChanged(textfield_); |
| 944 } |
| 945 |
| 754 void NativeTextfieldViews::OnBeforeUserAction() { | 946 void NativeTextfieldViews::OnBeforeUserAction() { |
| 755 TextfieldController* controller = textfield_->GetController(); | 947 TextfieldController* controller = textfield_->GetController(); |
| 756 if (controller) | 948 if (controller) |
| 757 controller->OnBeforeUserAction(textfield_); | 949 controller->OnBeforeUserAction(textfield_); |
| 758 } | 950 } |
| 759 | 951 |
| 760 void NativeTextfieldViews::OnAfterUserAction() { | 952 void NativeTextfieldViews::OnAfterUserAction() { |
| 761 TextfieldController* controller = textfield_->GetController(); | 953 TextfieldController* controller = textfield_->GetController(); |
| 762 if (controller) | 954 if (controller) |
| 763 controller->OnAfterUserAction(textfield_); | 955 controller->OnAfterUserAction(textfield_); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 822 } | 1014 } |
| 823 | 1015 |
| 824 void NativeTextfieldViews::TextfieldBorder::SetInsets(int top, | 1016 void NativeTextfieldViews::TextfieldBorder::SetInsets(int top, |
| 825 int left, | 1017 int left, |
| 826 int bottom, | 1018 int bottom, |
| 827 int right) { | 1019 int right) { |
| 828 insets_.Set(top, left, bottom, right); | 1020 insets_.Set(top, left, bottom, right); |
| 829 } | 1021 } |
| 830 | 1022 |
| 831 } // namespace views | 1023 } // namespace views |
| OLD | NEW |