From 043ca9998396bf447c693b1e72078e840f582d11 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 16 Aug 2015 14:06:07 +0100 Subject: [PATCH] Cleanup mouse handling a little --- src/input_handler.cc | 33 +++++++++++++++++---------------- src/ncurses_ui.cc | 11 ++++------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/input_handler.cc b/src/input_handler.cc index cefe65b1..c80ca7b1 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -73,43 +73,44 @@ struct MouseHandler return false; Buffer& buffer = context.buffer(); - if (key.modifiers == Key::Modifiers::MousePress) + ByteCoord cursor; + switch (key.modifiers) { + case Key::Modifiers::MousePress: m_dragging = true; m_anchor = context.window().buffer_coord(key.mouse_coord()); context.selections_write_only() = SelectionList{ buffer, m_anchor }; return true; - } - if (key.modifiers == Key::Modifiers::MouseRelease) - { + + case Key::Modifiers::MouseRelease: if (not m_dragging) return true; m_dragging = false; - auto cursor = context.window().buffer_coord(key.mouse_coord()); + cursor = context.window().buffer_coord(key.mouse_coord()); context.selections_write_only() = SelectionList{ buffer, Selection{buffer.clamp(m_anchor), cursor} }; return true; - } - if (key.modifiers == Key::Modifiers::MousePos) - { + + case Key::Modifiers::MousePos: if (not m_dragging) return true; - auto cursor = context.window().buffer_coord(key.mouse_coord()); + cursor = context.window().buffer_coord(key.mouse_coord()); context.selections_write_only() = SelectionList{ buffer, Selection{buffer.clamp(m_anchor), cursor} }; return true; - } - if (key.modifiers == Key::Modifiers::MouseWheelDown) - { + + case Key::Modifiers::MouseWheelDown: + m_dragging = false; wheel(context, 3); return true; - } - if (key.modifiers == Key::Modifiers::MouseWheelUp) - { + + case Key::Modifiers::MouseWheelUp: + m_dragging = false; wheel(context, -3); return true; + + default: return false; } - return false; } private: diff --git a/src/ncurses_ui.cc b/src/ncurses_ui.cc index b9ad3a52..9439ff81 100644 --- a/src/ncurses_ui.cc +++ b/src/ncurses_ui.cc @@ -482,14 +482,11 @@ Key NCursesUI::get_key() MEVENT ev; if (getmouse(&ev) == OK) { - auto wheel_down_mask = NCURSES_MOUSE_MASK(m_wheel_down_button, NCURSES_BUTTON_PRESSED); - auto wheel_up_mask = NCURSES_MOUSE_MASK(m_wheel_up_button, NCURSES_BUTTON_PRESSED); - CharCoord pos{ ev.y - (m_status_on_top ? 1 : 0), ev.x }; - if ((ev.bstate & BUTTON1_PRESSED) == BUTTON1_PRESSED) return mouse_press(pos); - if ((ev.bstate & BUTTON1_RELEASED) == BUTTON1_RELEASED) return mouse_release(pos); - if ((ev.bstate & wheel_down_mask) == wheel_down_mask) return mouse_wheel_down(pos); - if ((ev.bstate & wheel_up_mask) == wheel_up_mask) return mouse_wheel_up(pos); + if (BUTTON_PRESS(ev.bstate, 1)) return mouse_press(pos); + if (BUTTON_RELEASE(ev.bstate, 1)) return mouse_release(pos); + if (BUTTON_PRESS(ev.bstate, m_wheel_down_button)) return mouse_wheel_down(pos); + if (BUTTON_PRESS(ev.bstate, m_wheel_up_button)) return mouse_wheel_up(pos); return mouse_pos(pos); } }