OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <string> | 5 #include <string> |
6 #include <utility> | 6 #include <utility> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "ppapi/c/dev/ppb_cursor_control_dev.h" | 9 #include "ppapi/c/dev/ppb_cursor_control_dev.h" |
10 #include "ppapi/c/ppb_console.h" | 10 #include "ppapi/c/ppb_console.h" |
11 #include "ppapi/cpp/completion_callback.h" | 11 #include "ppapi/cpp/completion_callback.h" |
12 #include "ppapi/cpp/dev/font_dev.h" | 12 #include "ppapi/cpp/dev/font_dev.h" |
13 #include "ppapi/cpp/dev/ime_input_event_dev.h" | |
14 #include "ppapi/cpp/dev/text_input_dev.h" | |
15 #include "ppapi/cpp/graphics_2d.h" | 13 #include "ppapi/cpp/graphics_2d.h" |
16 #include "ppapi/cpp/image_data.h" | 14 #include "ppapi/cpp/image_data.h" |
17 #include "ppapi/cpp/input_event.h" | 15 #include "ppapi/cpp/input_event.h" |
18 #include "ppapi/cpp/instance.h" | 16 #include "ppapi/cpp/instance.h" |
19 #include "ppapi/cpp/module.h" | 17 #include "ppapi/cpp/module.h" |
20 #include "ppapi/cpp/rect.h" | 18 #include "ppapi/cpp/rect.h" |
21 #include "ppapi/cpp/size.h" | 19 #include "ppapi/cpp/size.h" |
| 20 #include "ppapi/cpp/text_input_controller.h" |
22 | 21 |
23 namespace { | 22 namespace { |
24 | 23 |
25 // Extracted from: ui/base/keycodes/keyboard_codes.h | 24 // Extracted from: ui/base/keycodes/keyboard_codes.h |
26 enum { | 25 enum { |
27 VKEY_BACK = 0x08, | 26 VKEY_BACK = 0x08, |
28 VKEY_SHIFT = 0x10, | 27 VKEY_SHIFT = 0x10, |
29 VKEY_DELETE = 0x2E, | 28 VKEY_DELETE = 0x2E, |
30 VKEY_LEFT = 0x25, | 29 VKEY_LEFT = 0x25, |
31 VKEY_UP = 0x26, | 30 VKEY_UP = 0x26, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 for (size_t step = 0; step < n; ++step) | 82 for (size_t step = 0; step < n; ++step) |
84 i = GetNextCharOffsetUtf8(str, i); | 83 i = GetNextCharOffsetUtf8(str, i); |
85 return i; | 84 return i; |
86 } | 85 } |
87 | 86 |
88 } // namespace | 87 } // namespace |
89 | 88 |
90 class TextFieldStatusHandler { | 89 class TextFieldStatusHandler { |
91 public: | 90 public: |
92 virtual ~TextFieldStatusHandler() {} | 91 virtual ~TextFieldStatusHandler() {} |
93 virtual void FocusIn(const pp::Rect& caret, const pp::Rect& bounding_box) {} | 92 virtual void FocusIn(const pp::Rect& caret) {} |
94 virtual void FocusOut() {} | 93 virtual void FocusOut() {} |
95 virtual void UpdateSelection(const std::string& text) {} | 94 virtual void UpdateSelection(const std::string& text) {} |
96 }; | 95 }; |
97 | 96 |
98 class TextFieldStatusNotifyingHandler : public TextFieldStatusHandler { | 97 class TextFieldStatusNotifyingHandler : public TextFieldStatusHandler { |
99 public: | 98 public: |
100 explicit TextFieldStatusNotifyingHandler(pp::Instance* instance) | 99 explicit TextFieldStatusNotifyingHandler(pp::Instance* instance) |
101 : textinput_control_(instance) { | 100 : textinput_control_(instance) { |
102 } | 101 } |
103 | 102 |
104 protected: | 103 protected: |
105 // Implement TextFieldStatusHandler. | 104 // Implement TextFieldStatusHandler. |
106 virtual void FocusIn(const pp::Rect& caret, const pp::Rect& bounding_box) { | 105 virtual void FocusIn(const pp::Rect& caret) { |
107 textinput_control_.SetTextInputType(PP_TEXTINPUT_TYPE_TEXT); | 106 textinput_control_.SetTextInputType(PP_TEXTINPUT_TYPE_TEXT); |
108 textinput_control_.UpdateCaretPosition(caret, bounding_box); | 107 textinput_control_.UpdateCaretPosition(caret); |
109 } | 108 } |
110 virtual void FocusOut() { | 109 virtual void FocusOut() { |
111 textinput_control_.CancelCompositionText(); | 110 textinput_control_.CancelCompositionText(); |
112 textinput_control_.SetTextInputType(PP_TEXTINPUT_TYPE_NONE); | 111 textinput_control_.SetTextInputType(PP_TEXTINPUT_TYPE_NONE); |
113 } | 112 } |
114 virtual void UpdateSelection(const std::string& text) { | 113 virtual void UpdateSelection(const std::string& text) { |
115 textinput_control_.SetSelectionText(text); | 114 textinput_control_.UpdateSurroundingText(text, 0, text.size()); |
116 textinput_control_.SelectionChanged(); | |
117 } | 115 } |
118 | 116 |
119 private: | 117 private: |
120 class MyTextInput : public pp::TextInput_Dev { | 118 pp::TextInputController textinput_control_; |
121 public: | |
122 MyTextInput(pp::Instance* instance) : pp::TextInput_Dev(instance) {} | |
123 virtual void RequestSurroundingText(uint32_t characters) { | |
124 UpdateSurroundingText(selection_text_, 0, selection_text_.size()); | |
125 } | |
126 void SetSelectionText(const std::string& text) { selection_text_ = text; } | |
127 std::string selection_text_; | |
128 }; | |
129 MyTextInput textinput_control_; | |
130 }; | 119 }; |
131 | 120 |
132 // Hand-made text field for demonstrating text input API. | 121 // Hand-made text field for demonstrating text input API. |
133 class MyTextField { | 122 class MyTextField { |
134 public: | 123 public: |
135 MyTextField(pp::Instance* instance, TextFieldStatusHandler* handler, | 124 MyTextField(pp::Instance* instance, TextFieldStatusHandler* handler, |
136 int x, int y, int width, int height) | 125 int x, int y, int width, int height) |
137 : instance_(instance), | 126 : instance_(instance), |
138 status_handler_(handler), | 127 status_handler_(handler), |
139 area_(x, y, width, height), | 128 area_(x, y, width, height), |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 | 356 |
368 private: | 357 private: |
369 // Notify the plugin instance that the caret position has changed. | 358 // Notify the plugin instance that the caret position has changed. |
370 void CaretPosChanged() { | 359 void CaretPosChanged() { |
371 if (Focused()) { | 360 if (Focused()) { |
372 std::string str = utf8_text_.substr(0, caret_pos_); | 361 std::string str = utf8_text_.substr(0, caret_pos_); |
373 if (!composition_.empty()) | 362 if (!composition_.empty()) |
374 str += composition_.substr(0, composition_selection_.first); | 363 str += composition_.substr(0, composition_selection_.first); |
375 int px = font_.MeasureSimpleText(str); | 364 int px = font_.MeasureSimpleText(str); |
376 pp::Rect caret(area_.x() + px, area_.y(), 0, area_.height() + 2); | 365 pp::Rect caret(area_.x() + px, area_.y(), 0, area_.height() + 2); |
377 status_handler_->FocusIn(caret, area_); | 366 status_handler_->FocusIn(caret); |
378 status_handler_->UpdateSelection( | 367 status_handler_->UpdateSelection( |
379 utf8_text_.substr(SelectionLeft(), | 368 utf8_text_.substr(SelectionLeft(), |
380 SelectionRight() - SelectionLeft())); | 369 SelectionRight() - SelectionLeft())); |
381 } | 370 } |
382 } | 371 } |
383 size_t SelectionLeft() const { | 372 size_t SelectionLeft() const { |
384 return std::min(caret_pos_, anchor_pos_); | 373 return std::min(caret_pos_, anchor_pos_); |
385 } | 374 } |
386 size_t SelectionRight() const { | 375 size_t SelectionRight() const { |
387 return std::max(caret_pos_, anchor_pos_); | 376 return std::max(caret_pos_, anchor_pos_); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE); | 410 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE); |
422 RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD); | 411 RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD); |
423 | 412 |
424 for (uint32_t i = 0; i < argc; ++i) { | 413 for (uint32_t i = 0; i < argc; ++i) { |
425 if (argn[i] == std::string("ime")) { | 414 if (argn[i] == std::string("ime")) { |
426 if (argv[i] == std::string("no")) { | 415 if (argv[i] == std::string("no")) { |
427 // Example of NO-IME plugins (e.g., games). | 416 // Example of NO-IME plugins (e.g., games). |
428 // | 417 // |
429 // When a plugin never wants to accept text input, at initialization | 418 // When a plugin never wants to accept text input, at initialization |
430 // explicitly turn off the text input feature by calling: | 419 // explicitly turn off the text input feature by calling: |
431 pp::TextInput_Dev(this).SetTextInputType(PP_TEXTINPUT_TYPE_NONE); | 420 pp::TextInputController(this).SetTextInputType( |
| 421 PP_TEXTINPUT_TYPE_NONE); |
432 } else if (argv[i] == std::string("unaware")) { | 422 } else if (argv[i] == std::string("unaware")) { |
433 // Demonstrating the behavior of IME-unaware plugins. | 423 // Demonstrating the behavior of IME-unaware plugins. |
434 // Never call any text input related APIs. | 424 // Never call any text input related APIs. |
435 // | 425 // |
436 // In such a case, the plugin is assumed to always accept text input. | 426 // In such a case, the plugin is assumed to always accept text input. |
437 // For example, when the plugin is focused in touch devices a virtual | 427 // For example, when the plugin is focused in touch devices a virtual |
438 // keyboard may pop up, or in environment IME is used, users can type | 428 // keyboard may pop up, or in environment IME is used, users can type |
439 // text via IME on the plugin. The characters are delivered to the | 429 // text via IME on the plugin. The characters are delivered to the |
440 // plugin via PP_INPUTEVENT_TYPE_CHAR events. | 430 // plugin via PP_INPUTEVENT_TYPE_CHAR events. |
441 } else if (argv[i] == std::string("caretmove")) { | 431 } else if (argv[i] == std::string("caretmove")) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 ret = OnKeyDown(keyEvent); | 485 ret = OnKeyDown(keyEvent); |
496 break; | 486 break; |
497 } | 487 } |
498 case PP_INPUTEVENT_TYPE_CHAR: { | 488 case PP_INPUTEVENT_TYPE_CHAR: { |
499 const pp::KeyboardInputEvent keyEvent(event); | 489 const pp::KeyboardInputEvent keyEvent(event); |
500 Log("Char [" + keyEvent.GetCharacterText().AsString() + "]"); | 490 Log("Char [" + keyEvent.GetCharacterText().AsString() + "]"); |
501 ret = OnChar(keyEvent); | 491 ret = OnChar(keyEvent); |
502 break; | 492 break; |
503 } | 493 } |
504 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_START: { | 494 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_START: { |
505 const pp::IMEInputEvent_Dev imeEvent(event); | 495 const pp::IMEInputEvent imeEvent(event); |
506 Log("CompositionStart [" + imeEvent.GetText().AsString() + "]"); | 496 Log("CompositionStart [" + imeEvent.GetText().AsString() + "]"); |
507 ret = true; | 497 ret = true; |
508 break; | 498 break; |
509 } | 499 } |
510 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE: { | 500 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE: { |
511 const pp::IMEInputEvent_Dev imeEvent(event); | 501 const pp::IMEInputEvent imeEvent(event); |
512 Log("CompositionUpdate [" + imeEvent.GetText().AsString() + "]"); | 502 Log("CompositionUpdate [" + imeEvent.GetText().AsString() + "]"); |
513 ret = OnCompositionUpdate(imeEvent); | 503 ret = OnCompositionUpdate(imeEvent); |
514 break; | 504 break; |
515 } | 505 } |
516 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_END: { | 506 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_END: { |
517 const pp::IMEInputEvent_Dev imeEvent(event); | 507 const pp::IMEInputEvent imeEvent(event); |
518 Log("CompositionEnd [" + imeEvent.GetText().AsString() + "]"); | 508 Log("CompositionEnd [" + imeEvent.GetText().AsString() + "]"); |
519 ret = OnCompositionEnd(imeEvent); | 509 ret = OnCompositionEnd(imeEvent); |
520 break; | 510 break; |
521 } | 511 } |
522 case PP_INPUTEVENT_TYPE_IME_TEXT: { | 512 case PP_INPUTEVENT_TYPE_IME_TEXT: { |
523 const pp::IMEInputEvent_Dev imeEvent(event); | 513 const pp::IMEInputEvent imeEvent(event); |
524 Log("ImeText [" + imeEvent.GetText().AsString() + "]"); | 514 Log("ImeText [" + imeEvent.GetText().AsString() + "]"); |
525 ret = OnImeText(imeEvent); | 515 ret = OnImeText(imeEvent); |
526 break; | 516 break; |
527 } | 517 } |
528 default: | 518 default: |
529 break; | 519 break; |
530 } | 520 } |
531 if (ret && (dragging_ || event.GetType() != PP_INPUTEVENT_TYPE_MOUSEMOVE)) | 521 if (ret && (dragging_ || event.GetType() != PP_INPUTEVENT_TYPE_MOUSEMOVE)) |
532 Paint(); | 522 Paint(); |
533 return ret; | 523 return ret; |
534 } | 524 } |
535 | 525 |
536 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) { | 526 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) { |
537 if (position.size() == last_size_) | 527 if (position.size() == last_size_) |
538 return; | 528 return; |
539 last_size_ = position.size(); | 529 last_size_ = position.size(); |
540 Paint(); | 530 Paint(); |
541 } | 531 } |
542 | 532 |
543 private: | 533 private: |
544 bool OnCompositionUpdate(const pp::IMEInputEvent_Dev& ev) { | 534 bool OnCompositionUpdate(const pp::IMEInputEvent& ev) { |
545 for (std::vector<MyTextField>::iterator it = textfield_.begin(); | 535 for (std::vector<MyTextField>::iterator it = textfield_.begin(); |
546 it != textfield_.end(); | 536 it != textfield_.end(); |
547 ++it) { | 537 ++it) { |
548 if (it->Focused()) { | 538 if (it->Focused()) { |
549 std::vector< std::pair<uint32_t, uint32_t> > segs; | 539 std::vector< std::pair<uint32_t, uint32_t> > segs; |
550 for (uint32_t i = 0; i < ev.GetSegmentNumber(); ++i) | 540 for (uint32_t i = 0; i < ev.GetSegmentNumber(); ++i) |
551 segs.push_back(std::make_pair(ev.GetSegmentOffset(i), | 541 segs.push_back(std::make_pair(ev.GetSegmentOffset(i), |
552 ev.GetSegmentOffset(i + 1))); | 542 ev.GetSegmentOffset(i + 1))); |
| 543 uint32_t selection_start; |
| 544 uint32_t selection_end; |
| 545 ev.GetSelection(&selection_start, &selection_end); |
553 it->SetComposition(ev.GetText().AsString(), | 546 it->SetComposition(ev.GetText().AsString(), |
554 segs, | 547 segs, |
555 ev.GetTargetSegment(), | 548 ev.GetTargetSegment(), |
556 ev.GetSelection()); | 549 std::make_pair(selection_start, selection_end)); |
557 return true; | 550 return true; |
558 } | 551 } |
559 } | 552 } |
560 return false; | 553 return false; |
561 } | 554 } |
562 | 555 |
563 bool OnCompositionEnd(const pp::IMEInputEvent_Dev& ev) { | 556 bool OnCompositionEnd(const pp::IMEInputEvent& ev) { |
564 for (std::vector<MyTextField>::iterator it = textfield_.begin(); | 557 for (std::vector<MyTextField>::iterator it = textfield_.begin(); |
565 it != textfield_.end(); | 558 it != textfield_.end(); |
566 ++it) { | 559 ++it) { |
567 if (it->Focused()) { | 560 if (it->Focused()) { |
568 it->SetComposition(std::string(), | 561 it->SetComposition(std::string(), |
569 std::vector<std::pair<uint32_t, uint32_t> >(), | 562 std::vector<std::pair<uint32_t, uint32_t> >(), |
570 0, | 563 0, |
571 std::make_pair(0, 0)); | 564 std::make_pair(0, 0)); |
572 return true; | 565 return true; |
573 } | 566 } |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 if (it->Focused()) { | 653 if (it->Focused()) { |
661 std::string str = ev.GetCharacterText().AsString(); | 654 std::string str = ev.GetCharacterText().AsString(); |
662 if (str != "\r" && str != "\n") | 655 if (str != "\r" && str != "\n") |
663 it->InsertText(str); | 656 it->InsertText(str); |
664 return true; | 657 return true; |
665 } | 658 } |
666 } | 659 } |
667 return false; | 660 return false; |
668 } | 661 } |
669 | 662 |
670 bool OnImeText(const pp::IMEInputEvent_Dev ev) { | 663 bool OnImeText(const pp::IMEInputEvent ev) { |
671 for (std::vector<MyTextField>::iterator it = textfield_.begin(); | 664 for (std::vector<MyTextField>::iterator it = textfield_.begin(); |
672 it != textfield_.end(); | 665 it != textfield_.end(); |
673 ++it) { | 666 ++it) { |
674 if (it->Focused()) { | 667 if (it->Focused()) { |
675 it->InsertText(ev.GetText().AsString()); | 668 it->InsertText(ev.GetText().AsString()); |
676 return true; | 669 return true; |
677 } | 670 } |
678 } | 671 } |
679 return false; | 672 return false; |
680 } | 673 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
729 } | 722 } |
730 }; | 723 }; |
731 | 724 |
732 namespace pp { | 725 namespace pp { |
733 | 726 |
734 Module* CreateModule() { | 727 Module* CreateModule() { |
735 return new MyModule(); | 728 return new MyModule(); |
736 } | 729 } |
737 | 730 |
738 } // namespace pp | 731 } // namespace pp |
OLD | NEW |