problem finding the position of a button in the gtk dialog
- From: "chirag juneja" <chiragjuneja gmail com>
- To: gtk-devel-list gnome org, gtk-app-devel-list-request gnome org
- Subject: problem finding the position of a button in the gtk dialog
- Date: Tue, 6 May 2008 18:58:45 +0530
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.
--
********************************************************************************
* LiFe iS BeaUTiFuL, LiVE iT To THe FuLLeST ----- CHiRaG JuNeJa *
********************************************************************************
/*
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;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Hello Buttons!");
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (delete_event), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 200);
box1 = gtk_vbox_new (FALSE , 10);
gtk_container_add (GTK_CONTAINER (window), box1);
GtkWidget *hscale;
hscale = gtk_hscale_new_with_range( 0.0, 1300.0 , 50.0);
gtk_scale_set_value_pos(GTK_SCALE(hscale),GTK_POS_RIGHT);
gtk_box_pack_start (GTK_BOX(box1), hscale, TRUE,TRUE, 0);
gtk_widget_show (hscale);
GtkWidget *scrollbar;
GtkWidget *seperator;
GtkObject *adj1;
adj1 = gtk_adjustment_new (0.0, 0.0, 201.0, 0.1, 1.0, 1.0);
scrollbar = gtk_hscrollbar_new (GTK_ADJUSTMENT(adj1));
gtk_box_pack_start (GTK_BOX (box1), scrollbar, TRUE, TRUE, 0);
gtk_widget_show (scrollbar);
GtkWidget *frame;
frame = gtk_frame_new ("Normal Label");
label = gtk_label_new ("This is a Normal label");
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_widget_show(label);
gtk_box_pack_start (GTK_BOX (box1), frame, FALSE, FALSE, 0);
gtk_widget_show(frame);
label = gtk_label_new("LABEL ");
gtk_misc_set_alignment (GTK_MISC(label),0,0);
gtk_box_pack_start (GTK_BOX(box1), label, TRUE,TRUE, 0);
gtk_widget_show (label);
GtkWidget *checkButton;
checkButton = gtk_check_button_new_with_label("check button");
gtk_box_pack_start (GTK_BOX(box1), checkButton, TRUE,TRUE, 0);
gtk_widget_show (checkButton);
GtkWidget *cal;
cal = gtk_calendar_new();
gtk_box_pack_start (GTK_BOX(box1), cal, TRUE,TRUE, 0);
gtk_widget_show (cal);
GtkTooltips *tooltips;
tooltips = gtk_tooltips_new ();
button = gtk_button_new_with_label ("Button 1");
gtk_tooltips_set_tip (tooltips, button, "This is button 1", NULL);
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (hello), (void*) window);
gtk_box_pack_start (GTK_BOX(box1), button, TRUE, TRUE, 0);
gtk_widget_show (button);
gtk_widget_show (box1);
gtk_widget_show (window);
gtk_main ();
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]