| 1 | package Gtk2::Ex::FileLocator::RecycleButton; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Gtk2; | ||
| 6 | use Glib qw(TRUE FALSE); | ||
| 7 | use Gnome2; | ||
| 8 | |||
| 9 | use File::Basename qw(dirname); | ||
| 10 | |||
| 11 | use Gtk2::Ex::FileLocator::Helper; | ||
| 12 | use Gtk2::Ex::FileLocator::FileChooser; | ||
| 13 | |||
| 14 | use Glib::Object::Subclass Gtk2::Ex::FileLocator::FileChooser::, | ||
| 15 | properties => [ | ||
| 16 | Glib::ParamSpec->boolean( | ||
| 17 | 'existing_files', 'file_exists', | ||
| 18 | 'Show only existing files', TRUE, | ||
| 19 | [qw/readable writable/] | ||
| 20 | ), | ||
| 21 | ], | ||
| 22 | ; | ||
| 23 | |||
| 24 | sub INIT_INSTANCE { | ||
| 25 | my ($this) = @_; | ||
| 26 | |||
| 27 | $this->{iconsize} = "medium"; | ||
| 28 | |||
| 29 | my $image = | ||
| 30 | Gtk2::Image->new_from_stock( 'gtk-refresh', "GTK_ICON_SIZE_BUTTON" ); | ||
| 31 | $image->show; | ||
| 32 | |||
| 33 | my $button = new Gtk2::Button; | ||
| 34 | $button->add($image); | ||
| 35 | $button->show; | ||
| 36 | $button->add_events('button-release-mask'); | ||
| 37 | $button->signal_connect( | ||
| 38 | 'button_release_event' => \&on_button_release_event, | ||
| 39 | $this | ||
| 40 | ); | ||
| 41 | |||
| 42 | $this->add($button); | ||
| 43 | |||
| 44 | #popup menu | ||
| 45 | $this->{menu} = new Gtk2::Menu; | ||
| 46 | $this->{menu}->show; | ||
| 47 | |||
| 48 | $this->signal_connect_after( 'map' => \&on_map ); | ||
| 49 | |||
| 50 | $this->get('chooser')->signal_connect( 'selection_changed', sub { $this->add_filename( $this->get_filename ) } ); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub on_map { | ||
| 54 | my $this = shift; | ||
| 55 | my $height = $this->allocation->height + 5; | ||
| 56 | $this->set_size_request( $height, 0 ); | ||
| 57 | return; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub add_filename { | ||
| 61 | my ( $this, $filename ) = @_; | ||
| 62 | return unless $filename; | ||
| 63 | |||
| 64 | $filename = string_shell_unescape($filename); | ||
| 65 | |||
| 66 | return unless -e $filename or not $this->get('existing_files'); | ||
| 67 | |||
| 68 | if ( -d $filename ) { | ||
| 69 | $filename =~ s|\.{1,2}/*$||o; # remove ../ or ../ from end | ||
| 70 | $filename .= "/"; # add a / at the end | ||
| 71 | $filename =~ s|/+|/|sgo; # remove double // | ||
| 72 | } | ||
| 73 | |||
| 74 | my $image = Gtk2::Image->new; | ||
| 75 | image_set_file_icon( $image, $filename, $this->{iconsize} ); | ||
| 76 | |||
| 77 | $filename = Unicode::MapUTF8::from_utf8( | ||
| 78 | { -string => $filename, -charset => 'ISO-8859-1' } ); | ||
| 79 | my @children = $this->{menu}->get_children; | ||
| 80 | return | ||
| 81 | if @children | ||
| 82 | and grep { $_->get_child->get_text eq $filename } @children; | ||
| 83 | |||
| 84 | $this->Debug($filename); | ||
| 85 | |||
| 86 | my $menuItem = Gtk2::ImageMenuItem->new($filename); | ||
| 87 | $menuItem->set_image($image); | ||
| 88 | $menuItem->signal_connect_after( 'activate' => \&on_menu_activated, $this ); | ||
| 89 | $menuItem->show; | ||
| 90 | |||
| 91 | $this->{menu}->append($menuItem); | ||
| 92 | return; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub on_button_release_event { | ||
| 96 | my ( $button, $event, $this ) = @_; | ||
| 97 | |||
| 98 | #printf "on_button_release_event %s\n", $this->get_filename; | ||
| 99 | $this->{menu} | ||
| 100 | ->popup( undef, undef, undef, undef, $event->button, $event->time ); | ||
| 101 | return; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub on_menu_activated { | ||
| 105 | my ( $item, $this ) = @_; | ||
| 106 | |||
| 107 | my $path = $item->get_child->get_text; | ||
| 108 | printf "on_menu_activated %s\n", $path; | ||
| 109 | |||
| 110 | $this->set_uri( sprintf "file://%s", $path ); | ||
| 111 | |||
| 112 | $this->get_toplevel->present; | ||
| 113 | 1; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; | ||
| 117 | __END__ |