The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Tickit::Window - a window for drawing operations

SYNOPSIS

 use Tickit;
 use Tickit::Pen;

 my $tickit = Tickit->new;

 my $rootwin = $tickit->rootwin;

 $rootwin->set_on_expose( with_rb => sub {
    my ( $win, $rb, $rect ) = @_;

    $rb->clear;

    $rb->text_at(
       int( $win->lines / 2 ), int( ($win->cols - 12) / 2 ),
       "Hello, world"
    );
 });
 $rootwin->set_on_geom_changed( sub { shift->expose } );
 $rootwin->set_pen( Tickit::Pen->new( fg => "white" ) );

 $rootwin->expose;
 $tickit->run;

DESCRIPTION

Provides coordination of widget drawing activities. A Window represents a region of the screen that a widget occupies.

Windows cannot directly be constructed. Instead they are obtained by sub-division of other windows, ultimately coming from the root window associated with the terminal.

Normally all windows are visible, but a window may be hidden by calling the hide method. After this, the window will not respond to any of the drawing methods, until it is made visible again with the show method. A hidden window will not receive focus or input events. It may still receive geometry change events if it is resized.

Sub Windows

A division of a window made by calling make_sub or make_float obtains a window that represents some portion of the drawing area of the parent window. Child windows are stored in order; make_sub adds a new child to the end of the list, and make_float adds one at the start.

Higher windows (windows more towards the start of the list), will always handle input events before lower siblings. The extent of windows also obscures lower windows; drawing on lower windows may not be visible because higher windows are above it.

METHODS

$win->close

Removes the window from its parent and clears any event handlers set using any of the set_on_* methods. Also recursively closes any child windows.

Currently this is an optional method, as child windows are stored as weakrefs, so should be destroyed when the last reference to them is dropped. Widgets should make sure to call this method anyway, because this will be changed in a future version.

$sub = $win->make_sub( $top, $left, $lines, $cols )

Constructs a new sub-window of the given geometry, and places it at the end of the child window list; below any other siblings.

$sub = $win->make_hidden_sub( $top, $left, $lines, $cols )

Constructs a new sub-window like make_sub, but the window starts initially hidden. This avoids having to call hide separately afterwards.

$float = $win->make_float( $top, $left, $lines, $cols )

Constructs a new sub-window of the given geometry, and places it at the start of the child window list; above any other siblings.

$popup = $win->make_popup( $top, $left, $lines, $cols )

Constructs a new floating popup window starting at the given coordinates relative to this window. It will be sized to the given limits.

This window will have the root window as its parent, rather than the window the method was called on. Additionally, a popup window will steal all keyboard and mouse events that happen, regardless of focus or mouse position. It is possible that, if the window has an on_mouse handler, that it may receive mouse events from outwide the bounds of the window.

$win->raise

$win->lower

Moves the order of the window in its parent one higher or lower relative to its siblings.

$win->raise_to_front

Moves the order of the window in its parent to be the front-most among its siblings.

$win->lower_to_back

Moves the order of the window in its parent to be the back-most among its siblings.

$parentwin = $win->parent

Returns the parent window; i.e. the window on which make_sub or make_float was called to create this one

@windows = $win->subwindows

Returns a list of the subwindows of this one. They are returned in order, highest first.

$rootwin = $win->root

Returns the root window

$term = $win->term

Returns the Tickit::Term instance of the terminal on which this window lives.

Returns the Tickit instance with which this window is associated.

$win->show

Makes the window visible. Allows drawing methods to output to the terminal. Calling this method also exposes the window, invoking the on_expose handler. Shows the cursor if this window currently has focus.

$win->hide

Makes the window invisible. Prevents drawing methods outputting to the terminal. Hides the cursor if this window currently has focus.

$visible = $win->is_visible

Returns true if the window is currently visible.

$win->resize( $lines, $cols )

Change the size of the window.

$win->reposition( $top, $left )

Move the window relative to its parent.

$win->change_geometry( $top, $left, $lines, $cols )

A combination of resize and reposition, to atomically change all the coordinates of the window. Will only invoke on_geom_changed once, rather than twice as would be the case calling the above methods individually.

$win->set_on_geom_changed( $on_geom_changed )

Set the callback to invoke whenever the window is resized or repositioned; i.e. whenever its geometry changes.

 $on_geom_changed->( $win )

$win->set_on_key( with_ev => $on_key )

Set the callback to invoke whenever a key is pressed while this window, or one of its child windows, has the input focus. The callback will be passed the window and an event structure.

 $handled = $on_key->( $win, $event )

$event is an object supporting methods called type, str and mod. type will be "text" for normal unmodified Unicode, or "key" for special keys or modified Unicode. str will be the UTF-8 string for text events, or the textual description of the key as rendered by Term::TermKey for key events. mod will be a bitmask of the modifier state.

The invoked code should return a true value if it considers the keypress dealt with, or false to pass it up to its parent window.

Before passing it to its parent, a window will also try any other non-focused sibling windows of the currently-focused window in order of creation (though note this order is not necessarily the order the child widgets that own those windows were created or added to their container).

If no window actually handles the keypress, then every window will eventually be consulted about it, preferring windows closer to the focused one.

This broadcast-like behaviour allows widgets to handle keypresses that should make sense even though their window does not actually have the keyboard focus. This feature should be used sparingly, to only capture one or two keypresses that really make sense; for example to capture the PageUp and PageDown keys in a scrolling list, or a numbered function key that performs some special action.

$win->set_on_key( $on_key )

A backward-compatibility wrapper which passes the event details as positional arguments rather than as a structure.

 $handled = $on_key->( $win, $type, $str, $mod )

This form is now discouraged, and may be removed in a later version.

$win->set_on_mouse( with_ev => $on_mouse )

Set the callback to invoke whenever a mouse event is received within the window's rectangle. The callback will be passed the window and an event structure.

 $handled = $on_mouse->( $win, $event )

$event is an object supporting methods called type, button, line, col and mod. type will contain the event name. button will contain the button number, though it may not be present for release events. line and col are 0-based. mod is a bitmask of modifier state. Behaviour of events involving more than one mouse button is not well-specified by terminals.

The following event names may be observed:

press

A mouse button has been pressed down on this cell

drag_start

The mouse was moved while a button was held, and was initially in the given cell

drag

The mouse was moved while a button was held, and is now in the given cell

drag_outside

The mouse was moved outside of the window that handled the drag_start event, and is still being dragged.

drag_drop

A mouse button was released after having been moved, while in the given cell

drag_stop

The drag operation has finished. This event is always given directly to the window that handled the drag_start event, rather than the window on which the mouse release event happened.

release

A mouse button was released after being pressed

The invoked code should return a true value if it considers the mouse event dealt with, or false to pass it up to its parent window.

Once a dragging operation has begun via drag_start, the window that handled the event will always receive drag, drag_outside, and an eventual drag_stop event even if the mouse moves outside that window. No other window will receive a drag_outside or drag_stop event than the one that started the operation.

$win->set_on_mouse( $on_mouse )

A backward-compatibility wrapper which passes the event detials as positional arguments rather than as a structure.

 $handled = $on_mouse->( $win, $type, $button, $line, $col, $mod )

This form is now discouraged, and may be removed in a later version.

$win->set_on_expose( with_rb => $on_expose )

Set the callback to invoke whenever a region of the window is exposed by the expose event. When invoked, it is passed the window itself, a Tickit::RenderBuffer and a Tickit::Rect representing the portion of the window that was exposed.

 $on_expose->( $win, $rb, $rect )

The buffer's origin will be that of the window, and its clipping region will already be set to the expose rect. This will automatically be flushed to the window when the callback returns.

$win->set_on_expose( $on_expose )

Legacy form that is passes only the rect and not a render buffer to the callback. Callbacks in this form will have to call the direct window drawing methods themselves.

 $on_expose->( $win, $rect )

This form is now discouraged, and may be removed in a later version.

$win->expose( $rect )

Marks the given region of the window as having been exposed, to invoke the on_expose event handler on itself, and all its child windows. The window's own handler will be invoked first, followed by all the child windows, in screen order (top to bottom, then left to right).

If $rect is not supplied it defaults to exposing the entire window area.

The on_expose event handler isn't invoked immediately; instead, the Tickit later method is used to invoke it at the next round of IO event handling. Until then, any other window could be exposed. Duplicates are suppressed; so if a window and any of its ancestors are both queued for expose, the actual handler will only be invoked once per unique region of the window.

$win->set_on_focus( $on_refocus )

Set the callback to invoke whenever the window gains or loses focus.

 $on_refocus->( $win, $has_focus )

Will be passed a boolean value, true if the focus was just gained, false if the focus was just lost.

$win->set_expose_after_scroll( $expose_after_scroll )

If set to a true value, the scrollrect method will expose the region of the window that requires redrawing. If scrollrect was successful, this will be just the newly-exposed portion that was scrolled in. If it was unsuccessful it will be the entire window region. If set false, no expose will happen, and the code calling scrollrect must re-expose as required.

This behaviour now defaults to true, and this is a temporary method to handle the transition of behaviours. This method exists only to allow this behaviour to be disabled as a temporary transition measure. It will be removed in a later version.

$top = $win->top

$bottom = $win->bottom

$left = $win->left

$right = $win->right

Returns the coordinates of the start of the window, relative to the parent window.

$top = $win->abs_top

$left = $win->abs_left

Returns the coordinates of the start of the window, relative to the root window.

$cols = $win->cols

$lines = $win->lines

Obtain the size of the window

$rect = $win->selfrect

Returns a Tickit::Rect containing representing the window's extent within itself. This will have top and left equal to 0.

$rect = $win->rect

Returns a Tickit::Rect containing representing the window's extent relative to its parent

$pen = $win->pen

Returns the current Tickit::Pen object associated with this window

$win->set_pen( $pen )

Replace the current Tickit::Pen object for this window with a new one. The object reference will be stored, allowing it to be shared with other objects. If undef is set, then a new, blank pen will be constructed.

$val = $win->getpenattr( $attr )

Returns a single attribue from the current pen

%attrs = $win->getpenattrs

Retrieve the current pen settings for the window.

This method is now deprecated and should not be used; instead use

 $win->pen->getattrs

$pen = $win->get_effective_pen

Returns a new Tickit::Pen containing the effective pen attributes for the window, combined by those of all its parents.

$val = $win->get_effective_penattr( $attr )

Returns the effective value of a pen attribute. This will be the value of this window's attribute if set, or the effective value of the attribute from its parent.

%attrs = $win->get_effective_penattrs

Retrieve the effective pen settings for the window. This will be the settings of the window and all its parents down to the root window, merged together.

This method is now deprecated and should not be used; instead use

 $win->get_effective_pen->getattrs

$win->goto( $line, $col )

Moves the cursor to the given position within the window. Both $line and $col are 0-based. The given position does not have to lie within the bounds of the window. Lines above or below the window are never displayed, columns to the left or right are clipped as appropriate. The virtual position of the cursor is still tracked even if it is not visibly displayed on the actual terminal.

$pos = $win->print( $text, $pen )

$pos = $win->print( $text, %attrs )

Print the given text to the terminal at the current cursor position using the pen of the window, possibly overridden by any extra attributes in the given Tickit::Pen instance, or directly in the given hash, if one is provided.

Returns a Tickit::StringPos object giving the total count of string printed, including in obscured sections covered by other windows, or clipped by window boundaries.

$pos = $win->erasech( $count, $moveend, $pen )

$pos = $win->erasech( $count, $moveend, %attrs )

Erase $count columns forwards. If $moveend is true, the cursor will be placed at the end of the erased region. If defined but false, it will not move from its current location. If undefined, the terminal will take which ever option it can implement most efficiently.

If a Tickit::Pen or pen attributes are provided, they are used to override the background colour for the erased region.

Returns a Tickit::StringPos object giving the total count of string printed, including in obscured sections covered by other windows, or clipped by window boundaries. Only the columns field will be valid; the others will be -1.

$win->clearrect( $rect )

$win->clearrect( $rect, $pen )

$win->clearrect( $rect, %attrs )

Erase the content of the window within the given Tickit::Rect. If a Tickit::Pen or pen attributes are provided, they are used to override the background colour for the erased region.

$success = $win->scrollrect( $rect, $downward, $rightward )

$success = $win->scrollrect( $top, $left, $lines, $cols, $downward, $rightward )

$success = $win->scrollrect( ..., $pen )

$success = $win->scrollrect( ..., %attrs )

Attempt to scroll the rectangle of the window (either given by a Tickit::Rect or defined by the first four parameters) by an amount given by the latter two. Since most terminals cannot perform arbitrary rectangle scrolling, this method returns a boolean to indicate if it was successful. The caller should test this return value and fall back to another drawing strategy if the attempt was unsuccessful.

Optionally, a Tickit::Pen instance or hash of pen attributes may be provided, to override the background colour used for erased sections behind the scroll.

The cursor may move as a result of calling this method; its location is undefined if this method returns successful. The terminal pen, in particular the background colour, may be modified by this method even if it fails to scroll the terminal (and returns false).

If the expose_after_scroll behavior is enabled, then this method will enqueue all of the required expose requests before returning, so in this case the return value is not interesting.

$success = $win->scroll( $downward, $rightward )

A shortcut for calling scrollrect on the entire region of the window.

$win->scroll_with_children( $downward, $rightward )

Similar to scroll but ignores child windows of this one, moving all of the terminal content paying attention only to obscuring by newer siblings of ancestor windows.

This method is experimental, intended only for use by Tickit::Widget::ScrollBox. After calling this method, the terminal content will have moved and the windows drawing them will be confused unless the window position was also updated. ScrollBox takes care to do this.

$win->cursor_at( $line, $col )

Sets the position in the window at which the terminal cursor will be placed if this window has focus. This method does not force the window to take the focus though; for that see take_focus.

$win->cursor_visible( $visible )

Sets whether the terminal cursor is visible on the window when it has focus. Normally it is, but passing a false value will make the cursor hidden even when the window is focused.

$win->cursor_shape( $shape )

Sets the shape that the terminal cursor will have if this window has focus. This method does not force the window to take the focus though; for that see take_focus. Valid values for $shape are the various TERM_CURSORSHAPE_* constants from Tickit::Term.

$win->take_focus

Causes this window to take the input focus, and updates the cursor position to the stored active position given by cursor_at.

$win->focus( $line, $col )

A convenient shortcut combining cursor_at with take_focus; setting the focus cursor position and taking the input focus.

$focused = $win->is_focused

Returns true if this window currently has the input focus

$win->restore

Restore the state of the terminal to its idle state. Places the cursor back at the focus position, and restores the pen.

$win->clearline( $line )

Erase the entire content of one line of the window

$win->clear

Erase the entire content of the window and reset it to the current background colour.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>