OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ui/views/controls/textfield/textfield.h" | 5 #include "ui/views/controls/textfield/textfield.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "grit/ui_strings.h" | 10 #include "grit/ui_strings.h" |
11 #include "ui/accessibility/ax_view_state.h" | 11 #include "ui/accessibility/ax_view_state.h" |
12 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 12 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
13 #include "ui/base/cursor/cursor.h" | 13 #include "ui/base/cursor/cursor.h" |
14 #include "ui/base/dragdrop/drag_drop_types.h" | 14 #include "ui/base/dragdrop/drag_drop_types.h" |
15 #include "ui/base/dragdrop/drag_utils.h" | 15 #include "ui/base/dragdrop/drag_utils.h" |
16 #include "ui/base/resource/resource_bundle.h" | 16 #include "ui/base/resource/resource_bundle.h" |
17 #include "ui/base/ui_base_switches_util.h" | 17 #include "ui/base/ui_base_switches_util.h" |
18 #include "ui/events/event.h" | 18 #include "ui/events/event.h" |
19 #include "ui/events/keycodes/keyboard_codes.h" | 19 #include "ui/events/keycodes/keyboard_codes.h" |
20 #include "ui/gfx/canvas.h" | 20 #include "ui/gfx/canvas.h" |
| 21 #include "ui/gfx/display.h" |
21 #include "ui/gfx/insets.h" | 22 #include "ui/gfx/insets.h" |
| 23 #include "ui/gfx/screen.h" |
22 #include "ui/native_theme/native_theme.h" | 24 #include "ui/native_theme/native_theme.h" |
23 #include "ui/views/background.h" | 25 #include "ui/views/background.h" |
24 #include "ui/views/controls/focusable_border.h" | 26 #include "ui/views/controls/focusable_border.h" |
| 27 #include "ui/views/controls/label.h" |
25 #include "ui/views/controls/menu/menu_item_view.h" | 28 #include "ui/views/controls/menu/menu_item_view.h" |
26 #include "ui/views/controls/menu/menu_runner.h" | 29 #include "ui/views/controls/menu/menu_runner.h" |
27 #include "ui/views/controls/native/native_view_host.h" | 30 #include "ui/views/controls/native/native_view_host.h" |
28 #include "ui/views/controls/textfield/textfield_controller.h" | 31 #include "ui/views/controls/textfield/textfield_controller.h" |
29 #include "ui/views/drag_utils.h" | 32 #include "ui/views/drag_utils.h" |
30 #include "ui/views/ime/input_method.h" | 33 #include "ui/views/ime/input_method.h" |
31 #include "ui/views/metrics.h" | 34 #include "ui/views/metrics.h" |
32 #include "ui/views/painter.h" | 35 #include "ui/views/painter.h" |
33 #include "ui/views/views_delegate.h" | 36 #include "ui/views/views_delegate.h" |
34 #include "ui/views/widget/widget.h" | 37 #include "ui/views/widget/widget.h" |
35 | 38 |
36 #if defined(OS_WIN) | 39 #if defined(OS_WIN) |
37 #include "base/win/win_util.h" | 40 #include "base/win/win_util.h" |
38 #endif | 41 #endif |
39 | 42 |
| 43 namespace views { |
| 44 |
40 namespace { | 45 namespace { |
41 | 46 |
42 // Default placeholder text color. | 47 // Default placeholder text color. |
43 const SkColor kDefaultPlaceholderTextColor = SK_ColorLTGRAY; | 48 const SkColor kDefaultPlaceholderTextColor = SK_ColorLTGRAY; |
44 | 49 |
45 void ConvertRectToScreen(const views::View* src, gfx::Rect* r) { | 50 void ConvertRectToScreen(const View* src, gfx::Rect* r) { |
46 DCHECK(src); | 51 DCHECK(src); |
47 | 52 |
48 gfx::Point new_origin = r->origin(); | 53 gfx::Point new_origin = r->origin(); |
49 views::View::ConvertPointToScreen(src, &new_origin); | 54 View::ConvertPointToScreen(src, &new_origin); |
50 r->set_origin(new_origin); | 55 r->set_origin(new_origin); |
51 } | 56 } |
52 | 57 |
53 } // namespace | 58 } // namespace |
54 | 59 |
55 namespace views { | |
56 | |
57 // static | 60 // static |
58 const char Textfield::kViewClassName[] = "Textfield"; | 61 const char Textfield::kViewClassName[] = "Textfield"; |
59 | 62 |
60 // static | 63 // static |
61 size_t Textfield::GetCaretBlinkMs() { | 64 size_t Textfield::GetCaretBlinkMs() { |
62 static const size_t default_value = 500; | 65 static const size_t default_value = 500; |
63 #if defined(OS_WIN) | 66 #if defined(OS_WIN) |
64 static const size_t system_value = ::GetCaretBlinkTime(); | 67 static const size_t system_value = ::GetCaretBlinkTime(); |
65 if (system_value != 0) | 68 if (system_value != 0) |
66 return (system_value == INFINITE) ? 0 : system_value; | 69 return (system_value == INFINITE) ? 0 : system_value; |
(...skipping 753 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
820 // Textfield, TextfieldModel::Delegate overrides: | 823 // Textfield, TextfieldModel::Delegate overrides: |
821 | 824 |
822 void Textfield::OnCompositionTextConfirmedOrCleared() { | 825 void Textfield::OnCompositionTextConfirmedOrCleared() { |
823 if (!skip_input_method_cancel_composition_) | 826 if (!skip_input_method_cancel_composition_) |
824 GetInputMethod()->CancelComposition(this); | 827 GetInputMethod()->CancelComposition(this); |
825 } | 828 } |
826 | 829 |
827 //////////////////////////////////////////////////////////////////////////////// | 830 //////////////////////////////////////////////////////////////////////////////// |
828 // Textfield, ContextMenuController overrides: | 831 // Textfield, ContextMenuController overrides: |
829 | 832 |
830 void Textfield::ShowContextMenuForView( | 833 void Textfield::ShowContextMenuForView(View* source, |
831 View* source, | 834 const gfx::Point& point, |
832 const gfx::Point& point, | 835 ui::MenuSourceType source_type) { |
833 ui::MenuSourceType source_type) { | |
834 UpdateContextMenu(); | 836 UpdateContextMenu(); |
835 if (context_menu_runner_->RunMenuAt(GetWidget(), NULL, | 837 ignore_result(context_menu_runner_->RunMenuAt(GetWidget(), NULL, |
836 gfx::Rect(point, gfx::Size()), views::MenuItemView::TOPLEFT, | 838 gfx::Rect(point, gfx::Size()), MenuItemView::TOPLEFT, source_type, |
837 source_type, | 839 MenuRunner::HAS_MNEMONICS | MenuRunner::CONTEXT_MENU)); |
838 MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU) == | |
839 MenuRunner::MENU_DELETED) | |
840 return; | |
841 } | 840 } |
842 | 841 |
843 //////////////////////////////////////////////////////////////////////////////// | 842 //////////////////////////////////////////////////////////////////////////////// |
844 // Textfield, views::DragController overrides: | 843 // Textfield, DragController overrides: |
845 | 844 |
846 void Textfield::WriteDragDataForView(views::View* sender, | 845 void Textfield::WriteDragDataForView(View* sender, |
847 const gfx::Point& press_pt, | 846 const gfx::Point& press_pt, |
848 OSExchangeData* data) { | 847 OSExchangeData* data) { |
849 DCHECK_NE(ui::DragDropTypes::DRAG_NONE, | 848 const base::string16& selected_text(GetSelectedText()); |
850 GetDragOperationsForView(sender, press_pt)); | 849 data->SetString(selected_text); |
851 data->SetString(model_->GetSelectedText()); | 850 Label label(selected_text, GetFontList()); |
| 851 const SkColor background = GetBackgroundColor(); |
| 852 label.SetBackgroundColor(SkColorSetA(background, SK_AlphaTRANSPARENT)); |
| 853 gfx::Size size(label.GetPreferredSize()); |
| 854 gfx::NativeView native_view = GetWidget()->GetNativeView(); |
| 855 gfx::Display display = gfx::Screen::GetScreenFor(native_view)-> |
| 856 GetDisplayNearestWindow(native_view); |
| 857 size.SetToMin(gfx::Size(display.size().width(), height())); |
| 858 label.SetBoundsRect(gfx::Rect(size)); |
852 scoped_ptr<gfx::Canvas> canvas( | 859 scoped_ptr<gfx::Canvas> canvas( |
853 views::GetCanvasForDragImage(GetWidget(), size())); | 860 GetCanvasForDragImage(GetWidget(), label.size())); |
854 GetRenderText()->DrawSelectedTextForDrag(canvas.get()); | 861 label.SetEnabledColor(GetTextColor()); |
855 drag_utils::SetDragImageOnDataObject(*canvas, size(), | 862 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
856 press_pt.OffsetFromOrigin(), | 863 // Desktop Linux Aura does not yet support transparency in drag images. |
857 data); | 864 canvas->DrawColor(background); |
| 865 #endif |
| 866 label.Paint(canvas.get()); |
| 867 const gfx::Vector2d kOffset(-15, 0); |
| 868 drag_utils::SetDragImageOnDataObject(*canvas, label.size(), kOffset, data); |
858 if (controller_) | 869 if (controller_) |
859 controller_->OnWriteDragData(data); | 870 controller_->OnWriteDragData(data); |
860 } | 871 } |
861 | 872 |
862 int Textfield::GetDragOperationsForView(views::View* sender, | 873 int Textfield::GetDragOperationsForView(View* sender, const gfx::Point& p) { |
863 const gfx::Point& p) { | |
864 int drag_operations = ui::DragDropTypes::DRAG_COPY; | 874 int drag_operations = ui::DragDropTypes::DRAG_COPY; |
865 if (!enabled() || text_input_type_ == ui::TEXT_INPUT_TYPE_PASSWORD || | 875 if (!enabled() || text_input_type_ == ui::TEXT_INPUT_TYPE_PASSWORD || |
866 !GetRenderText()->IsPointInSelection(p)) { | 876 !GetRenderText()->IsPointInSelection(p)) { |
867 drag_operations = ui::DragDropTypes::DRAG_NONE; | 877 drag_operations = ui::DragDropTypes::DRAG_NONE; |
868 } else if (sender == this && !read_only()) { | 878 } else if (sender == this && !read_only()) { |
869 drag_operations = | 879 drag_operations = |
870 ui::DragDropTypes::DRAG_MOVE | ui::DragDropTypes::DRAG_COPY; | 880 ui::DragDropTypes::DRAG_MOVE | ui::DragDropTypes::DRAG_COPY; |
871 } | 881 } |
872 if (controller_) | 882 if (controller_) |
873 controller_->OnGetDragOperationsForTextfield(&drag_operations); | 883 controller_->OnGetDragOperationsForTextfield(&drag_operations); |
(...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1486 const size_t length = selection_clipboard_text.length(); | 1496 const size_t length = selection_clipboard_text.length(); |
1487 range = gfx::Range(range.start() + length, range.end() + length); | 1497 range = gfx::Range(range.start() + length, range.end() + length); |
1488 } | 1498 } |
1489 model_->MoveCursorTo(gfx::SelectionModel(range, affinity)); | 1499 model_->MoveCursorTo(gfx::SelectionModel(range, affinity)); |
1490 UpdateAfterChange(true, true); | 1500 UpdateAfterChange(true, true); |
1491 OnAfterUserAction(); | 1501 OnAfterUserAction(); |
1492 } | 1502 } |
1493 } | 1503 } |
1494 | 1504 |
1495 } // namespace views | 1505 } // namespace views |
OLD | NEW |