From 115607b1fe8b5efb69aa5f9edad6d542dfe26153 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 13 Jan 2020 22:50:26 +0100 Subject: [PATCH] Basic UI --- conversations_screen.cpp | 139 ++++++++++++++++++++++++++++++++++++--- mainmenu.cpp | 25 +------ ui_utils.cpp | 26 ++++++++ ui_utils.h | 2 + 4 files changed, 160 insertions(+), 32 deletions(-) diff --git a/conversations_screen.cpp b/conversations_screen.cpp index b1dbd38..d03d52a 100644 --- a/conversations_screen.cpp +++ b/conversations_screen.cpp @@ -1,26 +1,149 @@ +#include + +#include +#include + #include +#include #include "helpers/userhelper.h" #include "conversations_screen.h" #include "helpers/conversationshelper.h" #include "entities/conversation.h" #include "entities/conversationslist.h" - +#include "ui_utils.h" using namespace std; +// Show the screen to choose a conversation +static void ChooseConv(ConversationsList &list, const UsersList &users, Conversation **choice) { + + ITEM **my_items; + int c; + MENU *my_menu; + WINDOW *my_menu_win; + int n_choices; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_CYAN, COLOR_BLACK); + + + erase(); + + /* Create the window to be associated with the menu */ + /* We must do this before creating the menu to get maximum + * size of menu entries names */ + int w,h; + getmaxyx(stdscr, w, h); + int numlines = w - 20, numcols = h-20; + my_menu_win = newwin(numlines, numcols, (w-numlines)/2, (h-numcols)/2); + keypad(my_menu_win, TRUE); + + + /* Create items */ + vector itemsStr; + int numItems = 0; + n_choices = static_cast(list.size()) + 2; + my_items = static_cast(calloc(static_cast(n_choices), sizeof(ITEM *))); + + // Go back + my_items[0] = new_item("Go back", "Go back"); + + // Conversations + for(size_t i = 0; i < list.size(); ++i) { + itemsStr.push_back(list[i].name(users)); + my_items[i+1] = new_item("yo", "d"); + } + + my_items[n_choices-1] = new_item(nullptr, nullptr); + + /* Create menu */ + my_menu = new_menu(static_cast(my_items)); + + + /* Set main window and sub window */ + set_menu_win(my_menu, my_menu_win); + set_menu_sub(my_menu, derwin(my_menu_win, numlines-4, numcols-2, 3, 1)); + set_menu_format(my_menu, numlines-4, 1); + + /* Set menu mark to the string " * " */ + set_menu_mark(my_menu, " * "); + + /* Print a border around the main window and print a title */ + box(my_menu_win, 0, 0); + print_in_middle(my_menu_win, 1, 0, numcols, "Conversations", COLOR_PAIR(1)); + mvwaddch(my_menu_win, 2, 0, ACS_LTEE); + mvwhline(my_menu_win, 2, 1, ACS_HLINE, numcols-2); + mvwaddch(my_menu_win, 2, numcols-1, ACS_RTEE); + + /* Post the menu */ + post_menu(my_menu); + wrefresh(my_menu_win); + + attron(COLOR_PAIR(2)); + mvprintw(LINES - 2, 0, ""); + mvprintw(LINES - 1, 0, "Arrow Keys to navigate"); + attroff(COLOR_PAIR(2)); + refresh(); + + int action = -1; + while(action == -1) + { + c = wgetch(my_menu_win); + switch(c) + { case KEY_DOWN: + menu_driver(my_menu, REQ_DOWN_ITEM); + break; + case KEY_UP: + menu_driver(my_menu, REQ_UP_ITEM); + break; + case KEY_NPAGE: + menu_driver(my_menu, REQ_SCR_DPAGE); + break; + case KEY_PPAGE: + menu_driver(my_menu, REQ_SCR_UPAGE); + break; + case 10: + auto item = current_item(my_menu); + action = item->index; + } + wrefresh(my_menu_win); + } + + /* Unpost and free all the memory taken up */ + unpost_menu(my_menu); + free_menu(my_menu); + for(int i = 0; i < n_choices; ++i) + free_item(my_items[i]); + endwin(); + +} + void showConversationsScreen() { - auto list = ConversationsHelper::GetList(); - auto users = UserHelper::getMultiple(list.usersList()); + bool stop = false; - for(auto c : list) - cout << "Conv: " << c.name(users) << endl; + while(!stop) { + // Get information from an online source + auto list = ConversationsHelper::GetList(); + auto users = UserHelper::getMultiple(list.usersList()); - cout << "done" << endl; - string s; - cin >> s; + // Get the conversation to show + Conversation *c = nullptr; + ChooseConv(list, users, &c); + + if(c == nullptr) { + stop = true; + break; + } + } } diff --git a/mainmenu.cpp b/mainmenu.cpp index fe6c4e9..ddfc9f2 100644 --- a/mainmenu.cpp +++ b/mainmenu.cpp @@ -11,6 +11,7 @@ #include "loginscreen.h" #include "mainmenu.h" #include "conversations_screen.h" +#include "ui_utils.h" using namespace std; @@ -24,7 +25,6 @@ static char *choices[] = { "Quit", nullptr }; -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); void showMainMenu() { @@ -138,26 +138,3 @@ while(true) { } } - -void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) -{ int length, x, y; - float temp; - - if(win == nullptr) - win = stdscr; - getyx(win, y, x); - if(startx != 0) - x = startx; - if(starty != 0) - y = starty; - if(width == 0) - width = 80; - - length = static_cast(strlen(string)); - temp = (width - length)/ 2; - x = startx + static_cast(temp); - wattron(win, color); - mvwprintw(win, y, x, "%s", string); - wattroff(win, color); - refresh(); -} diff --git a/ui_utils.cpp b/ui_utils.cpp index c487620..733ad13 100644 --- a/ui_utils.cpp +++ b/ui_utils.cpp @@ -1,3 +1,5 @@ +#include + #include #include @@ -44,3 +46,27 @@ void ui_utils::alert(WINDOW *win, const string &msg) erase(); refresh(); } + + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) +{ int length, x, y; + float temp; + + if(win == nullptr) + win = stdscr; + getyx(win, y, x); + if(startx != 0) + x = startx; + if(starty != 0) + y = starty; + if(width == 0) + width = 80; + + length = static_cast(strlen(string)); + temp = (width - length)/ 2; + x = startx + static_cast(temp); + wattron(win, color); + mvwprintw(win, y, x, "%s", string); + wattroff(win, color); + refresh(); +} diff --git a/ui_utils.h b/ui_utils.h index fa9ace0..b4c8f3a 100644 --- a/ui_utils.h +++ b/ui_utils.h @@ -14,3 +14,5 @@ namespace ui_utils { void alert(WINDOW *win, const std::string &msg); void print_base_screen(WINDOW *w); } + +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);