cxWidgets 1.0
cxUtils.h
Go to the documentation of this file.
1// Copyright (c) 2026 E. Oulashin
2#ifndef __CXUTILS_H__
3#define __CXUTILS_H__
4
5/*
6 * cxUtils.h - Contains cxWidgets utility functions.
7 * For the library to work, you will need to put
8 * "cx::init();" at the beginning of your program and
9 * "cx::cleanup();" at the end of your program.
10 * This does the screen initialization/cleanup.
11 *
12 * Copyright (c) 2005-2007 Michael H. Kinney
13 *
14 *
15 * Date User Description
16 * 08/21/07 erico Added new versions of messageBox() that let you specify
17 * colors for the different parts of the message box. Also
18 * added some default colors.
19 */
20
21
22#include "cxWindow.h"
23#include "cxColors.h"
24#include "cxInputOptions.h"
25#include "cxInputTypes.h"
26#include "cxWidgetsException.h" // Included here so that everything gets it
28#include <string>
29#include <set>
30#include <list>
31
35namespace cx
36{
37
47 void init(bool pEnableMouse = true);
48
53 void cleanup();
54
59 int top();
60
65 int centerRow();
66
71 int bottom();
72
77 int left();
78
83 int centerCol();
84
89 int right();
90
95 int width();
96
101 int height();
102
109 void getTermDimensions(int& pHeight, int& pWidth);
110
119 bool resizeTerm(int pNewHeight, int pNewWidth);
120
127
134
141
147 void setDefaultTitleColor(e_cxColors pColor);
148
155
162
169
176
189 void messageBox(int pRow, int pCol, int pHeight, int pWidth, const std::string& pTitle,
190 const std::string& pMessage, const std::string& pStatus = "",
191 eBorderStyle pBorderStyle = eBS_SINGLE_LINE);
192
201 void messageBox(int pRow, int pCol, const std::string& pTitle,
202 const std::string& pMessage, const std::string& pStatus);
203
211 void messageBox(int pRow, int pCol, const std::string& pMessage);
212
213 void messageBox(const std::string& pTitle, const std::string& pMessage,
214 const std::string& pStatus = "");
215
224 void messageBox(const std::string& pTitle, const std::string& pMessage,
225 const std::string& pStatus, int pHeight, int pWidth);
226
227
235 void messageBox(const std::string& pMessage, const std::string& pStatus, int pHeight, int pWidth);
236
247 void messageBox(const std::string& pMessage,
248 e_cxColors pMessageColor = getDefaultMessageColor(),
249 attr_t pMessageAttr = A_NORMAL,
250 e_cxColors pTitleColor = getDefaultTitleColor(),
251 e_cxColors pStatusColor = getDefaultStatusColor(),
252 e_cxColors pBorderColor = getDefaultBorderColor());
253
258 void messageDialog(const std::string& pMessage);
259
264 void messageDialog(const std::list<std::string>& pMessages);
265
272 void splash(const std::string& pTitle, const std::string& pMessage, int pSleep);
273
279 void splash(const std::string& pMessage, int pSleep);
280
284 void eraseScreen();
285
289 void updateWindows();
290
299 bool isFunctionKey(int pKey);
300
310 bool toggleCursor(bool pToggleCursor);
311
317 std::string getTermType();
318
322 static const std::string termType = getTermType();
323
331 bool colorIsBright(e_cxColors pColor);
332
341 int countHotkeys(const std::string& pStr);
342
351 std::string stringWithoutHotkeyChars(const std::string& pStr);
352
363 unsigned visualStrLen(const std::string& pStr);
364
375 std::string bringToTop(void* theWindow, void* unused);
376
390 std::string cxFormShowModal(void* theForm, void* unused);
391
405 std::string cxMenuShowModal(void* theMenu, void* unused);
406
416 std::string noOp(void *p1, void *p2);
417
429 std::string noOp(void *p1, void *p2, void *p3, void *p4);
430
446 int runSystemCmd(const std::string& pCmd);
447
459 void writeText(int pRow, int pCol, const std::string& pText, attr_t pAttr = A_NORMAL, e_cxColors pColor = eGRAY_BLACK);
460
474 void getHotkeyChars(const std::string& pStr, std::set<char>& pHotkeys,
475 bool pConvertCase = false, bool pToUpper = false);
476
484 void showAllWindows(bool pBringToTop = false);
485
494
503
511 std::string getKeyStr(int pKey);
512
520 std::string getMouseStateStr(int pMouseState);
521
529 std::string getReturnCodeStr(long pReturnCode);
530
539 std::string getInputOptionStr(eInputOptions pInputOption);
540
548 std::string getBorderStyleStr(eBorderStyle pBorderStyle);
549
557 std::string getInputTypeStr(eInputTypes pInputType);
558
564 void signalHandler(int pSignal);
565
580 bool promptYesNo(const std::string& pMessage, const std::string& pTitle = "",
581 long pButtons = cxYES_NO);
582
588 bool cxInitialized();
589
598 void addAttr(e_WidgetItems pItem, attr_t pAttr);
599
610 void setAttr(e_WidgetItems pItem, attr_t pAttr);
611
619 void removeAttr(e_WidgetItems pItem, attr_t pAttr);
620
627 void removeAttrs(e_WidgetItems pItem);
628
636 void getAttrs(e_WidgetItems pItem, std::set<attr_t>& pAttrs);
637
638
647 bool hasAttr(e_WidgetItems pItem, attr_t pAttr);
648
658 void enableAttrs(WINDOW *pWindow, e_WidgetItems pItem);
659
669 void disableAttrs(WINDOW *pWindow, e_WidgetItems pItem);
670
679 attr_t getAttr(const std::string& pAttr);
680
688 std::string getAttrStr(attr_t pAttr);
689
698 std::string getWidgetItemStr(e_WidgetItems pItem);
699
707 void setMenuClearKeywordKey(int pKey);
708
717
727 std::string dump(bool pFancy=false, const std::string& pFilename="");
728
740 bool isNavKey(int pKey);
741
748 void addNavKey(int pKey);
749
755 void removeNavKey(int pKey);
756
768 std::string getString(int pRow, int pCol, int pNumber, WINDOW *pWin = nullptr);
769
781 bool findString(const std::string& pSearch, int& pRow, int& pCol, WINDOW *pWin = nullptr);
782
783} // namespace cx
784
785#endif // __CXUTILS_H__
Represents a text-based window on the screen. Can contain a title, status, and a message to appear wi...
Definition cxWindow.h:195
Defines an enumeration for different input options (editable and.
Defines an enumeration for different input input types (text,.
cxBorderChars.h - Defines border characters to be used in drawing a box (i.e., in cxWindow and all it...
Definition cxApp.cpp:5
std::string cxMenuShowModal(void *theMenu, void *unused)
Runs showModal() on a cxMenu and returns the result. This.
Definition cxUtils.cpp:607
std::string stringWithoutHotkeyChars(const std::string &pStr)
Takes a string and returns a version without the & characters.
std::string getTermType()
Returns a string representing the terminal type. This is the.
Definition cxUtils.cpp:508
e_cxColors getDefaultTitleColor()
Returns the default title color.
Definition cxUtils.cpp:333
void signalHandler(int pSignal)
Handler for system signals.
Definition cxUtils.cpp:1154
attr_t getAttr(const std::string &pAttr)
Figure out the appropriate ncurses attr_t from a "string".
void setAttr(e_WidgetItems pItem, attr_t pAttr)
Sets the ncurses attribute to use for one of the items in the.
Definition cxUtils.cpp:1237
std::string getWidgetItemStr(e_WidgetItems pItem)
Returns a string version of an e_WidgetItems value.
Definition cxUtils.cpp:1585
void messageDialog(const std::string &pMessage)
Shows a message dialog (a message box with an OK button)
void disableAttrs(WINDOW *pWindow, e_WidgetItems pItem)
Disables all of a set of attributes for a given item type on an.
Definition cxUtils.cpp:1472
int top()
Returns the top row # of the main screen.
Definition cxUtils.cpp:246
void messageBox(int pRow, int pCol, int pHeight, int pWidth, const std::string &pTitle, const std::string &pMessage, const std::string &pStatus="", eBorderStyle pBorderStyle=eBS_SINGLE_LINE)
Shows a message box.
void removeNavKey(int pKey)
Removes a key from cx's list of "navigational" keys.
Definition cxUtils.cpp:1900
e_cxColors getDefaultStatusColor()
Returns the default status color.
Definition cxUtils.cpp:343
e_WidgetItems
Definition cxWidgetItems.h:42
void enableAttrs(WINDOW *pWindow, e_WidgetItems pItem)
Enables all of a set of attributes for a given item type on an.
Definition cxUtils.cpp:1420
bool isFunctionKey(int pKey)
Returns whether a key is a function key, pageDown, or pageUp (these keys are tested by cxInput and cx...
Definition cxUtils.cpp:483
void addNavKey(int pKey)
Add a key to be considered a "navigational" key. The key will.
Definition cxUtils.cpp:1895
eInputTypes
Definition cxInputTypes.h:20
bool findString(const std::string &pSearch, int &pRow, int &pCol, WINDOW *pWin=nullptr)
Look for a string on the screen (for debugging/testing)
void removeAttrs(e_WidgetItems pItem)
Removes all attributes for a given window item.
Definition cxUtils.cpp:1328
cxWindow * getTopWindow()
Gets a pointer to the topmost cxWindow on the stack. This could.
Definition cxUtils.cpp:721
std::string getMouseStateStr(int pMouseState)
Returns a string version of a mouse state.
Definition cxUtils.cpp:983
void cleanup()
Cleans up the cxWidgets library. Must be called after all.
Definition cxUtils.cpp:231
void updateWindows()
Updates (refreshes) all windows on the screen.
Definition cxUtils.cpp:472
int bottom()
Returns the bottom row # of the main screen.
Definition cxUtils.cpp:261
void getAttrs(e_WidgetItems pItem, std::set< attr_t > &pAttrs)
Returns the set of ncurses attributes for a given item.
int runSystemCmd(const std::string &pCmd)
Runs a command on the system, calling the system() function.
std::string bringToTop(void *theWindow, void *unused)
Brings a cxWindow to the top of the stack. Doesn't refresh.
Definition cxUtils.cpp:583
bool isNavKey(int pKey)
Returns whether a key is a navigational key, according to.
Definition cxUtils.cpp:1890
std::string getKeyStr(int pKey)
Returns a string version of a key.
Definition cxUtils.cpp:751
cxWindow * getBottomWindow()
Gets a pointer to the bottom-most cxWindow on the stack. This could.
Definition cxUtils.cpp:736
static const std::string termType
Contains the terminal type string.
Definition cxUtils.h:322
void setDefaultBorderColor(e_cxColors pColor)
Sets the default border color.
Definition cxUtils.cpp:358
eInputOptions
Definition cxInputOptions.h:20
void setDefaultMessageColor(e_cxColors pColor)
Sets the default message color.
Definition cxUtils.cpp:328
void init(bool pEnableMouse=true)
Initializes the cxWidgets library. This must be called before.
Definition cxUtils.cpp:79
std::string dump(bool pFancy=false, const std::string &pFilename="")
dump the contents to the screen to a file
bool cxInitialized()
Returns whether or not cxWidgets has been initialized.
Definition cxUtils.cpp:1187
std::string getInputTypeStr(eInputTypes pInputType)
Returns a string version of an eInputType value.
Definition cxUtils.cpp:1131
std::string noOp(void *p1, void *p2)
Does nothing. Can be used, for example, as an event function.
Definition cxUtils.cpp:620
std::string getReturnCodeStr(long pReturnCode)
Returns a string version of a return code.
Definition cxUtils.cpp:1059
void setDefaultTitleColor(e_cxColors pColor)
Sets the default title color.
Definition cxUtils.cpp:338
e_cxColors getDefaultMessageColor()
Returns the default message color.
Definition cxUtils.cpp:323
int right()
Returns the rightmost column # of the main screen.
Definition cxUtils.cpp:286
bool colorIsBright(e_cxColors pColor)
Returns whether a color has the brightness bit set.
Definition cxUtils.cpp:527
void eraseScreen()
Erases the screen.
Definition cxUtils.cpp:461
eBorderStyle
Definition cxBorderStyles.h:26
@ eBS_SINGLE_LINE
Definition cxBorderStyles.h:28
int countHotkeys(const std::string &pStr)
Counts the number of hotkeys in a string. This is useful when working with cxMenu,...
int height()
Returns the height of the main screen.
Definition cxUtils.cpp:305
std::string getBorderStyleStr(eBorderStyle pBorderStyle)
Returns a string version of an eBorderStyle value.
Definition cxUtils.cpp:1108
void getHotkeyChars(const std::string &pStr, std::set< char > &pHotkeys, bool pConvertCase=false, bool pToUpper=false)
Scans a string for hotkeys (characters preceeded by an ampersand)
bool toggleCursor(bool pToggleCursor)
Enables or disables the display of the cursor.
Definition cxUtils.cpp:501
std::string getString(int pRow, int pCol, int pNumber, WINDOW *pWin=nullptr)
Get the contents of the screen (for debugging/testing)
Definition cxUtils.cpp:1905
void setDefaultStatusColor(e_cxColors pColor)
Sets the default status color.
Definition cxUtils.cpp:348
bool resizeTerm(int pNewHeight, int pNewWidth)
Resizes the terminal screen.
Definition cxUtils.cpp:318
@ cxYES_NO
Definition cxMessageDialogStyles.h:18
void writeText(int pRow, int pCol, const std::string &pText, attr_t pAttr=A_NORMAL, e_cxColors pColor=eGRAY_BLACK)
Writes text on the screen.
e_cxColors getDefaultBorderColor()
Returns the default border color.
Definition cxUtils.cpp:353
bool promptYesNo(const std::string &pMessage, const std::string &pTitle="", long pButtons=cxYES_NO)
Prompts the user with a cxMessageDialog for a yes/no choice.
int width()
Returns the width of the main screen.
Definition cxUtils.cpp:296
void splash(const std::string &pTitle, const std::string &pMessage, int pSleep)
Shows a splash message.
std::string getAttrStr(attr_t pAttr)
Returns a string version of an ncurses attribute.
Definition cxUtils.cpp:1554
bool hasAttr(e_WidgetItems pItem, attr_t pAttr)
Determine if the widget has an attribute "set".
Definition cxUtils.cpp:1410
void removeAttr(e_WidgetItems pItem, attr_t pAttr)
Removes an ncurses attribute from one of the item lists.
Definition cxUtils.cpp:1283
int left()
Returns the leftmost column # of the main screen.
Definition cxUtils.cpp:270
std::string getInputOptionStr(eInputOptions pInputOption)
Returns a string version of a value from the eInputOptions.
Definition cxUtils.cpp:1088
void showAllWindows(bool pBringToTop=false)
Shows all windows currently in use everywhere, from the.
Definition cxUtils.cpp:692
void getTermDimensions(int &pHeight, int &pWidth)
Returns the terminal's current height & width.
Definition cxUtils.cpp:312
int centerCol()
Returns the center column # of the main screen.
Definition cxUtils.cpp:278
int getMenuClearKeywordKey()
Returns the key that is set to clear the "Keyword" input for.
Definition cxUtils.cpp:1631
int centerRow()
Returns the center row # of the main screen.
Definition cxUtils.cpp:253
unsigned visualStrLen(const std::string &pStr)
Returns the length of a string without its hotkey characters -.
std::string cxFormShowModal(void *theForm, void *unused)
Runs showModal() on a cxForm and returns the result. This.
Definition cxUtils.cpp:594
void addAttr(e_WidgetItems pItem, attr_t pAttr)
Adds an ncurses attribute to use for one of the items in the.
Definition cxUtils.cpp:1192
e_cxColors
Definition cxColors.h:46
@ eGRAY_BLACK
Definition cxColors.h:67
void setMenuClearKeywordKey(int pKey)
Sets the key that should be used to clear the "Keyword" input.
Definition cxUtils.cpp:1626