diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2015-03-22 12:17:01 +0000 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2015-03-22 12:17:01 +0000 |
| commit | 323b0359c713181f1446935e172f9513c30c9fd2 (patch) | |
| tree | c1d74060718e7712342e8c642039c22217c9ab67 /src/input_handler.cc | |
| parent | 5eaf472fc0fd0d2e5a76ab91f685ea89018a6d33 (diff) | |
Add support for mouse wheel
Diffstat (limited to 'src/input_handler.cc')
| -rw-r--r-- | src/input_handler.cc | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/input_handler.cc b/src/input_handler.cc index 7b6339ff..6d712697 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -80,10 +80,47 @@ struct MouseHandler context.selections() = SelectionList{ context.buffer(), Selection{m_anchor, cursor} }; return true; } + if (key.modifiers == Key::Modifiers::MouseWheelDown) + { + wheel(context, 3); + return true; + } + if (key.modifiers == Key::Modifiers::MouseWheelUp) + { + wheel(context, -3); + return true; + } return false; } private: + void wheel(Context& context, LineCount offset) + { + Window& window = context.window(); + Buffer& buffer = context.buffer(); + + CharCoord win_pos = window.position(); + CharCoord win_dim = window.dimensions(); + + const CharCoord max_offset{(win_dim.line - 1)/2, (win_dim.column - 1)/2}; + const CharCoord scrolloff = + std::min(context.options()["scrolloff"].get<CharCoord>(), max_offset); + + const LineCount line_count = buffer.line_count(); + win_pos.line = clamp(win_pos.line + offset, 0_line, line_count-1); + + SelectionList& selections = context.selections(); + const ByteCoord cursor = selections.main().cursor(); + + auto clamp_line = [&](LineCount line) { return clamp(line, 0_line, line_count-1); }; + auto min_coord = buffer.offset_coord(clamp_line(win_pos.line + scrolloff.line), win_pos.column); + auto max_coord = buffer.offset_coord(clamp_line(win_pos.line + win_dim.line - scrolloff.line), win_pos.column); + + selections = SelectionList{buffer, clamp(cursor, min_coord, max_coord)}; + + window.set_position(win_pos); + } + bool m_dragging = false; ByteCoord m_anchor; }; |
