1 // Copyright 2016 The Chromium Embedded Framework Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be found
3 // in the LICENSE file.
4
5 #include "libcef/browser/views/textfield_impl.h"
6
7 #include "libcef/browser/thread_util.h"
8
9 namespace {
CefCommandIdToChromeId(cef_text_field_commands_t command_id)10 static int CefCommandIdToChromeId(cef_text_field_commands_t command_id) {
11 switch (command_id) {
12 case cef_text_field_commands_t::CEF_TFC_CUT:
13 return views::Textfield::kCut;
14 case cef_text_field_commands_t::CEF_TFC_COPY:
15 return views::Textfield::kCopy;
16 case cef_text_field_commands_t::CEF_TFC_PASTE:
17 return views::Textfield::kPaste;
18 case cef_text_field_commands_t::CEF_TFC_UNDO:
19 return views::Textfield::kUndo;
20 case cef_text_field_commands_t::CEF_TFC_DELETE:
21 return views::Textfield::kDelete;
22 case cef_text_field_commands_t::CEF_TFC_SELECT_ALL:
23 return views::Textfield::kSelectAll;
24 }
25 }
26 } // namespace
27
28 // static
CreateTextfield(CefRefPtr<CefTextfieldDelegate> delegate)29 CefRefPtr<CefTextfield> CefTextfield::CreateTextfield(
30 CefRefPtr<CefTextfieldDelegate> delegate) {
31 return CefTextfieldImpl::Create(delegate);
32 }
33
34 // static
Create(CefRefPtr<CefTextfieldDelegate> delegate)35 CefRefPtr<CefTextfieldImpl> CefTextfieldImpl::Create(
36 CefRefPtr<CefTextfieldDelegate> delegate) {
37 CEF_REQUIRE_UIT_RETURN(nullptr);
38 CefRefPtr<CefTextfieldImpl> textfield = new CefTextfieldImpl(delegate);
39 textfield->Initialize();
40 return textfield;
41 }
42
SetPasswordInput(bool password_input)43 void CefTextfieldImpl::SetPasswordInput(bool password_input) {
44 CEF_REQUIRE_VALID_RETURN_VOID();
45 root_view()->SetTextInputType(password_input ? ui::TEXT_INPUT_TYPE_PASSWORD
46 : ui::TEXT_INPUT_TYPE_TEXT);
47 }
48
IsPasswordInput()49 bool CefTextfieldImpl::IsPasswordInput() {
50 CEF_REQUIRE_VALID_RETURN(false);
51 return (root_view()->GetTextInputType() == ui::TEXT_INPUT_TYPE_PASSWORD);
52 }
53
SetReadOnly(bool read_only)54 void CefTextfieldImpl::SetReadOnly(bool read_only) {
55 CEF_REQUIRE_VALID_RETURN_VOID();
56 root_view()->SetReadOnly(read_only);
57 }
58
IsReadOnly()59 bool CefTextfieldImpl::IsReadOnly() {
60 CEF_REQUIRE_VALID_RETURN(false);
61 return root_view()->GetReadOnly();
62 }
63
GetText()64 CefString CefTextfieldImpl::GetText() {
65 CEF_REQUIRE_VALID_RETURN(CefString());
66 return root_view()->GetText();
67 }
68
SetText(const CefString & text)69 void CefTextfieldImpl::SetText(const CefString& text) {
70 CEF_REQUIRE_VALID_RETURN_VOID();
71 root_view()->SetText(text);
72 }
73
AppendText(const CefString & text)74 void CefTextfieldImpl::AppendText(const CefString& text) {
75 CEF_REQUIRE_VALID_RETURN_VOID();
76 root_view()->AppendText(text);
77 }
78
InsertOrReplaceText(const CefString & text)79 void CefTextfieldImpl::InsertOrReplaceText(const CefString& text) {
80 CEF_REQUIRE_VALID_RETURN_VOID();
81 root_view()->InsertOrReplaceText(text);
82 }
83
HasSelection()84 bool CefTextfieldImpl::HasSelection() {
85 CEF_REQUIRE_VALID_RETURN(false);
86 return root_view()->HasSelection();
87 }
88
GetSelectedText()89 CefString CefTextfieldImpl::GetSelectedText() {
90 CEF_REQUIRE_VALID_RETURN(CefString());
91 return root_view()->GetSelectedText();
92 }
93
SelectAll(bool reversed)94 void CefTextfieldImpl::SelectAll(bool reversed) {
95 CEF_REQUIRE_VALID_RETURN_VOID();
96 root_view()->SelectAll(reversed);
97 }
98
ClearSelection()99 void CefTextfieldImpl::ClearSelection() {
100 CEF_REQUIRE_VALID_RETURN_VOID();
101 root_view()->ClearSelection();
102 }
103
GetSelectedRange()104 CefRange CefTextfieldImpl::GetSelectedRange() {
105 CEF_REQUIRE_VALID_RETURN(CefRange());
106 const gfx::Range& range = root_view()->GetSelectedRange();
107 return CefRange(range.start(), range.end());
108 }
109
SelectRange(const CefRange & range)110 void CefTextfieldImpl::SelectRange(const CefRange& range) {
111 CEF_REQUIRE_VALID_RETURN_VOID();
112 root_view()->SetSelectedRange(gfx::Range(range.from, range.to));
113 }
114
GetCursorPosition()115 size_t CefTextfieldImpl::GetCursorPosition() {
116 CEF_REQUIRE_VALID_RETURN(0U);
117 return root_view()->GetCursorPosition();
118 }
119
SetTextColor(cef_color_t color)120 void CefTextfieldImpl::SetTextColor(cef_color_t color) {
121 CEF_REQUIRE_VALID_RETURN_VOID();
122 root_view()->SetTextColor(color);
123 }
124
GetTextColor()125 cef_color_t CefTextfieldImpl::GetTextColor() {
126 CEF_REQUIRE_VALID_RETURN(0U);
127 return root_view()->GetTextColor();
128 }
129
SetSelectionTextColor(cef_color_t color)130 void CefTextfieldImpl::SetSelectionTextColor(cef_color_t color) {
131 CEF_REQUIRE_VALID_RETURN_VOID();
132 root_view()->SetSelectionTextColor(color);
133 }
134
GetSelectionTextColor()135 cef_color_t CefTextfieldImpl::GetSelectionTextColor() {
136 CEF_REQUIRE_VALID_RETURN(0U);
137 return root_view()->GetSelectionTextColor();
138 }
139
SetSelectionBackgroundColor(cef_color_t color)140 void CefTextfieldImpl::SetSelectionBackgroundColor(cef_color_t color) {
141 CEF_REQUIRE_VALID_RETURN_VOID();
142 root_view()->SetSelectionBackgroundColor(color);
143 }
144
GetSelectionBackgroundColor()145 cef_color_t CefTextfieldImpl::GetSelectionBackgroundColor() {
146 CEF_REQUIRE_VALID_RETURN(0U);
147 return root_view()->GetSelectionBackgroundColor();
148 }
149
SetFontList(const CefString & font_list)150 void CefTextfieldImpl::SetFontList(const CefString& font_list) {
151 CEF_REQUIRE_VALID_RETURN_VOID();
152 root_view()->SetFontList(gfx::FontList(font_list));
153 }
154
ApplyTextColor(cef_color_t color,const CefRange & range)155 void CefTextfieldImpl::ApplyTextColor(cef_color_t color,
156 const CefRange& range) {
157 CEF_REQUIRE_VALID_RETURN_VOID();
158 if (range.from == range.to)
159 root_view()->SetColor(color);
160 else
161 root_view()->ApplyColor(color, gfx::Range(range.from, range.to));
162 }
163
ApplyTextStyle(cef_text_style_t style,bool add,const CefRange & range)164 void CefTextfieldImpl::ApplyTextStyle(cef_text_style_t style,
165 bool add,
166 const CefRange& range) {
167 CEF_REQUIRE_VALID_RETURN_VOID();
168 if (range.from == range.to) {
169 root_view()->SetStyle(static_cast<gfx::TextStyle>(style), add);
170 } else {
171 root_view()->ApplyStyle(static_cast<gfx::TextStyle>(style), add,
172 gfx::Range(range.from, range.to));
173 }
174 }
175
IsCommandEnabled(cef_text_field_commands_t command_id)176 bool CefTextfieldImpl::IsCommandEnabled(cef_text_field_commands_t command_id) {
177 CEF_REQUIRE_VALID_RETURN(false);
178 return root_view()->IsCommandIdEnabled(CefCommandIdToChromeId(command_id));
179 }
180
ExecuteCommand(cef_text_field_commands_t command_id)181 void CefTextfieldImpl::ExecuteCommand(cef_text_field_commands_t command_id) {
182 CEF_REQUIRE_VALID_RETURN_VOID();
183 if (root_view()->IsCommandIdEnabled(CefCommandIdToChromeId(command_id)))
184 root_view()->ExecuteCommand(CefCommandIdToChromeId(command_id),
185 ui::EF_NONE);
186 }
187
ClearEditHistory()188 void CefTextfieldImpl::ClearEditHistory() {
189 CEF_REQUIRE_VALID_RETURN_VOID();
190 root_view()->ClearEditHistory();
191 }
192
SetPlaceholderText(const CefString & text)193 void CefTextfieldImpl::SetPlaceholderText(const CefString& text) {
194 CEF_REQUIRE_VALID_RETURN_VOID();
195 root_view()->SetPlaceholderText(text);
196 }
197
GetPlaceholderText()198 CefString CefTextfieldImpl::GetPlaceholderText() {
199 CEF_REQUIRE_VALID_RETURN(CefString());
200 return root_view()->GetPlaceholderText();
201 }
202
SetPlaceholderTextColor(cef_color_t color)203 void CefTextfieldImpl::SetPlaceholderTextColor(cef_color_t color) {
204 CEF_REQUIRE_VALID_RETURN_VOID();
205 root_view()->set_placeholder_text_color(color);
206 }
207
SetBackgroundColor(cef_color_t color)208 void CefTextfieldImpl::SetBackgroundColor(cef_color_t color) {
209 CEF_REQUIRE_VALID_RETURN_VOID();
210 root_view()->SetBackgroundColor(color);
211 }
212
GetBackgroundColor()213 cef_color_t CefTextfieldImpl::GetBackgroundColor() {
214 CEF_REQUIRE_VALID_RETURN(0U);
215 return root_view()->GetBackgroundColor();
216 }
217
SetAccessibleName(const CefString & name)218 void CefTextfieldImpl::SetAccessibleName(const CefString& name) {
219 CEF_REQUIRE_VALID_RETURN_VOID();
220 root_view()->SetAccessibleName(name);
221 }
222
CefTextfieldImpl(CefRefPtr<CefTextfieldDelegate> delegate)223 CefTextfieldImpl::CefTextfieldImpl(CefRefPtr<CefTextfieldDelegate> delegate)
224 : ParentClass(delegate) {}
225
CreateRootView()226 CefTextfieldView* CefTextfieldImpl::CreateRootView() {
227 return new CefTextfieldView(delegate());
228 }
229
InitializeRootView()230 void CefTextfieldImpl::InitializeRootView() {
231 static_cast<CefTextfieldView*>(root_view())->Initialize();
232 }
233