Prima::Widget::Link - routines for interactive links
The class can be used in widgets that need to feature links, i e highlighted rectangles, usually with a line of text. When the user moves the mouse or clicks on a link, depending on the link type, various actions can be executed. A "tooltip" link can display a hint with (rich) text, and a "hyperlink" link can open a browser or a pod viewer. The programmer can also customize these actions.
use Prima qw(Label Application); my $main_window = Prima::MainWindow->new( size => [400, 100] ); $main_window->insert( Label => centered => 1, text => \ "L<tip://$0/ttt|tip>, L<pod://Prima/|podviewer>, L<http://google.com/|browser>, L<id|custom>", onLink => sub { print "$_[2]\n" }, ); run Prima; =pod =head1 ttt this is a tooltip =for podview <img src="data:base64"> R0lGODdhFgAVAIAAAAAAAP///ywAAAAAFgAVAIAAAAD///8CLIyPqcutsKALQKI6qT11R69lWDJm 5omm6jqBjucycEx+bVOSNNf1+/NjCREFADs=
Link types can be set with the url syntax. Four recognized link types behave differently
These are not links in the strict sense, as clicking on them doesn't cause any action, however when the user hovers the mouse over a tooltip, the module loads the pod content from the URL and displays it as a hint.
The idea behind this feature is to collect all tooltip cards in a pod section and reference them in the text like in the example code in SYNOPSIS above.
Syntax: L<tip://FILEPATH_OR_MODULE/SECTION> or L<tip://FILEPATH_OR_MODULE> where FILEPATH_OR_MODULE can refer either to a file (path with slashes/backslashes) or a perl module (with ::s ).
L<tip://FILEPATH_OR_MODULE/SECTION>
L<tip://FILEPATH_OR_MODULE>
FILEPATH_OR_MODULE
::
The tooltip text, when selected, is underscored by a dashed line, vs all other link types that use a solid line.
These links display a pod section preview like the tooltip but also open a pod viewer with the referred section when clicked on.
Syntax: L<pod://FILEPATH_OR_MODULE/SECTION> or L<pod://FILEPATH_OR_MODULE> where FILEPATH_OR_MODULE can refer either to a file (path with slashes/backslashes) or a perl module (with ::s ).
L<pod://FILEPATH_OR_MODULE/SECTION>
L<pod://FILEPATH_OR_MODULE>
Links with schemes ftp://, http://, and https:// open a browser when clicked on.
ftp://
http://
https://
All other URLs, not matched by either scheme above, are expected to be handled programmatically. The preview, if any, should be handled by the LinkPreview event, and the mouse click by the Link event.
LinkPreview
Link
See Events below.
Since Prima::Widget::Link is not a widget by itself but a collection of routines in a class, an object of such class should be instantiated programmatically and attached to an owner widget that needs to display links.
Prima::Widget::Link
The owner widget needs to call the mouse and paint methods from inside its on_mousedown etc relevant events. The owner widget class might also want to overload link events, see below how.
on_mousedown
Prima::Drawable::Markup understands the L<..|..> command, which, unlike perlpod, is formatted with its arguments reversed, to stay consistent with the other markup commands (i e it is L<http://google.com|search>, not L<search|http://google.com> .
L<..|..>
L<http://google.com|search>
L<search|http://google.com>
The simple way to incorporate rich text in both the widget and link handler is to use Prima::Drawable::Markup to handle the markup parsing and use the resulting object from the same class both for widget drawing and for the link reactions. One just needs to add markup = $markup_object > to Prima::Widget::Link-new() >.
Prima::Drawable::Markup
markup =
Prima::Widget::Link-
Contains an array of rectangles in arbitrary coordinates that could be used to map screen coordinates to a URL. Filled automatically.
An array of URLs
Used when the link object is not bound to any markup object but recalculation of the visual rectangle that the link occupies is needed due to change in formatting, f ex after a change in widget size, font size, etc. %DEFAULTS is sent internally to tb::block_walk which may need eventual default parameters.
%DEFAULTS
tb::block_walk
Scans BLOCKS and add monotonically increasing LINK_ID to new link rectangles. Return new LINK_ID.
Clears the content of rectangles
rectangles
Returns rectangles mapped to a link ID. There can be more than 1 rectangle bound to a single link ID since link text could be f ex wrapped.
Opens a pod viewer with the URL
Opens a web browser with the URL
Used when the link object is bound to a markup object and recalculation of the visual rectangle that the link occupies is needed due to change in formatting, f ex after a change in widget size, font size, etc. %DEFAULTS is sent internally to tb::block_walk which may need eventual default parameters.
All events are sent to the owner, not to the link object itself, however, the SELF parameter which contains the link object is always the first parameter
SELF
Sent to the owner, if any, from within the on_mousedown event to indicate that the link was pressed on.
Sent to the owner, if any, from within the on_mousemove event. The owner may want to fill URL_REF with (rich) text that will be displayed as a link preview
on_mousemove
Since the owner may implement a scrollable view or any other view that has a coordinate system that is not necessarily consistent with the rectangles stored in the link object, this event will be called when a link rectangle needs to be mapped to the owner coordinates.
Dmitry Karasik, <dmitry@karasik.eu.org>.
Prima, Prima::Drawable::Markup, Prima::Label, Prima::Drawable::Pod
To install Prima, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Prima
CPAN shell
perl -MCPAN -e shell install Prima
For more information on module installation, please visit the detailed CPAN module installation guide.