Prima::Lists(3)
π NAME π
Prima::Lists(3) β okay bestie, this page is literally about Prima::Lists, aka list widgets. We're gonna get you comfy with the listbox family, no cap. π
π¦ DESCRIPTION π¦
So this module hands you several listbox classes, and the whole glow-up between them is how the items in the list widget are associated with data. The class hierarchy is giving family tree realness:
AbstractListViewer
AbstractListBox
ListViewer
ProtectedListBox
ListBox
The root class Prima::AbstractListViewer gives you a common interface, but ngl itβs not usable directly β itβs more like the blueprint, the main character energy without the full fit. π
The main differences between the classes are centered around the way the items are stored. The simplest organization of a text-only item list is provided by Prima::ListBox β it stores an array of text scalars in a widget. Cute, minimal, does the job. β¨ More elaborated storage and representation types are not realized, and the programmer is urged to use the more abstract classes to derive their own mechanisms. So like, if you want fancier storage, youβre lowkey invited to build your own thing on top of the abstract classes. π«Ά
For example, for a list of items that contain text strings and icons, see Prima::DirectoryListBox in Prima::Dialog::FileDialog. Iconic behavior, literally. π
To organize an item storage different from Prima::ListBox, itβs usually enough to overload either the Stringify, MeasureItem, and DrawItem events, or their method counterparts: get_item_text, get_item_width, and draw_items. Thatβs the whole little override squad β very slay, very βmake it yours.β π
π Prima::AbstractListViewer π
ok so Prima::AbstractListViewer is a descendant of Prima::Widget::GroupScroller (bestie, that means some of its properties are literally not described here, they live in the parent page, so don't @ me).
This class is the whole vibe for generic list browsing, plus extra sparkle for text-oriented lists. But heads up β you can't use this class directly, it's giving abstract base class energy, iykyk. π§
β¨ Properties β¨
autoHeightBOOLEAN β if it's 1, the item height auto-changes when the widget font changes, which is lowkey perfect for text items. If it's 0, the item height stays put, which is the move for non-text items. Default:1. πcountINTEGER β an integer property that gives you access to the number of items in the list. Since this is tied to how the item storage is organized (and therefore whether you can even change the item count), this property is often declared as read-only in descendants ofPrima::AbstractListViewer. fr fr read the fine print.dragableBOOLEAN β if 1, you can drag items interactively by holding the Control key together with the left mouse button. If 0, dragging is off. Default:0. π±οΈdrawGridBOOLEAN β if 1, vertical grid lines between columns get drawn withgridColor. Only actual in multi-column mode, bestie. Default:1.extendedSelectBOOLEAN β controls how the user selects multiple items, and it only matters whenmultiSelectis 1. If 0, you gotta click each item individually to mark it selected. If 1, you can drag the mouse or use the Shift key + arrow keys to do range selection, and the Control key selects individual items. Default:0. πfocusedItemINDEX β selects the focused item's index. If it's -1, nothing is focused. It's mostly a run-time property, but you can set it during widget creation given that the item list is also accessible at that stage. Default:-1.gridColorCOLOR β the color used for drawing those vertical divider lines in multi-column list widgets. The list classes also support setting the grid color indirectly, just like the widget does, via thecolorIndexproperty. To pull that off, theci::Gridconstant is declared (for more detail peepcolorIndexin Prima::Widget). Default:cl::Black. π€integralHeightBOOLEAN β if 1, only items that fully fit vertically in the widget interior get drawn. If 0, the partially visible ones get drawn too. Default:0.integralWidthBOOLEAN β if 1, only items that fully fit horizontally in the widget interior get drawn. If 0, partially visible items are drawn too. Only actual in multi-column mode. Default:0.itemHeightINTEGER β picks the height of items in pixels. Since list classes don't support items with variable heights, changing this affects all the items, no cap. Default: default font height. πitemWidthINTEGER β picks the width of items in pixels. Same deal β no variable widths supported, so changes affect all items. Default: default widget width.multiSelectBOOLEAN β if 0, the user can only select one item, and that's reported by thefocusedItemproperty. If 1, the user can select more than one item. In that case, thefocusedItem'th item isn't necessarily selected, which is a lil unhinged but that's the design. To access the selected item list, use theselectedItemsproperty. Default:0. πΈmultiColumnBOOLEAN β if 0, items are arranged vertically in a single column and the main scroll bar is vertical. If 1, items are arranged in several columns, eachitemWidthpixels wide, and the main scroll bar is horizontal. Periodt.offsetINTEGER β horizontal offset of an item list, in pixels.topItemINTEGER β selects the first item drawn. πselectedCountINTEGER β a read-only property, returns the number of selected items. (Don't try to write to it, she's read-only for a reason.)selectedItemsARRAY β ARRAY is an array of integer indices of selected items. π¦verticalBOOLEAN β sets the general direction of items in multi-column mode. If 1, items increase down-to-right. Otherwise, right-to-down. Doesn't have any effect in single-column mode. Default:1.
π οΈ Methods π οΈ
add_selection ARRAY, FLAGβ sets item indices from ARRAY into selected or deselected state depending on FLAG, which is correspondingly 1 or 0. Only for the multi-select mode.deselect_allβ clears the selection. Only for the multi-select mode. bye bye selections πdraw_items CANVAS, ITEM_DRAW_DATAβ called from within thePaintnotification to draw items. The default behavior calls theDrawItemnotification for every item in the ITEM_DRAW_DATA array. ITEM_DRAW_DATA is an array of arrays, where each array is the parameters passed to theDrawItemnotification.
This method is overridden in some descendant classes to speed up drawing. For example, std_draw_text_items is the optimized routine for drawing text-based items β it's used in the Prima::ListBox class. See DrawItem for the description of the parameters.
draw_text_items CANVAS, FIRST, LAST, STEP, X, Y, OFFSET, CLIP_RECTβ called bystd_draw_text_itemsto draw a sequence of text items with indices from FIRST to LAST, by STEP, on CANVAS, starting at point X, Y, and incrementing the vertical position with OFFSET. CLIP_RECT is a reference to an array of four integers given in inclusive-inclusive coordinates that represent the active clipping rectangle.
β οΈ note that OFFSET must be an integer, otherwise you'll see bad effects when text is drawn below Y=0. like, don't do it bestie π
get_item_text INDEXβ returns the text string assigned to the INDEXth item. Since the class doesn't assume how items are stored, the text is queried via theStringifynotification.get_item_width INDEXβ returns the width in pixels of the INDEXth item. Since the class doesn't assume the item storage organization, the value is queried via theMeasureItemnotification.is_selected INDEXβ returns 1 if the INDEXth item is selected, 0 otherwise. simple as that πitem2rect INDEX, [ WIDTH, HEIGHT ]β calculates and returns four integers with the rectangle coordinates of the INDEXth item. WIDTH and HEIGHT are optional parameters with pre-fetched dimensions of the widget; if they're not set, the dimensions get queried by calling thesizeproperty. If they are set though, thesizeproperty isn't called, so you get a lil speed-up. β‘point2item X, Yβ returns the index of the item containing the point at (X,Y). If the point belongs to an item outside the widget's interior, it returns the index of the first item outside the widget's interior in the direction of the point. wild but true.redraw_items INDICESβ redraws all items in the INDICES array.select_allβ selects all items. Only for the multi-select mode. β¨set_item_selected INDEX, FLAGβ sets the selection flag on the INDEXth item. If FLAG is 1, the item is selected. If 0, it's deselected. Only for the multi-select mode.select_item INDEXβ selects the INDEXth item. Only for the multi-select mode.std_draw_text_items CANVAS, ITEM_DRAW_DATAβ an optimized method that draws text-based items. It's fully compatible with thedraw_itemsinterface and is used in thePrima::ListBoxclass.
The optimization comes from the assumption that items share common background and foreground colors, and only differ between selected and non-selected states. The routine groups drawing requests for selected and non-selected items, then draws items with fewer calls to the color property. The background is drawn by the routine itself, while the foreground (usually text) is delegated to the draw_text_items method, so text positioning and any eventual decorations are easier to implement. π¨
ITEM_DRAW_DATA is an array of arrays of scalars, where each array contains parameters of the DrawItem notification. See DrawItem for the description of the parameters.
toggle_item INDEXβ toggles selection of the INDEXth item. Only for the multi-select mode. πunselect_item INDEXβ deselects the INDEXth item. Only for the multi-select mode.
π Events π
Clickβ called when the user presses the return key or double-clicks on an item. The index of the item is stored infocusedItem.DragItem OLD_INDEX, NEW_INDEXβ called when the user finishes dragging an item from OLD_INDEX to NEW_INDEX position. The default action rearranges the item list according to the dragging action. slay π«DrawItem CANVAS, INDEX, X1, Y1, X2, Y2, SELECTED, FOCUSED, PRELIGHT, COLUMNβ called when the INDEXth item is to be drawn on CANVAS. X1, Y1, X2, Y2 define the item rectangle in widget coordinates where the item is to be drawn. SELECTED, FOCUSED, and PRELIGHT are boolean flags, for whether the item must be drawn correspondingly in selected and focused states, with or without the prelight effect.MeasureItem INDEX, REFβ stores the width in pixels of the INDEXth item into the REF scalar reference. This notification must be called from within thebegin_paint_info/end_paint_infoblock. no exceptions π«ΆSelectItem INDEX, FLAGβ called when an item changes its selection state. INDEX is the index of the item, FLAG is its new selection state: 1 if it's selected, 0 if it's not.Stringify INDEX, TEXT_REFβ stores the text string associated with the INDEXth item into the TEXT_REF scalar reference.
π Prima::AbstractListBox π
The same as its ascendant Prima::AbstractListViewer except that it does not propagate the DrawItem message, because it's assuming all items must be drawn as text strings. iconic, honestly π
π Prima::ListViewer π
Okay so, Prima::ListViewer implements an item storage mechanism, but it leaves the definition of the format of the item to the programmer. The items are accessible via the items property and several other helper routines. Slay.
The class also defines user navigation by accepting character keyboard input and jumping to the items that have text assigned with the first letter that matches the input. And yes: Prima::ListViewer is derived from Prima::AbstractListViewer. Periodt.
β¨ Properties β¨
autoWidthBOOLEAN β selects if the item width must be recalculated automatically when either the font or the item list is changed. Default value:1πcountINTEGER β a read-only property; returns the number of items. Read-only, so no editing shenanigans, okay?itemsARRAY β accesses the storage array of the items. The format of items is not defined; it is merely treated as one scalar per index. Itβs giving βone scalar per indexβ energy.
π Methods π
add_items ITEMSβ appends an array ofITEMSto the end of the item list. Boom, appended.calibrateβ recalculates all item widths. AdjustsitemWidthifautoWidthis set. β¨delete_items INDICESβ deletes items from the list.INDICEScan be either an array or a reference to an array of item indices. Bye-bye, items.get_item_width INDEXβ returns the width in pixels of theINDEXth item from the internal cache. Pixels, bestie.get_items INDICESβ returns an array of items.INDICEScan be either an array or a reference to an array of item indices. Depending on the caller context, the results are different: in the array context the item list is returned; in scalar, only the first item from the list. Context really said βplot twist.βinsert_items OFFSET, ITEMSβ inserts an array of items at theOFFSETindex in the list. Offset must be a valid index; to insert items at the end of the list use theadd_itemsmethod.ITEMScan be either an array or a reference to an array of items. Valid index only, no cap.replace_items OFFSET, ITEMSβ replaces existing items at theOFFSETindex in the list. The offset must be a valid index.ITEMScan be either an array or a reference to an array of items. Swap era.
π‘οΈ Prima::ProtectedListBox π‘οΈ
A semi-demonstrational class derived from Prima::ListViewer, implementing certain protections for every item during drawing. Assuming that several item drawing algorithms can be used in the same widget, Prima::ProtectedListBox provides a safety layer between these. If an algorithm selects a font or a color and does not restore the old value, this does not affect the outlook of other items. Safety layer supremacy.
This functionality is implemented by overloading the draw_items method and also all graphic properties. iykyk.
π Prima::ListBox π
Descendant of Prima::ListViewer, declares that an item must be a single text string. Incorporating all the functionality of its predecessors, provides the standard workhorse listbox widget. Main character energy.
π Synopsis π
my $lb = Prima::ListBox-> create(
items => [qw(First Second Third)],
focusedItem => 2,
onClick => sub {
print $_[0]-> get_items( $_[0]-> focusedItem), " is selected\n";
}
);
π§ Methods π§
get_item_text INDEXβ returns the text string associated with theINDEXth item. Since the item storage organization is implemented, does so without calling theStringifynotification. NoStringifycall needed, bestie.
π AUTHOR π
Dmitry Karasik, <dmitry@karasik.eu.org>. The icon behind it all β¨
π SEE ALSO π
PrimaPrima::WidgetPrima::ComboBoxPrima::Dialog::FileDialogexamples/editor.pl
π Thatβs the whole Prima::Lists(3) page, bestie β from Perl v5.42.0, dated 2024-08-20. Go have the slayest listbox era. π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Prima 1.74000-bp160.1.13 Β· AGPL-3.0-only AND BSD-2-Clause
- opensuse-leap 16.0: perl-Prima 1.74000-bp160.1.13 Β· AGPL-3.0-only AND BSD-2-Clause
- opensuse-leap 16.0: perl-Prima 1.74000-bp160.1.13 Β· AGPL-3.0-only AND BSD-2-Clause
- opensuse-leap 16.0: perl-Prima 1.74000-bp160.1.13 Β· AGPL-3.0-only AND BSD-2-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms