Skip to content

Latest commit

 

History

History
135 lines (97 loc) · 3.51 KB

getting-started-with-widgets.md

File metadata and controls

135 lines (97 loc) · 3.51 KB

Getting Started with Widgets

We'll begin with the simplest use of this library, a lone Widget that paints some text to the screen:

#include <termox/termox.hpp>
using namespace ox;

class App : public Widget {
    auto paint_event(Painter& p) -> bool override
    {
        p.put("Hello, World!", {10, 5});
        return Widget::paint_event(p);
    }
};

int main() { return System{}.run<App>(); }

This program will paint the text Hello, World! to the screen at coordinates {10, 5}. We can expand on this to add color and traits to the text:

#include <termox/termox.hpp>
using namespace ox;

class App : public Widget {
    auto paint_event(Painter& p) -> bool override
    {
        auto text = U"╔═Hello═World═╝" | fg(Color::Green) | Trait::Bold;
        p.put(text, {10, 5});
        return Widget::paint_event(p);
    }
};

int main() { return System{}.run<App>(); }

This code will now print a bold ╔═Hello═World═╝ with green foreground starting at 10 cell positions to the right and 5 cell positions down from the top left of the terminal screen.

Breakdown

#include <termox/termox.hpp>
using namespace ox;

Make all ox namespace names accessible. The ox names used in this example are: Widget, Painter, System, Color, and Trait.


class App : public Widget {

Inherit from ox::Widget, creating a new Widget type.


 auto paint_event(Painter& p) -> bool override

Override the paint_event handler. This method is called every time the Widget needs to be re-painted, it is one of many event handlers that Widget has. The library internals take care of calling this method when needed.


auto text = U"╔═Hello═World═╝" | fg(Color::Green) | Trait::Bold;

Create a Glyph_string with a foreground Color and bold Trait. Traits and Colors are applied with the pipe operator, a wide string literal is used for the text.


p.put(text, {10, 5});

Put text Glyph to the screen at the given point. The put method takes a Glyph or Glyph_string and a Point. The coordinates are local to the Widget, with the origin at the top left, x extends to the right with positive values, and y extends downward with positive values. Each paint_event call begins with a blank display.


return Widget::paint_event(p);

Call down to the base class' implementation, this is common practice for all event handlers in TermOx.


int main() { return System{}.run<App>(); }

Run the library with your application as the head Widget, forwarding all user input as events to your Widget.


An entire application could be built within a single Widget by painting everything needed in the single paint_event method, and overriding other needed event handlers. This would quickly become overly complex for most apps.

Widgets perform best when they have a single, well-defined purpose. Layouts can be used to arrange the pieces of your application, and Signals to communicate between Widgets.

Most applications only need a handful of custom Widgets, the Widget Library provides a collection of common UI elements for re-use.

See Next

Previous