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

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

Issue 8769003: Pepper IME API for surrounding text retrieval. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge master. Created 8 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
« no previous file with comments | « ppapi/cpp/dev/text_input_dev.cc ('k') | ppapi/ppapi_proxy.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <string> 5 #include <string>
6 #include <utility> 6 #include <utility>
7 #include <vector> 7 #include <vector>
8 8
9 #include "ppapi/c/dev/ppb_console_dev.h" 9 #include "ppapi/c/dev/ppb_console_dev.h"
10 #include "ppapi/c/dev/ppb_cursor_control_dev.h" 10 #include "ppapi/c/dev/ppb_cursor_control_dev.h"
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 return i; 85 return i;
86 } 86 }
87 87
88 } // namespace 88 } // namespace
89 89
90 class TextFieldStatusHandler { 90 class TextFieldStatusHandler {
91 public: 91 public:
92 virtual ~TextFieldStatusHandler() {} 92 virtual ~TextFieldStatusHandler() {}
93 virtual void FocusIn(const pp::Rect& caret, const pp::Rect& bounding_box) {} 93 virtual void FocusIn(const pp::Rect& caret, const pp::Rect& bounding_box) {}
94 virtual void FocusOut() {} 94 virtual void FocusOut() {}
95 virtual void UpdateSelection(const std::string& text) {}
95 }; 96 };
96 97
97 class TextFieldStatusNotifyingHanlder : public TextFieldStatusHandler { 98 class TextFieldStatusNotifyingHandler : public TextFieldStatusHandler {
98 public: 99 public:
99 explicit TextFieldStatusNotifyingHanlder(pp::Instance* instance) 100 explicit TextFieldStatusNotifyingHandler(pp::Instance* instance)
100 : instance_(instance), 101 : textinput_control_(instance) {
101 textinput_control_(instance) {} 102 }
102 103
103 protected: 104 protected:
105 // Implement TextFieldStatusHandler.
104 virtual void FocusIn(const pp::Rect& caret, const pp::Rect& bounding_box) { 106 virtual void FocusIn(const pp::Rect& caret, const pp::Rect& bounding_box) {
105 textinput_control_.SetTextInputType(PP_TEXTINPUT_TYPE_TEXT); 107 textinput_control_.SetTextInputType(PP_TEXTINPUT_TYPE_TEXT);
106 textinput_control_.UpdateCaretPosition(caret, bounding_box); 108 textinput_control_.UpdateCaretPosition(caret, bounding_box);
107 } 109 }
108 virtual void FocusOut() { 110 virtual void FocusOut() {
109 textinput_control_.CancelCompositionText(); 111 textinput_control_.CancelCompositionText();
110 textinput_control_.SetTextInputType(PP_TEXTINPUT_TYPE_NONE); 112 textinput_control_.SetTextInputType(PP_TEXTINPUT_TYPE_NONE);
111 } 113 }
114 virtual void UpdateSelection(const std::string& text) {
115 textinput_control_.SetSelectionText(text);
116 textinput_control_.SelectionChanged();
117 }
112 118
113 private: 119 private:
114 pp::Instance* instance_; 120 class MyTextInput : public pp::TextInput_Dev {
115 pp::TextInput_Dev 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_;
116 }; 130 };
117 131
118 // Hand-made text field for demonstrating text input API. 132 // Hand-made text field for demonstrating text input API.
119 class MyTextField { 133 class MyTextField {
120 public: 134 public:
121 MyTextField(pp::Instance* instance, TextFieldStatusHandler* handler, 135 MyTextField(pp::Instance* instance, TextFieldStatusHandler* handler,
122 int x, int y, int width, int height) 136 int x, int y, int width, int height)
123 : instance_(instance), 137 : instance_(instance),
124 status_handler_(handler), 138 status_handler_(handler),
125 area_(x, y, width, height), 139 area_(x, y, width, height),
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 private: 368 private:
355 // Notify the plugin instance that the caret position has changed. 369 // Notify the plugin instance that the caret position has changed.
356 void CaretPosChanged() { 370 void CaretPosChanged() {
357 if (Focused()) { 371 if (Focused()) {
358 std::string str = utf8_text_.substr(0, caret_pos_); 372 std::string str = utf8_text_.substr(0, caret_pos_);
359 if (!composition_.empty()) 373 if (!composition_.empty())
360 str += composition_.substr(0, composition_selection_.first); 374 str += composition_.substr(0, composition_selection_.first);
361 int px = font_.MeasureSimpleText(str); 375 int px = font_.MeasureSimpleText(str);
362 pp::Rect caret(area_.x() + px, area_.y(), 0, area_.height() + 2); 376 pp::Rect caret(area_.x() + px, area_.y(), 0, area_.height() + 2);
363 status_handler_->FocusIn(caret, area_); 377 status_handler_->FocusIn(caret, area_);
378 status_handler_->UpdateSelection(
379 utf8_text_.substr(SelectionLeft(),
380 SelectionRight() - SelectionLeft()));
364 } 381 }
365 } 382 }
366 size_t SelectionLeft() const { 383 size_t SelectionLeft() const {
367 return std::min(caret_pos_, anchor_pos_); 384 return std::min(caret_pos_, anchor_pos_);
368 } 385 }
369 size_t SelectionRight() const { 386 size_t SelectionRight() const {
370 return std::max(caret_pos_, anchor_pos_); 387 return std::max(caret_pos_, anchor_pos_);
371 } 388 }
372 bool HasSelection() const { 389 bool HasSelection() const {
373 return caret_pos_ != anchor_pos_; 390 return caret_pos_ != anchor_pos_;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 // plugin via PP_INPUTEVENT_TYPE_CHAR events. 440 // plugin via PP_INPUTEVENT_TYPE_CHAR events.
424 } else if (argv[i] == std::string("caretmove")) { 441 } else if (argv[i] == std::string("caretmove")) {
425 // Demonstrating the behavior of plugins with limited IME support. 442 // Demonstrating the behavior of plugins with limited IME support.
426 // 443 //
427 // It uses SetTextInputType() and UpdateCaretPosition() API to notify 444 // It uses SetTextInputType() and UpdateCaretPosition() API to notify
428 // text input status to the browser, but unable to handle inline 445 // text input status to the browser, but unable to handle inline
429 // compositions. By using the notified information. the browser can, 446 // compositions. By using the notified information. the browser can,
430 // say, show virtual keyboards or IMEs only at appropriate timing 447 // say, show virtual keyboards or IMEs only at appropriate timing
431 // that the plugin does need to accept text input. 448 // that the plugin does need to accept text input.
432 delete status_handler_; 449 delete status_handler_;
433 status_handler_ = new TextFieldStatusNotifyingHanlder(this); 450 status_handler_ = new TextFieldStatusNotifyingHandler(this);
434 } else if (argv[i] == std::string("full")) { 451 } else if (argv[i] == std::string("full")) {
435 // Demonstrating the behavior of plugins fully supporting IME. 452 // Demonstrating the behavior of plugins fully supporting IME.
436 // 453 //
437 // It notifies updates of caret positions to the browser, 454 // It notifies updates of caret positions to the browser,
438 // and handles all text input events by itself. 455 // and handles all text input events by itself.
439 delete status_handler_; 456 delete status_handler_;
440 status_handler_ = new TextFieldStatusNotifyingHanlder(this); 457 status_handler_ = new TextFieldStatusNotifyingHandler(this);
441 RequestInputEvents(PP_INPUTEVENT_CLASS_IME); 458 RequestInputEvents(PP_INPUTEVENT_CLASS_IME);
442 } 459 }
443 break; 460 break;
444 } 461 }
445 } 462 }
446 463
447 textfield_.push_back(MyTextField(this, status_handler_, 464 textfield_.push_back(MyTextField(this, status_handler_,
448 10, 10, 300, 20)); 465 10, 10, 300, 20));
449 textfield_.back().SetText("Hello"); 466 textfield_.back().SetText("Hello");
450 textfield_.push_back(MyTextField(this, status_handler_, 467 textfield_.push_back(MyTextField(this, status_handler_,
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
710 } 727 }
711 }; 728 };
712 729
713 namespace pp { 730 namespace pp {
714 731
715 Module* CreateModule() { 732 Module* CreateModule() {
716 return new MyModule(); 733 return new MyModule();
717 } 734 }
718 735
719 } // namespace pp 736 } // namespace pp
OLDNEW
« no previous file with comments | « ppapi/cpp/dev/text_input_dev.cc ('k') | ppapi/ppapi_proxy.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698