Class Widget

Inheritance Relationships

Base Type

Derived Types

Class Documentation

class Widget : public nanogui::Object

Base class of all widgets.

Widget is the base class of all widgets in nanogui. It can also be used as an panel to arrange an arbitrary number of child widgets using a layout generator (see Layout).

Subclassed by nanogui::Button, nanogui::CheckBox, nanogui::ColorWheel, nanogui::GLCanvas, nanogui::Graph, nanogui::ImagePanel, nanogui::ImageView, nanogui::Label, nanogui::ProgressBar, nanogui::Screen, nanogui::Slider, nanogui::StackedWidget, nanogui::TabHeader, nanogui::TabWidget, nanogui::TextBox, nanogui::VScrollPanel, nanogui::Window

Public Functions

Widget(Widget *parent)

Construct a new widget with the given parent widget.

Widget *parent()

Return the parent widget.

const Widget *parent() const

Return the parent widget.

void setParent(Widget *parent)

Set the parent widget.

Layout *layout()

Return the used Layout generator.

const Layout *layout() const

Return the used Layout generator.

void setLayout(Layout *layout)

Set the used Layout generator.

Theme *theme()

Return the Theme used to draw this widget.

const Theme *theme() const

Return the Theme used to draw this widget.

virtual void setTheme(Theme *theme)

Set the Theme used to draw this widget.

const Vector2i &position() const

Return the position relative to the parent widget.

void setPosition(const Vector2i &pos)

Set the position relative to the parent widget.

Vector2i absolutePosition() const

Return the absolute position on screen.

const Vector2i &size() const

Return the size of the widget.

void setSize(const Vector2i &size)

set the size of the widget

int width() const

Return the width of the widget.

void setWidth(int width)

Set the width of the widget.

int height() const

Return the height of the widget.

void setHeight(int height)

Set the height of the widget.

void setFixedSize(const Vector2i &fixedSize)

Set the fixed size of this widget.

If nonzero, components of the fixed size attribute override any values computed by a layout generator associated with this widget. Note that just setting the fixed size alone is not enough to actually change its size; this is done with a call to setSize or a call to performLayout() in the parent widget.

const Vector2i &fixedSize() const

Return the fixed size (see setFixedSize())

int fixedWidth() const
int fixedHeight() const
void setFixedWidth(int width)

Set the fixed width (see setFixedSize())

void setFixedHeight(int height)

Set the fixed height (see setFixedSize())

bool visible() const

Return whether or not the widget is currently visible (assuming all parents are visible)

void setVisible(bool visible)

Set whether or not the widget is currently visible (assuming all parents are visible)

bool visibleRecursive() const

Check if this widget is currently visible, taking parent widgets into account.

int childCount() const

Return the number of child widgets.

const std::vector<Widget *> &children() const

Return the list of child widgets of the current widget.

virtual void addChild(int index, Widget *widget)

Add a child widget to the current widget at the specified index.

This function almost never needs to be called by hand, since the constructor of Widget automatically adds the current widget to its parent

void addChild(Widget *widget)

Convenience function which appends a widget at the end.

void removeChild(int index)

Remove a child widget by index.

void removeChild(const Widget *widget)

Remove a child widget by value.

const Widget *childAt(int index) const

Retrieves the child at the specific position.

Widget *childAt(int index)

Retrieves the child at the specific position.

int childIndex(Widget *widget) const

Returns the index of a specific child or -1 if not found.

template<typename WidgetClass, typename ...Args>
WidgetClass *add(const Args&... args)

Variadic shorthand notation to construct and add a child widget.

Window *window()

Walk up the hierarchy and return the parent window.

Screen *screen()

Walk up the hierarchy and return the parent screen.

void setId(const std::string &id)

Associate this widget with an ID value (optional)

const std::string &id() const

Return the ID value associated with this widget, if any.

bool enabled() const

Return whether or not this widget is currently enabled.

void setEnabled(bool enabled)

Set whether or not this widget is currently enabled.

bool focused() const

Return whether or not this widget is currently focused.

void setFocused(bool focused)

Set whether or not this widget is currently focused.

void requestFocus()

Request the focus to be moved to this widget.

const std::string &tooltip() const
void setTooltip(const std::string &tooltip)
int fontSize() const

Return current font size. If not set the default of the current theme will be returned.

void setFontSize(int fontSize)

Set the font size of this widget.

bool hasFontSize() const

Return whether the font size is explicitly specified for this widget.

float iconExtraScale() const

The amount of extra scaling applied to icon fonts. See nanogui::Widget::mIconExtraScale.

void setIconExtraScale(float scale)

Sets the amount of extra scaling applied to icon fonts. See nanogui::Widget::mIconExtraScale.

Cursor cursor() const

Return a pointer to the cursor of the widget.

void setCursor(Cursor cursor)

Set the cursor of the widget.

bool contains(const Vector2i &p) const

Check if the widget contains a certain position.

Widget *findWidget(const Vector2i &p)

Determine the widget located at the given position value (recursive)

virtual bool mouseButtonEvent(const Vector2i &p, int button, bool down, int modifiers)

Handle a mouse button event (default implementation: propagate to children)

virtual bool mouseMotionEvent(const Vector2i &p, const Vector2i &rel, int button, int modifiers)

Handle a mouse motion event (default implementation: propagate to children)

virtual bool mouseDragEvent(const Vector2i &p, const Vector2i &rel, int button, int modifiers)

Handle a mouse drag event (default implementation: do nothing)

virtual bool mouseEnterEvent(const Vector2i &p, bool enter)

Handle a mouse enter/leave event (default implementation: record this fact, but do nothing)

virtual bool scrollEvent(const Vector2i &p, const Vector2f &rel)

Handle a mouse scroll event (default implementation: propagate to children)

virtual bool focusEvent(bool focused)

Handle a focus change event (default implementation: record the focus status, but do nothing)

virtual bool keyboardEvent(int key, int scancode, int action, int modifiers)

Handle a keyboard event (default implementation: do nothing)

virtual bool keyboardCharacterEvent(unsigned int codepoint)

Handle text input (UTF-32 format) (default implementation: do nothing)

virtual Vector2i preferredSize(NVGcontext *ctx) const

Compute the preferred size of the widget.

virtual void performLayout(NVGcontext *ctx)

Invoke the associated layout generator to properly place child widgets, if any.

virtual void draw(NVGcontext *ctx)

Draw the widget (and all child widgets)

virtual void save(Serializer &s) const

Save the state of the widget into the given Serializer instance.

virtual bool load(Serializer &s)

Restore the state of the widget from the given Serializer instance.

Protected Functions

virtual ~Widget()

Free all resources used by the widget and any children.

float icon_scale() const

Convenience definition for subclasses to get the full icon scale for this class of Widget. It simple returns the value mTheme->mIconScale * this->mIconExtraScale.

Remark

See also: nanogui::Theme::mIconScale and nanogui::Widget::mIconExtraScale. This tiered scaling strategy may not be appropriate with fonts other than entypo.ttf.

Protected Attributes

Widget *mParent
ref<Theme> mTheme
ref<Layout> mLayout
std::string mId
Vector2i mPos
Vector2i mSize
Vector2i mFixedSize
std::vector<Widget *> mChildren
bool mVisible

Whether or not this Widget is currently visible. When a Widget is not currently visible, no time is wasted executing its drawing method.

bool mEnabled

Whether or not this Widget is currently enabled. Various different kinds of derived types use this to determine whether or not user input will be accepted. For example, when mEnabled == false, the state of a CheckBox cannot be changed, or a TextBox will not allow new input.

bool mFocused
bool mMouseFocus
std::string mTooltip
int mFontSize
float mIconExtraScale

The amount of extra icon scaling used in addition the the theme’s default icon font scale. Default value is 1.0, which implies that nanogui::Widget::icon_scale simply returns the value of nanogui::Theme::mIconScale.

Most widgets do not need extra scaling, but some (e.g., CheckBox, TextBox) need to adjust the Theme’s default icon scaling (nanogui::Theme::mIconScale) to properly display icons within their bounds (upscale, or downscale).

Note

When using nvgFontSize for icons in subclasses, make sure to call the nanogui::Widget::icon_scale() function. Expected usage when drawing icon fonts is something like:

virtual void draw(NVGcontext *ctx) {
    // fontSize depends on the kind of Widget.  Search for `FontSize`
    // in the Theme class (e.g., standard vs button)
    float ih = fontSize;
    // assuming your Widget has a declared `mIcon`
    if (nvgIsFontIcon(mIcon)) {
        ih *= icon_scale();
        nvgFontFace(ctx, "icons");
        nvgFontSize(ctx, ih);
        /// remaining drawing code (see button.cpp for more)
    }
}

Cursor mCursor