commit 4c6df876e806a51a83bd124e1606167121fd2d03 Author: Harsh Virdi Date: Fri Mar 27 09:16:49 2026 +0530 Basic MenuBar diff --git a/menu.h b/menu.h new file mode 100644 index 0000000..8543296 --- /dev/null +++ b/menu.h @@ -0,0 +1,74 @@ +#include +#include + +#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 diff --git a/tui.cpp b/tui.cpp new file mode 100644 index 0000000..b61f300 --- /dev/null +++ b/tui.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#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; +}