Chromium Code Reviews| 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/insets.h" | 21 #include "ui/gfx/insets.h" |
| 22 #include "ui/native_theme/native_theme.h" | 22 #include "ui/native_theme/native_theme.h" |
| 23 #include "ui/views/background.h" | 23 #include "ui/views/background.h" |
| 24 #include "ui/views/controls/focusable_border.h" | 24 #include "ui/views/controls/focusable_border.h" |
| 25 #include "ui/views/controls/label.h" | |
| 25 #include "ui/views/controls/menu/menu_item_view.h" | 26 #include "ui/views/controls/menu/menu_item_view.h" |
| 26 #include "ui/views/controls/menu/menu_runner.h" | 27 #include "ui/views/controls/menu/menu_runner.h" |
| 27 #include "ui/views/controls/native/native_view_host.h" | 28 #include "ui/views/controls/native/native_view_host.h" |
| 28 #include "ui/views/controls/textfield/textfield_controller.h" | 29 #include "ui/views/controls/textfield/textfield_controller.h" |
| 29 #include "ui/views/drag_utils.h" | 30 #include "ui/views/drag_utils.h" |
| 30 #include "ui/views/ime/input_method.h" | 31 #include "ui/views/ime/input_method.h" |
| 31 #include "ui/views/metrics.h" | 32 #include "ui/views/metrics.h" |
| 32 #include "ui/views/painter.h" | 33 #include "ui/views/painter.h" |
| 33 #include "ui/views/views_delegate.h" | 34 #include "ui/views/views_delegate.h" |
| 34 #include "ui/views/widget/widget.h" | 35 #include "ui/views/widget/widget.h" |
| 35 | 36 |
| 36 #if defined(OS_WIN) | 37 #if defined(OS_WIN) |
| 37 #include "base/win/win_util.h" | 38 #include "base/win/win_util.h" |
| 38 #endif | 39 #endif |
| 39 | 40 |
| 41 namespace views { | |
| 42 | |
| 40 namespace { | 43 namespace { |
| 41 | 44 |
| 42 // Default placeholder text color. | 45 // Default placeholder text color. |
| 43 const SkColor kDefaultPlaceholderTextColor = SK_ColorLTGRAY; | 46 const SkColor kDefaultPlaceholderTextColor = SK_ColorLTGRAY; |
| 44 | 47 |
| 45 void ConvertRectToScreen(const views::View* src, gfx::Rect* r) { | 48 void ConvertRectToScreen(const View* src, gfx::Rect* r) { |
| 46 DCHECK(src); | 49 DCHECK(src); |
| 47 | 50 |
| 48 gfx::Point new_origin = r->origin(); | 51 gfx::Point new_origin = r->origin(); |
| 49 views::View::ConvertPointToScreen(src, &new_origin); | 52 View::ConvertPointToScreen(src, &new_origin); |
| 50 r->set_origin(new_origin); | 53 r->set_origin(new_origin); |
| 51 } | 54 } |
| 52 | 55 |
| 53 } // namespace | 56 } // namespace |
| 54 | 57 |
| 55 namespace views { | |
| 56 | |
| 57 // static | 58 // static |
| 58 const char Textfield::kViewClassName[] = "Textfield"; | 59 const char Textfield::kViewClassName[] = "Textfield"; |
| 59 | 60 |
| 60 // static | 61 // static |
| 61 size_t Textfield::GetCaretBlinkMs() { | 62 size_t Textfield::GetCaretBlinkMs() { |
| 62 static const size_t default_value = 500; | 63 static const size_t default_value = 500; |
| 63 #if defined(OS_WIN) | 64 #if defined(OS_WIN) |
| 64 static const size_t system_value = ::GetCaretBlinkTime(); | 65 static const size_t system_value = ::GetCaretBlinkTime(); |
| 65 if (system_value != 0) | 66 if (system_value != 0) |
| 66 return (system_value == INFINITE) ? 0 : system_value; | 67 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: | 821 // Textfield, TextfieldModel::Delegate overrides: |
| 821 | 822 |
| 822 void Textfield::OnCompositionTextConfirmedOrCleared() { | 823 void Textfield::OnCompositionTextConfirmedOrCleared() { |
| 823 if (!skip_input_method_cancel_composition_) | 824 if (!skip_input_method_cancel_composition_) |
| 824 GetInputMethod()->CancelComposition(this); | 825 GetInputMethod()->CancelComposition(this); |
| 825 } | 826 } |
| 826 | 827 |
| 827 //////////////////////////////////////////////////////////////////////////////// | 828 //////////////////////////////////////////////////////////////////////////////// |
| 828 // Textfield, ContextMenuController overrides: | 829 // Textfield, ContextMenuController overrides: |
| 829 | 830 |
| 830 void Textfield::ShowContextMenuForView( | 831 void Textfield::ShowContextMenuForView(View* source, |
| 831 View* source, | 832 const gfx::Point& point, |
| 832 const gfx::Point& point, | 833 ui::MenuSourceType source_type) { |
| 833 ui::MenuSourceType source_type) { | |
| 834 UpdateContextMenu(); | 834 UpdateContextMenu(); |
| 835 if (context_menu_runner_->RunMenuAt(GetWidget(), NULL, | 835 ignore_result(context_menu_runner_->RunMenuAt(GetWidget(), NULL, |
| 836 gfx::Rect(point, gfx::Size()), views::MenuItemView::TOPLEFT, | 836 gfx::Rect(point, gfx::Size()), MenuItemView::TOPLEFT, source_type, |
| 837 source_type, | 837 MenuRunner::HAS_MNEMONICS | MenuRunner::CONTEXT_MENU)); |
| 838 MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU) == | |
| 839 MenuRunner::MENU_DELETED) | |
| 840 return; | |
| 841 } | 838 } |
| 842 | 839 |
| 843 //////////////////////////////////////////////////////////////////////////////// | 840 //////////////////////////////////////////////////////////////////////////////// |
| 844 // Textfield, views::DragController overrides: | 841 // Textfield, DragController overrides: |
| 845 | 842 |
| 846 void Textfield::WriteDragDataForView(views::View* sender, | 843 void Textfield::WriteDragDataForView(View* sender, |
| 847 const gfx::Point& press_pt, | 844 const gfx::Point& press_pt, |
| 848 OSExchangeData* data) { | 845 OSExchangeData* data) { |
| 849 DCHECK_NE(ui::DragDropTypes::DRAG_NONE, | 846 const base::string16& selected_text(GetSelectedText()); |
| 850 GetDragOperationsForView(sender, press_pt)); | 847 data->SetString(selected_text); |
| 851 data->SetString(model_->GetSelectedText()); | 848 Label label(selected_text, GetFontList()); |
| 849 const SkColor background = GetBackgroundColor(); | |
| 850 label.SetBackgroundColor(SkColorSetA(background, SK_AlphaTRANSPARENT)); | |
| 851 label.SetBoundsRect(gfx::Rect(label.GetPreferredSize())); | |
|
sky
2014/03/05 23:28:43
Should this be capped in anyway? What if the text
msw
2014/03/06 00:00:42
Good point! I tried some very long strings and it
| |
| 852 scoped_ptr<gfx::Canvas> canvas( | 852 scoped_ptr<gfx::Canvas> canvas( |
| 853 views::GetCanvasForDragImage(GetWidget(), size())); | 853 GetCanvasForDragImage(GetWidget(), label.size())); |
| 854 GetRenderText()->DrawSelectedTextForDrag(canvas.get()); | 854 label.SetEnabledColor(GetTextColor()); |
| 855 drag_utils::SetDragImageOnDataObject(*canvas, size(), | 855 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 856 press_pt.OffsetFromOrigin(), | 856 // Desktop Linux Aura does not yet support transparency in drag images. |
| 857 data); | 857 canvas->DrawColor(background); |
| 858 #endif | |
| 859 label.Paint(canvas.get()); | |
| 860 static const gfx::Vector2d kOffset(-15, 0); | |
|
sky
2014/03/05 23:28:43
nit: no static here.
Also, shouldn't the offset be
msw
2014/03/06 00:00:42
Removed static. No, a (0,0) offset here puts the t
| |
| 861 drag_utils::SetDragImageOnDataObject(*canvas, label.size(), kOffset, data); | |
| 858 if (controller_) | 862 if (controller_) |
| 859 controller_->OnWriteDragData(data); | 863 controller_->OnWriteDragData(data); |
| 860 } | 864 } |
| 861 | 865 |
| 862 int Textfield::GetDragOperationsForView(views::View* sender, | 866 int Textfield::GetDragOperationsForView(View* sender, const gfx::Point& p) { |
| 863 const gfx::Point& p) { | |
| 864 int drag_operations = ui::DragDropTypes::DRAG_COPY; | 867 int drag_operations = ui::DragDropTypes::DRAG_COPY; |
| 865 if (!enabled() || text_input_type_ == ui::TEXT_INPUT_TYPE_PASSWORD || | 868 if (!enabled() || text_input_type_ == ui::TEXT_INPUT_TYPE_PASSWORD || |
| 866 !GetRenderText()->IsPointInSelection(p)) { | 869 !GetRenderText()->IsPointInSelection(p)) { |
| 867 drag_operations = ui::DragDropTypes::DRAG_NONE; | 870 drag_operations = ui::DragDropTypes::DRAG_NONE; |
| 868 } else if (sender == this && !read_only()) { | 871 } else if (sender == this && !read_only()) { |
| 869 drag_operations = | 872 drag_operations = |
| 870 ui::DragDropTypes::DRAG_MOVE | ui::DragDropTypes::DRAG_COPY; | 873 ui::DragDropTypes::DRAG_MOVE | ui::DragDropTypes::DRAG_COPY; |
| 871 } | 874 } |
| 872 if (controller_) | 875 if (controller_) |
| 873 controller_->OnGetDragOperationsForTextfield(&drag_operations); | 876 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(); | 1489 const size_t length = selection_clipboard_text.length(); |
| 1487 range = gfx::Range(range.start() + length, range.end() + length); | 1490 range = gfx::Range(range.start() + length, range.end() + length); |
| 1488 } | 1491 } |
| 1489 model_->MoveCursorTo(gfx::SelectionModel(range, affinity)); | 1492 model_->MoveCursorTo(gfx::SelectionModel(range, affinity)); |
| 1490 UpdateAfterChange(true, true); | 1493 UpdateAfterChange(true, true); |
| 1491 OnAfterUserAction(); | 1494 OnAfterUserAction(); |
| 1492 } | 1495 } |
| 1493 } | 1496 } |
| 1494 | 1497 |
| 1495 } // namespace views | 1498 } // namespace views |
| OLD | NEW |