1
0
mirror of https://gitlab.com/comunic/comunicterm synced 2024-11-16 10:31:06 +00:00
comunicterm/mainmenu.cpp

143 lines
3.2 KiB
C++
Raw Permalink Normal View History

2020-01-10 15:03:03 +00:00
#include <stdlib.h>
#include <string.h>
#include <menu.h>
#include <curses.h>
#include <iostream>
2020-01-10 15:06:45 +00:00
#include <helpers/accounthelper.h>
#include "loginscreen.h"
2020-01-10 15:03:03 +00:00
#include "mainmenu.h"
2020-01-12 19:24:35 +00:00
#include "conversations_screen.h"
2020-01-13 21:50:26 +00:00
#include "ui_utils.h"
2020-01-10 15:03:03 +00:00
using namespace std;
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
static char *choices[] = {
2020-01-10 15:14:28 +00:00
"Conversations",
2020-01-10 15:03:03 +00:00
"Logout",
2020-01-10 15:14:28 +00:00
"Quit",
nullptr
2020-01-10 15:03:03 +00:00
};
void showMainMenu()
2020-01-10 15:14:28 +00:00
{
while(true) {
ITEM **my_items;
2020-01-10 15:03:03 +00:00
int c;
MENU *my_menu;
2020-01-10 15:14:28 +00:00
WINDOW *my_menu_win;
int n_choices, i;
2020-01-10 15:03:03 +00:00
/* Initialize curses */
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_CYAN, COLOR_BLACK);
2020-01-15 16:15:36 +00:00
erase();
2020-01-10 15:03:03 +00:00
/* Create items */
n_choices = ARRAY_SIZE(choices);
my_items = static_cast<ITEM **>(calloc(static_cast<size_t>(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<ITEM **>(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();
2020-01-10 15:06:45 +00:00
/* Process requested action */
int o = 0;
2020-01-10 15:14:28 +00:00
// Open user conversations
2020-01-10 15:06:45 +00:00
if(o++ == action) {
2020-01-12 19:24:35 +00:00
showConversationsScreen();
2020-01-10 15:14:28 +00:00
}
// Logout
else if(o++ == action) {
2020-01-10 15:06:45 +00:00
AccountHelper::SignOut();
2020-01-10 15:14:28 +00:00
LoginScreen().exec(false);
2020-01-10 15:06:45 +00:00
}
// Quit application
else {
cout << "Good bye !" << endl;
exit(0);
}
2020-01-10 15:14:28 +00:00
}
2020-01-10 15:03:03 +00:00
}