Re: [gtk-list] Getting the label from gtk_radio_menu_item_new_with_label()



On Fri, Apr 14, 2000 at 12:10:26AM -0400, Havoc Pennington wrote:
> 
> Andy Kahn <ackahn@netapp.com> writes: 
> > It suggests there's something happening during the "activate" callback
> > such that the menu item's child is moving elsewhere.  Where could it
> > be going?  Has it been moved (reparented?) within the option menu
> > heirarchy?
> 
> That sounds pretty darn unlikely - but anything can happen.
> 
> Can you give a small compilable program that demonstrates this?
> 

Yup.  Attached below.

I did a little bit of investigation and modified the program from the
code snippet I previously posted.  Instead of having an assert on
GTK_BIN(mitem)->child, I went ahead and simply printed the value of
the pointer.

When you run the code and change the menu item selection, you'll see
output like:

	option_menu_cb() entered (cb is 'one')
	child is 0x80748b8
	option_menu_cb() entered (cb is 'two')
	child is 0x0

Apparently, the "activate" signal for a menu item is triggered twice,
once for the currently selected item, and then once for the newly
selected item.

I'm only interested in what the label text is after the new selection,
but unfortunately, that menu item's child widget is NULL.  (Hence the
reason to my original email.)

I'm sure there's a reason why this is happening, but whatever it is,
I find the current behavior a little non-intuitive and a little
inconsistent.  Perhaps there's a different signal that I should be
using?

One solution would be to pass the label widget as part of the callback
data, but I'm really curious as to why what I'm doing now isn't what
I'm expecting.

Let me know if you have any ideas.

Thanks!
--andy

#include <gtk/gtk.h>

static void
option_menu_cb(GtkWidget * mitem, gpointer cbdata)
{
	GtkWidget *child;
	GList *children;

	g_assert(GTK_RADIO_MENU_ITEM(mitem));

	g_print("option_menu_cb() entered (cb is '%s')\n", (char *)cbdata);

	child = GTK_BIN(mitem)->child;
	g_print("child is 0x%x\n", child ? child : NULL);
}

int
main(int argc, char *argv[])
{
	GtkWidget *toplev, *omenu, *menu, *mitem;
	GSList *group;
	int i;
	char *texts[] = { "one", "two", "three" };

	gtk_init(&argc, &argv);

	toplev = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	omenu = gtk_option_menu_new();
	gtk_container_add(GTK_CONTAINER(toplev), omenu);
	menu = gtk_menu_new();

	group = NULL;
	for (i = 0; i < 3; i++) {
		mitem = gtk_radio_menu_item_new_with_label(group, texts[i]);
		gtk_signal_connect(GTK_OBJECT(mitem), "activate",
				   option_menu_cb, texts[i]);
		group = gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(mitem));
		gtk_menu_append(GTK_MENU(menu), mitem);
		gtk_widget_show(mitem);
	}

	gtk_option_menu_set_menu(GTK_OPTION_MENU(omenu), menu);
	gtk_option_menu_set_history(GTK_OPTION_MENU(omenu), 3);

	gtk_widget_show_all(toplev);
	gtk_main();
	return 0;
}


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