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 "views/controls/textfield/native_textfield_win.h" | 5 #include "views/controls/textfield/native_textfield_win.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/i18n/case_conversion.h" | 9 #include "base/i18n/case_conversion.h" |
10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 // See the code in textfield.cc that calls this for why this is here. | 166 // See the code in textfield.cc that calls this for why this is here. |
167 container_view_->set_focus_view(textfield_); | 167 container_view_->set_focus_view(textfield_); |
168 container_view_->Attach(m_hWnd); | 168 container_view_->Attach(m_hWnd); |
169 } | 169 } |
170 | 170 |
171 //////////////////////////////////////////////////////////////////////////////// | 171 //////////////////////////////////////////////////////////////////////////////// |
172 // NativeTextfieldWin, NativeTextfieldWrapper implementation: | 172 // NativeTextfieldWin, NativeTextfieldWrapper implementation: |
173 | 173 |
174 string16 NativeTextfieldWin::GetText() const { | 174 string16 NativeTextfieldWin::GetText() const { |
175 int len = GetTextLength() + 1; | 175 int len = GetTextLength() + 1; |
| 176 if (len <= 1) |
| 177 return string16(); |
| 178 |
176 string16 str; | 179 string16 str; |
177 GetWindowText(WriteInto(&str, len), len); | 180 GetWindowText(WriteInto(&str, len), len); |
178 // The text get from GetWindowText() might be wrapped with explicit bidi | 181 // The text get from GetWindowText() might be wrapped with explicit bidi |
179 // control characters. Refer to UpdateText() for detail. Without such | 182 // control characters. Refer to UpdateText() for detail. Without such |
180 // wrapping, in RTL chrome, a pure LTR string ending with parenthesis will | 183 // wrapping, in RTL chrome, a pure LTR string ending with parenthesis will |
181 // not be displayed correctly in a textfield. For example, "Yahoo!" will be | 184 // not be displayed correctly in a textfield. For example, "Yahoo!" will be |
182 // displayed as "!Yahoo", and "Google (by default)" will be displayed as | 185 // displayed as "!Yahoo", and "Google (by default)" will be displayed as |
183 // "(Google (by default". | 186 // "(Google (by default". |
184 return base::i18n::StripWrappingBidiControlCharacters(WideToUTF16(str)); | 187 return base::i18n::StripWrappingBidiControlCharacters(WideToUTF16(str)); |
185 } | 188 } |
(...skipping 11 matching lines...) Expand all Loading... |
197 | 200 |
198 void NativeTextfieldWin::AppendText(const string16& text) { | 201 void NativeTextfieldWin::AppendText(const string16& text) { |
199 int text_length = GetWindowTextLength(); | 202 int text_length = GetWindowTextLength(); |
200 ::SendMessage(m_hWnd, TBM_SETSEL, true, MAKELPARAM(text_length, text_length)); | 203 ::SendMessage(m_hWnd, TBM_SETSEL, true, MAKELPARAM(text_length, text_length)); |
201 ::SendMessage(m_hWnd, EM_REPLACESEL, false, | 204 ::SendMessage(m_hWnd, EM_REPLACESEL, false, |
202 reinterpret_cast<LPARAM>(text.c_str())); | 205 reinterpret_cast<LPARAM>(text.c_str())); |
203 } | 206 } |
204 | 207 |
205 string16 NativeTextfieldWin::GetSelectedText() const { | 208 string16 NativeTextfieldWin::GetSelectedText() const { |
206 // Figure out the length of the selection. | 209 // Figure out the length of the selection. |
207 long start; | 210 CHARRANGE sel; |
208 long end; | 211 GetSel(sel); |
209 GetSel(start, end); | 212 if (sel.cpMin == sel.cpMax) // GetSelText() crashes on NULL input. |
| 213 return string16(); |
210 | 214 |
211 // Grab the selected text. | 215 // Grab the selected text. |
212 string16 str; | 216 string16 str; |
213 long length = end - start; | 217 GetSelText(WriteInto(&str, sel.cpMax - sel.cpMin + 1)); |
214 if (length > 0) | |
215 GetSelText(WriteInto(&str, length + 1)); | |
216 | |
217 return str; | 218 return str; |
218 } | 219 } |
219 | 220 |
220 void NativeTextfieldWin::SelectAll() { | 221 void NativeTextfieldWin::SelectAll() { |
221 // Select from the end to the front so that the first part of the text is | 222 // Select from the end to the front so that the first part of the text is |
222 // always visible. | 223 // always visible. |
223 SetSel(GetTextLength(), 0); | 224 SetSel(GetTextLength(), 0); |
224 } | 225 } |
225 | 226 |
226 void NativeTextfieldWin::ClearSelection() { | 227 void NativeTextfieldWin::ClearSelection() { |
(...skipping 931 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1158 | 1159 |
1159 // static | 1160 // static |
1160 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( | 1161 NativeTextfieldWrapper* NativeTextfieldWrapper::CreateWrapper( |
1161 Textfield* field) { | 1162 Textfield* field) { |
1162 if (views::Widget::IsPureViews()) | 1163 if (views::Widget::IsPureViews()) |
1163 return new NativeTextfieldViews(field); | 1164 return new NativeTextfieldViews(field); |
1164 return new NativeTextfieldWin(field); | 1165 return new NativeTextfieldWin(field); |
1165 } | 1166 } |
1166 | 1167 |
1167 } // namespace views | 1168 } // namespace views |
OLD | NEW |