The following functions allow you to do text markup.
WidgetState
First, an introduction to the WidgetState enum is needed. The enum looks like this:
enum WidgetState
{
UIMGUI_TEXT_UTILS_WIDGET_STATE_NONE = 0,
UIMGUI_TEXT_UTILS_WIDGET_STATE_CLICKED = 1 << 0,
UIMGUI_TEXT_UTILS_WIDGET_STATE_HOVERED = 1 << 1,
UIMGUI_TEXT_UTILS_WIDGET_STATE_ALL = UIMGUI_TEXT_UTILS_WIDGET_STATE_CLICKED | UIMGUI_TEXT_UTILS_WIDGET_STATE_HOVERED,
};
Some widgets return this enum when it is not possible to use the normal dear imgui IsItemHovered and
IsMouseClicked functions. You can use bitwise operations to check whether a certain state is present.
Underlining text
There are 4 functions that can be used to underline text:
Underline(Colour colour)- this underlines the element abovetemplate<typename ...Args> WidgetState Underline(const char* fmt, Colour colour, Args... args)- this is a wrapper on top ofImGui::Text. It uses the standard format string and the colour argument for the line. Additionally, it takes a templated variadic list of arguments that will be passed toImGui::Text’s variadic listUnderlineWrapped(const char* text, const char* end, Colour colour)- Renders underlined text with wrapping. The last argument is the colour for the line, the first 2 are pointers to the beginning and end of the string, respectively.UnderlineWrapped(const std::string& text, Colour)- An abstraction of the function above, usingstd::stringinstead of pointers to the beginning and end of the string
The Colour argument in all these functions corresponds to the colour of the line, which is, by default, set to the
UIMGUI_TEXT_COLOUR macro, which simply fetches the default text colour. The Colour type is the same as an ImU32.
All functions, except the first one, return a WidgetState enum representing their state.
Strikethrough text
There are 4 functions that can be used to render strikethrough text:
Strikethrough(Colour colour)- this applies a strikethrough line to the element abovetemplate<typename ...Args> WidgetState Strikethrough(const char* fmt, Colour colour, Args... args)- this is a wrapper on top ofImGui::Text. It uses the standard format string and the colour argument for the line. Additionally, it takes a templated variadic list of arguments that will be passed toImGui::Text’s variadic list.StrikethroughWrapped(const char* text, const char* end, Colour colour)- Renders strikethrough text with wrapping. The last argument is the colour for the line, the first 2 are pointers to the beginning and end of the string, respectively.StrikethroughWrapped(const std::string& text, Colour)- An abstraction of the function above, usingstd::stringinstead of pointers to the beginning and end of the string
The Colour argument in all these functions corresponds to the colour of the line, which is, by default,
set to the UIMGUI_TEXT_COLOUR macro, which simply fetches the default text colour.
All functions, except the first one, return a WidgetState enum representing their state.
Highlight
There are 4 functions that can be used to render highlighted text:
Highlight(Colour colour)- this applies a highlight to the element abovetemplate<typename ...Args> WidgetState Highlight(const char* fmt, Colour colour, Args... args)- this is a wrapper on top ofImGui::Text. It uses the standard format string and the colour argument for the highlight. Additionally, it takes a templated variadic list of arguments that will be passed toImGui::Text’s variadic list.HighlightWrapped(const char* text, const char* end, Colour colour)- Renders highlighted text with wrapping. The last argument is the colour for the highlight, the first 2 are pointers to the beginning and end of the string, respectively.HighlightWrapped(const std::string& text, Colour)- An abstraction of the function above, usingstd::stringinstead of pointers to the beginning and end of the string
The Colour argument in all these functions corresponds to the colour of the highlight, which is, by default,
set to the UIMGUI_HIGHLIGHT_TEXT_COLOUR, which returns the following RGB colour: rgb(255, 255, 0, 64).
All functions, except the first one, return a WidgetState enum representing their state.
Links
There are 3 functions that can be used to render links:
Link(const char* text, Colour colour, const std::function<void(const char* link)>& clicked)- Renders a link without word wrappingLinkWrapped(const char* text, const char* end, Colour colour, const std::function<void(const char* link)>& clicked)- Renders a link with word wrappingLinkWrapped(const std::string& text, Colour colour, const std::function<void(const char* link)>& clicked)- An abstraction on top of the above function to usestd::stringinstead of providingbeginandendpointers.
The Colour argument in all these functions corresponds to the default unvisited colour used on most web browsers,
which is, by default, set to the UIMGUI_LINK_TEXT_UNVISITED macro, which returns the following RGB colour: rgb(0, 0, 238, 255). Additionally, the UIMGUI_LINK_TEXT_VISITED macro is provided, for use when a link is visited. It returns the following RGB colour: rgb(85, 26, 139, 255).
The clicked function is the function that will be called when the link is clicked. It has a default argument that
corresponds to the defaultLinkClickEvent member of the TextUtilsData struct that you have previously set.
You can override it if needed.
Note
Link rendering safely handles URL strings containing percent signs (such as percent-encoded URLs like %20), ensuring they are not evaluated as format specifiers.
Inline code
There are 4 functions which can be used to render inline code:
CodeInline(const char* begin, const char* end, Colour backgroundColour)- Renders inline code given pointers to the beginning and end of the stringCodeInline(const std::string& text, Colour backgroundColour)- Abstraction on top of the above function usingstd::stringinstead of pointers to the beginning and end of the string- The 2
CodeInlineWrappedfunctions are the same as the first 2 functions, but they render text with word wrapping enabled
Inline code is rendered with the monospaced font with a background which is provided by the backgroundColour argument.
backgroundColour defaults to being set to the UIMGUI_BLOCKQUOTE_TEXT_COLOUR macro, which produces a colour with the
following RGB: rgb(69, 71, 90, 255).
Inline code differs from CodeBlock as it does not fill the whole available space. Instead, the background colour
is only applied to the extent of each line of text.
Code blocks
There are 2 functions which can be used to render code blocks:
CodeBlock(const char* begin, const char* end, bool bWrapText, Colour backgroundColour)- Renders a code block given pointers to the beginning and end of the stringCodeBlock(const std::string& text, bool bWrapText, Colour backgroundColour)- Abstraction on top of the function above, usingstd::stringinstead of pointers to the beginning and end of the string
The bWrapText boolean toggles whether to apply word wrapping to the code block. The backgroundColour argument is
the background colour of the code block. It defaults to being set to the UIMGUI_BLOCKQUOTE_TEXT_COLOUR macro,
which produces a colour with the following RGB: rgb(69, 71, 90, 255).
Showcase
An example of normal, underlined, highlighted, strikethrough and inline code text, as well as links and code blocks with word wrapping enabled:
