Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ppapi / cpp / dev / text_input_dev.cc
blobb0fff8a413f2343db6c84fd4d1b31018b368e947
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
3 // found in the LICENSE file.
5 #include "ppapi/cpp/dev/text_input_dev.h"
7 #include "ppapi/c/dev/ppp_text_input_dev.h"
8 #include "ppapi/cpp/instance.h"
9 #include "ppapi/cpp/instance_handle.h"
10 #include "ppapi/cpp/module_impl.h"
11 #include "ppapi/cpp/rect.h"
13 namespace pp {
15 namespace {
17 static const char kPPPTextInputInterface[] = PPP_TEXTINPUT_DEV_INTERFACE;
19 void RequestSurroundingText(PP_Instance instance,
20 uint32_t desired_number_of_characters) {
21 void* object = Instance::GetPerInstanceObject(instance,
22 kPPPTextInputInterface);
23 if (!object)
24 return;
25 static_cast<TextInput_Dev*>(object)->RequestSurroundingText(
26 desired_number_of_characters);
29 const PPP_TextInput_Dev ppp_text_input = {
30 &RequestSurroundingText
33 template <> const char* interface_name<PPB_TextInput_Dev_0_2>() {
34 return PPB_TEXTINPUT_DEV_INTERFACE_0_2;
37 template <> const char* interface_name<PPB_TextInput_Dev_0_1>() {
38 return PPB_TEXTINPUT_DEV_INTERFACE_0_1;
41 } // namespace
44 TextInput_Dev::TextInput_Dev(Instance* instance)
45 : instance_(instance) {
46 Module::Get()->AddPluginInterface(kPPPTextInputInterface,
47 &ppp_text_input);
48 instance->AddPerInstanceObject(kPPPTextInputInterface, this);
51 TextInput_Dev::~TextInput_Dev() {
52 Instance::RemovePerInstanceObject(instance_, kPPPTextInputInterface, this);
55 void TextInput_Dev::RequestSurroundingText(uint32_t) {
56 // Default implementation. Send a null range.
57 UpdateSurroundingText(std::string(), 0, 0);
60 void TextInput_Dev::SetTextInputType(PP_TextInput_Type_Dev type) {
61 if (has_interface<PPB_TextInput_Dev_0_2>()) {
62 get_interface<PPB_TextInput_Dev_0_2>()->SetTextInputType(
63 instance_.pp_instance(), type);
64 } else if (has_interface<PPB_TextInput_Dev_0_1>()) {
65 get_interface<PPB_TextInput_Dev_0_1>()->SetTextInputType(
66 instance_.pp_instance(), type);
70 void TextInput_Dev::UpdateCaretPosition(const Rect& caret,
71 const Rect& bounding_box) {
72 if (has_interface<PPB_TextInput_Dev_0_2>()) {
73 get_interface<PPB_TextInput_Dev_0_2>()->UpdateCaretPosition(
74 instance_.pp_instance(), &caret.pp_rect(), &bounding_box.pp_rect());
75 } else if (has_interface<PPB_TextInput_Dev_0_1>()) {
76 get_interface<PPB_TextInput_Dev_0_1>()->UpdateCaretPosition(
77 instance_.pp_instance(), &caret.pp_rect(), &bounding_box.pp_rect());
81 void TextInput_Dev::CancelCompositionText() {
82 if (has_interface<PPB_TextInput_Dev_0_2>()) {
83 get_interface<PPB_TextInput_Dev_0_2>()->CancelCompositionText(
84 instance_.pp_instance());
85 } else if (has_interface<PPB_TextInput_Dev_0_1>()) {
86 get_interface<PPB_TextInput_Dev_0_1>()->CancelCompositionText(
87 instance_.pp_instance());
91 void TextInput_Dev::SelectionChanged() {
92 if (has_interface<PPB_TextInput_Dev_0_2>()) {
93 get_interface<PPB_TextInput_Dev_0_2>()->SelectionChanged(
94 instance_.pp_instance());
98 void TextInput_Dev::UpdateSurroundingText(const std::string& text,
99 uint32_t caret,
100 uint32_t anchor) {
101 if (has_interface<PPB_TextInput_Dev_0_2>()) {
102 get_interface<PPB_TextInput_Dev_0_2>()->UpdateSurroundingText(
103 instance_.pp_instance(), text.c_str(), caret, anchor);
108 } // namespace pp