| 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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, const pp::Rect& bounding_box) { |
| 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, bounding_box); |
| 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 281 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))); |
| 553 it->SetComposition(ev.GetText().AsString(), | 543 it->SetComposition(ev.GetText().AsString(), |
| 554 segs, | 544 segs, |
| 555 ev.GetTargetSegment(), | 545 ev.GetTargetSegment(), |
| 556 ev.GetSelection()); | 546 ev.GetSelection()); |
| 557 return true; | 547 return true; |
| 558 } | 548 } |
| 559 } | 549 } |
| 560 return false; | 550 return false; |
| 561 } | 551 } |
| 562 | 552 |
| 563 bool OnCompositionEnd(const pp::IMEInputEvent_Dev& ev) { | 553 bool OnCompositionEnd(const pp::IMEInputEvent& ev) { |
| 564 for (std::vector<MyTextField>::iterator it = textfield_.begin(); | 554 for (std::vector<MyTextField>::iterator it = textfield_.begin(); |
| 565 it != textfield_.end(); | 555 it != textfield_.end(); |
| 566 ++it) { | 556 ++it) { |
| 567 if (it->Focused()) { | 557 if (it->Focused()) { |
| 568 it->SetComposition(std::string(), | 558 it->SetComposition(std::string(), |
| 569 std::vector<std::pair<uint32_t, uint32_t> >(), | 559 std::vector<std::pair<uint32_t, uint32_t> >(), |
| 570 0, | 560 0, |
| 571 std::make_pair(0, 0)); | 561 std::make_pair(0, 0)); |
| 572 return true; | 562 return true; |
| 573 } | 563 } |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 if (it->Focused()) { | 650 if (it->Focused()) { |
| 661 std::string str = ev.GetCharacterText().AsString(); | 651 std::string str = ev.GetCharacterText().AsString(); |
| 662 if (str != "\r" && str != "\n") | 652 if (str != "\r" && str != "\n") |
| 663 it->InsertText(str); | 653 it->InsertText(str); |
| 664 return true; | 654 return true; |
| 665 } | 655 } |
| 666 } | 656 } |
| 667 return false; | 657 return false; |
| 668 } | 658 } |
| 669 | 659 |
| 670 bool OnImeText(const pp::IMEInputEvent_Dev ev) { | 660 bool OnImeText(const pp::IMEInputEvent ev) { |
| 671 for (std::vector<MyTextField>::iterator it = textfield_.begin(); | 661 for (std::vector<MyTextField>::iterator it = textfield_.begin(); |
| 672 it != textfield_.end(); | 662 it != textfield_.end(); |
| 673 ++it) { | 663 ++it) { |
| 674 if (it->Focused()) { | 664 if (it->Focused()) { |
| 675 it->InsertText(ev.GetText().AsString()); | 665 it->InsertText(ev.GetText().AsString()); |
| 676 return true; | 666 return true; |
| 677 } | 667 } |
| 678 } | 668 } |
| 679 return false; | 669 return false; |
| 680 } | 670 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 729 } | 719 } |
| 730 }; | 720 }; |
| 731 | 721 |
| 732 namespace pp { | 722 namespace pp { |
| 733 | 723 |
| 734 Module* CreateModule() { | 724 Module* CreateModule() { |
| 735 return new MyModule(); | 725 return new MyModule(); |
| 736 } | 726 } |
| 737 | 727 |
| 738 } // namespace pp | 728 } // namespace pp |
| OLD | NEW |