Re: problem finding the position of a button in the gtk dialog ( sample code inside )

2008-05-15 Thread Federico Mena Quintero
On Wed, 2008-05-07 at 11:33 +0530, chirag juneja wrote:

 approach i am using is :
 i will find the distance of the button from its parent, and then its
 parent's distance form its parent .. and so on, till i get toplevel
 window.
 then i am adding the decoration's height, border's width and distance
 of top level window from origin of root window(0,0).

That's a lot of overkill :)  You can simply do

void 
get_absolute_position (GtkWidget *widget, int *ret_x, int *ret_y)
{
  GdkWindow *parent;
  int parent_x, parent_y;

  parent = gtk_widget_get_parent_window (widget);
  gdk_window_get_origin (parent, parent_x, parent_y);

  *ret_x = parent_x + widget-allocation.x;
  *ret_y = parent_y + widget-allocation.y;
}

  Federico


___
gtk-devel-list mailing list
gtk-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-devel-list


problem finding the position of a button in the gtk dialog ( sample code inside )

2008-05-09 Thread chirag juneja
I am trying to find the position of a button in a gtk dialog.
but getting some strange results..

approach i am using is :
i will find the distance of the button from its parent, and then its
parent's distance form its parent .. and so on, till i get toplevel window.
then i am adding the decoration's height, border's width and distance of top
level window from origin of root window(0,0).

i am getting same (x,y) pair for the each level of iteration :(.

sample code i wrote is attached with this mail...
It will be great if someone can point out the mistake or can tell the
correct code for this.

code is :

/*
  call as :gcc `pkg-config --cflags --libs gtk+-2.0`
codename.c


./a.out
  then press button1 in the
dialog.
 */

#include gtk/gtk.h

gboolean GtkWidgetGetPos(GtkWidget *widget, int *x, int *y)
{
  printf( entered GtkWidgetGetPos\n);
  if(!widget)
return FALSE; //return
false;
  if(GTK_IS_WINDOW(widget))
{
  int win_x, win_y;
  gtk_window_get_position(GTK_WINDOW(widget), win_x, win_y);

  if(x)
*x = win_x;
  if(y)
*y = win_y;

  printf(1 widget : %p , x = %d , y = %d\n,widget,win_x,win_y);
  return TRUE ; // return
true;
}
  else if(GTK_WIDGET_REALIZED(widget))
{
  int win_x, win_y;
  gdk_window_get_position(widget-window, win_x, win_y);

  if(x)
*x = win_x;
  if(y)
*y = win_y;

  printf(2 widget : %p , x = %d , y = %d\n,widget,win_x,win_y);
  return TRUE; //return
true;
}
  else
{
  if(x)
*x = 0;
  if(y)
*y = 0;
  printf(widget : %p , x = %d , y = %d\n,widget,*x,*y);
  return FALSE ; //return
false;
}
}

void GtkWidgetGetAbsolutePos(GtkWidget *widget, int *x, int *y)
{
  printf( entered GtkWidgetGetAbsolutePos\n);
  int myX = 0, myY = 0;
  int tempX, tempY;

  //find position of widget w.r.t. toplevel
window
while(widget  !GTK_WIDGET_TOPLEVEL(widget))
{
  tempX = 0;
  tempY = 0;
  GtkWidgetGetPos(widget, tempX, tempY);
  printf( left GtkWidgetGetPos\n);
  myX += tempX;
  myY += tempY;

  widget = widget-parent;
}

  if (widget)
{
  tempX = 0;
  tempY = 0;
  GtkWidgetGetPos(widget, tempX, tempY);
  printf( left GtkWidgetGetPos\n);
  myX += tempX;
  myY += tempY;

  if(GTK_WIDGET_MAPPED(widget))
{
  gint win_x, win_y, win_width, win_height;
  gdk_window_get_geometry(widget-window, win_x, win_y,
win_width, win_height, NULL);

  GdkRectangle win_rect;
  gdk_window_get_frame_extents(widget-window, win_rect);

  int border_width = (win_rect.width - win_width) / 2;
  int title_width = (win_rect.height - win_height) - border_width;

  myX += border_width;
  myY += title_width;
}
}

  if (x)
*x = myX;
  if (y)
*y = myY;
  printf( leaving GtkWidgetGetAbsolutePos\n);
}




void iterate_container(GtkWidget *container)
{
  printf( entered iterate_container\n);
  GtkWidget *retVal = NULL;
  GList *children = gtk_container_get_children(GTK_CONTAINER(container));
  GList *current_child = NULL;

  for(current_child = children; ((retVal == NULL)  (current_child !=
NULL)); current_child = current_child-next)
{
  gtk_widget_realize(GTK_WIDGET(current_child-data));
  if(GTK_IS_BUTTON(current_child-data))
{
  printf(inside button\n);
  char* lbl
=(char*)gtk_button_get_label(GTK_BUTTON(current_child-data));
  if(lbl)
{
  int x = 0,y = 0;
  printf(calling GtkWidgetGetAbsolutePos \n);

GtkWidgetGetAbsolutePos(GTK_WIDGET(current_child-data),x,y);
  //
gdk_window_get_position(GTK_WIDGET(current_child-data)-window,x,y);


  printf(x = %d , y = %d\n,x,y);

}
  printf(leaving button\n);
}
  if (GTK_IS_CONTAINER(current_child-data))
iterate_container(GTK_WIDGET(current_child-data));
}

  g_list_free(children);

  printf( leaving iterate_container\n);
  return
;//retVal;


}



/* This is a callback function. The data arguments are
ignored

 * in this example. More on callbacks below. */
static void hello( GtkWidget *widget,
   gpointer   data )
{
  g_print (Hello World\n);
  iterate_container(GTK_WIDGET(data));
}

static gboolean delete_event( GtkWidget *widget,
  GdkEvent  *event,
  gpointer   data )
{
  g_print (delete event occurred\n);
  return FALSE; // return false; //  this will call destroy
fn...
}

static void destroy( GtkWidget *widget,
 gpointer   data )
{
  g_print(inside... destroy);
  gtk_main_quit ();
}

int main( int   argc,
  char *argv[] )
{

  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *box1;
  GtkWidget *label;

  gtk_init (argc, argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), Hello 

problem finding the position of a button in the gtk dialog ( sample code inside )

2008-05-07 Thread chirag juneja
I am trying to find the position of a button in a gtk dialog.
but getting some strange results..

approach i am using is :
i will find the distance of the button from its parent, and then its
parent's distance form its parent .. and so on, till i get toplevel window.
then i am adding the decoration's height, border's width and distance of top
level window from origin of root window(0,0).

i am getting same (x,y) pair for the each level of iteration :(.

sample code i wrote is attached with this mail...

It will be great if someone can point out the mistake or can tell the
correct code for this.


code is :

/*

  call as :gcc `pkg-config --cflags --libs gtk+-2.0`
codename.c


./a.out

  then press button1 in the
dialog.

 */

#include gtk/gtk.h

gboolean GtkWidgetGetPos(GtkWidget *widget, int *x, int *y)
{
  printf( entered GtkWidgetGetPos\n);
  if(!widget)
return FALSE; //return
false;


  if(GTK_IS_WINDOW(widget))
{
  int win_x, win_y;
  gtk_window_get_position(GTK_WINDOW(widget), win_x, win_y);

  if(x)
*x = win_x;
  if(y)
*y = win_y;

  printf(1 widget : %p , x = %d , y = %d\n,widget,win_x,win_y);
  return TRUE ; // return
true;

}
  else if(GTK_WIDGET_REALIZED(widget))
{
  int win_x, win_y;
  gdk_window_get_position(widget-window, win_x, win_y);

  if(x)
*x = win_x;
  if(y)
*y = win_y;

  printf(2 widget : %p , x = %d , y = %d\n,widget,win_x,win_y);
  return TRUE; //return
true;

}
  else
{
  if(x)
*x = 0;
  if(y)
*y = 0;
  printf(widget : %p , x = %d , y = %d\n,widget,*x,*y);
  return FALSE ; //return
false;

}
}

void GtkWidgetGetAbsolutePos(GtkWidget *widget, int *x, int *y)
{
  printf( entered GtkWidgetGetAbsolutePos\n);
  int myX = 0, myY = 0;
  int tempX, tempY;

  //find position of widget w.r.t. toplevel
window

  while(widget  !GTK_WIDGET_TOPLEVEL(widget))
{
  tempX = 0;
  tempY = 0;
  GtkWidgetGetPos(widget, tempX, tempY);
  printf( left GtkWidgetGetPos\n);
  myX += tempX;
  myY += tempY;

  widget = widget-parent;
}

  if (widget)
{
  tempX = 0;
  tempY = 0;
  GtkWidgetGetPos(widget, tempX, tempY);
  printf( left GtkWidgetGetPos\n);
  myX += tempX;
  myY += tempY;

  if(GTK_WIDGET_MAPPED(widget))
{
  gint win_x, win_y, win_width, win_height;
  gdk_window_get_geometry(widget-window, win_x, win_y,
win_width, win_height, NULL);

  GdkRectangle win_rect;
  gdk_window_get_frame_extents(widget-window, win_rect);

  //We assume that the bottom border is equivalent to the side
borders.

  int border_width = (win_rect.width - win_width) / 2;
  int title_width = (win_rect.height - win_height) - border_width;

  myX += border_width;
  myY += title_width;
}
}

  if (x)
*x = myX;
  if (y)
*y = myY;
  printf( leaving GtkWidgetGetAbsolutePos\n);
}




void iterate_container(GtkWidget *container)
{
  printf( entered iterate_container\n);
  GtkWidget *retVal = NULL;
  GList *children = gtk_container_get_children(GTK_CONTAINER(container));
  GList *current_child = NULL;

  for(current_child = children; ((retVal == NULL)  (current_child !=
NULL)); current_child = current_child-next)
{
  gtk_widget_realize(GTK_WIDGET(current_child-data));
  if(GTK_IS_BUTTON(current_child-data))
{
  printf(inside button\n);
  char* lbl
=(char*)gtk_button_get_label(GTK_BUTTON(current_child-data));
  if(lbl)
{
  int x = 0,y = 0;
  printf(calling GtkWidgetGetAbsolutePos \n);

GtkWidgetGetAbsolutePos(GTK_WIDGET(current_child-data),x,y);
  //
gdk_window_get_position(GTK_WIDGET(current_child-data)-window,x,y);


  printf(x = %d , y = %d\n,x,y);

}
  printf(leaving button\n);
}
  if (GTK_IS_CONTAINER(current_child-data))
iterate_container(GTK_WIDGET(current_child-data));
}

  g_list_free(children);

  printf( leaving iterate_container\n);
  return
;//retVal;


}



/* This is a callback function. The data arguments are
ignored

 * in this example. More on callbacks below. */
static void hello( GtkWidget *widget,
   gpointer   data )
{
  g_print (Hello World\n);
  iterate_container(GTK_WIDGET(data));
}

static gboolean delete_event( GtkWidget *widget,
  GdkEvent  *event,
  gpointer   data )
{
  g_print (delete event occurred\n);

  //return TRUE; // return
true;

  return FALSE; // return false; //  this will call destroy
fn...

}

static void destroy( GtkWidget *widget,
 gpointer   data )
{
  g_print(inside... destroy);
  gtk_main_quit ();
}

int main( int   argc,
  char *argv[] )
{

  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *box1;
  GtkWidget *label;