Re: How to copy data between widgets?



On 8/8/06, Michael L Torrie <torriem chem byu edu> wrote:
On Tue, 2006-08-08 at 11:48 -0300, RomÃn Gorojovsky wrote:
> As I said before, It's a problem with my C, not my gtk.  A quite
> embarrassing mistake, sorry for wasting your time.

Another way to do it is to assign each widget a string name using
g_object_set_data.  Once this is set, then in your callback you can use
the emitting-object's GtkWidget pointer to lookup the parent widget and
then search down the widget hiearchy for the named label widget.

Ah! this is another thing that I wanted to know, how to follow the
widget hierarchy.  The example is good enough, thanks.


Glade-generated code (which is of course deprecated but demonstrates
this solution) does this:

#define GLADE_HOOKUP_OBJECT(component,widget,name) \
  g_object_set_data_full (G_OBJECT (component), name, \
    gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)

#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
  g_object_set_data (G_OBJECT (component), name, widget)



GtkWidget*
lookup_widget                          (GtkWidget       *widget,
                                        const gchar     *widget_name)
{
  GtkWidget *parent, *found_widget;

  for (;;)
    {
      if (GTK_IS_MENU (widget))
        parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
      else
        parent = widget->parent;
      if (!parent)
        parent = (GtkWidget*) g_object_get_data (G_OBJECT (widget),
"GladeParentKey");
      if (parent == NULL)
        break;
      widget = parent;
    }

  found_widget = (GtkWidget*) g_object_get_data (G_OBJECT (widget),
                                                 widget_name);
  if (!found_widget)
    g_warning ("Widget not found: %s", widget_name);
  return found_widget;
}

GtkWidget*
create_window1 (void)
{
  GtkWidget *window1;
  GtkWidget *vbox1;
  GtkWidget *label;
  GtkWidget *entry;
  GtkWidget *button;

  window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window1), _("window1"));

  vbox1 = gtk_vbox_new (FALSE, 0);
  gtk_widget_show (vbox1);
  gtk_container_add (GTK_CONTAINER (window1), vbox1);

  label = gtk_label_new (_("label1"));
  gtk_widget_show (label);
  gtk_box_pack_start (GTK_BOX (vbox1), label, FALSE, FALSE, 0);

  entry = gtk_entry_new ();
  gtk_widget_show (entry);
  gtk_box_pack_start (GTK_BOX (vbox1), entry, FALSE, FALSE, 0);

  button = gtk_button_new_with_mnemonic (_("button1"));
  gtk_widget_show (button);
  gtk_box_pack_start (GTK_BOX (vbox1), button, FALSE, FALSE, 0);

  /* Store pointers to all widgets, for use by lookup_widget(). */
  GLADE_HOOKUP_OBJECT_NO_REF (window1, window1, "window1");
  GLADE_HOOKUP_OBJECT (window1, vbox1, "vbox1");
  GLADE_HOOKUP_OBJECT (window1, label, "label");
  GLADE_HOOKUP_OBJECT (window1, entry, "entry");
  GLADE_HOOKUP_OBJECT (window1, button, "button");

  return window1;
}


This is somewhat arbitrary, and you could use your own method for doing
the names and searching.  I've found this type of thing to work very
well.

Michael


>
> Thanks a lot again.
>
> > Cheers,
> >                     -Tristan
> >
>
>




--
RomÃn


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