summaryrefslogtreecommitdiff
path: root/LibGUI/GTextEditor.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-03-08 17:53:02 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-03-08 17:53:02 +0100
commitf40d11f06d78734d080cd1103c143da52204d20f (patch)
treeccc73b4a3a18409fdfd7d56f90b26d384f21a0a3 /LibGUI/GTextEditor.cpp
parent6d172725c077ff0b94279816ed810c3c19fad865 (diff)
downloadserenity-f40d11f06d78734d080cd1103c143da52204d20f.zip
LibGUI+WindowServer: Implement drag-to-select behavior in GTextEditor.
To make this feel right, I needed to start passing keyboard modifiers along with mouse events. That allows shift-clicking to extend the selection. :^)
Diffstat (limited to 'LibGUI/GTextEditor.cpp')
-rw-r--r--LibGUI/GTextEditor.cpp60
1 files changed, 48 insertions, 12 deletions
diff --git a/LibGUI/GTextEditor.cpp b/LibGUI/GTextEditor.cpp
index 278b9cc279..f9eaffbb67 100644
--- a/LibGUI/GTextEditor.cpp
+++ b/LibGUI/GTextEditor.cpp
@@ -107,11 +107,47 @@ GTextPosition GTextEditor::text_position_at(const Point& a_position) const
void GTextEditor::mousedown_event(GMouseEvent& event)
{
- set_cursor(text_position_at(event.position()));
- // FIXME: Allow mouse selection!
- if (m_selection_start.is_valid()) {
- m_selection_start = { };
+ if (event.button() == GMouseButton::Left) {
+ if (event.modifiers() & Mod_Shift) {
+ if (!has_selection())
+ m_selection_start = m_cursor;
+ } else {
+ m_selection_start = { };
+ }
+
+ m_in_drag_select = true;
+ set_global_cursor_tracking(true);
+
+ set_cursor(text_position_at(event.position()));
+
+ if (!(event.modifiers() & Mod_Shift)) {
+ if (!has_selection())
+ m_selection_start = m_cursor;
+ }
+
+ // FIXME: Only update the relevant rects.
update();
+ return;
+ }
+}
+
+void GTextEditor::mouseup_event(GMouseEvent& event)
+{
+ if (event.button() == GMouseButton::Left) {
+ if (m_in_drag_select) {
+ m_in_drag_select = false;
+ set_global_cursor_tracking(false);
+ }
+ return;
+ }
+}
+
+void GTextEditor::mousemove_event(GMouseEvent& event)
+{
+ if (m_in_drag_select) {
+ set_cursor(text_position_at(event.position()));
+ update();
+ return;
}
}
@@ -475,14 +511,14 @@ void GTextEditor::set_cursor(const GTextPosition& position)
ASSERT(!m_lines.is_empty());
ASSERT(position.line() < m_lines.size());
ASSERT(position.column() <= m_lines[position.line()]->length());
- if (m_cursor == position)
- return;
- auto old_cursor_line_rect = line_widget_rect(m_cursor.line());
- m_cursor = position;
- m_cursor_state = true;
- scroll_cursor_into_view();
- update(old_cursor_line_rect);
- update_cursor();
+ if (m_cursor != position) {
+ auto old_cursor_line_rect = line_widget_rect(m_cursor.line());
+ m_cursor = position;
+ m_cursor_state = true;
+ scroll_cursor_into_view();
+ update(old_cursor_line_rect);
+ update_cursor();
+ }
if (on_cursor_change)
on_cursor_change(*this);
}