File size: 34,099 Bytes
7578e9d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 | ###################################################
#
# Copyright (C) 2008-2013 Mario Kemper <mario.kemper@gmail.com>
#
# This file is part of Shutter.
#
# Shutter is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Shutter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Shutter; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
###################################################
#perl -x -S perltidy -l=0 -b "%f"
package Shutter::Screenshot::SelectorAdvanced;
#modules
#--------------------------------------
use utf8;
use strict;
use warnings;
use Gtk3::ImageView;
use GooCanvas2;
use GooCanvas2::CairoTypes;
use Shutter::Screenshot::Main;
use Shutter::Screenshot::History;
use Data::Dumper;
our @ISA = qw(Shutter::Screenshot::Main);
#Glib
use Glib qw/TRUE FALSE/;
#--------------------------------------
sub new {
my $class = shift;
#call constructor of super class (shutter_common, include_cursor, delay, notify_timeout)
my $self = $class->SUPER::new(shift, shift, shift, shift);
$self->{_zoom_active} = shift;
$self->{_hide_time} = shift; #a short timeout to give the server a chance to redraw the area that was obscured
$self->{_show_help} = shift; #hide help text?
#initial selection size
$self->{_init_x} = shift;
$self->{_init_y} = shift;
$self->{_init_w} = shift;
$self->{_init_h} = shift;
$self->{_confirmation_necessary} = shift;
$self->{_dpi_scale} = Gtk3::Window->new('toplevel')->get('scale-factor');
#view, selector, dragger
$self->{_view} = Gtk3::ImageView->new;
$self->{_selector} = Gtk3::ImageView::Tool::Selector->new($self->{_view});
#$self->{_dragger} = Gtk3::ImageView::Tool::Dragger->new($self->{_view});
$self->{_view}->set_interpolation('nearest');
$self->{_view}->set_tool($self->{_selector});
$self->{_view}->set('zoom-step', 1.2);
#WORKAROUND
#upstream bug
#http://trac.bjourne.webfactional.com/ticket/21
#left => zoom in
#right => zoom out
$self->{_view}->signal_connect(
'scroll-event',
sub {
my ($view, $ev) = @_;
if ($ev->direction eq 'left') {
$ev->direction('up');
} elsif ($ev->direction eq 'right') {
$ev->direction('down');
}
return FALSE;
});
bless $self, $class;
return $self;
}
#~ sub DESTROY {
#~ my $self = shift;
#~ print "$self dying at\n";
#~ }
sub select_advanced {
my $self = shift;
#return value
my $output = 5;
my $d = $self->{_sc}->get_gettext;
#create pixbuf (root window)
my $clean_pixbuf = Gtk3::Gdk::pixbuf_get_from_window($self->{_root}, 0, 0, $self->{_root}->{w}, $self->{_root}->{h});
$self->{_view}->set_pixbuf($clean_pixbuf);
#show help text (do not show help text if predefined selection area is enabled)?
if ($self->{_init_w} < 1 || $self->{_init_h} < 1) {
if ($self->{_show_help}) {
Glib::Idle->add(
sub {
#we display the tip only on the current monitor
#if we would use the root window we would display the next
#right in the middle of both screens, this is pretty ugly
my $mon1 = $self->get_current_monitor;
print "Using monitor: " . $mon1->{x} . " - " . $mon1->{y} . " - " . $mon1->{width} . " - " . $mon1->{height} . "\n"
if $self->{_sc}->get_debug;
#obtain current colors and font_desc from the main window
my $style = $self->{_sc}->get_mainwindow->get_style_context;
my $sel_bg = Gtk3::Gdk::RGBA::parse('#131313');
my $font_fam = $style->get_font('normal')->get_family;
my $font_size = $style->get_font('normal')->get_size * $self->{_dpi_scale} / Pango::SCALE;
#create cairo context und layout
my $surface = Cairo::ImageSurface->create('argb32', $self->{_root}->{w}*$self->{_dpi_scale}, $self->{_root}->{h}*$self->{_dpi_scale});
my $cr = Cairo::Context->create($surface);
#set_source_pixbuf
Gtk3::Gdk::cairo_set_source_pixbuf($cr, $clean_pixbuf, 0, 0);
$cr->paint;
my $layout = Pango::Cairo::create_layout($cr);
$layout->set_width(int($mon1->{width} * $self->{_dpi_scale} / 2) * Pango::SCALE);
$layout->set_alignment('left');
$layout->set_wrap('word');
#determine font-size
my $size1 = int($font_size * 2.0);
my $size2 = int($font_size * 1.5);
my $size3 = int($font_size * 1.0);
my $text1 = $d->get("Draw a rectangular area using the mouse.");
my $text2 = $d->get("To take a screenshot, double-click or press the Enter key.\nPress Esc to abort.");
my $text3 =
$d->get("<b>shift/right-click</b> → selection dialog on/off") . "\n"
. $d->get("<b>scrollwheel</b> → zoom in/out") . "\n"
. $d->get("<b>space</b> → zoom window on/off") . "\n"
. $d->get("<b>cursor keys</b> → move cursor") . "\n"
. $d->get("<b>cursor keys + alt</b> → move selection") . "\n"
. $d->get("<b>cursor keys + ctrl</b> → resize selection");
#use this one for white font-color
$layout->set_markup(
"<span font_desc=\"$font_fam $size1\" foreground=\"#FFFFFF\">$text1</span>\n<span font_desc=\"$font_fam $size2\" foreground=\"#FFFFFF\">$text2</span>\n\n<span font_desc=\"$font_fam $size3\" foreground=\"#FFFFFF\">$text3</span>"
);
#draw the rectangle
$cr->set_source_rgba($sel_bg->red, $sel_bg->green, $sel_bg->blue, 0.85);
my ($lw, $lh) = $layout->get_pixel_size;
my $w = $lw + $size1 * 2;
my $h = $lh + $size1 * 2;
my $x = int(($mon1->{width}*$self->{_dpi_scale} - $w) / 2) + $mon1->{x};
my $y = int(($mon1->{height}*$self->{_dpi_scale} - $h) / 2) + $mon1->{y};
my $r = 20*$self->{_dpi_scale};
$cr->move_to($x + $r, $y);
$cr->line_to($x + $w - $r, $y);
$cr->curve_to($x + $w, $y, $x + $w, $y, $x + $w, $y + $r);
$cr->line_to($x + $w, $y + $h - $r);
$cr->curve_to($x + $w, $y + $h, $x + $w, $y + $h, $x + $w - $r, $y + $h);
$cr->line_to($x + $r, $y + $h);
$cr->curve_to($x, $y + $h, $x, $y + $h, $x, $y + $h - $r);
$cr->line_to($x, $y + $r);
$cr->curve_to($x, $y, $x, $y, $x + $r, $y);
$cr->fill;
$cr->move_to($x + $size1, $y + $size1);
#draw the pango layout
Pango::Cairo::show_layout($cr, $layout);
#write surface to pixbuf
my $loader = Gtk3::Gdk::PixbufLoader->new;
$surface->write_to_png_stream(
sub {
my ($closure, $data) = @_;
$loader->write([map ord, split //, $data]);
return TRUE;
});
$loader->close;
#set pixbuf
$self->{_view}->set_pixbuf($loader->get_pixbuf);
return FALSE;
});
}
}
#define zoom window
$self->{_zoom_window} = Gtk3::Window->new('popup');
$self->{_zoom_window}->set_decorated(FALSE);
$self->{_zoom_window}->set_skip_taskbar_hint(TRUE);
$self->{_zoom_window}->set_skip_pager_hint(TRUE);
$self->{_zoom_window}->set_keep_above(TRUE);
$self->{_zoom_window}->set_accept_focus(FALSE);
#pack canvas to a scrolled window
my $scwin = Gtk3::ScrolledWindow->new();
$scwin->set_policy('never', 'never');
#define and setup the canvas
my $canvas = GooCanvas2::Canvas->new();
$canvas->set_size_request(105, 105);
$canvas->modify_bg('normal', Gtk3::Gdk::RGBA::parse('#00000000'));
$canvas->set_bounds(-10*$self->{_dpi_scale}, -10*$self->{_dpi_scale}, ($self->{_root}->{w}+10)*$self->{_dpi_scale}, ($self->{_root}->{h}+10)*$self->{_dpi_scale});
$canvas->set_scale(5);
my $canvas_root = $canvas->get_root_item();
$scwin->add($canvas);
my $xlabel = Gtk3::Label->new("X: ");
my $ylabel = Gtk3::Label->new("Y: ");
my $rlabel = Gtk3::Label->new("0 x 0");
$ylabel->set_max_width_chars(10);
$xlabel->set_max_width_chars(10);
$rlabel->set_max_width_chars(10);
my $zoom_vbox = Gtk3::VBox->new;
$zoom_vbox->pack_start($scwin, TRUE, TRUE, 0);
$zoom_vbox->pack_start($xlabel, TRUE, TRUE, 0);
$zoom_vbox->pack_start($ylabel, TRUE, TRUE, 0);
$zoom_vbox->pack_start($rlabel, TRUE, TRUE, 0);
#do some packing
$self->{_zoom_window}->add($zoom_vbox);
$self->{_zoom_window}->move($self->{_root}->{x}, $self->{_root}->{y});
#define shutter cursor (frame)
my $shutter_cursor_pixbuf_frame = Gtk3::Gdk::Pixbuf->new_from_file($self->{_sc}->get_root . "/share/shutter/resources/icons/shutter_cursor_frame.png");
#create root...
my $root_item = GooCanvas2::CanvasImage->new(
parent => $canvas_root,
x => 0,
y => 0,
pixbuf => $clean_pixbuf
);
GooCanvas2::CairoTypes::cairoize_pattern($root_item->get('pattern'))->set_filter('nearest');
#...and cursor icon
my $cursor_item = GooCanvas2::CanvasImage->new(
parent => $canvas_root,
x => 0,
y => 0,
pixbuf => $shutter_cursor_pixbuf_frame,
);
GooCanvas2::CairoTypes::cairoize_pattern($cursor_item->get('pattern'))->set_filter('nearest');
#starting point
my ($window_at_pointer, $xinit, $yinit, $mask) = $self->{_root}->get_pointer;
#move cursor on the canvas...
$cursor_item->set(
x => $xinit - 10,
y => $yinit - 10,
);
#scroll region
#$canvas->set_scroll_region($xinit - 9, $yinit - 9, $xinit + 10, $yinit + 10);
$canvas->scroll_to($xinit - 10, $yinit - 10);
#window to manipulate the selection
$self->{_prop_window} = $self->select_dialog();
$self->{_prop_active} = FALSE;
#window that contains the imageview widget
$self->{_select_window} = Gtk3::Window->new('popup');
$self->{_select_window}->set_type_hint('splashscreen');
$self->{_select_window}->set_can_focus(TRUE);
$self->{_select_window}->set_accept_focus(TRUE);
$self->{_select_window}->set_modal(TRUE);
$self->{_select_window}->set_decorated(FALSE);
$self->{_select_window}->set_skip_taskbar_hint(TRUE);
$self->{_select_window}->set_skip_pager_hint(TRUE);
$self->{_select_window}->set_keep_above(TRUE);
$self->{_select_window}->add($self->{_view});
$self->{_select_window}->set_default_size($self->{_root}->{w}, $self->{_root}->{h});
$self->{_select_window}->resize($self->{_root}->{w}, $self->{_root}->{h});
$self->{_select_window}->move($self->{_root}->{x}, $self->{_root}->{y});
$self->{_select_window}->show_all;
$self->{_select_window}->present;
#init state flags
if ($self->{_show_help}) {
$self->{_selector_init} = TRUE;
} else {
$self->{_selector_init} = FALSE;
}
$self->{_selector_init_zoom} = 0;
#hide help text when selector is invoked
$self->{_selector_handler} = $self->{_selector}->signal_connect(
'selection-changed' => sub {
#hide initial text
if ($self->{_selector_init}) {
$self->{_view}->set_pixbuf($clean_pixbuf, FALSE);
$self->{_selector_init} = FALSE;
$self->{_selector_init_zoom}++;
}
#update prop dialog values
$self->adjust_prop_values();
});
#handle zoom events
#ignore zoom values smaller 1
$self->{_view_zoom_handler} = $self->{_view}->signal_connect(
'zoom-changed' => sub {
my ($view, $zoom) = @_;
if ($zoom >= 1) {
$view->set_interpolation('nearest');
$view->set_zoom(10) if $zoom > 10;
} else {
$view->set_interpolation('bilinear');
$view->set_zoom(1);
}
if ($self->{_zoom_active}) {
if ($zoom > 1) {
$self->{_zoom_window}->hide;
} else {
$self->{_zoom_window}->show_all;
$self->zoom_check_pos();
}
}
#hide help text when zoomed
if ($self->{_selector_init_zoom} == 1) {
$view->set_pixbuf($clean_pixbuf, FALSE);
$self->{_selector_init} = FALSE;
} else {
$self->{_selector_init_zoom}++;
}
});
#set initial size
Glib::Idle->add(
sub {
if ($self->{_init_w} && $self->{_init_h}) {
$self->{_selector}->set_selection({x=>$self->{_init_x}, y=>$self->{_init_y}, width=>$self->{_init_w}, height=>$self->{_init_h}});
}
return FALSE;
});
#event-handling
#we simulate a 2button-press here
$self->{_view_button_handler} = $self->{_view}->signal_connect(
'button-press-event' => sub {
my ($view, $event) = @_;
return FALSE unless defined $event;
my $s = $self->{_selector}->get_selection;
if ($event->button == 1) {
unless (defined $self->{_dclick}) {
$self->{_dclick} = $event->time;
return FALSE;
} else {
if ($event->time - $self->{_dclick} <= 500) {
$self->{_select_window}->hide;
$self->{_zoom_window}->hide;
$self->{_prop_window}->hide;
#A short timeout to give the server a chance to
#redraw the area
Glib::Timeout->add(
$self->{_hide_time},
sub {
Gtk3->main_quit;
return FALSE;
});
Gtk3->main();
$output = $self->take_screenshot($s, $clean_pixbuf);
$self->quit;
} else {
$self->{_dclick} = $event->time;
return FALSE;
}
}
}
});
#event-handling
#all other events
$self->{_view_event_handler} = $self->{_view}->signal_connect(
'event' => sub {
my ($window, $event) = @_;
return FALSE unless defined $event;
my $s = $self->{_selector}->get_selection;
#~ print $event->type, "\n";
#handle button-release event
if ($event->type eq 'button-release') {
if ($event->button == 3) {
if ($self->{_prop_active}) {
Gtk3::Gdk::keyboard_ungrab(Gtk3::get_current_event_time());
$self->{_prop_window}->hide;
$self->{_prop_active} = FALSE;
Gtk3::Gdk::keyboard_grab($self->{_select_window}->get_window, 0, Gtk3::get_current_event_time());
} else {
Gtk3::Gdk::keyboard_ungrab(Gtk3::get_current_event_time());
my ($window_at_pointer, $x, $y, $mask) = $self->{_root}->get_pointer;
$self->{_prop_window}->move($x, $y);
$self->{_prop_window}->show_all;
$self->{_prop_active} = TRUE;
Gtk3::Gdk::keyboard_grab($self->{_prop_window}->get_window, 0, Gtk3::get_current_event_time());
}
} elsif ($event->button == 1) {
if (not $self->{_confirmation_necessary}) {
$self->{_select_window}->hide;
$self->{_zoom_window}->hide;
$self->{_prop_window}->hide;
#A short timeout to give the server a chance to
#redraw the area
Glib::Timeout->add(
$self->{_hide_time},
sub {
Gtk3->main_quit;
return FALSE;
});
Gtk3->main();
$output = $self->take_screenshot($s, $clean_pixbuf);
$self->quit;
}
}
#handle motion-notify
} elsif ($event->type eq 'motion-notify') {
#update zoom window
if ($self->{_zoom_active} && $self->{_view}->get_zoom == 1) {
my $s = $self->{_selector}->get_selection;
my $v = $self->{_view}->get_viewport;
my ($window_at_pointer, $x, $y, $mask) = $self->{_root}->get_pointer;
#event coordinates
my $zoom = $self->{_view}->get_zoom;
my $ev_x = int($v->{x} / $zoom + $x * $self->{_dpi_scale} / $zoom);
my $ev_y = int($v->{y} / $zoom + $y * $self->{_dpi_scale} / $zoom);
#sync cursor with selection
if (0 && defined $s) {
my $cursor = $self->{_selector}->cursor_at_point($x, $y)->get_cursor_type;
print Dumper($cursor);
my $sx = $s->{x};
my $sy = $s->{y};
my $sw = $s->{width};
my $sh = $s->{height};
if ($cursor eq 'bottom-right-corner') {
$ev_x = $sx + $sw - 1;
$ev_y = $sy + $sh - 1;
} elsif ($cursor eq 'right-side') {
$ev_x = $sx + $sw - 1;
} elsif ($cursor eq 'top-right-corner') {
$ev_x = $sx + $sw - 1;
$ev_y = $sy;
} elsif ($cursor eq 'top-side') {
$ev_y = $sy;
} elsif ($cursor eq 'top-left-corner') {
$ev_x = $sx;
$ev_y = $sy;
} elsif ($cursor eq 'left-side') {
$ev_x = $sx;
} elsif ($cursor eq 'bottom-left-corner') {
$ev_x = $sx;
$ev_y = $sy + $sh - 1;
} elsif ($cursor eq 'bottom-side') {
$ev_y = $sy + $sh - 1;
}
}
#update label in zoom_window
$xlabel->set_text("X: " . ($ev_x + 1));
$ylabel->set_text("Y: " . ($ev_y + 1));
#check pos and geometry of the zoom window and move it if needed
$self->zoom_check_pos();
#move cursor on the canvas...
$cursor_item->set(
x => $ev_x - 10,
y => $ev_y - 10,
);
#update scroll region
#this is significantly faster than
#scroll_to
#$canvas->set_scroll_region($ev_x - 9, $ev_y - 9, $ev_x + 10, $ev_y + 10);
$canvas->scroll_to($ev_x - 10, $ev_y - 10);
#update zoom_window text
if (defined $s) {
$rlabel->set_text($s->{width} . " x " . $s->{height});
} else {
$rlabel->set_text("0 x 0");
}
} #zoom active
#handle key-press
}
});
$self->{_key_handler} = $self->{_select_window}->signal_connect(
'key-press-event' => sub {
my ($window, $event) = @_;
return FALSE unless defined $event;
my $s = $self->{_selector}->get_selection;
#where is the pointer currently?
my ($window_at_pointer, $x, $y, $mask) = $self->{_root}->get_pointer;
#toggle zoom window
if ($event->keyval == Gtk3::Gdk::keyval_from_name('space')) {
if ($self->{_zoom_active}) {
$self->{_zoom_window}->hide;
$self->{_zoom_active} = FALSE;
} elsif ($self->{_view}->get_zoom == 1) {
$self->zoom_check_pos();
$self->{_zoom_active} = TRUE;
}
#toggle prop dialog
} elsif ($event->keyval == Gtk3::Gdk::keyval_from_name('Shift_L') || $event->keyval == Gtk3::Gdk::keyval_from_name('Shift_R')) {
if ($self->{_prop_active}) {
Gtk3::Gdk::keyboard_ungrab(Gtk3::get_current_event_time());
$self->{_prop_window}->hide;
$self->{_prop_active} = FALSE;
Gtk3::Gdk::keyboard_grab($self->{_select_window}->get_window, 0, Gtk3::get_current_event_time());
} else {
Gtk3::Gdk::keyboard_ungrab(Gtk3::get_current_event_time());
my ($window_at_pointer, $x, $y, $mask) = $self->{_root}->get_pointer;
$self->{_prop_window}->move($x, $y);
$self->{_prop_window}->show_all;
$self->{_prop_active} = TRUE;
Gtk3::Gdk::keyboard_grab($self->{_prop_window}->get_window, 0, Gtk3::get_current_event_time());
}
#abort screenshot
} elsif ($event->keyval == Gtk3::Gdk::keyval_from_name('Escape')) {
$self->quit;
#move / resize selector
} elsif ($event->keyval == Gtk3::Gdk::keyval_from_name('Up')) {
if ($event->state >= 'control-mask' && $s) {
$s->{height} -= 1;
$self->{_selector}->set_selection($s);
$self->{_gdk_display}->warp_pointer($self->{_gdk_screen}, $s->{width} + $s->{x}, $s->{height} + $s->{y});
} elsif ($event->state >= 'mod1-mask' && $s) {
$s->{y} -= 1;
$self->{_selector}->set_selection($s);
$self->{_gdk_display}->warp_pointer($self->{_gdk_screen}, $s->{x}, $s->{y});
} else {
$self->{_gdk_display}->warp_pointer($self->{_gdk_screen}, $x, $y - 1);
}
} elsif ($event->keyval == Gtk3::Gdk::keyval_from_name('Down')) {
if ($event->state >= 'control-mask' && $s) {
$s->{height} += 1;
$self->{_selector}->set_selection($s);
$self->{_gdk_display}->warp_pointer($self->{_gdk_screen}, $s->{width} + $s->{x}, $s->{height} + $s->{y});
} elsif ($event->state >= 'control-mask') {
$self->{_selector}->set_selection({x=>$x, y=>$y, width=>1, height=>2});
$self->{_gdk_display}->warp_pointer($self->{_gdk_screen}, $x + 1, $y + 2);
} elsif ($event->state >= 'mod1-mask' && $s) {
$s->{y} += 1;
$self->{_selector}->set_selection($s);
$self->{_gdk_display}->warp_pointer($self->{_gdk_screen}, $s->{x}, $s->{y});
} else {
$self->{_gdk_display}->warp_pointer($self->{_gdk_screen}, $x, $y + 1);
}
} elsif ($event->keyval == Gtk3::Gdk::keyval_from_name('Left')) {
if ($event->state >= 'control-mask' && $s) {
$s->{width} -= 1;
$self->{_selector}->set_selection($s);
$self->{_gdk_display}->warp_pointer($self->{_gdk_screen}, $s->{width} + $s->{x}, $s->{height} + $s->{y});
} elsif ($event->state >= 'mod1-mask' && $s) {
$s->{x} -= 1;
$self->{_selector}->set_selection($s);
$self->{_gdk_display}->warp_pointer($self->{_gdk_screen}, $s->{x}, $s->{y});
} else {
$self->{_gdk_display}->warp_pointer($self->{_gdk_screen}, $x - 1, $y);
}
} elsif ($event->keyval == Gtk3::Gdk::keyval_from_name('Right')) {
if ($event->state >= 'control-mask' && $s) {
$s->{width} += 1;
$self->{_selector}->set_selection($s);
$self->{_gdk_display}->warp_pointer($self->{_gdk_screen}, $s->{width} + $s->{x}, $s->{height} + $s->{y});
} elsif ($event->state >= 'control-mask') {
$self->{_selector}->set_selection({x=>$x, y=>$y, width=>2, height=>1});
$self->{_gdk_display}->warp_pointer($self->{_gdk_screen}, $x + 2, $y + 1);
} elsif ($event->state >= 'mod1-mask' && $s) {
$s->{x} += 1;
$self->{_selector}->set_selection($s);
$self->{_gdk_display}->warp_pointer($self->{_gdk_screen}, $s->{x}, $s->{y});
} else {
$self->{_gdk_display}->warp_pointer($self->{_gdk_screen}, $x + 1, $y);
}
#zoom in
} elsif ($event->keyval == Gtk3::Gdk::keyval_from_name('KP_Add')
|| $event->keyval == Gtk3::Gdk::keyval_from_name('plus')
|| $event->keyval == Gtk3::Gdk::keyval_from_name('equal'))
{
if ($event->state >= 'control-mask') {
$self->{_view}->zoom_in;
}
#zoom out
} elsif ($event->keyval == Gtk3::Gdk::keyval_from_name('KP_Subtract')
|| $event->keyval == Gtk3::Gdk::keyval_from_name('minus'))
{
if ($event->state >= 'control-mask') {
$self->{_view}->zoom_out;
}
#zoom normal
} elsif ($event->keyval == Gtk3::Gdk::keyval_from_name('0')) {
if ($event->state >= 'control-mask') {
$self->{_view}->set_zoom(1);
}
#take screenshot
} elsif ($event->keyval == Gtk3::Gdk::keyval_from_name('Return') || $event->keyval == Gtk3::Gdk::keyval_from_name('KP_Enter')) {
$self->{_select_window}->hide;
$self->{_zoom_window}->hide;
$self->{_prop_window}->hide;
#A short timeout to give the server a chance to
#redraw the area
Glib::Timeout->add(
$self->{_hide_time},
sub {
Gtk3->main_quit;
return FALSE;
});
Gtk3->main();
$output = $self->take_screenshot($s, $clean_pixbuf);
$self->quit;
}
});
my $status = Gtk3::Gdk::keyboard_grab($self->{_select_window}->get_window, 0, Gtk3::get_current_event_time());
#~ if($status eq 'success'){
if ($self->{_zoom_active}) {
$self->{_zoom_window}->show_all;
$self->{_zoom_window}->get_window->set_override_redirect(TRUE);
$self->zoom_check_pos();
$self->{_zoom_window}->get_window->raise;
}
Gtk3->main();
#~ }else{
#~ $output = 1;
#~ $self->clean;
#~ }
return $output;
}
sub zoom_check_pos {
my $self = shift;
my $s = $self->{_selector}->get_selection;
my $v = $self->{_view}->get_viewport;
return FALSE unless defined $v;
my ($window_at_pointer, $x, $y, $mask) = $self->{_root}->get_pointer;
#event coordinates
my $zoom = $self->{_view}->get_zoom;
my $ev_x = int($v->{x} / $zoom + $x * $self->{_dpi_scale} / $zoom);
my $ev_y = int($v->{y} / $zoom + $y * $self->{_dpi_scale} / $zoom);
my ($zw, $zh) = $self->{_zoom_window}->get_size;
my ($zx, $zy) = $self->{_zoom_window}->get_position;
my $distance = 50 * $self->{_dpi_scale};
my $zzw = $zw * $self->{_dpi_scale} + $distance;
my $zzh = $zh * $self->{_dpi_scale} + $distance;
my $sregion = undef;
if (defined $s) {
$sregion = Cairo::Region->create({x=>$s->{x}, y=>$s->{y}, width=>$s->{width} + $distance, height=>$s->{height} + $distance});
} else {
$sregion = Cairo::Region->create({x=>$ev_x, y=>$ev_y, width=>$distance, height=>$distance});
}
my $otype = $sregion->contains_rectangle({x=>$zx, y=>$zy, width=>$zzw, height=>$zzh});
if ($otype eq 'in' || $otype eq 'part' || !$self->{_zoom_window}->get_visible) {
my $moved = FALSE;
#possible positions if we need to move the zoom window
my @pos = (
{x=>$self->{_root}->{x}, y=>$self->{_root}->{y}, },
{x=>$self->{_root}->{x}, y=>$self->{_root}->{h} - $zh},
{x=>$self->{_root}->{w} - $zw, y=>$self->{_root}->{y}, },
{x=>$self->{_root}->{w} - $zw, y=>$self->{_root}->{h} - $zh});
foreach (@pos) {
my $otypet = $sregion->contains_rectangle({x=>$_->{x}*$self->{_dpi_scale}, y=>$_->{y}*$self->{_dpi_scale}, width=>$zzw, height=>$zzh});
if ($otypet eq 'out') {
$self->{_zoom_window}->move($_->{x}, $_->{y});
$self->{_zoom_window}->show_all;
$moved = TRUE;
last;
}
}
#if window could not be moved without covering the selection area
unless ($moved) {
$moved = FALSE;
$self->{_zoom_window}->hide;
}
}
}
sub adjust_prop_values {
my $self = shift;
#block 'value-change' handlers for widgets
#so we do not apply the changes twice
$self->{_x_spin_w}->signal_handler_block($self->{_x_spin_w_handler});
$self->{_y_spin_w}->signal_handler_block($self->{_y_spin_w_handler});
$self->{_width_spin_w}->signal_handler_block($self->{_width_spin_w_handler});
$self->{_height_spin_w}->signal_handler_block($self->{_height_spin_w_handler});
my $s = $self->{_selector}->get_selection;
if ($s) {
$self->{_x_spin_w}->set_value($s->{x});
$self->{_x_spin_w}->set_range(0, $self->{_root}->{w} - $s->{width});
$self->{_y_spin_w}->set_value($s->{y});
$self->{_y_spin_w}->set_range(0, $self->{_root}->{h} - $s->{height});
$self->{_width_spin_w}->set_value($s->{width});
$self->{_width_spin_w}->set_range(0, $self->{_root}->{w} - $s->{x});
$self->{_height_spin_w}->set_value($s->{height});
$self->{_height_spin_w}->set_range(0, $self->{_root}->{h} - $s->{y});
}
#unblock 'value-change' handlers for widgets
$self->{_x_spin_w}->signal_handler_unblock($self->{_x_spin_w_handler});
$self->{_y_spin_w}->signal_handler_unblock($self->{_y_spin_w_handler});
$self->{_width_spin_w}->signal_handler_unblock($self->{_width_spin_w_handler});
$self->{_height_spin_w}->signal_handler_unblock($self->{_height_spin_w_handler});
return TRUE;
}
sub select_dialog {
my $self = shift;
my $d = $self->{_sc}->get_gettext;
#current selection
my $s = $self->{_selector}->get_selection;
my $sx = 0;
my $sy = 0;
my $sw = 0;
my $sh = 0;
if (defined $s) {
$sx = $s->{x};
$sy = $s->{y};
$sw = $s->{width};
$sh = $s->{height};
}
my $value_callback = sub {
$self->{_selector}
->set_selection({x=>$self->{_x_spin_w}->get_value, y=>$self->{_y_spin_w}->get_value, width=>$self->{_width_spin_w}->get_value, height=>$self->{_height_spin_w}->get_value});
};
#X
my $xw_label = Gtk3::Label->new($d->get("X") . ":");
$self->{_x_spin_w} = Gtk3::SpinButton->new_with_range(0, $self->{_root}->{w}, 1);
$self->{_x_spin_w}->set_value($sx);
$self->{_x_spin_w_handler} = $self->{_x_spin_w}->signal_connect(
'value-changed' => $value_callback);
my $xw_hbox = Gtk3::HBox->new(FALSE, 5);
$xw_hbox->pack_start($xw_label, FALSE, FALSE, 5);
$xw_hbox->pack_start($self->{_x_spin_w}, FALSE, FALSE, 5);
#y
my $yw_label = Gtk3::Label->new($d->get("Y") . ":");
$self->{_y_spin_w} = Gtk3::SpinButton->new_with_range(0, $self->{_root}->{h}, 1);
$self->{_y_spin_w}->set_value($sy);
$self->{_y_spin_w_handler} = $self->{_y_spin_w}->signal_connect(
'value-changed' => $value_callback);
my $yw_hbox = Gtk3::HBox->new(FALSE, 5);
$yw_hbox->pack_start($yw_label, FALSE, FALSE, 5);
$yw_hbox->pack_start($self->{_y_spin_w}, FALSE, FALSE, 5);
#width
my $widthw_label = Gtk3::Label->new($d->get("Width") . ":");
$self->{_width_spin_w} = Gtk3::SpinButton->new_with_range(0, $self->{_root}->{w}, 1);
$self->{_width_spin_w}->set_value($sw);
$self->{_width_spin_w_handler} = $self->{_width_spin_w}->signal_connect(
'value-changed' => $value_callback);
my $ww_hbox = Gtk3::HBox->new(FALSE, 5);
$ww_hbox->pack_start($widthw_label, FALSE, FALSE, 5);
$ww_hbox->pack_start($self->{_width_spin_w}, FALSE, FALSE, 5);
#height
my $heightw_label = Gtk3::Label->new($d->get("Height") . ":");
$self->{_height_spin_w} = Gtk3::SpinButton->new_with_range(0, $self->{_root}->{h}, 1);
$self->{_height_spin_w}->set_value($sh);
$self->{_height_spin_w_handler} = $self->{_height_spin_w}->signal_connect(
'value-changed' => $value_callback);
my $hw_hbox = Gtk3::HBox->new(FALSE, 5);
$hw_hbox->pack_start($heightw_label, FALSE, FALSE, 5);
$hw_hbox->pack_start($self->{_height_spin_w}, FALSE, FALSE, 5);
my $prop_dialog = Gtk3::Window->new('toplevel');
$prop_dialog->set_modal(TRUE);
$prop_dialog->set_decorated(FALSE);
$prop_dialog->set_skip_taskbar_hint(TRUE);
$prop_dialog->set_skip_pager_hint(TRUE);
$prop_dialog->set_keep_above(TRUE);
$prop_dialog->set_accept_focus(TRUE);
$prop_dialog->set_resizable(FALSE);
$prop_dialog->signal_connect(
'key-press-event' => sub {
my $window = shift;
my $event = shift;
#toggle zoom window
if ($event->keyval == Gtk3::Gdk::keyval_from_name('Space')) {
if ($self->{_zoom_active}) {
$self->{_zoom_window}->hide;
$self->{_zoom_active} = FALSE;
} elsif ($self->{_view}->get_zoom == 1) {
$self->zoom_check_pos();
$self->{_zoom_active} = TRUE;
}
#toggle prop dialog
} elsif ($event->keyval == Gtk3::Gdk::keyval_from_name('Shift_L') || $event->keyval == Gtk3::Gdk::keyval_from_name('Shift_R')) {
if ($self->{_prop_active}) {
Gtk3::Gdk::keyboard_ungrab(Gtk3::get_current_event_time());
$self->{_prop_window}->hide;
$self->{_prop_active} = FALSE;
Gtk3::Gdk::keyboard_grab($self->{_select_window}->get_window, 0, Gtk3::get_current_event_time());
} else {
Gtk3::Gdk::keyboard_ungrab(Gtk3::get_current_event_time());
my ($window_at_pointer, $x, $y, $mask) = $self->{_root}->get_pointer;
$self->{_prop_window}->move($x, $y);
$self->{_prop_window}->show_all;
$self->{_prop_active} = TRUE;
Gtk3::Gdk::keyboard_grab($self->{_prop_window}->get_window, 0, Gtk3::get_current_event_time());
}
#abort screenshot
} elsif ($event->keyval == Gtk3::Gdk::keyval_from_name('Escape')) {
$self->quit;
}
});
my $hide_btn = Gtk3::Button->new_with_mnemonic($d->get("_Hide"));
$hide_btn->set_image(Gtk3::Image->new_from_stock('gtk-close', 'button'));
$hide_btn->set_can_default(TRUE);
$hide_btn->signal_connect(
'clicked' => sub {
Gtk3::Gdk::keyboard_ungrab(Gtk3::get_current_event_time());
$prop_dialog->hide;
$self->{_prop_active} = FALSE;
Gtk3::Gdk::keyboard_grab($self->{_select_window}->get_window, 0, Gtk3::get_current_event_time());
});
#final_packing
#all labels = one size
$xw_label->set_alignment(0, 0.5);
$yw_label->set_alignment(0, 0.5);
$widthw_label->set_alignment(0, 0.5);
$heightw_label->set_alignment(0, 0.5);
my $sg_main = Gtk3::SizeGroup->new('horizontal');
$sg_main->add_widget($xw_label);
$sg_main->add_widget($yw_label);
$sg_main->add_widget($widthw_label);
$sg_main->add_widget($heightw_label);
my $vbox = Gtk3::VBox->new(FALSE, 5);
$vbox->pack_start($xw_hbox, FALSE, FALSE, 3);
$vbox->pack_start($yw_hbox, FALSE, FALSE, 3);
$vbox->pack_start($ww_hbox, FALSE, FALSE, 3);
$vbox->pack_start($hw_hbox, FALSE, FALSE, 3);
$vbox->pack_start($hide_btn, FALSE, FALSE, 3);
#nice frame as well
my $frame_label = Gtk3::Label->new;
$frame_label->set_markup("<b>" . $d->get("Selection") . "</b>");
my $frame = Gtk3::Frame->new();
$frame->set_border_width(5);
$frame->set_label_widget($frame_label);
$frame->set_shadow_type('none');
$frame->add($vbox);
$prop_dialog->add($frame);
$prop_dialog->realize;
$prop_dialog->set_transient_for($self->{_select_window});
$prop_dialog->get_window->set_override_redirect(TRUE);
return $prop_dialog;
}
sub take_screenshot {
my $self = shift;
my $s = shift;
my $clean_pixbuf = shift;
my $d = $self->{_sc}->get_gettext;
my $output;
#no delay? then we take a subsection of the pixbuf in memory
if ($s && $clean_pixbuf && $self->{_delay} == 0) {
$output = $clean_pixbuf->new_subpixbuf($s->{x}, $s->{y}, $s->{width}, $s->{height});
#include cursor
if ($self->{_include_cursor}) {
$output = $self->include_cursor($s->{x}, $s->{y}, $s->{width}, $s->{height}, $self->{_root}, $output);
}
#if there is a delay != 0 set, we have to wait and get a new pixbuf from the root window
} elsif ($s && $self->{_delay} != 0) {
($output) = $self->get_pixbuf_from_drawable($self->{_root}, $s->{x}, $s->{y}, $s->{width}, $s->{height});
#section not valid
} else {
$output = 0;
}
#we don't have a useful string for wildcards (e.g. $name)
if ($output =~ /Gtk3/) {
$self->{_action_name} = $d->get("Selection");
}
#set history object
if ($s) {
$self->{_history} = Shutter::Screenshot::History->new($self->{_sc}, $self->{_root}, $s->{x}, $s->{y}, $s->{width}, $s->{height});
}
return $output;
}
sub redo_capture {
my $self = shift;
my $output = 3;
if (defined $self->{_history}) {
($output) = $self->get_pixbuf_from_drawable($self->{_history}->get_last_capture);
}
return $output;
}
sub get_history {
my $self = shift;
return $self->{_history};
}
sub get_error_text {
my $self = shift;
return $self->{_error_text};
}
sub get_action_name {
my $self = shift;
return $self->{_action_name};
}
sub quit {
my $self = shift;
$self->ungrab_pointer_and_keyboard(FALSE, FALSE, TRUE);
$self->clean;
}
sub clean {
my $self = shift;
$self->{_selector}->signal_handler_disconnect($self->{_selector_handler});
$self->{_view}->signal_handler_disconnect($self->{_view_zoom_handler});
$self->{_view}->signal_handler_disconnect($self->{_view_button_handler});
$self->{_view}->signal_handler_disconnect($self->{_view_event_handler});
$self->{_select_window}->signal_handler_disconnect($self->{_key_handler});
$self->{_select_window}->destroy;
$self->{_zoom_window}->destroy;
$self->{_prop_window}->destroy;
}
1;
|