| 1 | package Gtk3::ImageView::Tool::Dragger; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use base 'Gtk3::ImageView::Tool'; | ||
| 6 | use Glib qw(TRUE FALSE); # To get TRUE and FALSE | ||
| 7 | use Readonly; | ||
| 8 | Readonly my $FLOAT_EPS => 0.01; | ||
| 9 | Readonly my $RIGHT_BUTTON => 3; | ||
| 10 | |||
| 11 | our $VERSION = 8; | ||
| 12 | |||
| 13 | sub button_pressed { | ||
| 14 | my $self = shift; | ||
| 15 | my $event = shift; | ||
| 16 | |||
| 17 | # Don't block context menu | ||
| 18 | if ( $event->button == $RIGHT_BUTTON ) { | ||
| 19 | return FALSE; | ||
| 20 | } | ||
| 21 | |||
| 22 | $self->{drag_start} = { x => $event->x, y => $event->y }; | ||
| 23 | $self->{dnd_start} = { x => $event->x, y => $event->y }; | ||
| 24 | $self->{dnd_eligible} = TRUE; | ||
| 25 | $self->{dragging} = TRUE; | ||
| 26 | $self->{button} = $event->button; | ||
| 27 | $self->view->update_cursor( $event->x, $event->y ); | ||
| 28 | return TRUE; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub button_released { | ||
| 32 | my $self = shift; | ||
| 33 | my $event = shift; | ||
| 34 | $self->{dragging} = FALSE; | ||
| 35 | $self->view->update_cursor( $event->x, $event->y ); | ||
| 36 | return; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub motion { | ||
| 40 | my $self = shift; | ||
| 41 | my $event = shift; | ||
| 42 | if ( not $self->{dragging} ) { return FALSE } | ||
| 43 | my $offset = $self->view->get_offset; | ||
| 44 | my $zoom = $self->view->get_zoom; | ||
| 45 | my $ratio = $self->view->get_resolution_ratio; | ||
| 46 | my $offset_x = | ||
| 47 | $offset->{x} + ( $event->x - $self->{drag_start}{x} ) / $zoom * $ratio; | ||
| 48 | my $offset_y = | ||
| 49 | $offset->{y} + ( $event->y - $self->{drag_start}{y} ) / $zoom; | ||
| 50 | ( $self->{drag_start}{x}, $self->{drag_start}{y} ) = | ||
| 51 | ( $event->x, $event->y ); | ||
| 52 | $self->view->set_offset( $offset_x, $offset_y ); | ||
| 53 | my $new_offset = $self->view->get_offset; | ||
| 54 | |||
| 55 | if ( not $self->{dnd_eligible} ) { | ||
| 56 | return; | ||
| 57 | } | ||
| 58 | |||
| 59 | if ( _approximately( $new_offset->{x}, $offset_x ) | ||
| 60 | and _approximately( $new_offset->{y}, $offset_y ) ) | ||
| 61 | { | ||
| 62 | # If there was a movement in the image, disable start of dnd until | ||
| 63 | # mouse button is pressed again | ||
| 64 | $self->{dnd_eligible} = FALSE; | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | # movement was clamped because of the edge, but did mouse move far enough? | ||
| 69 | if ( | ||
| 70 | $self->view->drag_check_threshold( | ||
| 71 | $self->{dnd_start}{x}, $self->{dnd_start}{y}, | ||
| 72 | $event->x, $event->y | ||
| 73 | ) | ||
| 74 | and $self->view->signal_emit( | ||
| 75 | 'dnd-start', $event->x, $event->y, $self->{button} | ||
| 76 | ) | ||
| 77 | ) | ||
| 78 | { | ||
| 79 | $self->{dragging} = FALSE; | ||
| 80 | } | ||
| 81 | return; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _approximately { | ||
| 85 | my ( $a, $b ) = @_; | ||
| 86 | return abs( $a - $b ) < $FLOAT_EPS; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub cursor_type_at_point { | ||
| 90 | my ( $self, $x, $y ) = @_; | ||
| 91 | ( $x, $y ) = $self->view->to_image_coords( $x, $y ); | ||
| 92 | my $pixbuf_size = $self->view->get_pixbuf_size; | ||
| 93 | if ( $x > 0 | ||
| 94 | and $x < $pixbuf_size->{width} | ||
| 95 | and $y > 0 | ||
| 96 | and $y < $pixbuf_size->{height} ) | ||
| 97 | { | ||
| 98 | if ( $self->{dragging} ) { | ||
| 99 | return 'grabbing'; | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | return 'grab'; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | return; | ||
| 106 | } | ||
| 107 | |||
| 108 | 1; |