This time I was looking for a way to get the rectangle of the widget and I found an old article with this C code:
[cc lang="C"]
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;
}
[/cc]
I translated it to C#
[cc lang="C#"]
private Gdk.Rectangle getRectInScreen(Gtk.Widget widget)
{
int x, y, w, h;
Gdk.Rectangle extents = new Gdk.Rectangle();
Gdk.Window window;
Gdk.Rectangle r;
window = widget.ParentWindow;
window.GetRootOrigin(out x, out y);
window.GetSize(out w, out h);
extents = window.FrameExtents;
r.X = x + (extents.Width – w) / 2 + widget.Allocation.X;
r.Y = y + (extents.Height – h) – (extents.Width – w) / 2 + widget.Allocation.Y;
r.Width = widget.Allocation.Width;
r.Height = widget.Allocation.Height;
return r;
}
[/cc]