DlangUI

Cross Platform GUI for D programming language

Home API Docs Screenshots Download .zip View on GitHub

dlangui.widgets.widget

This module contains declaration of Widget class - base class for all widgets.

Widgets are styleable. Use styleId property to set style to use from current Theme.

When any of styleable attributes is being overriden, widget's own copy of style is being created to hold modified attributes (defaults to parent style).

Two phase layout model (like in Android UI) is used - measure() call is followed by layout() is used to measure and layout widget and its children.abstract

Method onDraw will be called to draw widget on some surface. Widget.onDraw() draws widget background (if any).



Synopsis:
import dlangui.widgets.widget;

// access attributes as properties
auto w = new Widget("id1");
w.backgroundColor = 0xFFFF00;
w.layoutWidth = FILL_PARENT;
w.layoutHeight = FILL_PARENT;
w.padding(Rect(10,10,10,10));
// same, but using chained method call
auto w = new Widget("id1").backgroundColor(0xFFFF00).layoutWidth(FILL_PARENT).layoutHeight(FILL_PARENT).padding(Rect(10,10,10,10));




License:
Boost License 1.0

Authors:
Vadim Lopatin, coolreader.org@gmail.com

enum Visibility: ubyte;
Visibility (see Android View Visibility)

Visible
Visible on screen (default)

Invisible
Not visible, but occupies a space in layout

Gone
Completely hidden, as not has been added

interface OnClickHandler;
interface - slot for onClick

interface OnCheckHandler;
interface - slot for onCheckChanged

interface OnFocusHandler;
interface - slot for onFocusChanged

interface OnKeyHandler;
interface - slot for onKey

interface OnMouseHandler;
interface - slot for onMouse

enum FocusMovement: int;
focus movement options

None
no focus movement

Next
next focusable (Tab)

Previous
previous focusable (Shift+Tab)

Up
move to nearest above

Down
move to nearest below

Left
move to nearest at left

Right
move to nearest at right

enum CursorType: int;
standard mouse cursor types

Parent
use parent's cursor

class Widget;
Base class for all widgets.

protected string _id;
widget id

protected Rect _pos;
current widget position, set by layout()

protected Visibility _visibility;
widget visibility: either Visible, Invisible, Gone

protected string _styleId;
style id to lookup style in theme

protected Style _ownStyle;
own copy of style - to override some of style properties, null of no properties overriden

protected uint _state;
widget state (set of flags from State enum)

protected int _measuredWidth;
width measured by measure()

protected int _measuredHeight;
height measured by measure()

protected bool _needLayout;
true to force layout

protected bool _needDraw;
true to force redraw

protected Widget _parent;
parent widget

protected Window _window;
window (to be used for top level widgets only!)

protected bool _trackHover;
does widget need to track mouse Hover

const @property bool trackHover();
mouse movement processing flag (when true, widget will change Hover state while mouse is moving)

@property Widget trackHover(bool v);
set new trackHover flag value (when true, widget will change Hover state while mouse is moving)

uint getCursorType(int x, int y);
returns mouse cursor type for widget

this(string ID = null);
create widget, with optional id

protected const @property const(Style) style();
accessor to style - by lookup in theme by styleId (if style id is not set, theme base style will be used).

protected const @property const(Style) style(uint stateFlags);
accessor to style - by lookup in theme by styleId (if style id is not set, theme base style will be used).

protected const @property const(Style) stateStyle();
returns style for current widget state

protected @property Style ownStyle();
enforces widget's own style - allows override some of style properties

const @property string id();
returns widget id, null if not set

@property Widget id(string id);
set widget id

const bool compareId(string id);
compare widget id with specified value, returs true if matches

const @property uint state();
widget state (set of flags from State enum)

protected void handleFocusChange(bool focused);
override to handle focus changes

protected void handleCheckChange(bool checked);
override to handle check changes

@property Widget state(uint newState);
set new widget state (set of flags from State enum)

@property Widget setState(uint stateFlagsToSet);
add state flags (set of flags from State enum)

@property Widget resetState(uint stateFlagsToUnset);
remove state flags (set of flags from State enum)

const @property string styleId();
returns widget style id, null if not set

@property Widget styleId(string id);
set widget style id

const @property Rect margins();
get margins (between widget bounds and its background)

@property Widget margins(Rect rc);
set margins for widget - override one from style

const @property Rect padding();
get padding (between background bounds and content of widget)

@property Widget padding(Rect rc);
set padding for widget - override one from style

const @property uint backgroundColor();
returns background color

@property Widget backgroundColor(uint color);
set background color for widget - override one from style

const @property string backgroundImageId();
background image id

@property Widget backgroundImageId(string imageId);
background image id

const @property DrawableRef backgroundDrawable();
background drawable

const @property uint alpha();
widget drawing alpha value (0=opaque .. 255=transparent)

@property Widget alpha(uint value);
set widget drawing alpha value (0=opaque .. 255=transparent)

const @property uint textColor();
get text color (ARGB 32 bit value)

@property Widget textColor(uint value);
set text color (ARGB 32 bit value)

@property uint textFlags();
get text flags (bit set of TextFlag enum values)

@property Widget textFlags(uint value);
set text flags (bit set of TextFlag enum values)

const @property string fontFace();
returns font face

@property Widget fontFace(string face);
set font face for widget - override one from style

const @property bool fontItalic();
returns font style (italic/normal)

@property Widget fontItalic(bool italic);
set font style (italic/normal) for widget - override one from style

const @property ushort fontWeight();
returns font weight

@property Widget fontWeight(ushort weight);
set font weight for widget - override one from style

const @property ushort fontSize();
returns font size in pixels

@property Widget fontSize(ushort size);
set font size for widget - override one from style

const @property FontFamily fontFamily();
returns font family

@property Widget fontFamily(FontFamily family);
set font family for widget - override one from style

const @property ubyte alignment();
returns alignment (combined vertical and horizontal)

@property Widget alignment(ubyte value);
sets alignment (combined vertical and horizontal)

@property Align valign();
returns horizontal alignment

@property Align halign();
returns vertical alignment

const @property FontRef font();
returns font set for widget using style or set manually

@property dstring text();
returns widget content text (override to support this)

@property Widget text(dstring s);
sets widget content text (override to support this)

@property Widget text(UIString s);
sets widget content text (override to support this)

@property bool needLayout();
returns true if layout is required for widget and its children

@property bool needDraw();
returns true if redraw is required for widget and its children

@property bool animating();
returns true is widget is being animated - need to call animate() and redraw

void animate(long interval);
animates window; interval is time left from previous draw, in hnsecs (1/10000000 of second)

@property auto measuredWidth();
returns measured width (calculated during measure() call)

@property auto measuredHeight();
returns measured height (calculated during measure() call)

@property int width();
returns current width of widget in pixels

@property int height();
returns current height of widget in pixels

@property int top();
returns widget rectangle top position

@property int left();
returns widget rectangle left position

@property Rect pos();
returns widget rectangle

@property int minWidth();
returns min width constraint

@property int maxWidth();
returns max width constraint (SIZE_UNSPECIFIED if no constraint set)

@property int minHeight();
returns min height constraint

@property int maxHeight();
returns max height constraint (SIZE_UNSPECIFIED if no constraint set)

@property Widget maxWidth(int value);
set max width constraint (SIZE_UNSPECIFIED for no constraint)

@property Widget minWidth(int value);
set max width constraint (0 for no constraint)

@property Widget maxHeight(int value);
set max height constraint (SIZE_UNSPECIFIED for no constraint)

@property Widget minHeight(int value);
set max height constraint (0 for no constraint)

@property int layoutWidth();
returns layout width options (WRAP_CONTENT, FILL_PARENT, or some constant value)

@property int layoutHeight();
returns layout height options (WRAP_CONTENT, FILL_PARENT, or some constant value)

@property int layoutWeight();
returns layout weight (while resizing to fill parent, widget will be resized proportionally to this value)

@property Widget layoutWidth(int value);
sets layout width options (WRAP_CONTENT, FILL_PARENT, or some constant value)

@property Widget layoutHeight(int value);
sets layout height options (WRAP_CONTENT, FILL_PARENT, or some constant value)

@property Widget layoutWeight(int value);
sets layout weight (while resizing to fill parent, widget will be resized proportionally to this value)

@property Visibility visibility();
returns widget visibility (Visible, Invisible, Gone)

@property Widget visibility(Visibility visible);
sets widget visibility (Visible, Invisible, Gone)

bool isPointInside(int x, int y);
returns true if point is inside of this widget

@property bool enabled();
return true if state has State.Enabled flag set

@property Widget enabled(bool flg);
change enabled state

@property bool clickable();
when true, user can click this control, and get onClick listeners called

@property bool checkable();
when true, control supports Checked state

@property bool checked();
get checked state

@property Widget checked(bool flg);
set checked state

@property bool focusable();
whether widget can be focused

@property bool wantsKeyTracking();
override and return true to track key events even when not focused

@property bool focusGroup();
When focus group is set for some parent widget, focus from one of containing widgets can be moved using keyboard only to one of other widgets containing in it and cannot bypass bounds of focusGroup.

If focused widget doesn't have any parent with focusGroup == true, focus may be moved to any focusable within window.

@property Widget focusGroup(bool flg);
set focus group flag for container widget

Widget focusGroupWidget();
find nearest parent of this widget with focusGroup flag, returns topmost parent if no focusGroup flag set to any of parents.

@property ushort tabOrder();
tab order - hint for focus movement using Tab/Shift+Tab

@property bool visible();
returns true if this widget and all its parents are visible

@property bool canFocus();
returns true if widget is focusable and visible

Widget setFocus();
sets focus to this widget or suitable focusable child, returns previously focused widget

Widget findFocusableChild(bool defaultOnly);
searches children for first focusable item, returns null if not found

bool handleAction(const Action a);
override to handle specific actions

protected Action findKeyAction(uint keyCode, uint flags);
map key to action

bool onKeyEvent(KeyEvent event);
process key event, return true if event is processed.

bool onMouseEvent(MouseEvent event);
process mouse event; return true if event is processed by widget.

Signal!OnClickHandler onClickListener;
on click event listener (bool delegate(Widget))

Signal!OnCheckHandler onCheckChangeListener;
checked state change event listener (bool delegate(Widget, bool))

Signal!OnFocusHandler onFocusChangeListener;
focus state change event listener (bool delegate(Widget, bool))

Signal!OnKeyHandler onKeyListener;
key event listener (bool delegate(Widget, KeyEvent)) - return true if event is processed by handler

Signal!OnMouseHandler onMouseListener;
mouse event listener (bool delegate(Widget, MouseEvent)) - return true if event is processed by handler

Widget addOnClickListener(bool delegate(Widget) listener);
helper function to add onCheckChangeListener in method chain

Widget addOnCheckChangeListener(bool delegate(Widget, bool) listener);
helper function to add onCheckChangeListener in method chain

Widget addOnFocusChangeListener(bool delegate(Widget, bool) listener);
helper function to add onFocusChangeListener in method chain

void requestLayout();
request relayout of widget and its children

void invalidate();
request redraw

protected void measuredContent(int parentWidth, int parentHeight, int contentWidth, int contentHeight);
helper function for implement measure() when widget's content dimensions are known

void measure(int parentWidth, int parentHeight);
Measure widget according to desired width and height constraints. (Step 1 of two phase layout).

void layout(Rect rc);
Set widget rectangle to specified value and layout widget contents. (Step 2 of two phase layout).

void onDraw(DrawBuf buf);
Draw widget at its position to buffer

void applyMargins(ref Rect rc);
Helper function: applies margins to rectangle

void applyPadding(ref Rect rc);
Helper function: applies padding to rectangle

static void applyAlign(ref Rect rc, Point sz, Align ha, Align va);
Applies alignment for content of size sz - set rectangle rc to aligned value of content inside of initial value of rc.

void applyAlign(ref Rect rc, Point sz);
Applies alignment for content of size sz - set rectangle rc to aligned value of content inside of initial value of rc.

bool canShowPopupMenu(int x, int y);
returns true if widget can show popup menu (e.g. by mouse right click at point x,y)

void showPopupMenu(int x, int y);
shows popup menu at (x,y)

bool isActionEnabled(const Action action);
override to change popup menu items state

@property int childCount();
returns number of children of this widget

Widget child(int index);
returns child by index

Widget addChild(Widget item);
adds child, returns added item

Widget removeChild(int index);
removes child, returns removed item

Widget removeChild(string id);
removes child by ID, returns removed item

int childIndex(Widget item);
returns index of widget in child list, -1 if passed widget is not a child of this widget

bool isChild(Widget item, bool deepSearch = true);
returns true if item is child of this widget (when deepSearch == true - returns true if item is this widget or one of children inside children tree).

Widget childById(string id, bool deepSearch = true);
find child by id, returns null if not found

@property Widget parent();
returns parent widget, null for top level widget

@property Widget parent(Widget parent);
sets parent for widget

@property Window window();
returns window (if widget or its parent is attached to window)

@property void window(Window window);
sets window (to be used for top level widget from Window implementation). TODO: hide it from API?

struct ObjectList(T);
object list holder, owning its objects - on destroy of holder, all own objects will be destroyed

const @property int count();
returns count of items

T get(int index);
get item by index

T add(T item);
add item to list

T insert(T item, int index = -1);
add item to list

int indexOf(T item);
find child index for item, return -1 if not found

int indexOf(string id);
find child index for item by id, return -1 if not found

T remove(int index);
remove item from list, return removed item

void replace(T item, int index);
Replace item with another value, destroy old value.

void clear();
remove and destroy all items

alias WidgetList = ObjectList!(Widget).ObjectList;
Widget list holder.

class WidgetGroup: dlangui.widgets.widget.Widget;
Base class for widgets which have children.

@property int childCount();
returns number of children of this widget

Widget child(int index);
returns child by index

Widget addChild(Widget item);
adds child, returns added item

Widget removeChild(int index);
removes child, returns removed item

Widget removeChild(string ID);
removes child by ID, returns removed item

int childIndex(Widget item);
returns index of widget in child list, -1 if passed widget is not a child of this widget