The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Tk::FileBrowser - Multi column file system explorer

SYNOPSIS

 require Tk::FileBrowser;
 my $b = $window->FileBrowser(@options)->pack;
 $b->load($folder);

DESCRIPTION

A multicolumn file browser widget. Columns are configurable, sortable and resizable.

if you left-click the header bar, you will get a popup menu to configure case dependant sort, directories first and show hidden.

if you left-click the tree widget, you will get a popup menu to open the current selected entry.

CONFIG VARIABLES

Switch: -casedependantsort

Default value 0;

If you change the value you have to call refresh to see your changes.

Switch: -columns

Specify a list of column names to display. Only available at create time. Allowed values are 'Accessed', 'Created', 'Link', 'Modified', 'Size' and any you have defined through the -columntypes option.

Default value ['Size', 'Modified'].

The 'Name' column is always present and always first.

Switch: -columntypes

Specify a list of column types you ish to define. Here is an example we use in our test file. I adds the colun 'Big', which marks every entry that is geater than 2048 with an 'X', and sorts on size.

 my $fb = $app->FileBrowser(
    -columns => [qw[Size Modified Big]],
    -columntypes => [
       Big => {
          display => sub {
             my ($data) = @_;
              return 'X' if $data->size > 2048;
             return '';
          },
          test => sub {
             my ($data1, $data2) = @_;
             return $fb->testSize($data1, $data2);
          },
       },
    ],
 )->pack(
    -expand => 1,
   -fill => 'both',
 );
Switch -dateformat

Defaultvalue: "%Y-%m-%d %H:%M". Specifies how time stamps should be represented.

Switch: -directoriesfirst

Default value 1.

If you change the value you have to call refresh to see your changes.

Switch: -diriconcall

Callback for obtaining the dir icon. By default it is set to a call that returns the default folder.xpm in the Perl/Tk distribution.

Switch: -fileiconcall

Callback for obtaining the file icon. By default it is set to a call that returns the default file.xpm in the Perl/Tk distribution.

Switch: -filtercase

Default value 0. Specifies if filtering is case dependant.

The value of this filter will change when you use the filter bar.

If you change the value you have to call refresh to see your changes.

Switch: -filterfolders

Default value 1. Specifies if folders are filtered.

Switch: -headermenu

Specifies a list of menuitems for the context menu of the header. By default it is set to a list with checkbuttons entries for -sortcase, -directoriesfirst and -showhidden.

Switch: -invokefile

This callback is executed when a user double clicks a file.

Switch: -linkiconcall

Callback for obtaining the link icon. By default it is set to a call that returns an xpm inside this module.

Switch: -listmenu

Specifies a list of menuitems for the context menu of the file list. By default it returns a list with a an Open command.

Switch: -loadfilter

Filter applied during load. Default value ''.

Switch: -loadfilterfolders

Specifies if filters are applied to folders during load. Default value 0.

If you change the value you have to call reload to see your changes.

Switch: -refreshfilter

Filter applied during refresh. Default value ''. The value of this filter will change when you use the filter bar.

If you change the value you have to call refresh to see your changes.

Switch: -refreshfilterfolders

Specifies if filters are applied to folders during refresh. Default value 0.

If you change the value you have to call refresh to see your changes.

Switch: -reloadimage

Image for the reload button.

Switch: -showhidden

Default value 0;

If you change the value you have to call reload to see your changes.

Switch: -sorton

Can be any valid column name. Default value 'Name'.

If you change the value you have to call refresh to see your changes.

Switch: -sortorder

Can be 'ascending' or 'descending'. Default value 'ascending'.

If you change the value you have to call refresh to see your changes.

ADVERTISED SUBWIDGETS

Entry

Class Tk::ListEntry.

Tree

Class Tk::ITree.

FilterFrame

Class Tk::Frame.

FilterEntry

Class Tk::Entry.

KEYBINDINGS

CTRL+F

Shows the filter bar.

METHODS

filterHide

Removes the filter bar.

filterShow

Shows the filter bar.

folder

Returns the name of the folder loaded. Returns undef if nothing is loaded.

load($folder)

loads $folder into memory and refreshes the display if succesfull.

refresh

Deletes all entries in the list and rebuilds it.

reload

Reloads the current folder, if one is loaded.

COLUMN DEFINITIONS

The -columntypes options allows you to define your own column types. The structure is like this:

 my $fb = $app->FileBrowser(
    -columns => [qw[Size Modified MyColumn]],
    -columntypes => [
       MyColumn => {
          display => sub { ... },
          test => sub { ... },
       },
    ],
 );

MyColumn is followed by a reference to a hash with the keys display and test. Both keys hold a reference to an anonymous sub.

The display sub receives one argument, a Tk::FirleBrowser::Item object. It should return the string to be displayed in the column.

The test sub receives two arguments, Both a Tk::FirleBrowser::Item object. It should test both objects against each other, taking -sortorder into account. It should return the boolean result of the test.

The methods below can help you writing your own column definitions. Whenever you see $data here, it refers to a Tk::FirleBrowser::Item object.

dateString($type, $data)

Returns the formatted date string of one of the date items in $data; $type can be 'Accessed', 'Created' or 'Modified'.

linkString($data)

Returns the formatted size string to be displayed for $data.

nameString($data)

Returns the name to be displayed for $data.

sizeString($data)

Returns the formatted size string to be displayed for $data.

testDate($type, $data1, $data2)

$type can be 'Accessed', 'Created' or 'Modified'.

Compares the date stamps in $data1 and $data2 and returns true if $data1 wins.

testLink($data1, $data2)

Checks if both $data1 and $data2 represent symbolic links and returns true if $data1 target wins.

testName($data1, $data2)

Compares the names of $data1 and $data2 and returns true if $data1 wins.

testSize($data1, $data2)

Compares the sizes of $data1 and $data2 and returns true if $data1 wins.

LICENSE

Same as Perl.

AUTHOR

Hans Jeuken (hanje at cpan dot org)

TODO

Allow columns to be configured on the fly.

BUGS AND CAVEATS

Loading and sorting large folders takes ages.

If you find any bugs, please contact the author.

SEE ALSO

Tk::ITree
Tk::Tree