| OLD | NEW |
| 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 Loading... |
| 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 TextFieldStatusNotifyingHanlder : public TextFieldStatusHandler { |
| 98 public: | 99 public: |
| 99 explicit TextFieldStatusNotifyingHanlder(pp::Instance* instance) | 100 explicit TextFieldStatusNotifyingHanlder(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 Loading... |
| 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 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 |
| OLD | NEW |