From 1232539495efcc6941b030c6f8b1e2c85a345b8b Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 11 Dec 2016 20:29:15 -0500 Subject: [PATCH] Move buffer-related keypress handling from Window to BufferPane --- src/gui/BufferPane.cc | 114 ++++++++++++++++++++++++++++++++++++++++++ src/gui/BufferPane.h | 12 +++++ src/gui/Window.cc | 111 ++-------------------------------------- src/gui/Window.h | 11 ---- 4 files changed, 129 insertions(+), 119 deletions(-) diff --git a/src/gui/BufferPane.cc b/src/gui/BufferPane.cc index 0fb3f4f..f2523d4 100644 --- a/src/gui/BufferPane.cc +++ b/src/gui/BufferPane.cc @@ -123,3 +123,117 @@ void BufferPane::draw_cursor(int x, int y, bool insert_mode) int height = m_font->get_line_height(); m_gl->draw_rect(win_x(x), win_y(y), width, height, 1.0, 0.2, 1.0, 1.0); } + +void BufferPane::handle_key(uint32_t keyval) +{ +#if 0 + if (m_buffer->piece_table->in_insert_mode()) + { + if (keyval == '\033') + { + m_buffer->piece_table->end_insert(); + } + else if (keyval <= 0xFFu) + { + m_buffer->piece_table->insert_code_point(keyval); + } + redraw(); + } + else +#endif + { + switch (keyval) + { + case '0': + cursor_move(CursorMovement::SOL); + break; + case '$': + cursor_move(CursorMovement::EOL); + break; + case 'h': + cursor_move(CursorMovement::LEFT); + break; + case 'j': + cursor_move(CursorMovement::DOWN); + break; + case 'k': + cursor_move(CursorMovement::UP); + break; + case 'l': + cursor_move(CursorMovement::RIGHT); + break; + } + } +} + +void BufferPane::cursor_move(CursorMovement which) +{ +#if 0 + bool moved = false; + int current_row_offset = m_cursor->column / m_columns; + int row_offset = 0; + + switch (which) + { + case CursorMovement::LEFT: + moved = m_cursor->check_go_left(1); + m_target_column = m_cursor->column; + break; + case CursorMovement::RIGHT: + moved = m_cursor->check_go_right(1, false); + m_target_column = m_cursor->column; + break; + case CursorMovement::UP: + moved = m_cursor->check_go_up(1, m_target_column); + break; + case CursorMovement::DOWN: + moved = m_cursor->check_go_down(1, m_target_column); + break; + case CursorMovement::SOL: + moved = m_cursor->check_go_start_of_line(); + m_target_column = 0u; + break; + case CursorMovement::EOL: + moved = m_cursor->check_go_end_of_line(false); + m_target_column = 0x7FFFFFFFu; + break; + } + if (moved) + { + switch (which) + { + case CursorMovement::LEFT: + case CursorMovement::RIGHT: + row_offset = m_cursor->column / m_columns - current_row_offset; + break; + case CursorMovement::UP: + { + PieceTable::Cursor c = *m_cursor; + c.go_end_of_line(false); + row_offset = m_cursor->column / m_columns - c.column / m_columns - 1 - current_row_offset; + } + break; + case CursorMovement::DOWN: + { + PieceTable::Cursor c = *m_cursor; + c.go_up(1, 0u); + c.go_end_of_line(false); + row_offset = c.column / m_columns - current_row_offset + 1 + m_cursor->column / m_columns; + } + break; + case CursorMovement::SOL: + row_offset = -current_row_offset; + break; + case CursorMovement::EOL: + { + PieceTable::Cursor c = *m_cursor; + c.go_end_of_line(false); + row_offset = c.column / m_columns - current_row_offset; + } + break; + } + update_cursor_row(m_cursor_row + row_offset); + redraw(); + } +#endif +} diff --git a/src/gui/BufferPane.h b/src/gui/BufferPane.h index 063c9db..40395cc 100644 --- a/src/gui/BufferPane.h +++ b/src/gui/BufferPane.h @@ -16,8 +16,19 @@ public: void resize(int width, int height) override; void draw(); std::shared_ptr cursor() { return m_cursor; } + void handle_key(uint32_t keyval); protected: + enum class CursorMovement : uint8_t + { + LEFT, + RIGHT, + UP, + DOWN, + SOL, + EOL, + }; + int effective_scroll_offset() { return std::min(m_scroll_offset, (m_rows - 1) / 2); @@ -36,6 +47,7 @@ protected: { return m_height - (row + 1) * m_font->get_line_height(); } + void cursor_move(CursorMovement which); std::shared_ptr m_buffer; std::shared_ptr m_font; diff --git a/src/gui/Window.cc b/src/gui/Window.cc index 874dd85..b681d90 100644 --- a/src/gui/Window.cc +++ b/src/gui/Window.cc @@ -221,121 +221,16 @@ void Window::handle_keysym(uint32_t keysym) void Window::handle_keyval(uint32_t keyval) { -#if 0 - if (m_buffer->piece_table->in_insert_mode()) + if (keyval == KEYMOD_CTRL + 'q') { - if (keyval == '\033') - { - m_buffer->piece_table->end_insert(); - } - else if (keyval <= 0xFFu) - { - m_buffer->piece_table->insert_code_point(keyval); - } - redraw(); + m_exit_requested = true; } else -#endif { - switch (keyval) - { - case KEYMOD_CTRL + 'q': - m_exit_requested = true; - break; - case '0': - cursor_move(CURSOR_SOL); - break; - case '$': - cursor_move(CURSOR_EOL); - break; - case 'h': - cursor_move(CURSOR_LEFT); - break; - case 'j': - cursor_move(CURSOR_DOWN); - break; - case 'k': - cursor_move(CURSOR_UP); - break; - case 'l': - cursor_move(CURSOR_RIGHT); - break; - } + m_buffer_pane->handle_key(keyval); } } -void Window::cursor_move(int which) -{ -#if 0 - bool moved = false; - int current_row_offset = m_cursor->column / m_columns; - int row_offset = 0; - - switch (which) - { - case CURSOR_LEFT: - moved = m_cursor->check_go_left(1); - m_target_column = m_cursor->column; - break; - case CURSOR_RIGHT: - moved = m_cursor->check_go_right(1, false); - m_target_column = m_cursor->column; - break; - case CURSOR_UP: - moved = m_cursor->check_go_up(1, m_target_column); - break; - case CURSOR_DOWN: - moved = m_cursor->check_go_down(1, m_target_column); - break; - case CURSOR_SOL: - moved = m_cursor->check_go_start_of_line(); - m_target_column = 0u; - break; - case CURSOR_EOL: - moved = m_cursor->check_go_end_of_line(false); - m_target_column = 0x7FFFFFFFu; - break; - } - if (moved) - { - switch (which) - { - case CURSOR_LEFT: - case CURSOR_RIGHT: - row_offset = m_cursor->column / m_columns - current_row_offset; - break; - case CURSOR_UP: - { - PieceTable::Cursor c = *m_cursor; - c.go_end_of_line(false); - row_offset = m_cursor->column / m_columns - c.column / m_columns - 1 - current_row_offset; - } - break; - case CURSOR_DOWN: - { - PieceTable::Cursor c = *m_cursor; - c.go_up(1, 0u); - c.go_end_of_line(false); - row_offset = c.column / m_columns - current_row_offset + 1 + m_cursor->column / m_columns; - } - break; - case CURSOR_SOL: - row_offset = -current_row_offset; - break; - case CURSOR_EOL: - { - PieceTable::Cursor c = *m_cursor; - c.go_end_of_line(false); - row_offset = c.column / m_columns - current_row_offset; - } - break; - } - update_cursor_row(m_cursor_row + row_offset); - redraw(); - } -#endif -} - void Window::resize() { SDL_GetWindowSize(m_window, &m_width, &m_height); diff --git a/src/gui/Window.h b/src/gui/Window.h index 7f1e7b9..80c7fac 100644 --- a/src/gui/Window.h +++ b/src/gui/Window.h @@ -17,16 +17,6 @@ public: void run_event_loop(); protected: - enum - { - CURSOR_LEFT, - CURSOR_RIGHT, - CURSOR_UP, - CURSOR_DOWN, - CURSOR_SOL, - CURSOR_EOL, - }; - enum : uint32_t { KEYMOD_CTRL = 0x10000, @@ -40,7 +30,6 @@ protected: void handle_event(SDL_Event & event); void handle_keysym(uint32_t keysym); void handle_keyval(uint32_t keyval); - void cursor_move(int which); uint32_t get_keyval(SDL_Keycode keysym); uint32_t get_shifted(uint32_t keysym);