From 81258de89d168f5f3bb938aa01df49e1c1d66485 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 10 Jan 2020 16:03:03 +0100 Subject: [PATCH] Show basic menu --- ComunicTerm.pro | 8 +-- loginscreen.cpp | 4 ++ main.cpp | 2 + mainmenu.cpp | 134 ++++++++++++++++++++++++++++++++++++++++++++++++ mainmenu.h | 6 +++ 5 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 mainmenu.cpp create mode 100644 mainmenu.h diff --git a/ComunicTerm.pro b/ComunicTerm.pro index 443aa1b..c70a906 100644 --- a/ComunicTerm.pro +++ b/ComunicTerm.pro @@ -3,7 +3,7 @@ CONFIG += console c++11 CONFIG -= app_bundle CONFIG -= qt -LIBS += -lboost_system -lcrypto -lssl -lcpprest -lncurses +LIBS += -lboost_system -lcrypto -lssl -lcpprest -lncurses -lmenu SOURCES += \ main.cpp \ @@ -13,7 +13,8 @@ SOURCES += \ ui_utils.cpp \ helpers/accounthelper.cpp \ helpers/userhelper.cpp \ - entities/user.cpp + entities/user.cpp \ + mainmenu.cpp HEADERS += \ config.h \ @@ -23,4 +24,5 @@ HEADERS += \ ui_utils.h \ helpers/accounthelper.h \ helpers/userhelper.h \ - entities/user.h + entities/user.h \ + mainmenu.h diff --git a/loginscreen.cpp b/loginscreen.cpp index 05c558b..3e215a8 100644 --- a/loginscreen.cpp +++ b/loginscreen.cpp @@ -9,6 +9,7 @@ #include "loginscreen.h" #include "ui_utils.h" +#include "mainmenu.h" using namespace std; @@ -69,6 +70,9 @@ bool LoginScreen::exec(bool startApp) endwin(); /* End curses mode */ + if(startApp) + showMainMenu(); + return true; } diff --git a/main.cpp b/main.cpp index 077bef2..b7294ee 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ #include "api_request.h" #include "loginscreen.h" +#include "mainmenu.h" using namespace std; @@ -51,6 +52,7 @@ int main(int argc, char **argv) } // Start directly main main menu + showMainMenu(); return 0; } diff --git a/mainmenu.cpp b/mainmenu.cpp new file mode 100644 index 0000000..4a9a7ef --- /dev/null +++ b/mainmenu.cpp @@ -0,0 +1,134 @@ +#include +#include + +#include +#include + +#include + +#include "mainmenu.h" + +using namespace std; + + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define CTRLD 4 + +static char *choices[] = { + "Logout", + "Quit" +}; +void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); + +void showMainMenu() +{ ITEM **my_items; + int c; + MENU *my_menu; + WINDOW *my_menu_win; + int n_choices, i; + + /* Initialize curses */ + initscr(); + start_color(); + cbreak(); + noecho(); + keypad(stdscr, TRUE); + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_CYAN, COLOR_BLACK); + + /* Create items */ + n_choices = ARRAY_SIZE(choices); + my_items = static_cast(calloc(static_cast(n_choices), sizeof(ITEM *))); + for(i = 0; i < n_choices; ++i) + my_items[i] = new_item(choices[i], choices[i]); + + /* Create menu */ + my_menu = new_menu(static_cast(my_items)); + + /* Create the window to be associated with the menu */ + int w,h; + getmaxyx(stdscr, w, h); + my_menu_win = newwin(10, 40, (w-10)/2, (h-40)/2); + keypad(my_menu_win, TRUE); + + /* Set main window and sub window */ + set_menu_win(my_menu, my_menu_win); + set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); + set_menu_format(my_menu, 5, 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, 40, "ComunicTerm", COLOR_PAIR(1)); + mvwaddch(my_menu_win, 2, 0, ACS_LTEE); + mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); + mvwaddch(my_menu_win, 2, 39, 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(i = 0; i < n_choices; ++i) + free_item(my_items[i]); + endwin(); + + cout << action << endl; +} + +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/mainmenu.h b/mainmenu.h new file mode 100644 index 0000000..f972616 --- /dev/null +++ b/mainmenu.h @@ -0,0 +1,6 @@ +#pragma once + +/** + * Show main menu + */ +void showMainMenu();