Widgets(3pm)
okay bestie, welcome to Widgets(3pm) โ the User Contributed Perl Documentation page that's basically the blueprint for making your own terminal widgets with the Curses::Application framework. It's giving base class energy, and honestly? iconic. ๐
โจ NAME โจ
Curses::Widgets โ Base widget class for use with the Curses::Application framework. It's the parent-energy class, the blueprint, the main character of this whole widget situation. ๐
๐ MODULE VERSION ๐
$Id: Widgets.pm,v 1.997 2002/11/14 01:30:19 corliss Exp corliss $
๐ SYNOPSIS ๐
Here's the whole vibe in code, bestie. Copy it exactly, no cap:
use Curses::Widgets;
$rv = test_colour();
test_color();
$colpr = select_colour($fore, $back);
$colpr = select_color($fore, $back);
$key = scankey($mwh);
@lines = textwrap($text, 40);
# The following are provided for use with descendent
# classes, and while they are not expected to be
# overridden, they can be.
$obj = Curses::Widgets->new({KEY => 'value'});
$obj->_copy($href1, $href2);
$obj->reset;
$obj->input($string);
$value = $obj->getField('VALUE');
$obj->setField(
'FIELD1' => 1,
'FIELD2' => 'value'
);
$obj->execute($mwh);
$obj->draw($mwh, 1);
@geom = $obj->_geometry;
@geom = $obj->_cgeometry;
$dwh = $obj->_canvas($mwh, @geom);
$obj->_save($mwh);
$obj->_restore($mwh);
$obj->_border($mwh);
$obj->_caption
# The following are provided for use with descendent
# classes, and are expected to be overridden.
$obj->_conf(%conf);
$obj->input_key($ch);
$obj->_content($mwh);
$obj->_cursor
๐ฆ REQUIREMENTS ๐ฆ
Cursesโ that's the one requirement. short king behavior, honestly. ๐
๐ DESCRIPTION ๐
This module serves two purposes: to provide a framework for creating custom widget classes, and importing a few useful functions for global use. So it's both a build-your-own-widget kit and a grab-bag of global helpers. lowkey versatile, highkey useful. โจ
Widget specific methods are documented in each Widget's pod, of which the following widgets are currently available:
- Button Set (
Curses::Widgets::ButtonSet) - Calendar (
Curses::Widgets::Calendar) - Combo-Box (
Curses::Widgets::ComboBox) - Label (
Curses::Widgets::Label) - List Box (
Curses::Widgets::ListBox) - Multicolumn List Box (
Curses::Widgets::ListBox::MultiColumn) - Menu (
Curses::Widgets::Menu) - Progress Bar (
Curses::Widgets::ProgressBar) - Text Field (
Curses::Widgets::TextField) - Text Memo (
Curses::Widgets::TextMemo)
The following tutorials are available:
- Widget Usage -- General Usage & Tips (
Curses::Widgets::Tutorial) - Widget Creation (
Curses::Widgets::Tutorial::Creation) - Widget Creation -- ComboBox Example (
Curses::Widgets::Tutorial::ComboBox)
For even higher (and simpler) level control over collections of widgets on "forms", please see Curses::Forms, which uses this module as well.
๐ EXPORTED FUNCTIONS ๐
These are the global goodies. iykyk. ๐ซถ
๐ test_colour/test_color ๐
$rv = test_colour();
test_color();
This function tests the console for colour capability, and if found, it will set $Curses::Widgets::DEFAULTFG and $Curses::Widgets::DEFAULTBG to the default foreground and background colour, respectively.
It also calls the Curses start_color for you. Unless you need to know the default foreground/background colours ahead of time, you won't need to call this, select_colour will do it for you the first time it's called, if necessary.
This function returns a true or false, designating colour support. fr fr, that's the whole tea. ๐ต
๐ธ select_colour/select_color ๐ธ
$colpr = select_colour($fore, $back);
$colpr = select_color($fore, $back);
This function returns the number of the specified colour pair. In doing so, it saves quite a few steps.
After the initial colour test, this function will safely (and quietly) return on all subsequent calls if no colour support is found. It returns '0', which is hardwired to your terminal default. If colour support is present, it allocates the colour pair using (n)curses init_pair for you, if it hasn't been done already.
๐ colour pair tea: most terminals have a limited number of colour pairs that can be defined. Because of this 0 (the terminal default colour pair) will be returned in lieu of attempting to allocate more colour pairs than the terminal supports. If you need a specific set of colours to be available, you might want allocate each pair ahead of time using this function to prevent less important pairs from running you out of pairs.
As a final note, yes, both the British and American spellings of 'colo(u)r' are supported. ๐
Known colours:
blackcyangreenmagentaredwhiteyellowblue
The colours are not case sensitive.
๐ง scankey ๐ง
$key = scankey($mwh);
The scankey function returns the key pressed, when it does. All it does is loop over a (n)curses getch call until something other than -1 is returned. Whether or not the getch call is (half)-blocking or cooked output is determined by how the (n)curses environment was initialised by your application. This is provided only to provide the most basic input functionality to your application, should you decide not to implement your own.
The only argument is a handle to a curses/window object.
๐ textwrap ๐
@lines = textwrap($text, 40);
The textwrap function takes a string and splits according to the passed column limit, splitting preferrably along whitespace. Newlines are preserved. periodt. โจ
๐ METHODS ๐
okay bestie, this is where the base class spills the tea on how every widget actually works. no cap, these methods are the whole personality of Curses::Widgets and its descendants. ๐
๐ new
$obj = Curses::Widgets->new({KEY => 'value'});
The new class method is like the basic constructor for all descendent widget classes โ the main character energy that gets everything started. โจ Internally, it assumes any configuration info comes in as a hash ref and that hash ref is the sole argument. It dereferences that ref and hands it to the internal method _conf, which is expected to do any input validation/initialisation your widget needs. That method should return a 1 or 0, and that return value decides if new gives you back a handle to the shiny new object or not.
If _conf returns a 1, then _copy is called to back up the initial state information. ๐ซถ
If descendent widgets use the methods provided in the class (instead of overriding them), then these keys should always be recognised:
FOREGROUNDโ Foreground colourBACKGROUNDโ Background colourBORDERCOLโ Border (foreground) colourCAPTIONCOLโ Caption (foreground) colourBORDERโ Whether or not to display a borderCAPTIONโ The string to use as the caption
The colours will default to the terminal foreground/background defaults. Other arguments may have defaults defined by the descendent classes. iykyk ๐
๐งช _conf
$obj->_conf(%conf);
This method should be overridden in your descendant class. As mentioned above, it should do any initialisation and validation required, based on the passed configuration hash. It should return a 1 or 0, depending on whether any critical errors were encountered during instantiation. critical errors? not cute. ๐ญ
Note: your_confmethod should call, as a last act,SUPER::_conf. This is important to do, since this method takes care of some colour initialisation steps for you automatically.
The following keys are known by this module, and are used by certain rendering and initiation methods:
FOREGROUNDโ Default:(terminal default)โ Default foreground colourBACKGROUNDโ Default:(terminal default)โ Default background colourBORDERCOLโ Default:(FOREGROUND)โ Default border colourCAPTIONCOLโ Default:(FOREGROUND)โ Default caption colour
As a final note, here are some rules regarding the structure of your configuration hash. You must save your state information in this hash. Another subroutine will copy that information after object instantiation in order to support the reset method. Also note that everything stored in this should not be more than one additional level deep (in other words, values can be hash or array refs, but none of the values in that structure should be refs), otherwise those refs will be copied over, instead of the data inside the structure. This essentially destroys your backup. ๐ not the backup getting deleted, bestie.
If you have special requirements, override the _copy method as well.
๐ _copy
$obj->_copy($href1, $href2);
This method copies the contents of $href1 to $href2. This will only copy two levels of data, so any reference values deeper than that will be passed by reference, not as a copy of reference's (dereferenced) value. fr fr, shallow-ish copy energy.
๐ reset
$obj->reset;
The reset method resets the object back to the original state by copying the original configuration information into the working hash. factory reset but make it widget. โจ
โจ๏ธ input_key
$obj->input_key($ch);
The input_key method should be overridden in all descendent classes. This method should accept character input and update it's internal state information appropriately. This method will be used in both interactive and non-interactive modes to send keystrokes to the widget. keyboard go brrr. ๐
โจ๏ธโจ input
$obj->input($string);
The input method provides a non-interactive method for sending input to the widget. This is essentially just a wrapper for the input_key method, but will accept any number of string arguments at once. It splits all of the input into separate characters for feeding to the input_key method. it's giving char-by-char delivery service. ๐
๐ฎ execute
$obj->execute($mwh);
This method puts the widget into interactive mode, which consists of calling the draw method, scanning for keyboard input, feeding it to the input_key method, and redrawing. main character loop! ๐ฅ
execute uses the widget's configuration information to allow easy modification of its behavoiur. First, it checks for the existance of a INPUTFUNC key. Setting its value to a subroutine reference allows you to substitute any custom keyboard scanning/polling routine in leiu of the default scankey provided by this module.
Second, it checks the return value of the input function against the regular expression stored in FOCUSSWITCH, if any. Any matches against that expression will tell this method to exit, returning the key that matches it. This effectively causes the widget to 'lose focus'. focus? gone. poof. ๐ฆ
The only argument is a handle to a valid curses window object.
NOTE: If\tis in your regex,KEY_STABwill also be a trigger for a focus switch.
๐ getField
$value = $obj->getField('VALUE');
The getField method retrieves the value(s) for every field requested that exists in the configuration hash. grabby hands but legal. ๐
๐ setField
$obj->setField(
'FIELD1' => 1,
'FIELD2' => 'value'
);
The setField method sets the value for every key/value pair passed. set it and forget it, periodt. ๐
๐จ draw
$obj->draw($mwh, 1);
The draw method can be overridden in each descendant class. It is reponsible for the rendering of the widget, and only that. The first argument is mandatory, being a valid window handle with which to create the widget's derived window. The second is optional, but if set to true, will tell the widget to draw itself in an 'active' state. For instance, the TextField widget will also render a cursor, while a ButtonSet widget will render the selected button in standout mode. active state? it's giving glow-up. โจ
The rendering sequence defined in this class is as follows:
# Get the canvas geometry and create a window handle to it
$dwh = $self->_canvas($mwh, $self->_geometry);
return 0 unless $dwh;
$self->_init($dwh);
$self->_border($dwh);
$self->_caption($dwh);
# Get the content area geometry and create a window handle to it
$cwh = $self->_canvas($dwh, $self->_cgeometry);
unless (defined $cwh) {
$dwh->delwin;
return 0;
}
$self->_content($cwh);
$self->_cursor($cwh) if $active;
๐ _geometry
@geom = $obj->_geometry;
This method returns the size of the canvas, with dimensions adjusted to account for a border (based on the value of BORDER in the configuration hash). math but make it fashion. ๐
๐โจ _cgeometry
@geom = $obj->_cgeometry;
This method returns the size of the content area. The Y and X coordinates are adjusted appropriately for rendering in a widget canvas. (0, 0) is returned for widgets with no border, and (1, 1) is returned for widgets with a border (based on the value of BORDER in the configuration hash). coordinates? slay. ๐
๐ช _canvas
$dwh = $obj->_canvas($mwh, @geom);
This method returns a window handle to a derived window in the passed window, using the specified geometry. This will return undef and produce a warning if the call fails for any reason. warning? not the vibe, but okay. ๐ญ
๐งผ _init
$obj->_init($mwh);
This method erases the window and sets the foreground/background colours as found in the configuration hash. clean slate energy. ๐ง
๐พ _save
$obj->_save($mwh);
This method saves the current attributes and colour pair in the passed window. This method would typically be called by the draw routine after _init is called on the derived window (though the current _init method calls this for you). save point! ๐
โป๏ธ _restore
$obj->_restore($mwh);
This method restores the last saved attributes and colour pair used in the window. This should be called at the end of any rendering phase that may alter the default colour and attribute settings. rewind time, bestie. โช
๐ผ๏ธ _border
$obj->_border($mwh);
This method draws the border around the passed window if BORDER is true within the configuration hash. border check: valid. ๐
๐ท๏ธ _caption
$obj->_caption
This method draws a caption on the first line of the passed window if CAPTION is defined within the configuration hash. caption era. ๐
๐ _content
$obj->_content($mwh);
This method should be overridden in all descendent classes, and should render any content in the passed window. The draw method, as defined in this class, will pass a window the exact size of the content area, so no adjustments will need to be made to accommodate a border. content? we love her. ๐ซถ
๐ฑ๏ธ _cursor
$obj->_cursor
This method should be overridden in all descendent classes that display a cursor in the content area. The draw method, as defined in this class, calls this method after the content is rendered, and passes it a window handle the exact size of the content area. cursor core. ๐ฆ
๐ HISTORY ๐
2001/07/05 -- First implementation of the base class. the origin story. โจ
๐ AUTHOR/COPYRIGHT ๐
(c) 2001 Arthur Corliss (corliss@digitalmages.com) respect the author, bestie. ๐
This page is from perl v5.34.0, dated 2022-06-12, and it's Widgets(3pm) โ mwah ๐
๐ฆ where this page comes from
- debian bookworm: libcurses-widgets-perl 1.997-8
- debian trixie: libcurses-widgets-perl 1.997-8
- ubuntu noble: libcurses-widgets-perl 1.997-8
- ubuntu resolute: libcurses-widgets-perl 1.997-8
- ubuntu stonking: libcurses-widgets-perl 1.997-8
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms