cxWidgets 1.0
cxMenuBar.h
Go to the documentation of this file.
1// Copyright (c) 2026 E. Oulashin
2#ifndef __CXMENUBAR_H__
3#define __CXMENUBAR_H__
4
5// cxMenuBar: A horizontal menu bar derived from cxWindow.
6// Displays menu items horizontally across a single row at the top of the
7// screen. Each item can have an associated cxMenu that drops down when
8// the item is selected. Navigation is via left/right arrow keys, Enter
9// to open a menu, and ESC to close.
10//
11// Usage:
12// cxMenuBar menuBar(nullptr, 0, 0, termWidth);
13// cxMenu fileMenu(...);
14// fileMenu.append("&Open", 1, "", cxITEM_NORMAL, true);
15// menuBar.addMenu("&File", &fileMenu);
16// menuBar.addMenu("&Help", &helpMenu);
17// long result = menuBar.showModal();
18
19#include "cxWindow.h"
20#include "cxMenu.h"
21#include "cxFunction.h"
22#include <string>
23#include <vector>
24#include <memory>
25#include <map>
26
27namespace cx {
28
33class cxMenuBar : public cxWindow
34{
35 public:
36
46 explicit cxMenuBar(cxWindow *pParentWindow = nullptr,
47 int pRow = 0, int pCol = 0,
48 int pWidth = DEFAULT_WIDTH,
49 eBorderStyle pBorderStyle = eBS_NOBORDER);
50
54 cxMenuBar(const cxMenuBar& pThat);
55
59 virtual ~cxMenuBar();
60
61 // Overridden virtual functions from cxWindow
62
63 virtual long show(bool pBringToTop = false, bool pShowSubwindows = true) override;
64 virtual long showModal(bool pShowSelf = true, bool pBringToTop = true,
65 bool pShowSubwindows = true) override;
66 virtual bool modalGetsKeypress() const override;
67 virtual void draw() override;
68 virtual void hide(bool pHideSubwindows = true) override;
69 virtual void unhide(bool pUnhideSubwindows = true) override;
70 virtual void erase(bool pEraseSubwindows = true) override;
71 virtual bool move(int pNewRow, int pNewCol, bool pRefresh = true) override;
72 virtual void resize(int pNewHeight, int pNewWidth, bool pRefresh = true) override;
73 virtual void clear(bool pRefresh = false) override;
74 virtual void bringToTop(bool pRefresh = true) override;
75 virtual bool hasFocus() const override;
76 virtual void setColor(e_WidgetItems pItem, e_cxColors pColor) override;
77 virtual void setBorderStyle(eBorderStyle pBorderStyle) override;
78 virtual void setLastKey(int pLastKey) override;
79 virtual void quitNow() override;
80 virtual void exitNow() override;
81 virtual bool setKeyFunction(int pKey, const std::shared_ptr<cxFunction>& pFunction) override;
82 virtual void clearKeyFunction(int pKey) override;
83 virtual void clearKeyFunctions() override;
84 virtual void removeQuitKey(int pKey) override;
85 virtual void removeExitKey(int pKey) override;
86 virtual void setEnabled(bool pEnabled) override;
87 virtual void setDisableCursorOnShow(bool pDisableCursorOnShow) override;
88 virtual std::string cxTypeStr() const override;
89 virtual cxWindow* getParent() const override;
90 virtual void addAttr(e_WidgetItems pItem, attr_t pAttr) override;
91 virtual void setAttr(e_WidgetItems pItem, attr_t pAttr) override;
92 virtual void removeAttr(e_WidgetItems pItem, attr_t pAttr) override;
93 virtual void removeAttrs(e_WidgetItems pItem) override;
94 virtual void getAttrs(e_WidgetItems pItem, std::set<attr_t>& pAttrs) const override;
95 virtual void enableAttrs(WINDOW *pWin, e_WidgetItems pItem) override;
96 virtual void disableAttrs(WINDOW *pWin, e_WidgetItems pItem) override;
97 virtual e_cxColors getItemColor(e_WidgetItems pItem) const override;
98
99 // MenuBar-specific functions
100
108 void addMenu(const std::string& pLabel, cxMenu* pMenu);
109
113 int numMenus() const;
114
119 int getCurrentIndex() const;
120
125 void setCurrentIndex(int pIndex);
126
132 std::string getMenuLabel(int pIndex) const;
133
149 long showModalWithClick(int pClickX, bool pShowSelf = true,
150 bool pBringToTop = true,
151 bool pShowSubwindows = true);
152
163 void setMenuHotkey(int pKey, int pMenuIndex);
164
170 long getLastMenuReturnCode() const;
171
176 void setBarColor(e_cxColors pColor);
177
182 void setHighlightColor(e_cxColors pColor);
183
184 private:
185
186 // A menu bar entry: label + pointer to the dropdown menu
187 struct MenuEntry
188 {
189 std::string label; // Display label (with & hotkey markers)
190 std::string cleanLabel; // Label without hotkey chars
191 char hotkeyChar; // The hotkey character (lowercase), or '\0'
192 cxMenu* menu; // Pointer to the dropdown menu (not owned)
193 int xPos; // X position of this label in the bar
194 };
195
196 std::vector<MenuEntry> mEntries;
197
198 // Currently highlighted menu index
199 int mCurrentIndex;
200
201 // Return code from the last dropdown menu selection
202 long mLastMenuReturnCode;
203
204 // Hotkeys that map to menu indices
205 std::map<int, int> mMenuHotkeys;
206
207 // Colors
208 short mBarColorPair;
209 short mHighlightColorPair;
210
211 // Whether the bar currently has focus
212 bool mHasFocus;
213
214 // Helper: draw the bar contents
215 void drawBar();
216
217 // Helper: open the dropdown menu for the current index
218 // Returns the return code from the dropdown menu
219 long openDropdown(int pIndex);
220
221 // Helper: the main input loop
222 long inputLoop();
223};
224
225} // namespace cx
226
227#endif // __CXMENUBAR_H__
This is a menu bar class, designed for displaying a horizontal.
Definition cxMenuBar.h:34
virtual void removeExitKey(int pKey) override
Removes an exit key.
Definition cxMenuBar.cpp:441
virtual void clearKeyFunctions() override
Clears the list of external functions fired by hotkeys.
Definition cxMenuBar.cpp:431
virtual bool move(int pNewRow, int pNewCol, bool pRefresh=true) override
Changes the window's position, based on a new upper-left corner.
Definition cxMenuBar.cpp:366
virtual void bringToTop(bool pRefresh=true) override
Brings the window to the top.
Definition cxMenuBar.cpp:386
virtual void removeAttr(e_WidgetItems pItem, attr_t pAttr) override
Removes an ncurses attribute from one of the item lists.
Definition cxMenuBar.cpp:476
virtual void hide(bool pHideSubwindows=true) override
Hides the window.
Definition cxMenuBar.cpp:350
virtual void draw() override
Fills the member ncurses window structure with the current.
Definition cxMenuBar.cpp:345
virtual bool setKeyFunction(int pKey, const std::shared_ptr< cxFunction > &pFunction) override
Sets a function to be called when a key is pressed.
Definition cxMenuBar.cpp:421
virtual void clear(bool pRefresh=false) override
Clears the text from the window.
Definition cxMenuBar.cpp:381
void addMenu(const std::string &pLabel, cxMenu *pMenu)
Adds a menu to the menu bar.
Definition cxMenuBar.cpp:51
virtual void resize(int pNewHeight, int pNewWidth, bool pRefresh=true) override
Changes the window's width and height. The window's upper-left.
Definition cxMenuBar.cpp:376
virtual cxWindow * getParent() const override
Returns a pointer to the parent window.
Definition cxMenuBar.cpp:461
virtual void removeAttrs(e_WidgetItems pItem) override
Removes all attributes for a given window item.
Definition cxMenuBar.cpp:481
virtual void enableAttrs(WINDOW *pWin, e_WidgetItems pItem) override
Enables the attributes for one of the m*Attrs sets for an ncurses window.
Definition cxMenuBar.cpp:491
virtual void setLastKey(int pLastKey) override
Sets the last keypress.
Definition cxMenuBar.cpp:406
virtual void getAttrs(e_WidgetItems pItem, std::set< attr_t > &pAttrs) const override
Returns the set of ncurses attributes for a given item.
Definition cxMenuBar.cpp:486
virtual void erase(bool pEraseSubwindows=true) override
Erases the window.
Definition cxMenuBar.cpp:361
int numMenus() const
Returns the number of menus in the menu bar.
Definition cxMenuBar.cpp:86
void setBarColor(e_cxColors pColor)
Sets the color for the menu bar background.
Definition cxMenuBar.cpp:120
void setMenuHotkey(int pKey, int pMenuIndex)
Sets a global hotkey that activates a specific menu. When this key is pressed while the menu bar does...
Definition cxMenuBar.cpp:109
virtual ~cxMenuBar()
Destructor.
Definition cxMenuBar.cpp:46
virtual void setEnabled(bool pEnabled) override
Enables or disables the window. If the window is.
Definition cxMenuBar.cpp:446
virtual bool modalGetsKeypress() const override
Returns whether or not a call to showModal() will wait for a.
Definition cxMenuBar.cpp:340
virtual void addAttr(e_WidgetItems pItem, attr_t pAttr) override
Adds an ncurses attribute to use for one of the items in the.
Definition cxMenuBar.cpp:466
void setCurrentIndex(int pIndex)
Sets the currently selected menu index.
Definition cxMenuBar.cpp:96
virtual void setAttr(e_WidgetItems pItem, attr_t pAttr) override
Sets the ncurses attribute to use for one of the items in the.
Definition cxMenuBar.cpp:471
virtual std::string cxTypeStr() const override
Returns the name of the cxWidgets class. This is overridden.
Definition cxMenuBar.cpp:456
virtual void exitNow() override
This is mainly for deriving classes. It tells the object.
Definition cxMenuBar.cpp:416
virtual long show(bool pBringToTop=false, bool pShowSubwindows=true) override
Shows the window.
Definition cxMenuBar.cpp:220
void setHighlightColor(e_cxColors pColor)
Sets the color for the highlighted/selected menu item.
Definition cxMenuBar.cpp:125
virtual e_cxColors getItemColor(e_WidgetItems pItem) const override
Returns the color of one of the items in a window.
Definition cxMenuBar.cpp:501
virtual void disableAttrs(WINDOW *pWin, e_WidgetItems pItem) override
Disables the attributes for one of the m*Attrs sets for an ncurses window.
Definition cxMenuBar.cpp:496
virtual void clearKeyFunction(int pKey) override
Removes a function pointer for a keypress so that it will no.
Definition cxMenuBar.cpp:426
virtual void removeQuitKey(int pKey) override
Removes a quit key.
Definition cxMenuBar.cpp:436
virtual void unhide(bool pUnhideSubwindows=true) override
Un-hides the window.
Definition cxMenuBar.cpp:355
long showModalWithClick(int pClickX, bool pShowSelf=true, bool pBringToTop=true, bool pShowSubwindows=true)
Shows the menu bar and immediately opens the dropdown for whichever menu item is at screen column pCl...
Definition cxMenuBar.cpp:242
virtual void setColor(e_WidgetItems pItem, e_cxColors pColor) override
Sets the color of one of the window items.
Definition cxMenuBar.cpp:396
virtual void setBorderStyle(eBorderStyle pBorderStyle) override
Sets the border style.
Definition cxMenuBar.cpp:401
virtual void setDisableCursorOnShow(bool pDisableCursorOnShow) override
Sets whether the window should disable the cursor.
Definition cxMenuBar.cpp:451
virtual void quitNow() override
This is mainly for deriving classes. This causes the object.
Definition cxMenuBar.cpp:411
virtual long showModal(bool pShowSelf=true, bool pBringToTop=true, bool pShowSubwindows=true) override
Shows the window and waits for input.
Definition cxMenuBar.cpp:228
std::string getMenuLabel(int pIndex) const
Gets the label of a menu item (without hotkey chars).
Definition cxMenuBar.cpp:102
virtual bool hasFocus() const override
Returns true if window has focus, false otherwise.
Definition cxMenuBar.cpp:391
long getLastMenuReturnCode() const
Returns the return code from the last menu selection. After showModal() returns, this holds the retur...
Definition cxMenuBar.cpp:115
int getCurrentIndex() const
Gets the currently selected menu index.
Definition cxMenuBar.cpp:91
Represents a list of items, one of which may be selected.
Definition cxMenu.h:62
Represents a text-based window on the screen. Can contain a title, status, and a message to appear wi...
Definition cxWindow.h:195
#define DEFAULT_WIDTH
Definition cxWindow.h:69
cxBorderChars.h - Defines border characters to be used in drawing a box (i.e., in cxWindow and all it...
Definition cxApp.cpp:5
e_WidgetItems
Definition cxWidgetItems.h:42
eBorderStyle
Definition cxBorderStyles.h:26
@ eBS_NOBORDER
Definition cxBorderStyles.h:27
e_cxColors
Definition cxColors.h:46