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

Side by Side Diff: ppapi/examples/ime/ime.cc

Issue 18671004: PPAPI: Move IMEInputEvent and TextInput to stable. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 5 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) 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"
15 #include "ppapi/cpp/ime_input_event.h"
17 #include "ppapi/cpp/input_event.h" 16 #include "ppapi/cpp/input_event.h"
18 #include "ppapi/cpp/instance.h" 17 #include "ppapi/cpp/instance.h"
19 #include "ppapi/cpp/module.h" 18 #include "ppapi/cpp/module.h"
20 #include "ppapi/cpp/rect.h" 19 #include "ppapi/cpp/rect.h"
21 #include "ppapi/cpp/size.h" 20 #include "ppapi/cpp/size.h"
21 #include "ppapi/cpp/text_input.h"
22 22
23 namespace { 23 namespace {
24 24
25 // Extracted from: ui/base/keycodes/keyboard_codes.h 25 // Extracted from: ui/base/keycodes/keyboard_codes.h
26 enum { 26 enum {
27 VKEY_BACK = 0x08, 27 VKEY_BACK = 0x08,
28 VKEY_SHIFT = 0x10, 28 VKEY_SHIFT = 0x10,
29 VKEY_DELETE = 0x2E, 29 VKEY_DELETE = 0x2E,
30 VKEY_LEFT = 0x25, 30 VKEY_LEFT = 0x25,
31 VKEY_UP = 0x26, 31 VKEY_UP = 0x26,
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 virtual void FocusOut() { 110 virtual void FocusOut() {
111 textinput_control_.CancelCompositionText(); 111 textinput_control_.CancelCompositionText();
112 textinput_control_.SetTextInputType(PP_TEXTINPUT_TYPE_NONE); 112 textinput_control_.SetTextInputType(PP_TEXTINPUT_TYPE_NONE);
113 } 113 }
114 virtual void UpdateSelection(const std::string& text) { 114 virtual void UpdateSelection(const std::string& text) {
115 textinput_control_.SetSelectionText(text); 115 textinput_control_.SetSelectionText(text);
116 textinput_control_.SelectionChanged(); 116 textinput_control_.SelectionChanged();
117 } 117 }
118 118
119 private: 119 private:
120 class MyTextInput : public pp::TextInput_Dev { 120 class MyTextInput : public pp::TextInput {
121 public: 121 public:
122 MyTextInput(pp::Instance* instance) : pp::TextInput_Dev(instance) {} 122 MyTextInput(pp::Instance* instance) : pp::TextInput(instance) {}
123 virtual void RequestSurroundingText(uint32_t characters) { 123 virtual void RequestSurroundingText(uint32_t characters) {
124 UpdateSurroundingText(selection_text_, 0, selection_text_.size()); 124 UpdateSurroundingText(selection_text_, 0, selection_text_.size());
125 } 125 }
126 void SetSelectionText(const std::string& text) { selection_text_ = text; } 126 void SetSelectionText(const std::string& text) { selection_text_ = text; }
127 std::string selection_text_; 127 std::string selection_text_;
128 }; 128 };
129 MyTextInput textinput_control_; 129 MyTextInput textinput_control_;
130 }; 130 };
131 131
132 // Hand-made text field for demonstrating text input API. 132 // Hand-made text field for demonstrating text input API.
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE); 421 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE);
422 RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD); 422 RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD);
423 423
424 for (uint32_t i = 0; i < argc; ++i) { 424 for (uint32_t i = 0; i < argc; ++i) {
425 if (argn[i] == std::string("ime")) { 425 if (argn[i] == std::string("ime")) {
426 if (argv[i] == std::string("no")) { 426 if (argv[i] == std::string("no")) {
427 // Example of NO-IME plugins (e.g., games). 427 // Example of NO-IME plugins (e.g., games).
428 // 428 //
429 // When a plugin never wants to accept text input, at initialization 429 // When a plugin never wants to accept text input, at initialization
430 // explicitly turn off the text input feature by calling: 430 // explicitly turn off the text input feature by calling:
431 pp::TextInput_Dev(this).SetTextInputType(PP_TEXTINPUT_TYPE_NONE); 431 pp::TextInput(this).SetTextInputType(PP_TEXTINPUT_TYPE_NONE);
432 } else if (argv[i] == std::string("unaware")) { 432 } else if (argv[i] == std::string("unaware")) {
433 // Demonstrating the behavior of IME-unaware plugins. 433 // Demonstrating the behavior of IME-unaware plugins.
434 // Never call any text input related APIs. 434 // Never call any text input related APIs.
435 // 435 //
436 // In such a case, the plugin is assumed to always accept text input. 436 // 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 437 // 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 438 // 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 439 // text via IME on the plugin. The characters are delivered to the
440 // plugin via PP_INPUTEVENT_TYPE_CHAR events. 440 // plugin via PP_INPUTEVENT_TYPE_CHAR events.
441 } else if (argv[i] == std::string("caretmove")) { 441 } else if (argv[i] == std::string("caretmove")) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 ret = OnKeyDown(keyEvent); 495 ret = OnKeyDown(keyEvent);
496 break; 496 break;
497 } 497 }
498 case PP_INPUTEVENT_TYPE_CHAR: { 498 case PP_INPUTEVENT_TYPE_CHAR: {
499 const pp::KeyboardInputEvent keyEvent(event); 499 const pp::KeyboardInputEvent keyEvent(event);
500 Log("Char [" + keyEvent.GetCharacterText().AsString() + "]"); 500 Log("Char [" + keyEvent.GetCharacterText().AsString() + "]");
501 ret = OnChar(keyEvent); 501 ret = OnChar(keyEvent);
502 break; 502 break;
503 } 503 }
504 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_START: { 504 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_START: {
505 const pp::IMEInputEvent_Dev imeEvent(event); 505 const pp::IMEInputEvent imeEvent(event);
506 Log("CompositionStart [" + imeEvent.GetText().AsString() + "]"); 506 Log("CompositionStart [" + imeEvent.GetText().AsString() + "]");
507 ret = true; 507 ret = true;
508 break; 508 break;
509 } 509 }
510 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE: { 510 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_UPDATE: {
511 const pp::IMEInputEvent_Dev imeEvent(event); 511 const pp::IMEInputEvent imeEvent(event);
512 Log("CompositionUpdate [" + imeEvent.GetText().AsString() + "]"); 512 Log("CompositionUpdate [" + imeEvent.GetText().AsString() + "]");
513 ret = OnCompositionUpdate(imeEvent); 513 ret = OnCompositionUpdate(imeEvent);
514 break; 514 break;
515 } 515 }
516 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_END: { 516 case PP_INPUTEVENT_TYPE_IME_COMPOSITION_END: {
517 const pp::IMEInputEvent_Dev imeEvent(event); 517 const pp::IMEInputEvent imeEvent(event);
518 Log("CompositionEnd [" + imeEvent.GetText().AsString() + "]"); 518 Log("CompositionEnd [" + imeEvent.GetText().AsString() + "]");
519 ret = OnCompositionEnd(imeEvent); 519 ret = OnCompositionEnd(imeEvent);
520 break; 520 break;
521 } 521 }
522 case PP_INPUTEVENT_TYPE_IME_TEXT: { 522 case PP_INPUTEVENT_TYPE_IME_TEXT: {
523 const pp::IMEInputEvent_Dev imeEvent(event); 523 const pp::IMEInputEvent imeEvent(event);
524 Log("ImeText [" + imeEvent.GetText().AsString() + "]"); 524 Log("ImeText [" + imeEvent.GetText().AsString() + "]");
525 ret = OnImeText(imeEvent); 525 ret = OnImeText(imeEvent);
526 break; 526 break;
527 } 527 }
528 default: 528 default:
529 break; 529 break;
530 } 530 }
531 if (ret && (dragging_ || event.GetType() != PP_INPUTEVENT_TYPE_MOUSEMOVE)) 531 if (ret && (dragging_ || event.GetType() != PP_INPUTEVENT_TYPE_MOUSEMOVE))
532 Paint(); 532 Paint();
533 return ret; 533 return ret;
534 } 534 }
535 535
536 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) { 536 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) {
537 if (position.size() == last_size_) 537 if (position.size() == last_size_)
538 return; 538 return;
539 last_size_ = position.size(); 539 last_size_ = position.size();
540 Paint(); 540 Paint();
541 } 541 }
542 542
543 private: 543 private:
544 bool OnCompositionUpdate(const pp::IMEInputEvent_Dev& ev) { 544 bool OnCompositionUpdate(const pp::IMEInputEvent& ev) {
545 for (std::vector<MyTextField>::iterator it = textfield_.begin(); 545 for (std::vector<MyTextField>::iterator it = textfield_.begin();
546 it != textfield_.end(); 546 it != textfield_.end();
547 ++it) { 547 ++it) {
548 if (it->Focused()) { 548 if (it->Focused()) {
549 std::vector< std::pair<uint32_t, uint32_t> > segs; 549 std::vector< std::pair<uint32_t, uint32_t> > segs;
550 for (uint32_t i = 0; i < ev.GetSegmentNumber(); ++i) 550 for (uint32_t i = 0; i < ev.GetSegmentNumber(); ++i)
551 segs.push_back(std::make_pair(ev.GetSegmentOffset(i), 551 segs.push_back(std::make_pair(ev.GetSegmentOffset(i),
552 ev.GetSegmentOffset(i + 1))); 552 ev.GetSegmentOffset(i + 1)));
553 it->SetComposition(ev.GetText().AsString(), 553 it->SetComposition(ev.GetText().AsString(),
554 segs, 554 segs,
555 ev.GetTargetSegment(), 555 ev.GetTargetSegment(),
556 ev.GetSelection()); 556 ev.GetSelection());
557 return true; 557 return true;
558 } 558 }
559 } 559 }
560 return false; 560 return false;
561 } 561 }
562 562
563 bool OnCompositionEnd(const pp::IMEInputEvent_Dev& ev) { 563 bool OnCompositionEnd(const pp::IMEInputEvent& ev) {
564 for (std::vector<MyTextField>::iterator it = textfield_.begin(); 564 for (std::vector<MyTextField>::iterator it = textfield_.begin();
565 it != textfield_.end(); 565 it != textfield_.end();
566 ++it) { 566 ++it) {
567 if (it->Focused()) { 567 if (it->Focused()) {
568 it->SetComposition(std::string(), 568 it->SetComposition(std::string(),
569 std::vector<std::pair<uint32_t, uint32_t> >(), 569 std::vector<std::pair<uint32_t, uint32_t> >(),
570 0, 570 0,
571 std::make_pair(0, 0)); 571 std::make_pair(0, 0));
572 return true; 572 return true;
573 } 573 }
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
660 if (it->Focused()) { 660 if (it->Focused()) {
661 std::string str = ev.GetCharacterText().AsString(); 661 std::string str = ev.GetCharacterText().AsString();
662 if (str != "\r" && str != "\n") 662 if (str != "\r" && str != "\n")
663 it->InsertText(str); 663 it->InsertText(str);
664 return true; 664 return true;
665 } 665 }
666 } 666 }
667 return false; 667 return false;
668 } 668 }
669 669
670 bool OnImeText(const pp::IMEInputEvent_Dev ev) { 670 bool OnImeText(const pp::IMEInputEvent ev) {
671 for (std::vector<MyTextField>::iterator it = textfield_.begin(); 671 for (std::vector<MyTextField>::iterator it = textfield_.begin();
672 it != textfield_.end(); 672 it != textfield_.end();
673 ++it) { 673 ++it) {
674 if (it->Focused()) { 674 if (it->Focused()) {
675 it->InsertText(ev.GetText().AsString()); 675 it->InsertText(ev.GetText().AsString());
676 return true; 676 return true;
677 } 677 }
678 } 678 }
679 return false; 679 return false;
680 } 680 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
729 } 729 }
730 }; 730 };
731 731
732 namespace pp { 732 namespace pp {
733 733
734 Module* CreateModule() { 734 Module* CreateModule() {
735 return new MyModule(); 735 return new MyModule();
736 } 736 }
737 737
738 } // namespace pp 738 } // namespace pp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698