Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: views/controls/textfield/native_textfield_views.cc

Issue 6675005: Integrate the new input method API for Views into Chromium. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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
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
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
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 // This code can't be compiled because of a bug in base::Callback.
562 #if 0
oshima 2011/03/24 22:58:47 Has this been fixed?
James Su 2011/03/25 05:57:21 Yes it's been fixed. I need to try it though.
563 if (GetTextInputType() != ui::TEXT_INPUT_TYPE_TEXT || range.is_empty())
564 return false;
565
566 callback.Run(model_->GetTextFromRange(range));
567 return true;
568 #endif
569 return false;
570 }
571
572 void NativeTextfieldViews::OnInputMethodChanged() {
573 NOTIMPLEMENTED();
574 }
575
576 bool NativeTextfieldViews::ChangeTextDirectionAndLayoutAlignment(
577 base::i18n::TextDirection direction) {
578 NOTIMPLEMENTED();
579 return false;
580 }
581
582 void NativeTextfieldViews::OnCompositionTextConfirmedOrCleared() {
583 if (skip_input_method_cancel_composition_ || !textfield_->HasFocus())
584 return;
585
586 InputMethod* input_method = textfield_->GetInputMethod();
587 if (input_method)
588 input_method->CancelComposition(textfield_);
589 }
420 590
421 const gfx::Font& NativeTextfieldViews::GetFont() const { 591 const gfx::Font& NativeTextfieldViews::GetFont() const {
422 return textfield_->font(); 592 return textfield_->font();
423 } 593 }
424 594
425 SkColor NativeTextfieldViews::GetTextColor() const { 595 SkColor NativeTextfieldViews::GetTextColor() const {
426 return textfield_->text_color(); 596 return textfield_->text_color();
427 } 597 }
428 598
429 void NativeTextfieldViews::UpdateCursor() { 599 void NativeTextfieldViews::UpdateCursor() {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 text_offset_ = -cursor_bounds_.x(); 639 text_offset_ = -cursor_bounds_.x();
470 } else if(full_width > width && text_offset_ + full_width < width) { 640 } else if(full_width > width && text_offset_ + full_width < width) {
471 // when the cursor moves within the textfield with the text 641 // when the cursor moves within the textfield with the text
472 // longer than the field. 642 // longer than the field.
473 text_offset_ = width - full_width; 643 text_offset_ = width - full_width;
474 } else { 644 } else {
475 // move cursor freely. 645 // move cursor freely.
476 } 646 }
477 // shift cursor bounds to fit insets. 647 // shift cursor bounds to fit insets.
478 cursor_bounds_.set_x(cursor_bounds_.x() + text_offset_ + insets.left()); 648 cursor_bounds_.set_x(cursor_bounds_.x() + text_offset_ + insets.left());
649
650 OnCaretBoundsChanged();
479 } 651 }
480 652
481 void NativeTextfieldViews::PaintTextAndCursor(gfx::Canvas* canvas) { 653 void NativeTextfieldViews::PaintTextAndCursor(gfx::Canvas* canvas) {
482 gfx::Insets insets = GetInsets(); 654 gfx::Insets insets = GetInsets();
483 655
484 canvas->Save(); 656 canvas->Save();
485 canvas->ClipRectInt(insets.left(), insets.top(), 657 canvas->ClipRectInt(insets.left(), insets.top(),
486 width() - insets.width(), height() - insets.height()); 658 width() - insets.width(), height() - insets.height());
487 659
488 // TODO(oshima): bidi support 660 // TODO(oshima): bidi support
489 // TODO(varunjain): re-implement this so only that dirty text is painted. 661 // TODO(varunjain): re-implement this so only that dirty text is painted.
490 TextfieldViewsModel::TextFragments fragments; 662 TextfieldViewsModel::TextFragments fragments;
491 model_->GetFragments(&fragments); 663 model_->GetFragments(&fragments);
492 int x_offset = text_offset_ + insets.left(); 664 int x_offset = text_offset_ + insets.left();
493 int y = insets.top(); 665 int y = insets.top();
494 int text_height = height() - insets.height(); 666 int text_height = height() - insets.height();
495 SkColor selection_color = 667 SkColor selection_color =
496 textfield_->HasFocus() ? 668 textfield_->HasFocus() ?
497 kFocusedSelectionColor : kUnfocusedSelectionColor; 669 kFocusedSelectionColor : kUnfocusedSelectionColor;
498 SkColor text_color = 670 SkColor text_color =
499 textfield_->read_only() ? kReadonlyTextColor : GetTextColor(); 671 textfield_->read_only() ? kReadonlyTextColor : GetTextColor();
500 672
501 for (TextfieldViewsModel::TextFragments::const_iterator iter = 673 for (TextfieldViewsModel::TextFragments::const_iterator iter =
502 fragments.begin(); 674 fragments.begin();
503 iter != fragments.end(); 675 iter != fragments.end();
504 iter++) { 676 iter++) {
505 string16 text = model_->GetVisibleText((*iter).begin, (*iter).end); 677 string16 text = model_->GetVisibleText(iter->start, iter->end);
678
679 gfx::Font font = GetFont();
680 if (iter->underline)
681 font = font.DeriveFont(0, font.GetStyle() | gfx::Font::UNDERLINED);
682
506 // TODO(oshima): This does not give the accurate position due to 683 // TODO(oshima): This does not give the accurate position due to
507 // kerning. Figure out how webkit does this with skia. 684 // kerning. Figure out how webkit does this with skia.
508 int width = GetFont().GetStringWidth(text); 685 int width = font.GetStringWidth(text);
509 686
510 if ((*iter).selected) { 687 if (iter->selected) {
511 canvas->FillRectInt(selection_color, x_offset, y, width, text_height); 688 canvas->FillRectInt(selection_color, x_offset, y, width, text_height);
512 canvas->DrawStringInt(text, GetFont(), kSelectedTextColor, 689 canvas->DrawStringInt(text, font, kSelectedTextColor,
513 x_offset, y, width, text_height); 690 x_offset, y, width, text_height);
514 } else { 691 } else {
515 canvas->DrawStringInt(text, GetFont(), text_color, 692 canvas->DrawStringInt(text, font, text_color,
516 x_offset, y, width, text_height); 693 x_offset, y, width, text_height);
517 } 694 }
518 x_offset += width; 695 x_offset += width;
519 } 696 }
520 canvas->Restore(); 697 canvas->Restore();
521 698
522 if (textfield_->IsEnabled() && is_cursor_visible_ && 699 if (textfield_->IsEnabled() && is_cursor_visible_ &&
523 !model_->HasSelection()) { 700 !model_->HasSelection()) {
524 // Paint Cursor. Replace cursor is drawn as rectangle for now. 701 // Paint Cursor. Replace cursor is drawn as rectangle for now.
525 canvas->DrawRectInt(kCursorColor, 702 canvas->DrawRectInt(kCursorColor,
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 case ui::VKEY_LEFT: 748 case ui::VKEY_LEFT:
572 control ? model_->MoveCursorToPreviousWord(selection) 749 control ? model_->MoveCursorToPreviousWord(selection)
573 : model_->MoveCursorLeft(selection); 750 : model_->MoveCursorLeft(selection);
574 cursor_changed = true; 751 cursor_changed = true;
575 break; 752 break;
576 case ui::VKEY_END: 753 case ui::VKEY_END:
577 model_->MoveCursorToEnd(selection); 754 model_->MoveCursorToEnd(selection);
578 cursor_changed = true; 755 cursor_changed = true;
579 break; 756 break;
580 case ui::VKEY_HOME: 757 case ui::VKEY_HOME:
581 model_->MoveCursorToStart(selection); 758 model_->MoveCursorToHome(selection);
582 cursor_changed = true; 759 cursor_changed = true;
583 break; 760 break;
584 case ui::VKEY_BACK: 761 case ui::VKEY_BACK:
585 if (!editable) 762 if (!editable)
586 break; 763 break;
587 if (!model_->HasSelection()) { 764 if (!model_->HasSelection()) {
588 if (selection && control) { 765 if (selection && control) {
589 // If both shift and control are pressed, then erase upto the 766 // If both shift and control are pressed, then erase upto the
590 // beginning of the buffer in ChromeOS. In windows, do nothing. 767 // beginning of the buffer in ChromeOS. In windows, do nothing.
591 #if defined(OS_WIN) 768 #if defined(OS_WIN)
592 break; 769 break;
593 #else 770 #else
594 model_->MoveCursorToStart(true); 771 model_->MoveCursorToHome(true);
595 #endif 772 #endif
596 } else if (control) { 773 } else if (control) {
597 // If only control is pressed, then erase the previous word. 774 // If only control is pressed, then erase the previous word.
598 model_->MoveCursorToPreviousWord(true); 775 model_->MoveCursorToPreviousWord(true);
599 } 776 }
600 } 777 }
601 text_changed = model_->Backspace(); 778 text_changed = model_->Backspace();
602 cursor_changed = true; 779 cursor_changed = true;
603 break; 780 break;
604 case ui::VKEY_DELETE: 781 case ui::VKEY_DELETE:
(...skipping 15 matching lines...) Expand all
620 } 797 }
621 cursor_changed = text_changed = model_->Delete(); 798 cursor_changed = text_changed = model_->Delete();
622 break; 799 break;
623 case ui::VKEY_INSERT: 800 case ui::VKEY_INSERT:
624 insert_ = !insert_; 801 insert_ = !insert_;
625 cursor_changed = true; 802 cursor_changed = true;
626 break; 803 break;
627 default: 804 default:
628 break; 805 break;
629 } 806 }
630 char16 ch = key_event.GetCharacter(); 807
631 if (editable && ShouldInsertChar(ch, key_event.flags())) { 808 // Only handle text input by ourselves if there is no input method.
632 if (insert_) 809 if (!textfield_->GetInputMethod()) {
633 model_->Insert(ch); 810 char16 ch = key_event.GetCharacter();
634 else 811 if (editable && ShouldInsertChar(ch, key_event.flags())) {
635 model_->Replace(ch); 812 if (insert_)
636 text_changed = true; 813 model_->InsertChar(ch);
814 else
815 model_->ReplaceChar(ch);
816 text_changed = true;
817 }
637 } 818 }
638 819
639 UpdateAfterChange(text_changed, cursor_changed); 820 UpdateAfterChange(text_changed, cursor_changed);
640 OnAfterUserAction(); 821 OnAfterUserAction();
641 return (text_changed || cursor_changed); 822 return (text_changed || cursor_changed);
642 } 823 }
643 return false; 824 return false;
644 } 825 }
645 826
646 size_t NativeTextfieldViews::FindCursorPosition(const gfx::Point& point) const { 827 size_t NativeTextfieldViews::FindCursorPosition(const gfx::Point& point) const {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
711 892
712 void NativeTextfieldViews::SetCursorForMouseClick(const views::MouseEvent& e) { 893 void NativeTextfieldViews::SetCursorForMouseClick(const views::MouseEvent& e) {
713 size_t pos = FindCursorPosition(e.location()); 894 size_t pos = FindCursorPosition(e.location());
714 if (model_->MoveCursorTo(pos, false)) { 895 if (model_->MoveCursorTo(pos, false)) {
715 UpdateCursorBoundsAndTextOffset(); 896 UpdateCursorBoundsAndTextOffset();
716 } 897 }
717 } 898 }
718 899
719 void NativeTextfieldViews::PropagateTextChange() { 900 void NativeTextfieldViews::PropagateTextChange() {
720 textfield_->SyncText(); 901 textfield_->SyncText();
721 TextfieldController* controller = textfield_->GetController();
722 if (controller)
723 controller->ContentsChanged(textfield_, GetText());
724 } 902 }
725 903
726 void NativeTextfieldViews::UpdateAfterChange(bool text_changed, 904 void NativeTextfieldViews::UpdateAfterChange(bool text_changed,
727 bool cursor_changed) { 905 bool cursor_changed) {
728 if (text_changed) 906 if (text_changed)
729 PropagateTextChange(); 907 PropagateTextChange();
730 if (cursor_changed) { 908 if (cursor_changed) {
731 is_cursor_visible_ = true; 909 is_cursor_visible_ = true;
732 RepaintCursor(); 910 RepaintCursor();
733 } 911 }
(...skipping 10 matching lines...) Expand all
744 context_menu_contents_->AddItemWithStringId(IDS_APP_CUT, IDS_APP_CUT); 922 context_menu_contents_->AddItemWithStringId(IDS_APP_CUT, IDS_APP_CUT);
745 context_menu_contents_->AddItemWithStringId(IDS_APP_COPY, IDS_APP_COPY); 923 context_menu_contents_->AddItemWithStringId(IDS_APP_COPY, IDS_APP_COPY);
746 context_menu_contents_->AddItemWithStringId(IDS_APP_PASTE, IDS_APP_PASTE); 924 context_menu_contents_->AddItemWithStringId(IDS_APP_PASTE, IDS_APP_PASTE);
747 context_menu_contents_->AddItemWithStringId(IDS_APP_DELETE, IDS_APP_DELETE); 925 context_menu_contents_->AddItemWithStringId(IDS_APP_DELETE, IDS_APP_DELETE);
748 context_menu_contents_->AddSeparator(); 926 context_menu_contents_->AddSeparator();
749 context_menu_contents_->AddItemWithStringId(IDS_APP_SELECT_ALL, 927 context_menu_contents_->AddItemWithStringId(IDS_APP_SELECT_ALL,
750 IDS_APP_SELECT_ALL); 928 IDS_APP_SELECT_ALL);
751 context_menu_menu_.reset(new Menu2(context_menu_contents_.get())); 929 context_menu_menu_.reset(new Menu2(context_menu_contents_.get()));
752 } 930 }
753 931
932 void NativeTextfieldViews::OnTextInputTypeChanged() {
933 if (!textfield_->HasFocus())
934 return;
935
936 InputMethod* input_method = textfield_->GetInputMethod();
937 if (input_method)
938 input_method->OnTextInputTypeChanged(textfield_);
939 }
940
941 void NativeTextfieldViews::OnCaretBoundsChanged() {
942 if (!textfield_->HasFocus())
943 return;
944
945 InputMethod* input_method = textfield_->GetInputMethod();
946 if (input_method)
947 input_method->OnCaretBoundsChanged(textfield_);
948 }
949
754 void NativeTextfieldViews::OnBeforeUserAction() { 950 void NativeTextfieldViews::OnBeforeUserAction() {
755 TextfieldController* controller = textfield_->GetController(); 951 TextfieldController* controller = textfield_->GetController();
756 if (controller) 952 if (controller)
757 controller->OnBeforeUserAction(textfield_); 953 controller->OnBeforeUserAction(textfield_);
758 } 954 }
759 955
760 void NativeTextfieldViews::OnAfterUserAction() { 956 void NativeTextfieldViews::OnAfterUserAction() {
761 TextfieldController* controller = textfield_->GetController(); 957 TextfieldController* controller = textfield_->GetController();
762 if (controller) 958 if (controller)
763 controller->OnAfterUserAction(textfield_); 959 controller->OnAfterUserAction(textfield_);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
822 } 1018 }
823 1019
824 void NativeTextfieldViews::TextfieldBorder::SetInsets(int top, 1020 void NativeTextfieldViews::TextfieldBorder::SetInsets(int top,
825 int left, 1021 int left,
826 int bottom, 1022 int bottom,
827 int right) { 1023 int right) {
828 insets_.Set(top, left, bottom, right); 1024 insets_.Set(top, left, bottom, right);
829 } 1025 }
830 1026
831 } // namespace views 1027 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698