Re: how to access a widget ?





Tiago Cogumbreiro wrote:
On Sun, 2004-08-29 at 22:54, edward hage wrote:
I have a call-back function toggle_location_event which can change the text of the button which is pressed. Now I also want to change the text of a button that is not pressed ( in toggle_location_event). I know the location i and j of that button, but I don't know how to change the text because I don't have the object-name myself (because I use button = gtk_button_new () lots of times to create more widgets which don't have unique names).

How can I retrieve the widget from the known location so I can change the text on the button ?


You can cache a matrix with your buttons and the last changed buttons in
a structure[1] and then send this structure[2] as user data (in the
callback).

...........

Thank you for the advice,
Got that, I made the following structures. Looks much like you proposed, only use GArray to fill it with buttons and location (i,j) info.

struct _buttonreeks
{
  GArray *buttonids;
  guint i;
  guint j;
};

struct table_description
{
  GtkButton *buttons;
  guint i;
  guint j;
};


..... now declaring in function:

struct _buttonreeks buttonreeks;
GtkWidget *button;
struct table_description foo;
GArray *buts;

   buts = g_array_sized_new (FALSE, FALSE, sizeof (struct table_description), 1);

   for (i=0; i < vertical - 1; i++)
   {

 ............ etc. etc.............
      for (j=1; j < vertical; j++)
      {
        if (i < j)
        {
	  button = gtk_button_new ();
          gtk_button_set_label ((GtkButton *) button , " ");
          gtk_table_attach_defaults (GTK_TABLE (table), button , 1 + j, 2 + j, 1 + i, 2 + i);
	  foo.buttons = (GtkButton *) button;
          foo.i = i;
	  foo.j = j;
          g_array_append_vals (buts, &foo, 1);
	}
      }
    }


    buttonreeks.buttonids = buts;
    for (i=0; buts->len; i++)
    {
     buttonreeks.i        = g_array_index(buts, struct table_description, i).i;
     buttonreeks.j        = g_array_index(buts, struct table_description, i).j;
     button = (GtkWidget *) g_array_index(buts, struct table_description, i).buttons;

g_signal_connect (GTK_OBJECT(button), "clicked", (GCallback) toggle_location_event, &buttonreeks);
    }

}

I am doing the g_signal_connects in a separate iteration because when I do this in the i-j-cycle then the buts-array is not completed yet.

> So your problem resolves in two issues: a) you need a central structure
> for holding your data (maybe a GObject?); b) you may need to create
> auxiliar structures for sending multiple arguments trough callbacks.
>
Point b) is in this way still not solved.
I get the following error-message when I run the program (compiling and linking all okay!)

edward linux:~/Projects/modeller> ./modeller9

(modeller9:8722): GLib-GObject-WARNING **: invalid (NULL) pointer instance

(modeller9:8722): GLib-GObject-CRITICAL **: file gsignal.c: line 1861 (g_signal_connect_data): assertion `G_TYPE_CHECK_INSTANCE (instance)' failed
Segmentatie fout
edward linux:~/Projects/modeller>

How can I solve this problem ?





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