| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_views.h" | 5 #include "views/controls/textfield/native_textfield_views.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 "views/NativeTextfieldViews"; | 50 "views/NativeTextfieldViews"; |
| 51 | 51 |
| 52 NativeTextfieldViews::NativeTextfieldViews(Textfield* parent) | 52 NativeTextfieldViews::NativeTextfieldViews(Textfield* parent) |
| 53 : textfield_(parent), | 53 : textfield_(parent), |
| 54 model_(new TextfieldViewsModel()), | 54 model_(new TextfieldViewsModel()), |
| 55 text_border_(new TextfieldBorder()), | 55 text_border_(new TextfieldBorder()), |
| 56 text_offset_(0), | 56 text_offset_(0), |
| 57 insert_(true), | 57 insert_(true), |
| 58 is_cursor_visible_(false), | 58 is_cursor_visible_(false), |
| 59 ALLOW_THIS_IN_INITIALIZER_LIST(cursor_timer_(this)) { | 59 ALLOW_THIS_IN_INITIALIZER_LIST(cursor_timer_(this)) { |
| 60 SetFocusable(true); | |
| 61 set_border(text_border_); | 60 set_border(text_border_); |
| 62 | 61 |
| 63 // Multiline is not supported. | 62 // Multiline is not supported. |
| 64 DCHECK_NE(parent->style(), Textfield::STYLE_MULTILINE); | 63 DCHECK_NE(parent->style(), Textfield::STYLE_MULTILINE); |
| 65 // Lowercase is not supported. | 64 // Lowercase is not supported. |
| 66 DCHECK_NE(parent->style(), Textfield::STYLE_LOWERCASE); | 65 DCHECK_NE(parent->style(), Textfield::STYLE_LOWERCASE); |
| 67 } | 66 } |
| 68 | 67 |
| 69 NativeTextfieldViews::~NativeTextfieldViews() { | 68 NativeTextfieldViews::~NativeTextfieldViews() { |
| 70 } | 69 } |
| 71 | 70 |
| 72 //////////////////////////////////////////////////////////////////////////////// | 71 //////////////////////////////////////////////////////////////////////////////// |
| 73 // NativeTextfieldViews, View overrides: | 72 // NativeTextfieldViews, View overrides: |
| 74 | 73 |
| 75 bool NativeTextfieldViews::OnMousePressed(const views::MouseEvent& e) { | 74 bool NativeTextfieldViews::OnMousePressed(const views::MouseEvent& e) { |
| 76 RequestFocus(); | 75 textfield_->RequestFocus(); |
| 77 size_t pos = FindCursorPosition(e.location()); | 76 size_t pos = FindCursorPosition(e.location()); |
| 78 if (model_->MoveCursorTo(pos, false)) { | 77 if (model_->MoveCursorTo(pos, false)) { |
| 79 UpdateCursorBoundsAndTextOffset(); | 78 UpdateCursorBoundsAndTextOffset(); |
| 80 SchedulePaint(); | 79 SchedulePaint(); |
| 81 } | 80 } |
| 82 return true; | 81 return true; |
| 83 } | 82 } |
| 84 | 83 |
| 85 bool NativeTextfieldViews::OnMouseDragged(const views::MouseEvent& e) { | 84 bool NativeTextfieldViews::OnMouseDragged(const views::MouseEvent& e) { |
| 86 size_t pos = FindCursorPosition(e.location()); | 85 size_t pos = FindCursorPosition(e.location()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 103 handled = controller->HandleKeystroke(textfield_, ks); | 102 handled = controller->HandleKeystroke(textfield_, ks); |
| 104 } | 103 } |
| 105 return handled || HandleKeyEvent(e); | 104 return handled || HandleKeyEvent(e); |
| 106 } | 105 } |
| 107 | 106 |
| 108 bool NativeTextfieldViews::OnKeyReleased(const views::KeyEvent& e) { | 107 bool NativeTextfieldViews::OnKeyReleased(const views::KeyEvent& e) { |
| 109 return true; | 108 return true; |
| 110 } | 109 } |
| 111 | 110 |
| 112 void NativeTextfieldViews::Paint(gfx::Canvas* canvas) { | 111 void NativeTextfieldViews::Paint(gfx::Canvas* canvas) { |
| 113 text_border_->set_has_focus(HasFocus()); | 112 text_border_->set_has_focus(textfield_->HasFocus()); |
| 114 PaintBackground(canvas); | 113 PaintBackground(canvas); |
| 115 PaintTextAndCursor(canvas); | 114 PaintTextAndCursor(canvas); |
| 116 if (textfield_->draw_border()) | 115 if (textfield_->draw_border()) |
| 117 PaintBorder(canvas); | 116 PaintBorder(canvas); |
| 118 } | 117 } |
| 119 | 118 |
| 120 void NativeTextfieldViews::WillGainFocus() { | 119 void NativeTextfieldViews::WillGainFocus() { |
| 121 } | 120 } |
| 122 | 121 |
| 123 void NativeTextfieldViews::DidGainFocus() { | 122 void NativeTextfieldViews::DidGainFocus() { |
| 124 is_cursor_visible_ = true; | 123 is_cursor_visible_ = true; |
| 125 SchedulePaint(); | 124 SchedulePaint(); |
| 126 // Start blinking cursor. | 125 // Start blinking cursor. |
| 127 MessageLoop::current()->PostDelayedTask( | 126 MessageLoop::current()->PostDelayedTask( |
| 128 FROM_HERE, | 127 FROM_HERE, |
| 129 cursor_timer_.NewRunnableMethod(&NativeTextfieldViews::UpdateCursor), | 128 cursor_timer_.NewRunnableMethod(&NativeTextfieldViews::UpdateCursor), |
| 130 kCursorVisibleTimeMs); | 129 kCursorVisibleTimeMs); |
| 131 } | 130 } |
| 132 | 131 |
| 133 void NativeTextfieldViews::WillLoseFocus() { | 132 void NativeTextfieldViews::WillLoseFocus() { |
| 134 // Stop blinking cursor. | 133 // Stop blinking cursor. |
| 135 cursor_timer_.RevokeAll(); | 134 cursor_timer_.RevokeAll(); |
| 136 if (is_cursor_visible_) { | 135 if (is_cursor_visible_) { |
| 137 is_cursor_visible_ = false; | 136 is_cursor_visible_ = false; |
| 138 RepaintCursor(); | 137 RepaintCursor(); |
| 139 } | 138 } |
| 140 } | 139 } |
| 141 | 140 |
| 142 void NativeTextfieldViews::DidChangeBounds(const gfx::Rect& previous, | 141 void NativeTextfieldViews::DidChangeBounds(const gfx::Rect& previous, |
| 143 const gfx::Rect& current) { | 142 const gfx::Rect& current) { |
| 144 UpdateCursorBoundsAndTextOffset(); | 143 UpdateCursorBoundsAndTextOffset(); |
| 145 } | 144 } |
| 146 | 145 |
| 147 ///////////////////////////////////////////////////////////////// | 146 ///////////////////////////////////////////////////////////////// |
| 148 // NativeTextfieldViews, NativeTextifieldWrapper overrides: | 147 // NativeTextfieldViews, NativeTextifieldWrapper overrides: |
| 149 | 148 |
| 150 string16 NativeTextfieldViews::GetText() const { | 149 string16 NativeTextfieldViews::GetText() const { |
| 151 return model_->text(); | 150 return model_->text(); |
| 152 } | 151 } |
| 153 | 152 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 UpdateCursorBoundsAndTextOffset(); | 217 UpdateCursorBoundsAndTextOffset(); |
| 219 } | 218 } |
| 220 | 219 |
| 221 void NativeTextfieldViews::UpdateIsPassword() { | 220 void NativeTextfieldViews::UpdateIsPassword() { |
| 222 model_->set_is_password(textfield_->IsPassword()); | 221 model_->set_is_password(textfield_->IsPassword()); |
| 223 UpdateCursorBoundsAndTextOffset(); | 222 UpdateCursorBoundsAndTextOffset(); |
| 224 SchedulePaint(); | 223 SchedulePaint(); |
| 225 } | 224 } |
| 226 | 225 |
| 227 void NativeTextfieldViews::UpdateEnabled() { | 226 void NativeTextfieldViews::UpdateEnabled() { |
| 227 SetEnabled(textfield_->IsEnabled()); |
| 228 SchedulePaint(); | 228 SchedulePaint(); |
| 229 } | 229 } |
| 230 | 230 |
| 231 bool NativeTextfieldViews::IsPassword() { | |
| 232 // looks unnecessary. should we remove? | |
| 233 NOTREACHED(); | |
| 234 return false; | |
| 235 } | |
| 236 | |
| 237 gfx::Insets NativeTextfieldViews::CalculateInsets() { | 231 gfx::Insets NativeTextfieldViews::CalculateInsets() { |
| 238 return GetInsets(); | 232 return GetInsets(); |
| 239 } | 233 } |
| 240 | 234 |
| 241 void NativeTextfieldViews::UpdateHorizontalMargins() { | 235 void NativeTextfieldViews::UpdateHorizontalMargins() { |
| 242 int left, right; | 236 int left, right; |
| 243 if (!textfield_->GetHorizontalMargins(&left, &right)) | 237 if (!textfield_->GetHorizontalMargins(&left, &right)) |
| 244 return; | 238 return; |
| 245 gfx::Insets inset = GetInsets(); | 239 gfx::Insets inset = GetInsets(); |
| 246 | 240 |
| 247 text_border_->SetInsets(inset.top(), left, inset.bottom(), right); | 241 text_border_->SetInsets(inset.top(), left, inset.bottom(), right); |
| 248 UpdateCursorBoundsAndTextOffset(); | 242 UpdateCursorBoundsAndTextOffset(); |
| 249 } | 243 } |
| 250 | 244 |
| 251 void NativeTextfieldViews::UpdateVerticalMargins() { | 245 void NativeTextfieldViews::UpdateVerticalMargins() { |
| 252 int top, bottom; | 246 int top, bottom; |
| 253 if (!textfield_->GetVerticalMargins(&top, &bottom)) | 247 if (!textfield_->GetVerticalMargins(&top, &bottom)) |
| 254 return; | 248 return; |
| 255 gfx::Insets inset = GetInsets(); | 249 gfx::Insets inset = GetInsets(); |
| 256 | 250 |
| 257 text_border_->SetInsets(top, inset.left(), bottom, inset.right()); | 251 text_border_->SetInsets(top, inset.left(), bottom, inset.right()); |
| 258 UpdateCursorBoundsAndTextOffset(); | 252 UpdateCursorBoundsAndTextOffset(); |
| 259 } | 253 } |
| 260 | 254 |
| 261 void NativeTextfieldViews::SetFocus() { | 255 bool NativeTextfieldViews::SetFocus() { |
| 262 RequestFocus(); | 256 return false; |
| 263 } | 257 } |
| 264 | 258 |
| 265 View* NativeTextfieldViews::GetView() { | 259 View* NativeTextfieldViews::GetView() { |
| 266 return this; | 260 return this; |
| 267 } | 261 } |
| 268 | 262 |
| 269 gfx::NativeView NativeTextfieldViews::GetTestingHandle() const { | 263 gfx::NativeView NativeTextfieldViews::GetTestingHandle() const { |
| 270 NOTREACHED(); | 264 NOTREACHED(); |
| 271 return NULL; | 265 return NULL; |
| 272 } | 266 } |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 width() - insets.width(), height() - insets.height()); | 357 width() - insets.width(), height() - insets.height()); |
| 364 | 358 |
| 365 // TODO(oshima): bidi support | 359 // TODO(oshima): bidi support |
| 366 // TODO(varunjain): re-implement this so only that dirty text is painted. | 360 // TODO(varunjain): re-implement this so only that dirty text is painted. |
| 367 TextfieldViewsModel::TextFragments fragments; | 361 TextfieldViewsModel::TextFragments fragments; |
| 368 model_->GetFragments(&fragments); | 362 model_->GetFragments(&fragments); |
| 369 int x_offset = text_offset_ + insets.left(); | 363 int x_offset = text_offset_ + insets.left(); |
| 370 int y = insets.top(); | 364 int y = insets.top(); |
| 371 int text_height = height() - insets.height(); | 365 int text_height = height() - insets.height(); |
| 372 SkColor selection_color = | 366 SkColor selection_color = |
| 373 HasFocus() ? kFocusedSelectionColor : kUnfocusedSelectionColor; | 367 textfield_->HasFocus() ? |
| 368 kFocusedSelectionColor : kUnfocusedSelectionColor; |
| 374 SkColor text_color = | 369 SkColor text_color = |
| 375 textfield_->read_only() ? kReadonlyTextColor : GetTextColor(); | 370 textfield_->read_only() ? kReadonlyTextColor : GetTextColor(); |
| 376 | 371 |
| 377 for (TextfieldViewsModel::TextFragments::const_iterator iter = | 372 for (TextfieldViewsModel::TextFragments::const_iterator iter = |
| 378 fragments.begin(); | 373 fragments.begin(); |
| 379 iter != fragments.end(); | 374 iter != fragments.end(); |
| 380 iter++) { | 375 iter++) { |
| 381 string16 text = model_->GetVisibleText((*iter).begin, (*iter).end); | 376 string16 text = model_->GetVisibleText((*iter).begin, (*iter).end); |
| 382 // TODO(oshima): This does not give the accurate position due to | 377 // TODO(oshima): This does not give the accurate position due to |
| 383 // kerning. Figure out how webkit does this with skia. | 378 // kerning. Figure out how webkit does this with skia. |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 695 } | 690 } |
| 696 | 691 |
| 697 void NativeTextfieldViews::TextfieldBorder::SetInsets(int top, | 692 void NativeTextfieldViews::TextfieldBorder::SetInsets(int top, |
| 698 int left, | 693 int left, |
| 699 int bottom, | 694 int bottom, |
| 700 int right) { | 695 int right) { |
| 701 insets_.Set(top, left, bottom, right); | 696 insets_.Set(top, left, bottom, right); |
| 702 } | 697 } |
| 703 | 698 |
| 704 } // namespace views | 699 } // namespace views |
| OLD | NEW |