| 1 | |||
| 2 | package App::Asciio::Actions::Unsorted ; | ||
| 3 | |||
| 4 | use strict ; | ||
| 5 | use warnings ; | ||
| 6 | use utf8 ; | ||
| 7 | use Encode ; | ||
| 8 | |||
| 9 | use File::Temp qw/ tempfile / ; | ||
| 10 | use File::Slurp ; | ||
| 11 | use Data::TreeDumper ; | ||
| 12 | use List::Util qw(min max) ; | ||
| 13 | |||
| 14 | #---------------------------------------------------------------------------------------------- | ||
| 15 | |||
| 16 | sub manpage_in_browser | ||
| 17 | { | ||
| 18 | my ($self) = @_ ; | ||
| 19 | |||
| 20 | my (undef, $manpage) = tempfile() ; | ||
| 21 | |||
| 22 | system("perldoc App::Asciio >$manpage") ; | ||
| 23 | |||
| 24 | if(defined $ENV{BROWSER} && $ENV{BROWSER} ne '') | ||
| 25 | { | ||
| 26 | qx"$ENV{BROWSER} --new-window 'file://$manpage' &" ; | ||
| 27 | } | ||
| 28 | else | ||
| 29 | { | ||
| 30 | $self->display_message_modal("Environmen variable 'BROWSER' not set.") ; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | #---------------------------------------------------------------------------------------------- | ||
| 35 | |||
| 36 | sub display_help | ||
| 37 | { | ||
| 38 | my ($self) = @_ ; | ||
| 39 | |||
| 40 | |||
| 41 | $self->display_message_modal(<<EOM) ; | ||
| 42 | |||
| 43 | "alt+lef_mouse" "." Quick link | ||
| 44 | |||
| 45 | "ib", insert a box | ||
| 46 | "iB", insert a shrink box | ||
| 47 | "i^b" Add multiple boxes | ||
| 48 | |||
| 49 | "it", Add a text element | ||
| 50 | "i^t" Add multiple texts | ||
| 51 | |||
| 52 | "ia" Add a wirl arrow | ||
| 53 | "ad" Change arrow direction | ||
| 54 | "af" Flip the arrows | ||
| 55 | "as" Append multi_wirl section | ||
| 56 | |||
| 57 | "ctl-z" "u" Undo | ||
| 58 | "ctl-y" "ctl-r" Redo | ||
| 59 | |||
| 60 | "tab" "n" Select next element | ||
| 61 | "ctl-a" "V" Select all elements | ||
| 62 | |||
| 63 | "ctl-c/ctl-v" "y/p" Copy/Paste elements | ||
| 64 | "ctl-e" "Y" Export as ascii | ||
| 65 | "ctl-V" "P" Import to box | ||
| 66 | "alt-p" "A-P" Import to text | ||
| 67 | |||
| 68 | "gg" Group selected elements | ||
| 69 | "gu" Group object | ||
| 70 | |||
| 71 | "double-click/enter" Edit object | ||
| 72 | |||
| 73 | "right-clik" Show context menu. | ||
| 74 | |||
| 75 | "+" Zoom in | ||
| 76 | "-" Zoom out | ||
| 77 | |||
| 78 | ":e" Open | ||
| 79 | ":w" Save | ||
| 80 | ":q" Quit | ||
| 81 | |||
| 82 | ":m" Displays manpage | ||
| 83 | |||
| 84 | EOM | ||
| 85 | } | ||
| 86 | |||
| 87 | #---------------------------------------------------------------------------------------------- | ||
| 88 | |||
| 89 | sub zoom | ||
| 90 | { | ||
| 91 | my ($self, $direction) = @_ ; | ||
| 92 | |||
| 93 | my ($family, $size) = $self->get_font() ; | ||
| 94 | |||
| 95 | my ($character_width, $character_height) = $self->get_character_size() ; | ||
| 96 | |||
| 97 | $self->set_font($family, $size + $direction) ; | ||
| 98 | |||
| 99 | my $zoom_step = $self->{ZOOM_STEP} // 1 ; | ||
| 100 | my $font_min = $self->{FONT_MIN} // 1 ; | ||
| 101 | |||
| 102 | $size += ($direction * $zoom_step) ; | ||
| 103 | |||
| 104 | my $remainder = $size % $zoom_step ; | ||
| 105 | $size += $zoom_step - $remainder if $remainder ; | ||
| 106 | |||
| 107 | $size = $font_min if $size < $font_min ; | ||
| 108 | |||
| 109 | # making the character size change | ||
| 110 | $self->set_font($family, $size); | ||
| 111 | |||
| 112 | # resize canvas | ||
| 113 | if($self->{UI} eq 'GUI') | ||
| 114 | { | ||
| 115 | my $h_value = $self->{sc_window}->get_hadjustment()->get_value() ; | ||
| 116 | my $v_value = $self->{sc_window}->get_vadjustment()->get_value() ; | ||
| 117 | |||
| 118 | $self->invalidate_rendering_cache() ; | ||
| 119 | |||
| 120 | my ($new_character_width, $new_character_height) = $self->get_character_size() ; | ||
| 121 | my ($canvas_width, $canvas_height) = ($self->{CANVAS_WIDTH} * $new_character_width, $self->{CANVAS_HEIGHT} * $new_character_height) ; | ||
| 122 | |||
| 123 | $self->{widget}->set_size_request($canvas_width, $canvas_height); | ||
| 124 | |||
| 125 | # The state equation of the scroll bar before and after zooming, | ||
| 126 | # using the coordinates of the mouse as the zoom point | ||
| 127 | # MOUSE_X * character_width - h_value = MOUSE_X * new_character_width - new_h_value | ||
| 128 | # MOUSE_Y * character_height - v_value = MOUSE_Y * new_character_height - new_v_value | ||
| 129 | my $new_h_value = $self->{MOUSE_X} * ($new_character_width-$character_width) + $h_value ; | ||
| 130 | my $new_v_value = $self->{MOUSE_Y} * ($new_character_height-$character_height) + $v_value ; | ||
| 131 | |||
| 132 | $new_h_value = max(0, min($canvas_width, $new_h_value)) ; | ||
| 133 | $new_v_value = max(0, min($canvas_height, $new_v_value)) ; | ||
| 134 | |||
| 135 | $self->{sc_window}->get_hadjustment()->set_value($new_h_value) ; | ||
| 136 | $self->{sc_window}->get_vadjustment()->set_value($new_v_value) ; | ||
| 137 | |||
| 138 | } | ||
| 139 | |||
| 140 | $self->invalidate_rendering_cache() ; | ||
| 141 | $self->update_display() ; | ||
| 142 | } | ||
| 143 | |||
| 144 | #---------------------------------------------------------------------------------------------- | ||
| 145 | |||
| 146 | sub display_keyboard_mapping_in_browser | ||
| 147 | { | ||
| 148 | my ($self) = @_ ; | ||
| 149 | |||
| 150 | my $mapping_file = get_keyboard_mapping_file($self) ; | ||
| 151 | |||
| 152 | if(defined $ENV{BROWSER} && $ENV{BROWSER} ne '') | ||
| 153 | { | ||
| 154 | qx"$ENV{BROWSER} --new-window 'file://$mapping_file' &" ; | ||
| 155 | } | ||
| 156 | else | ||
| 157 | { | ||
| 158 | $self->display_message_modal("Environmen variable 'BROWSER' not set.") ; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | sub get_keyboard_mapping_file | ||
| 163 | { | ||
| 164 | my ($self) = @_ ; | ||
| 165 | |||
| 166 | my (@key_actions, @action_keys, @groups) ; | ||
| 167 | |||
| 168 | for (sort keys %{$self->{ACTIONS}}) | ||
| 169 | { | ||
| 170 | if(exists $self->{ACTIONS}{$_}{IS_GROUP}) | ||
| 171 | { | ||
| 172 | push @groups, [$_, $self->{ACTIONS}{$_}] ; | ||
| 173 | } | ||
| 174 | else | ||
| 175 | { | ||
| 176 | push @key_actions, sprintf "%-40s: %s\n", $_, $self->{ACTIONS}{$_}{NAME} ; | ||
| 177 | push @action_keys, sprintf "%-40s: %s\n", $self->{ACTIONS}{$_}{NAME}, $_ ; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | for my $group (sort { $a->[0] cmp $b->[0] } @groups) | ||
| 182 | { | ||
| 183 | for (grep { 'HASH' eq ref $group->[1]{$_} } sort keys %{$group->[1]}) | ||
| 184 | { | ||
| 185 | push @key_actions, sprintf "%-40s: %s\n", "$group->[0] + $_", $group->[1]{$_}{NAME} ; | ||
| 186 | push @action_keys, sprintf "%-40s: %s\n", $group->[1]{$_}{NAME}, "$group->[0] + $_" ; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | my $mapping_file = (tempfile())[1] ; | ||
| 191 | write_file($mapping_file, @key_actions, "\n\n", sort @action_keys) ; | ||
| 192 | |||
| 193 | return $mapping_file ; | ||
| 194 | } | ||
| 195 | |||
| 196 | #---------------------------------------------------------------------------------------------- | ||
| 197 | |||
| 198 | sub display_commands | ||
| 199 | { | ||
| 200 | my ($self) = @_ ; | ||
| 201 | |||
| 202 | #~ print Data::TreeDumper::DumpTree $self->{ACTIONS_BY_NAME}, 'ACTIONS_BY_NAME:'; | ||
| 203 | |||
| 204 | my $commands = get_commands($self->{ACTIONS_BY_NAME}) ; | ||
| 205 | |||
| 206 | $self->show_dump_window | ||
| 207 | ( | ||
| 208 | $commands, | ||
| 209 | 'commands:', | ||
| 210 | DISPLAY_ADDRESS => 0, | ||
| 211 | ) | ||
| 212 | } | ||
| 213 | |||
| 214 | sub get_commands | ||
| 215 | { | ||
| 216 | my ($actions) = @_ ; | ||
| 217 | |||
| 218 | my $commands ; | ||
| 219 | |||
| 220 | for my $action (keys %{$actions}) | ||
| 221 | { | ||
| 222 | if('ARRAY' eq ref $actions->{$action}) | ||
| 223 | { | ||
| 224 | my $shortcut = ref $actions->{$action}[0] eq '' | ||
| 225 | ? $actions->{$action}[0] | ||
| 226 | : '[' . join('/', @{$actions->{$action}[0]}) . ']'; | ||
| 227 | |||
| 228 | $commands->{$action . " [$shortcut]"} = {FILE=> $actions->{$action}[6]} ; | ||
| 229 | } | ||
| 230 | elsif('HASH' eq ref $actions->{$action}) | ||
| 231 | { | ||
| 232 | my $sub_commands = get_commands($actions->{$action}) ; | ||
| 233 | |||
| 234 | for my $shortcut (keys %{$sub_commands}) | ||
| 235 | { | ||
| 236 | my ($name, $shortcut_text) = $shortcut =~ /([^\[]*)(.*)/ ; | ||
| 237 | my $start_shortcut = '[' . join('/', $actions->{$action}{SHORTCUTS}) . '] + '; | ||
| 238 | $commands->{$name . $start_shortcut . $shortcut_text} = $sub_commands->{$shortcut} ; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | else | ||
| 242 | { | ||
| 243 | #~ die "unknown type while running 'dump_keyboard_mapping'\n" ; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 | return($commands) ; | ||
| 248 | } | ||
| 249 | |||
| 250 | #---------------------------------------------------------------------------------------------- | ||
| 251 | |||
| 252 | sub display_action_files | ||
| 253 | { | ||
| 254 | my ($self) = @_ ; | ||
| 255 | |||
| 256 | my $actions_per_file = {} ; | ||
| 257 | |||
| 258 | generate_keyboard_mapping_text_dump($self->{ACTIONS_BY_NAME}, $actions_per_file) ; | ||
| 259 | |||
| 260 | $self->show_dump_window | ||
| 261 | ( | ||
| 262 | $actions_per_file, | ||
| 263 | 'Action files:', | ||
| 264 | DISPLAY_ADDRESS => 0, | ||
| 265 | GLYPHS => [' ', ' ', ' ', ' '], | ||
| 266 | NO_NO_ELEMENTS => 1, | ||
| 267 | FILTER => \&filter_keyboard_mapping | ||
| 268 | ) ; | ||
| 269 | } | ||
| 270 | |||
| 271 | sub filter_keyboard_mapping | ||
| 272 | { | ||
| 273 | my $s = shift ; | ||
| 274 | |||
| 275 | if('HASH' eq ref $s) | ||
| 276 | { | ||
| 277 | my (%hash, @keys) ; | ||
| 278 | |||
| 279 | for my $entry (sort keys %{$s}) | ||
| 280 | { | ||
| 281 | if('ARRAY' eq ref $s->{$entry}) | ||
| 282 | { | ||
| 283 | my $shortcuts = $s->{$entry}[0] ; | ||
| 284 | |||
| 285 | $shortcuts = join(' ', @{$shortcuts}) if('ARRAY' eq ref $shortcuts) ; | ||
| 286 | |||
| 287 | my $key_name = "$entry [$shortcuts]" ; | ||
| 288 | |||
| 289 | $hash{$key_name} = [] ; | ||
| 290 | |||
| 291 | push @keys, $key_name ; | ||
| 292 | } | ||
| 293 | else | ||
| 294 | { | ||
| 295 | $hash{$entry} = $s->{$entry} ; | ||
| 296 | push @keys, $entry ; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | return('HASH', \%hash, @keys) ; | ||
| 301 | } | ||
| 302 | |||
| 303 | return(Data::TreeDumper::DefaultNodesToDisplay($s)) ; | ||
| 304 | } | ||
| 305 | |||
| 306 | sub generate_keyboard_mapping_text_dump | ||
| 307 | { | ||
| 308 | my ($key_mapping, $actions_per_file) = @_ ; | ||
| 309 | |||
| 310 | die "Need argument!" unless defined $actions_per_file ; | ||
| 311 | |||
| 312 | for my $action (keys %{$key_mapping}) | ||
| 313 | { | ||
| 314 | if('ARRAY' eq ref $key_mapping->{$action}) | ||
| 315 | { | ||
| 316 | $actions_per_file->{$key_mapping->{$action}[6]}{$action} = $key_mapping->{$action} ; | ||
| 317 | } | ||
| 318 | elsif('HASH' eq ref $key_mapping->{$action}) | ||
| 319 | { | ||
| 320 | my $sub_actions = {} ; | ||
| 321 | |||
| 322 | { | ||
| 323 | local $key_mapping->{$action}{GROUP_NAME} = undef ; | ||
| 324 | local $key_mapping->{$action}{ORIGIN} = undef ; | ||
| 325 | local $key_mapping->{$action}{SHORTCUTS} = undef ; | ||
| 326 | |||
| 327 | generate_keyboard_mapping_text_dump($key_mapping->{$action}, $sub_actions) ; | ||
| 328 | } | ||
| 329 | |||
| 330 | #~ print Data::TreeDumper::DumpTree $key_mapping->{$action} ; | ||
| 331 | #~ print Data::TreeDumper::DumpTree $sub_actions ; | ||
| 332 | |||
| 333 | my $shortcuts = $key_mapping->{$action}{SHORTCUTS} ; | ||
| 334 | $shortcuts = join(' ', @{$key_mapping->{$action}{SHORTCUTS}}) | ||
| 335 | if('ARRAY' eq ref $key_mapping->{$action}{SHORTCUTS}) ; | ||
| 336 | |||
| 337 | $actions_per_file->{$key_mapping->{$action}{ORIGIN}}{"group: $action [$shortcuts]"} | ||
| 338 | = $sub_actions->{$key_mapping->{$action}{ORIGIN}} ; | ||
| 339 | } | ||
| 340 | else | ||
| 341 | { | ||
| 342 | #~ print Data::TreeDumper::DumpTree $key_mapping->{$action}, $action ; | ||
| 343 | #~ die "unknown type while running 'dump_keyboard_mapping'\n" ; | ||
| 344 | } | ||
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | #---------------------------------------------------------------------------------------------- | ||
| 349 | |||
| 350 | sub undo { my ($self) = @_ ; $self->undo(1) ; } | ||
| 351 | |||
| 352 | #---------------------------------------------------------------------------------------------- | ||
| 353 | |||
| 354 | sub redo { my ($self) = @_ ; $self->redo(1) ; } | ||
| 355 | |||
| 356 | #---------------------------------------------------------------------------------------------- | ||
| 357 | |||
| 358 | sub display_undo_stack_statistics | ||
| 359 | { | ||
| 360 | my ($self) = @_ ; | ||
| 361 | |||
| 362 | my $statistics = { DO_STACK_POINTER => $self->{DO_STACK_POINTER} } ; | ||
| 363 | |||
| 364 | my $total_size = 0 ; | ||
| 365 | |||
| 366 | for my $stack_element (@{$self->{DO_STACK}}) | ||
| 367 | { | ||
| 368 | push @{$statistics->{ELEMENT_SIZE}}, length($stack_element) ; | ||
| 369 | $total_size += length($stack_element) ; | ||
| 370 | } | ||
| 371 | |||
| 372 | $statistics->{TOTAL_SIZE} = $total_size ; | ||
| 373 | |||
| 374 | $self->show_dump_window($statistics, 'Undo stack statistics:') ; | ||
| 375 | } | ||
| 376 | |||
| 377 | #---------------------------------------------------------------------------------------------- | ||
| 378 | |||
| 379 | sub insert_multiple_boxes_from_text_description | ||
| 380 | { | ||
| 381 | my ($self, $boxed) = @_ ; | ||
| 382 | |||
| 383 | my $text = $self->display_edit_dialog('multiple objects from input', "\ntext\ntext\ntext\ntext", $self) ; | ||
| 384 | |||
| 385 | if(defined $text && $text ne '') | ||
| 386 | { | ||
| 387 | $self->create_undo_snapshot() ; | ||
| 388 | |||
| 389 | my ($current_x, $current_y) = ($self->{MOUSE_X}, $self->{MOUSE_Y}) ; | ||
| 390 | my ($separator) = split("\n", $text) ; | ||
| 391 | |||
| 392 | $text =~ s/$separator\n// ; | ||
| 393 | |||
| 394 | my @new_elements ; | ||
| 395 | |||
| 396 | for my $element_text (split("$separator\n", $text)) | ||
| 397 | { | ||
| 398 | chomp $element_text ; | ||
| 399 | |||
| 400 | my $new_element = new App::Asciio::stripes::editable_box2 | ||
| 401 | ({ | ||
| 402 | TITLE => '', | ||
| 403 | TEXT_ONLY => $element_text, | ||
| 404 | EDITABLE => 1, | ||
| 405 | RESIZABLE => 1, | ||
| 406 | }) ; | ||
| 407 | |||
| 408 | if(! $boxed) | ||
| 409 | { | ||
| 410 | my $box_type = $new_element->get_box_type() ; | ||
| 411 | |||
| 412 | for my $box_element (@{$box_type}) | ||
| 413 | { | ||
| 414 | $box_element->[0] = 0 ; | ||
| 415 | } | ||
| 416 | |||
| 417 | $new_element->set_box_type($box_type) ; | ||
| 418 | $new_element->shrink() ; | ||
| 419 | } | ||
| 420 | |||
| 421 | @$new_element{'X', 'Y'} = ($current_x, $current_y) ; | ||
| 422 | $current_x += $self->{COPY_OFFSET_X} ; | ||
| 423 | $current_y += $self->{COPY_OFFSET_Y} ; | ||
| 424 | |||
| 425 | push @new_elements , $new_element ; | ||
| 426 | } | ||
| 427 | |||
| 428 | $self->deselect_all_elements() ; | ||
| 429 | $self->add_elements(@new_elements) ; | ||
| 430 | $self->select_elements(1, @new_elements) ; | ||
| 431 | $self->update_display() ; | ||
| 432 | } | ||
| 433 | } | ||
| 434 | |||
| 435 | #---------------------------------------------------------------------------------------------- | ||
| 436 | |||
| 437 | sub flip_connector_display | ||
| 438 | { | ||
| 439 | my ($self) = @_ ; | ||
| 440 | $self->{DISPLAY_ALL_CONNECTORS} ^=1 ; | ||
| 441 | $self->update_display() ; | ||
| 442 | } | ||
| 443 | |||
| 444 | #---------------------------------------------------------------------------------------------- | ||
| 445 | |||
| 446 | sub flip_grid_display | ||
| 447 | { | ||
| 448 | my ($self) = @_ ; | ||
| 449 | $self->{DISPLAY_GRID} ^=1 ; | ||
| 450 | delete $self->{CACHE}{GRID} ; | ||
| 451 | $self->update_display() ; | ||
| 452 | } | ||
| 453 | |||
| 454 | #---------------------------------------------------------------------------------------------- | ||
| 455 | |||
| 456 | sub flip_hint_lines | ||
| 457 | { | ||
| 458 | my ($self) = @_ ; | ||
| 459 | $self->{DRAW_HINT_LINES} ^=1 ; | ||
| 460 | $self->update_display() ; | ||
| 461 | } | ||
| 462 | |||
| 463 | #---------------------------------------------------------------------------------------------- | ||
| 464 | |||
| 465 | sub transparent_elements | ||
| 466 | { | ||
| 467 | my ($self) = @_ ; | ||
| 468 | $self->{OPAQUE_ELEMENTS} ^=1 ; | ||
| 469 | $self->update_display() ; | ||
| 470 | } | ||
| 471 | |||
| 472 | #---------------------------------------------------------------------------------------------- | ||
| 473 | |||
| 474 | sub change_font | ||
| 475 | { | ||
| 476 | my ($self) = @_ ; | ||
| 477 | |||
| 478 | my ($family, $size) = $self->get_font() ; | ||
| 479 | if($family eq 'Monospace') | ||
| 480 | { | ||
| 481 | $self->set_font('sarasa mono sc', 12) ; | ||
| 482 | $self->{FONT_MIN} = 3 ; | ||
| 483 | $self->{ZOOM_STEP} = 3 ; | ||
| 484 | } | ||
| 485 | else | ||
| 486 | { | ||
| 487 | $self->set_font('Monospace', 12) ; | ||
| 488 | $self->{FONT_MIN} = 3 ; | ||
| 489 | $self->{ZOOM_STEP} = 1 ; | ||
| 490 | } | ||
| 491 | |||
| 492 | $self->invalidate_rendering_cache() ; | ||
| 493 | $self->update_display() ; | ||
| 494 | } | ||
| 495 | |||
| 496 | #---------------------------------------------------------------------------------------------- | ||
| 497 | |||
| 498 | 1 ; | ||
| 499 |