Add support for page up and page down

This commit is contained in:
Maxime Coste 2012-09-07 21:09:23 +02:00
parent 499bb77491
commit 46565723b1
4 changed files with 35 additions and 1 deletions

View File

@ -16,7 +16,7 @@ struct Key
Alt = 2, Alt = 2,
ControlAlt = 3 ControlAlt = 3
}; };
enum NamedKeys : Character enum NamedKey : Character
{ {
Backspace = 256, Backspace = 256,
Escape, Escape,
@ -24,6 +24,8 @@ struct Key
Down, Down,
Left, Left,
Right, Right,
PageUp,
PageDown,
}; };
Modifiers modifiers; Modifiers modifiers;

View File

@ -236,6 +236,31 @@ void do_select_object(Context& context)
}); });
} }
template<Key::NamedKey key>
void do_scroll(Context& context)
{
static_assert(key == Key::PageUp or key == Key::PageDown,
"do_scrool only implements PageUp and PageDown");
Window& window = context.window();
Buffer& buffer = context.buffer();
BufferCoord position = window.position();
BufferIterator cursor_pos;
if (key == Key::PageUp)
{
position.line -= (window.dimensions().line - 2);
cursor_pos = buffer.iterator_at(position);
}
else if (key == Key::PageDown)
{
position.line += (window.dimensions().line - 2);
cursor_pos = buffer.iterator_at(position + BufferCoord{window.dimensions().line - 1, 0});
}
window.select(cursor_pos);
window.set_position(position);
}
template<typename T> template<typename T>
class Repeated class Repeated
{ {
@ -370,6 +395,9 @@ std::unordered_map<Key, std::function<void (Context& context)>> keymap =
{ { Key::Modifiers::Alt, 'x' }, [](Context& context) { context.editor().select(select_whole_lines); } }, { { Key::Modifiers::Alt, 'x' }, [](Context& context) { context.editor().select(select_whole_lines); } },
{ { Key::Modifiers::Alt, 'c' }, [](Context& context) { if (context.has_window()) context.window().center_selection(); } }, { { Key::Modifiers::Alt, 'c' }, [](Context& context) { if (context.has_window()) context.window().center_selection(); } },
{ { Key::Modifiers::None, Key::PageUp }, do_scroll<Key::PageUp> },
{ { Key::Modifiers::None, Key::PageDown }, do_scroll<Key::PageDown> },
}; };
} }

View File

@ -187,6 +187,8 @@ Key NCursesClient::get_key()
case KEY_DOWN: return Key::Down; case KEY_DOWN: return Key::Down;
case KEY_LEFT: return Key::Left; case KEY_LEFT: return Key::Left;
case KEY_RIGHT: return Key::Right; case KEY_RIGHT: return Key::Right;
case KEY_PPAGE: return Key::PageUp;
case KEY_NPAGE: return Key::PageDown;
} }
return c; return c;
} }

View File

@ -25,7 +25,9 @@ public:
~Window(); ~Window();
const BufferCoord& position() const { return m_position; } const BufferCoord& position() const { return m_position; }
void set_position(const BufferCoord& position) { m_position = buffer().clamp(position); }
const DisplayCoord& dimensions() const { return m_dimensions; }
void set_dimensions(const DisplayCoord& dimensions); void set_dimensions(const DisplayCoord& dimensions);
const DisplayBuffer& display_buffer() const { return m_display_buffer; } const DisplayBuffer& display_buffer() const { return m_display_buffer; }