diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2012-08-30 21:53:22 +0200 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2012-08-30 21:53:22 +0200 |
| commit | 51e80558d9824a2795c522c38cc58fdffaba0314 (patch) | |
| tree | 15ca54d1c4a9447683676c4845682be9cf32f341 /src/ncurses.cc | |
| parent | 1ca502d23de987de52eaf4ea698b6bd006a3e5bc (diff) | |
Change client menu api to give more control to the caller
Diffstat (limited to 'src/ncurses.cc')
| -rw-r--r-- | src/ncurses.cc | 75 |
1 files changed, 35 insertions, 40 deletions
diff --git a/src/ncurses.cc b/src/ncurses.cc index cde33183..6809c3dd 100644 --- a/src/ncurses.cc +++ b/src/ncurses.cc @@ -3,8 +3,6 @@ #include "window.hh" #include "register_manager.hh" -#include <ncurses.h> -#include <menu.h> #include <map> #define CTRL(x) x - 'a' + 1 @@ -13,6 +11,7 @@ namespace Kakoune { NCursesClient::NCursesClient() + : m_menu(nullptr) { // setlocale(LC_ALL, ""); initscr(); @@ -317,50 +316,46 @@ void NCursesClient::print_status(const String& status) refresh(); } -int NCursesClient::menu(const memoryview<String>& choices) +void NCursesClient::show_menu(const memoryview<String>& choices) { - std::vector<ITEM*> items; - std::vector<String> counts; - for (int i = 0; i < choices.size(); ++i) - counts.push_back(int_to_str(i+1)); - for (int i = 0; i < choices.size(); ++i) - items.push_back(new_item(counts[i].c_str(), choices[i].c_str())); - items.push_back(NULL); - MENU* menu = new_menu(&items[0]); + assert(m_menu == nullptr); + m_choices = std::vector<String>(choices.begin(), choices.end()); + for (int i = 0; i < m_choices.size(); ++i) + m_counts.push_back(int_to_str(i+1)); + for (int i = 0; i < m_choices.size(); ++i) + m_items.push_back(new_item(m_counts[i].c_str(), m_choices[i].c_str())); + m_items.push_back(NULL); + m_menu = new_menu(&m_items[0]); int max_x,max_y; getmaxyx(stdscr, max_y, max_x); - int pos_y = max_y - std::min(10, (int)choices.size()) - 1; - set_menu_sub(menu, derwin(stdscr, max_y - pos_y - 1, max_x, pos_y, 0)); - post_menu(menu); + int pos_y = max_y - std::min(10, (int)m_choices.size()) - 1; + set_menu_sub(m_menu, derwin(stdscr, max_y - pos_y - 1, max_x, pos_y, 0)); + post_menu(m_menu); +} - int res = -1; - while (true) +void NCursesClient::menu_ctrl(MenuCommand command) +{ + switch(command) { - int c = getch(); - if ('0' <= c and c <= '9') - { - res = c - '0'; - break; - } - if (c == KEY_DOWN or c == CTRL('n')) - menu_driver(menu, REQ_DOWN_ITEM); - if (c == KEY_UP or c == CTRL('p')) - menu_driver(menu, REQ_UP_ITEM); - if (c == 27) - break; - if (c == '\r') - { - res = item_index(current_item(menu)) + 1; - break; - } + case MenuCommand::SelectNext: + menu_driver(m_menu, REQ_DOWN_ITEM); + break; + case MenuCommand::SelectPrev: + menu_driver(m_menu, REQ_UP_ITEM); + break; + case MenuCommand::Close: + { + unpost_menu(m_menu); + free_menu(m_menu); + for (auto item : m_items) + if (item) + free_item(item); + m_menu = nullptr; + m_items.clear(); + m_counts.clear(); + break; + } } - - unpost_menu(menu); - for (auto item : items) - if (item) - free_item(item); - free_menu(menu); - return res; } } |
