Basic MenuBar
This commit is contained in:
@@ -0,0 +1,74 @@
|
|||||||
|
#include <string>
|
||||||
|
#include <curses.h>
|
||||||
|
|
||||||
|
#ifndef _MENU_H_
|
||||||
|
#define _MENU_H_
|
||||||
|
|
||||||
|
class Menu {
|
||||||
|
public:
|
||||||
|
Menu(std::string text, char trigger){
|
||||||
|
this->text = text;
|
||||||
|
this->trigger = trigger;
|
||||||
|
}
|
||||||
|
|
||||||
|
int start_x;
|
||||||
|
std::string text;
|
||||||
|
char trigger;
|
||||||
|
};
|
||||||
|
|
||||||
|
class MenuBar {
|
||||||
|
public:
|
||||||
|
MenuBar(WINDOW *win, Menu *menus, int num_menus){
|
||||||
|
this->win = win;
|
||||||
|
this->menus = menus;
|
||||||
|
this->num_menus = num_menus;
|
||||||
|
this->selected_menu = -1;
|
||||||
|
|
||||||
|
int current_pos = 2;
|
||||||
|
|
||||||
|
for(int i = 0; i < num_menus; i++)
|
||||||
|
{
|
||||||
|
this->menus[i].start_x = current_pos;
|
||||||
|
current_pos += this->menus[i].text.length() + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw()
|
||||||
|
{
|
||||||
|
for(int i = 0; i < num_menus; i++)
|
||||||
|
{
|
||||||
|
int start_x = this->menus[i].start_x;
|
||||||
|
std::string text = this->menus[i].text;
|
||||||
|
if(selected_menu == i)
|
||||||
|
{
|
||||||
|
wattron(win,A_STANDOUT);
|
||||||
|
}
|
||||||
|
mvwprintw(win,0,start_x,text.c_str());
|
||||||
|
if(selected_menu == i)
|
||||||
|
{
|
||||||
|
wattroff(win,A_STANDOUT);
|
||||||
|
}
|
||||||
|
wrefresh(win);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleTrigger(char trigger)
|
||||||
|
{
|
||||||
|
selected_menu = -1;
|
||||||
|
for(int i = 0; i < num_menus; i++)
|
||||||
|
{
|
||||||
|
if(trigger == this->menus[i].trigger)
|
||||||
|
{
|
||||||
|
selected_menu = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WINDOW *win;
|
||||||
|
Menu *menus;
|
||||||
|
int num_menus;
|
||||||
|
int selected_menu;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string>
|
||||||
|
#include <curses.h>
|
||||||
|
#include "menu.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
initscr();
|
||||||
|
noecho();
|
||||||
|
curs_set(0);
|
||||||
|
|
||||||
|
int yMax, xMax;
|
||||||
|
|
||||||
|
getmaxyx(stdscr, yMax, xMax);
|
||||||
|
|
||||||
|
WINDOW *win = newwin(yMax/2, xMax/2, yMax/4, xMax/4);
|
||||||
|
box(win,0,0);
|
||||||
|
|
||||||
|
Menu menus[3] = {
|
||||||
|
Menu("File",'f'),
|
||||||
|
Menu("Edit",'e'),
|
||||||
|
Menu("Options",'o'),
|
||||||
|
};
|
||||||
|
|
||||||
|
MenuBar menubar = MenuBar(win, menus, 3);
|
||||||
|
menubar.draw();
|
||||||
|
|
||||||
|
char ch;
|
||||||
|
while(ch = wgetch(win))
|
||||||
|
{
|
||||||
|
menubar.handleTrigger(ch);
|
||||||
|
menubar.draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
endwin();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user