Basic Menu System

This commit is contained in:
2026-03-27 11:33:07 +05:30
parent cde7733a2d
commit 6299c7fc19
2 changed files with 113 additions and 0 deletions
+74
View File
@@ -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
+39
View File
@@ -0,0 +1,39 @@
#include <string>
#include <stdlib.h>
#include <stdio.h>
#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;
}