« blib/lib/App/Asciio/setup/import_export/svg.pl
↑ Summary
blib/lib/App/Asciio/setup/import_export/ascii.pl »
| 1 | |||
| 2 | package App::Asciio::GTK::Asciio ; | ||
| 3 | |||
| 4 | $|++ ; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Data::TreeDumper ; | ||
| 10 | |||
| 11 | use Glib ':constants'; | ||
| 12 | use Gtk3 -init; | ||
| 13 | |||
| 14 | #------------------------------------------------------------------------------------------------------ | ||
| 15 | |||
| 16 | sub display_popup_menu | ||
| 17 | { | ||
| 18 | my ($self, $event) = @_; | ||
| 19 | |||
| 20 | my ($popup_x, $popup_y) = @{$self}{'MOUSE_X', 'MOUSE_Y'} ; | ||
| 21 | |||
| 22 | my @menu_items ; | ||
| 23 | |||
| 24 | for my $element (@{$self->{ELEMENT_TYPES}}) | ||
| 25 | { | ||
| 26 | (my $name_with_underscore = $element->{NAME}) =~ s/_/__/g ; | ||
| 27 | |||
| 28 | push @menu_items, [ "/$name_with_underscore", undef , insert_generator($self, $element, $popup_x, $popup_y), 0 , '<Item>', undef] ; | ||
| 29 | } | ||
| 30 | |||
| 31 | for my $menu_entry (@{$self->get_context_menu_entries($popup_x, $popup_y)}) | ||
| 32 | { | ||
| 33 | my ($name, $sub, $data) = @{$menu_entry} ; | ||
| 34 | (my $name_with_underscore = $name) =~ s/_/__/g ; | ||
| 35 | |||
| 36 | push @menu_items, [ $name_with_underscore, undef , $self->menu_entry_wrapper($sub, $data), 0, '<Item>', undef], | ||
| 37 | } | ||
| 38 | |||
| 39 | push @menu_items, | ||
| 40 | ( | ||
| 41 | ['/File/open', undef , sub { $self->run_actions_by_name('Open') ; }, 0 , '<Item>', undef], | ||
| 42 | ['/File/save', undef , sub { $self->run_actions_by_name('Save') ; }, 0 , '<Item>', undef], | ||
| 43 | ['/File/save as', undef , sub { $self->run_actions_by_name(['Save', 1]) ; }, 0 , '<Item>', undef], | ||
| 44 | ) ; | ||
| 45 | |||
| 46 | if($self->get_selected_elements(1) == 1) | ||
| 47 | { | ||
| 48 | push @menu_items, [ '/File/save stencil', undef , $self->menu_entry_wrapper(\&App::Asciio::save_stencil), 0 , '<Item>', undef ] ; | ||
| 49 | } | ||
| 50 | |||
| 51 | my $menu = Gtk3::Menu->new() ; | ||
| 52 | |||
| 53 | insert_menu_items($menu, \@menu_items) ; | ||
| 54 | |||
| 55 | $menu->popup(undef, undef, undef, undef, $event->{BUTTON}, $event->{TIME}) ; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub insert_menu_items | ||
| 59 | { | ||
| 60 | my ($root, $menu_entry_definitions) = @_ ; | ||
| 61 | |||
| 62 | my %menus ; | ||
| 63 | |||
| 64 | for my $menu_entry_definition (map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [$_, $_->[0]] } @$menu_entry_definitions) | ||
| 65 | { | ||
| 66 | my ($path, undef, $sub, undef, $item) = @$menu_entry_definition ; | ||
| 67 | |||
| 68 | $path =~ s~^/~~ or die "Menu path '$path' doesn't start at root" ; | ||
| 69 | my @path_elements = split m~/~, $path ; | ||
| 70 | my $name = pop @path_elements ; | ||
| 71 | |||
| 72 | my $container = $root ; | ||
| 73 | |||
| 74 | for my $path_element (@path_elements) | ||
| 75 | { | ||
| 76 | if(exists $menus{$path_element}) | ||
| 77 | { | ||
| 78 | $container = $menus{$path_element} ; | ||
| 79 | } | ||
| 80 | else | ||
| 81 | { | ||
| 82 | my $menu = Gtk3::Menu->new() ; | ||
| 83 | $menu->show() ; | ||
| 84 | $menus{$path_element} = $menu ; | ||
| 85 | |||
| 86 | my $menu_item = Gtk3::MenuItem->new_with_label($path_element); | ||
| 87 | $menu_item->show() ; | ||
| 88 | $menu_item->set_submenu($menu) ; | ||
| 89 | |||
| 90 | $container->append($menu_item) ; | ||
| 91 | $container = $menu | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | my $menu_item=Gtk3::MenuItem->new($name) ; | ||
| 96 | $menu_item->signal_connect('activate' => $sub); | ||
| 97 | $menu_item->show() ; | ||
| 98 | |||
| 99 | $container->append($menu_item) ; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | sub insert_generator | ||
| 104 | { | ||
| 105 | my ($self, $element, $x, $y) = @_ ; | ||
| 106 | |||
| 107 | return sub | ||
| 108 | { | ||
| 109 | my $new_element = $self->add_new_element_of_type($element, $x, $y) ; | ||
| 110 | $self->deselect_all_elements() ; | ||
| 111 | $self->select_elements(1, $new_element) ; | ||
| 112 | $self->update_display(); | ||
| 113 | } ; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub menu_entry_wrapper | ||
| 117 | { | ||
| 118 | my ($self, $sub, $data) = @_ ; | ||
| 119 | |||
| 120 | return sub { $sub->($self, $data) ; } ; | ||
| 121 | } | ||
| 122 | |||
| 123 | #------------------------------------------------------------------------------------------------------ | ||
| 124 | |||
| 125 | sub get_context_menu_entries | ||
| 126 | { | ||
| 127 | my ($self, $popup_x, $popup_y) = @_ ; | ||
| 128 | my @context_menu_entries ; | ||
| 129 | |||
| 130 | for my $context_menu_handler | ||
| 131 | ( | ||
| 132 | map {$self->{ACTIONS}{$_}} | ||
| 133 | grep | ||
| 134 | { | ||
| 135 | 'HASH' eq ref $self->{ACTIONS}{$_} # not a sub actions definition | ||
| 136 | && defined $self->{ACTIONS}{$_}{CONTEXT_MENU_SUB} | ||
| 137 | } sort keys %{$self->{ACTIONS}} | ||
| 138 | ) | ||
| 139 | { | ||
| 140 | # print "Adding context menu from action '$context_menu_handler->{NAME}'.\n" ; | ||
| 141 | |||
| 142 | if(defined $context_menu_handler->{CONTEXT_MENU_ARGUMENTS}) | ||
| 143 | { | ||
| 144 | push @context_menu_entries, | ||
| 145 | $context_menu_handler->{CONTEXT_MENU_SUB}-> | ||
| 146 | ( | ||
| 147 | $self, | ||
| 148 | $context_menu_handler->{CONTEXT_MENU_ARGUMENTS}, | ||
| 149 | $popup_x, $popup_y, | ||
| 150 | ) ; | ||
| 151 | } | ||
| 152 | else | ||
| 153 | { | ||
| 154 | push @context_menu_entries, $context_menu_handler->{CONTEXT_MENU_SUB}->($self, $popup_x, $popup_y) ; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | return \@context_menu_entries ; | ||
| 159 | } | ||
| 160 | |||
| 161 | #------------------------------------------------------------------------------------------------------ | ||
| 162 | |||
| 163 | 1 ; |