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_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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 const char NativeTextfieldViews::kViewClassName[] = | 52 const char NativeTextfieldViews::kViewClassName[] = |
53 "views/NativeTextfieldViews"; | 53 "views/NativeTextfieldViews"; |
54 | 54 |
55 NativeTextfieldViews::NativeTextfieldViews(Textfield* parent) | 55 NativeTextfieldViews::NativeTextfieldViews(Textfield* parent) |
56 : textfield_(parent), | 56 : textfield_(parent), |
57 model_(new TextfieldViewsModel()), | 57 model_(new TextfieldViewsModel()), |
58 text_border_(new TextfieldBorder()), | 58 text_border_(new TextfieldBorder()), |
59 text_offset_(0), | 59 text_offset_(0), |
60 insert_(true), | 60 insert_(true), |
61 is_cursor_visible_(false), | 61 is_cursor_visible_(false), |
62 ALLOW_THIS_IN_INITIALIZER_LIST(cursor_timer_(this)) { | 62 ALLOW_THIS_IN_INITIALIZER_LIST(cursor_timer_(this)), |
| 63 last_mouse_press_time_(base::Time::FromInternalValue(0)), |
| 64 click_state_(NONE) { |
63 set_border(text_border_); | 65 set_border(text_border_); |
64 | 66 |
65 // Multiline is not supported. | 67 // Multiline is not supported. |
66 DCHECK_NE(parent->style(), Textfield::STYLE_MULTILINE); | 68 DCHECK_NE(parent->style(), Textfield::STYLE_MULTILINE); |
67 // Lowercase is not supported. | 69 // Lowercase is not supported. |
68 DCHECK_NE(parent->style(), Textfield::STYLE_LOWERCASE); | 70 DCHECK_NE(parent->style(), Textfield::STYLE_LOWERCASE); |
69 | 71 |
70 SetContextMenuController(this); | 72 SetContextMenuController(this); |
71 } | 73 } |
72 | 74 |
73 NativeTextfieldViews::~NativeTextfieldViews() { | 75 NativeTextfieldViews::~NativeTextfieldViews() { |
74 } | 76 } |
75 | 77 |
76 //////////////////////////////////////////////////////////////////////////////// | 78 //////////////////////////////////////////////////////////////////////////////// |
77 // NativeTextfieldViews, View overrides: | 79 // NativeTextfieldViews, View overrides: |
78 | 80 |
79 bool NativeTextfieldViews::OnMousePressed(const views::MouseEvent& e) { | 81 bool NativeTextfieldViews::OnMousePressed(const views::MouseEvent& e) { |
80 textfield_->RequestFocus(); | 82 if (HandleMousePressed(e)) |
81 if (e.IsLeftMouseButton()) { | 83 SchedulePaint(); |
82 size_t pos = FindCursorPosition(e.location()); | |
83 if (model_->MoveCursorTo(pos, false)) { | |
84 UpdateCursorBoundsAndTextOffset(); | |
85 SchedulePaint(); | |
86 } | |
87 } | |
88 return true; | 84 return true; |
89 } | 85 } |
90 | 86 |
91 bool NativeTextfieldViews::OnMouseDragged(const views::MouseEvent& e) { | 87 bool NativeTextfieldViews::OnMouseDragged(const views::MouseEvent& e) { |
92 size_t pos = FindCursorPosition(e.location()); | 88 size_t pos = FindCursorPosition(e.location()); |
93 if (model_->MoveCursorTo(pos, true)) { | 89 if (model_->MoveCursorTo(pos, true)) { |
94 UpdateCursorBoundsAndTextOffset(); | 90 UpdateCursorBoundsAndTextOffset(); |
95 SchedulePaint(); | 91 SchedulePaint(); |
96 } | 92 } |
97 return true; | 93 return true; |
(...skipping 679 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 } else if (pivot == x) { | 773 } else if (pivot == x) { |
778 return pivot_pos; | 774 return pivot_pos; |
779 } else { | 775 } else { |
780 right = pivot; | 776 right = pivot; |
781 right_pos = pivot_pos; | 777 right_pos = pivot_pos; |
782 } | 778 } |
783 } | 779 } |
784 return left_pos; | 780 return left_pos; |
785 } | 781 } |
786 | 782 |
| 783 bool NativeTextfieldViews::HandleMousePressed(const views::MouseEvent& e) { |
| 784 textfield_->RequestFocus(); |
| 785 base::TimeDelta time_delta = e.GetTimeStamp() - last_mouse_press_time_; |
| 786 gfx::Point location_delta = e.location().Subtract(last_mouse_press_location_); |
| 787 last_mouse_press_time_ = e.GetTimeStamp(); |
| 788 last_mouse_press_location_ = e.location(); |
| 789 if (e.IsLeftMouseButton()) { |
| 790 if (!ExceededDragThreshold(location_delta.x(), location_delta.y()) |
| 791 && time_delta.InMilliseconds() <= GetDoubleClickTimeMS()) { |
| 792 // Multiple mouse press detected. Check for double or triple. |
| 793 switch (click_state_) { |
| 794 case TRACKING_DOUBLE_CLICK: |
| 795 click_state_ = TRACKING_TRIPLE_CLICK; |
| 796 model_->SelectWord(); |
| 797 return true; |
| 798 case TRACKING_TRIPLE_CLICK: |
| 799 click_state_ = NONE; |
| 800 model_->SelectAll(); |
| 801 return true; |
| 802 case NONE: |
| 803 click_state_ = TRACKING_DOUBLE_CLICK; |
| 804 SetCursorForMouseClick(e); |
| 805 return true; |
| 806 } |
| 807 } else { |
| 808 // Single mouse press. |
| 809 click_state_ = TRACKING_DOUBLE_CLICK; |
| 810 SetCursorForMouseClick(e); |
| 811 return true; |
| 812 } |
| 813 } |
| 814 return false; |
| 815 } |
| 816 |
| 817 void NativeTextfieldViews::SetCursorForMouseClick(const views::MouseEvent& e) { |
| 818 size_t pos = FindCursorPosition(e.location()); |
| 819 if (model_->MoveCursorTo(pos, false)) { |
| 820 UpdateCursorBoundsAndTextOffset(); |
| 821 } |
| 822 } |
| 823 |
787 void NativeTextfieldViews::PropagateTextChange() { | 824 void NativeTextfieldViews::PropagateTextChange() { |
788 textfield_->SyncText(); | 825 textfield_->SyncText(); |
789 Textfield::Controller* controller = textfield_->GetController(); | 826 Textfield::Controller* controller = textfield_->GetController(); |
790 if (controller) | 827 if (controller) |
791 controller->ContentsChanged(textfield_, GetText()); | 828 controller->ContentsChanged(textfield_, GetText()); |
792 } | 829 } |
793 | 830 |
794 void NativeTextfieldViews::InitContextMenuIfRequired() { | 831 void NativeTextfieldViews::InitContextMenuIfRequired() { |
795 if (context_menu_menu_.get()) | 832 if (context_menu_menu_.get()) |
796 return; | 833 return; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
853 } | 890 } |
854 | 891 |
855 void NativeTextfieldViews::TextfieldBorder::SetInsets(int top, | 892 void NativeTextfieldViews::TextfieldBorder::SetInsets(int top, |
856 int left, | 893 int left, |
857 int bottom, | 894 int bottom, |
858 int right) { | 895 int right) { |
859 insets_.Set(top, left, bottom, right); | 896 insets_.Set(top, left, bottom, right); |
860 } | 897 } |
861 | 898 |
862 } // namespace views | 899 } // namespace views |
OLD | NEW |