cxWidgets
1.0
home
erico
projects
cxWidgets
src
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
27
namespace
cx
{
28
33
class
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__
cx::cxMenuBar
This is a menu bar class, designed for displaying a horizontal.
Definition
cxMenuBar.h:34
cx::cxMenuBar::removeExitKey
virtual void removeExitKey(int pKey) override
Removes an exit key.
Definition
cxMenuBar.cpp:441
cx::cxMenuBar::clearKeyFunctions
virtual void clearKeyFunctions() override
Clears the list of external functions fired by hotkeys.
Definition
cxMenuBar.cpp:431
cx::cxMenuBar::move
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
cx::cxMenuBar::bringToTop
virtual void bringToTop(bool pRefresh=true) override
Brings the window to the top.
Definition
cxMenuBar.cpp:386
cx::cxMenuBar::removeAttr
virtual void removeAttr(e_WidgetItems pItem, attr_t pAttr) override
Removes an ncurses attribute from one of the item lists.
Definition
cxMenuBar.cpp:476
cx::cxMenuBar::hide
virtual void hide(bool pHideSubwindows=true) override
Hides the window.
Definition
cxMenuBar.cpp:350
cx::cxMenuBar::draw
virtual void draw() override
Fills the member ncurses window structure with the current.
Definition
cxMenuBar.cpp:345
cx::cxMenuBar::setKeyFunction
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
cx::cxMenuBar::clear
virtual void clear(bool pRefresh=false) override
Clears the text from the window.
Definition
cxMenuBar.cpp:381
cx::cxMenuBar::addMenu
void addMenu(const std::string &pLabel, cxMenu *pMenu)
Adds a menu to the menu bar.
Definition
cxMenuBar.cpp:51
cx::cxMenuBar::resize
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
cx::cxMenuBar::getParent
virtual cxWindow * getParent() const override
Returns a pointer to the parent window.
Definition
cxMenuBar.cpp:461
cx::cxMenuBar::removeAttrs
virtual void removeAttrs(e_WidgetItems pItem) override
Removes all attributes for a given window item.
Definition
cxMenuBar.cpp:481
cx::cxMenuBar::enableAttrs
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
cx::cxMenuBar::setLastKey
virtual void setLastKey(int pLastKey) override
Sets the last keypress.
Definition
cxMenuBar.cpp:406
cx::cxMenuBar::getAttrs
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
cx::cxMenuBar::erase
virtual void erase(bool pEraseSubwindows=true) override
Erases the window.
Definition
cxMenuBar.cpp:361
cx::cxMenuBar::numMenus
int numMenus() const
Returns the number of menus in the menu bar.
Definition
cxMenuBar.cpp:86
cx::cxMenuBar::setBarColor
void setBarColor(e_cxColors pColor)
Sets the color for the menu bar background.
Definition
cxMenuBar.cpp:120
cx::cxMenuBar::setMenuHotkey
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
cx::cxMenuBar::~cxMenuBar
virtual ~cxMenuBar()
Destructor.
Definition
cxMenuBar.cpp:46
cx::cxMenuBar::setEnabled
virtual void setEnabled(bool pEnabled) override
Enables or disables the window. If the window is.
Definition
cxMenuBar.cpp:446
cx::cxMenuBar::modalGetsKeypress
virtual bool modalGetsKeypress() const override
Returns whether or not a call to showModal() will wait for a.
Definition
cxMenuBar.cpp:340
cx::cxMenuBar::addAttr
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
cx::cxMenuBar::setCurrentIndex
void setCurrentIndex(int pIndex)
Sets the currently selected menu index.
Definition
cxMenuBar.cpp:96
cx::cxMenuBar::setAttr
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
cx::cxMenuBar::cxTypeStr
virtual std::string cxTypeStr() const override
Returns the name of the cxWidgets class. This is overridden.
Definition
cxMenuBar.cpp:456
cx::cxMenuBar::exitNow
virtual void exitNow() override
This is mainly for deriving classes. It tells the object.
Definition
cxMenuBar.cpp:416
cx::cxMenuBar::show
virtual long show(bool pBringToTop=false, bool pShowSubwindows=true) override
Shows the window.
Definition
cxMenuBar.cpp:220
cx::cxMenuBar::setHighlightColor
void setHighlightColor(e_cxColors pColor)
Sets the color for the highlighted/selected menu item.
Definition
cxMenuBar.cpp:125
cx::cxMenuBar::getItemColor
virtual e_cxColors getItemColor(e_WidgetItems pItem) const override
Returns the color of one of the items in a window.
Definition
cxMenuBar.cpp:501
cx::cxMenuBar::disableAttrs
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
cx::cxMenuBar::clearKeyFunction
virtual void clearKeyFunction(int pKey) override
Removes a function pointer for a keypress so that it will no.
Definition
cxMenuBar.cpp:426
cx::cxMenuBar::removeQuitKey
virtual void removeQuitKey(int pKey) override
Removes a quit key.
Definition
cxMenuBar.cpp:436
cx::cxMenuBar::unhide
virtual void unhide(bool pUnhideSubwindows=true) override
Un-hides the window.
Definition
cxMenuBar.cpp:355
cx::cxMenuBar::showModalWithClick
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
cx::cxMenuBar::setColor
virtual void setColor(e_WidgetItems pItem, e_cxColors pColor) override
Sets the color of one of the window items.
Definition
cxMenuBar.cpp:396
cx::cxMenuBar::setBorderStyle
virtual void setBorderStyle(eBorderStyle pBorderStyle) override
Sets the border style.
Definition
cxMenuBar.cpp:401
cx::cxMenuBar::setDisableCursorOnShow
virtual void setDisableCursorOnShow(bool pDisableCursorOnShow) override
Sets whether the window should disable the cursor.
Definition
cxMenuBar.cpp:451
cx::cxMenuBar::quitNow
virtual void quitNow() override
This is mainly for deriving classes. This causes the object.
Definition
cxMenuBar.cpp:411
cx::cxMenuBar::showModal
virtual long showModal(bool pShowSelf=true, bool pBringToTop=true, bool pShowSubwindows=true) override
Shows the window and waits for input.
Definition
cxMenuBar.cpp:228
cx::cxMenuBar::getMenuLabel
std::string getMenuLabel(int pIndex) const
Gets the label of a menu item (without hotkey chars).
Definition
cxMenuBar.cpp:102
cx::cxMenuBar::hasFocus
virtual bool hasFocus() const override
Returns true if window has focus, false otherwise.
Definition
cxMenuBar.cpp:391
cx::cxMenuBar::getLastMenuReturnCode
long getLastMenuReturnCode() const
Returns the return code from the last menu selection. After showModal() returns, this holds the retur...
Definition
cxMenuBar.cpp:115
cx::cxMenuBar::getCurrentIndex
int getCurrentIndex() const
Gets the currently selected menu index.
Definition
cxMenuBar.cpp:91
cx::cxMenu
Represents a list of items, one of which may be selected.
Definition
cxMenu.h:62
cx::cxWindow
Represents a text-based window on the screen. Can contain a title, status, and a message to appear wi...
Definition
cxWindow.h:195
cxFunction.h
cxMenu.h
cxWindow.h
DEFAULT_WIDTH
#define DEFAULT_WIDTH
Definition
cxWindow.h:69
cx
cxBorderChars.h - Defines border characters to be used in drawing a box (i.e., in cxWindow and all it...
Definition
cxApp.cpp:5
cx::e_WidgetItems
e_WidgetItems
Definition
cxWidgetItems.h:42
cx::eBorderStyle
eBorderStyle
Definition
cxBorderStyles.h:26
cx::eBS_NOBORDER
@ eBS_NOBORDER
Definition
cxBorderStyles.h:27
cx::e_cxColors
e_cxColors
Definition
cxColors.h:46
Generated by
1.9.8