Re: GtkBuilder and widget names



Well, right now I am using these names to identify visual objects in a unified way. For example, I have a set of check-boxes which are part of my "Options" dialog. Obviously, I need to remember those settings between each program run. So instead of dealing with each and every check-box individually, and writes dozens of callbacks, I have a code like this:


extern GtkBuilder *builder;
extern GKeyFile *settings;    // myapp.ini
extern gboolean flag1, flag2; // some global options

struct Widgets_Flags {
	gchar *name; gboolean *flag;
} flags[] = { {"chk1", &flag1}, {"chk2", &flag2}};

for(i=0; i<sizeof(flags)/sizeof(flags[0]); i++) {
  GtkWidget *w = GTK_WIDGET(
		gtk_builder_get_object(builder, flags[i].name)
				);
  if(!w)
	g_critical("Panic! UI does not have object %s!", flags[i].name);

  gboolean check = g_key_file_get_boolean(settings, "main",
		//gtk_widget_get_name(w));
		// now it is replaced with
		gtk_buildable_get_name(GTK_BUILDABLE(w)));

  g_signal_connect(G_OBJECT(w), "toggled",
		G_CALLBACK(toggled_option_checkbox), flags[i].flag);
}

// And a similar code for the callback itself:
void toggled_option_checkbox(GtkCheckMenuItem *checkMenuItem,
				 gpointer *data) {
  gboolean *flag = (gboolean *) data;
  *flag = gtk_check_menu_item_get_active(checkMenuItem);
  g_key_file_set_boolean(settings, "main",
	gtk_buildable_get_name(GTK_BUILDABLE(checkMenuItem)),
	*flag);
}

And that is the whole code to deal with boolean flags in the "Option Dialog".

The point is, now I do not need to keep a dictionary to translate between the widget's ID as it is defined in the Glade editor and the text name for the same option as it would be stored in the settings file. The transition between widget, variable in memory, and serialized variable become much easier.

On 12/23/2011 1:44 AM, Tristan Van Berkom wrote:
Yes it's intended.

There is a way to access the name of the widget key in the builder file,
however I've always been a little ticked about that.

Can you name me one use case of how that would be useful ?

I think gtk_buildable_get_name() will return what you want, but
again... is there any reason why GTK+ should have that api ?

Cheers,
              -Tristan

On Fri, Dec 23, 2011 at 7:46 AM, George Brink<siberianowl yahoo com>  wrote:
I draw an UI in Glade, loaded it in with the GtkBuilder object.
Now I am accessing one widget in this UI:
---
GtkWidget *w = gtk_builder_get_object(builder, "obj1");
g_message(gtk_widget_get_name(w));
----
This prints GtkLabel, instead of obj1. Why? Is it intended behavior?
How can I read "obj1" from the w?

_______________________________________________
gtk-list mailing list
gtk-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-list




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