Re: How to get Widgets absolute X/Y co-ordinates.



Tapan Satpathy Ansuman-A19108 wrote:

Hi,
I am a newbie in GTK. I am trying to write a simple application.

I want to know if there is anyway how to get the absolute co-ordinates/geometry of any GtkWidget.
Hello,
First of all, about what coordinates are you talking ? Screen coordinates or window relative ?

Window relative case is simple - just use GtkWidget::allocation field.

Absolute screen coordinates is much harder. Here's what we have:
1) Widget's real size and position inside the *parent window* stored in GtkWidget::allocation field. 2) gtk_widget_get_parent_window() function can give us a pointer to the parent window. 3) Parent window coordinates in *root window* (screen coordinates) accessible via gdk_window_get_root_origin(). Looks simple ? Yep, but there's a window manager application that runs somewhere on your box and decorates your windows with nice titlebars and frames. Your widgets will be packed in decorated window with some offset, known by window manager.
So also we have:
4) gdk_window_get_frame_extents() function can talk to window manager and ask it about decorated window coordinates. 5) We can use gdk_drawable_get_size() to ask gdk layer about window size without decorations.

After that we know that widget's screen coordinates are somewhere inside the extents rectangle. Assuming that window decoration left and right borders are equal we can get *x* coordinate, assuming that bottom border is equal to left and right borders we can get *y* coordinate. Two assumption ;) try this with different themes and see what happened.

http://developer.gnome.org/doc/API/2.0/gtk/GtkWidget.html#GtkAllocation
http://developer.gnome.org/doc/API/2.0/gtk/GtkWidget.html#gtk-widget-get-parent-window
http://developer.gnome.org/doc/API/2.0/gdk/gdk-Windows.html#gdk-window-get-root-origin
http://developer.gnome.org/doc/API/2.0/gdk/gdk-Windows.html#gdk-window-get-deskrelative-origin
http://developer.gnome.org/doc/API/2.0/gdk/gdk-Drawing-Primitives.html#gdk-drawable-get-size

Sample code:
static void
widget_get_rect_in_screen (GtkWidget *widget, GdkRectangle *r)
{
       gint                      x,y,w,h;
       GdkRectangle    extents;
       GdkWindow       *window;
window = gtk_widget_get_parent_window(widget); /* getting parent window */ gdk_window_get_root_origin(window, &x,&y); /* parent's left-top screen coordinates */ gdk_drawable_get_size(window, &w,&h); /* parent's width and height */ gdk_window_get_frame_extents(window, &extents); /* parent's extents (including decorations) */ r->x = x + (extents.width-w)/2 + widget->allocation.x; /* calculating x (assuming: left border size == right border size) */ r->y = y + (extents.height-h)-(extents.width-w)/2 + widget->allocation.y; /* calculating y (assuming: left border size == right border size == bottom border size) */
       r->width = widget->allocation.width;
       r->height = widget->allocation.height;
}

PS: perhaps someone know how to obtain the border sizes and title size from specific window manager. Try to look at:
http://www.freedesktop.org/Standards/wm-spec

   Olexiy




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