Re: How do I get absolute screen position of a widget?



Jörn Reder <joern zyn de> writes:

  my ($x, $y, $width, $height) = $gtk_entry->get_parent_window->get_geometry;

$window->get_origin gives a position in root window coordinates, once
you get $widget->window.  If the widget is no-window you add the
allocation x,y position.  But perhaps there's an easier way.

A harder way I tried recently was adding up the window position of each
successive parent.  It seems to come out right, and avoids a round-trip
to the server.  (warp_pointer takes root window coords, the same as I
think menu popup needs, but I had widget coords ...)



# Warp the mouse pointer to ($x,$y) in $widget coordinates.
#
sub _widget_warp_pointer {
  my ($widget, $x, $y) = @_;
  my $window = $widget->window
    or croak "Cannot warp mouse pointer on unrealized $widget";

  if ($widget->flags & 'no-window') {
    my $alloc = $widget->allocation;
    $x += $alloc->x;
    $y += $alloc->y;
  }
  my ($wx, $wy) = _window_get_root_position ($window);
  $window->get_display->warp_pointer ($window->get_screen, $x+$wx, $y+$wy);
}

# Return two values ($x,$y) which is the position of $window in root window
# coordinates.  This uses gdk_window_get_position() and so takes the most
# recent configure notify positions, as opposed to $window->get_origin which
# gets the latest with an X server round-trip (XTranslateCoordinates()).
#
# The loop here is similar to what gtk_widget_translate_coordinates() does
# chasing up through window ancestors.
#
sub _window_get_root_position {
  my ($window) = @_;
  my $x = 0;
  my $y = 0;
  while ($window->get_window_type ne 'root') {
    my ($parent_x, $parent_y) = $window->get_position;
    $x += $parent_x;
    $y += $parent_y;
    $window = $window->get_parent
      or croak '_window_get_root_position(): oops, didn\'t reach root window';
  }
  return ($x, $y);
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]