1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/controls/textarea/textarea.h"
#include "base/logging.h"
#include "ui/base/ime/text_edit_commands.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/events/event.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/canvas.h"
namespace views {
Textarea::Textarea() {
set_placeholder_text_draw_flags(placeholder_text_draw_flags() |
gfx::Canvas::MULTI_LINE);
GetRenderText()->SetMultiline(true);
GetRenderText()->SetVerticalAlignment(gfx::ALIGN_TOP);
GetRenderText()->SetWordWrapBehavior(gfx::WRAP_LONG_WORDS);
SetTextInputType(ui::TextInputType::TEXT_INPUT_TYPE_TEXT_AREA);
}
size_t Textarea::GetNumLines() {
return GetRenderText()->GetNumLines();
}
bool Textarea::OnMouseWheel(const ui::MouseWheelEvent& event) {
GetRenderText()->SetDisplayOffset(GetRenderText()->GetUpdatedDisplayOffset() +
gfx::Vector2d(0, event.y_offset()));
UpdateCursorViewPosition();
UpdateCursorVisibility();
SchedulePaint();
return true;
}
Textfield::EditCommandResult Textarea::DoExecuteTextEditCommand(
ui::TextEditCommand command) {
bool rtl = GetTextDirection() == base::i18n::RIGHT_TO_LEFT;
gfx::VisualCursorDirection begin = rtl ? gfx::CURSOR_RIGHT : gfx::CURSOR_LEFT;
gfx::VisualCursorDirection end = rtl ? gfx::CURSOR_LEFT : gfx::CURSOR_RIGHT;
switch (command) {
case ui::TextEditCommand::MOVE_UP:
textfield_model()->MoveCursor(gfx::CHARACTER_BREAK, gfx::CURSOR_UP,
gfx::SELECTION_NONE);
break;
case ui::TextEditCommand::MOVE_DOWN:
textfield_model()->MoveCursor(gfx::CHARACTER_BREAK, gfx::CURSOR_DOWN,
gfx::SELECTION_NONE);
break;
case ui::TextEditCommand::MOVE_UP_AND_MODIFY_SELECTION:
textfield_model()->MoveCursor(gfx::CHARACTER_BREAK, gfx::CURSOR_UP,
gfx::SELECTION_RETAIN);
break;
case ui::TextEditCommand::MOVE_DOWN_AND_MODIFY_SELECTION:
textfield_model()->MoveCursor(gfx::CHARACTER_BREAK, gfx::CURSOR_DOWN,
gfx::SELECTION_RETAIN);
break;
case ui::TextEditCommand::
MOVE_TO_BEGINNING_OF_DOCUMENT_AND_MODIFY_SELECTION:
case ui::TextEditCommand::MOVE_PAGE_UP_AND_MODIFY_SELECTION:
textfield_model()->MoveCursor(gfx::FIELD_BREAK, begin,
kPageSelectionBehavior);
break;
case ui::TextEditCommand::
MOVE_TO_BEGINNING_OF_PARAGRAPH_AND_MODIFY_SELECTION:
textfield_model()->MoveCursor(gfx::FIELD_BREAK, begin,
kMoveParagraphSelectionBehavior);
break;
case ui::TextEditCommand::MOVE_TO_END_OF_DOCUMENT_AND_MODIFY_SELECTION:
case ui::TextEditCommand::MOVE_PAGE_DOWN_AND_MODIFY_SELECTION:
textfield_model()->MoveCursor(gfx::FIELD_BREAK, end,
kPageSelectionBehavior);
break;
case ui::TextEditCommand::MOVE_TO_END_OF_PARAGRAPH_AND_MODIFY_SELECTION:
textfield_model()->MoveCursor(gfx::FIELD_BREAK, end,
kMoveParagraphSelectionBehavior);
break;
default:
return Textfield::DoExecuteTextEditCommand(command);
}
// TODO(jongkwon.lee): Return |cursor_changed| with actual value. It's okay
// for now because |cursor_changed| is detected afterward in
// |Textfield::ExecuteTextEditCommand|.
return {false, false};
}
bool Textarea::PreHandleKeyPressed(const ui::KeyEvent& event) {
if (event.key_code() == ui::VKEY_RETURN) {
DoInsertChar('\n');
return true;
}
return false;
}
ui::TextEditCommand Textarea::GetCommandForKeyEvent(const ui::KeyEvent& event) {
if (event.type() != ui::EventType::kKeyPressed || event.IsUnicodeKeyCode()) {
return Textfield::GetCommandForKeyEvent(event);
}
const bool shift = event.IsShiftDown();
switch (event.key_code()) {
case ui::VKEY_UP:
return shift ? ui::TextEditCommand::MOVE_UP_AND_MODIFY_SELECTION
: ui::TextEditCommand::MOVE_UP;
case ui::VKEY_DOWN:
return shift ? ui::TextEditCommand::MOVE_DOWN_AND_MODIFY_SELECTION
: ui::TextEditCommand::MOVE_DOWN;
default:
return Textfield::GetCommandForKeyEvent(event);
}
}
BEGIN_METADATA(Textarea)
END_METADATA
} // namespace views
|