0.8
Creating New Widgets

This chapter discusses how to extend and customize widgets.

EGT is designed to support extension by way of custom widgets and functionality. Essentially, to change the behavior or look and feel of an existing widget, create a derived class of a similar and existing widget and reimplement the functions that should be changed. If you are creating a completely new type of widget, you would inherit directly from Widget or one of the other base widget types.

An example of this happening is fluent throughout the EGT library itself. For example, CheckBox implements a standard traditional checkBox that shows an X when the checkBox is CheckBox::checked(). However, to change the look and keep the same logical operation, ToggleBox derives from CheckBox and changes the ToggleBox::draw() method.

The new widget class that only intends to change the look of a widget would look something like this:

class ToggleBox : public CheckBox
{
public:
explicit ToggleBox(const Rect& rect = Rect());
virtual void draw(Painter& painter, const Rect& rect) override;
virtual ~ToggleBox();
};

This new widget is basically a checkBox, but we want to change how it looks and operates. The new ToggleBox::draw() method looks like this:

void ToggleBox::draw(Painter& painter, const Rect& rect)
{
ignoreparam(rect);
painter.draw_basic_box(box(), palette().color(Palette::BORDER),
palette().color(Palette::BG));
if (checked())
{
Rect rect = box();
rect.w /= 2;
rect.x += rect.w;
painter.draw_gradient_box(rect, palette().color(Palette::BORDER),
false,
palette().color(Palette::HIGHLIGHT));
}
else
{
Rect rect = box();
rect.w /= 2;
painter.draw_gradient_box(rect, palette().color(Palette::BORDER),
false,
palette().color(Palette::MID));
}
}
egt::v1::ToggleBox::draw
virtual void draw(Painter &painter, const Rect &rect) override
Draw the widget.
egt::v1::ToggleBox::ToggleBox
ToggleBox(const Rect &rect={}) noexcept
egt::v1::Widget::checked
bool checked() const
Return the boolean checked state of the a widget.
Definition: widget.h:939
egt::v1::ignoreparam
void ignoreparam(T &&)
Utility function to safely ignore a parameter to a function.
Definition: utils.h:37
egt::v1::Widget::color
Palette::pattern_type color(Palette::ColorId id) const
Get a Widget color.
egt::v1::Rect
RectType< default_dim_type, detail::compatible::normal > Rect
Helper type for a default rect.
Definition: geometry.h:910
egt::v1::Widget::box
virtual const Rect & box() const
Bounding box for the Widget.
egt::v1::ToggleBox::~ToggleBox
virtual ~ToggleBox()=default