GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Apr 30, 2016 4:56 pm

All times are UTC

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Remove GTK+ container children, repopulate it, then refresh
PostPosted: Wed Nov 28, 2012 10:43 pm 
Familiar Face

Joined: Thu Nov 22, 2012 3:46 pm
Posts: 29
I'm into a problem with a GTK+ C application. I have a container that, contains a item text. During the running an user interation must cause this widget to contain more or less of them.
I need to write a function that removes all the "old" inner item, less last four witch is application command, then adds all the ones from a list and finally refresh the view.

I have create a panel menu which display a menu like in screenshot. First part of the menu, to the first line separator, displays the names of TV channels whose number is updated. After first separator (in screensot after "WHITE", I have same control of application that I would like to be fixed in menu.

I want to remove all item least last 6.

void create_panel_menu(GtkWidget *app) {
   GList *node = settings.channels;
   int i;
   panel_menu = gtk_menu_new();

   for (i = 0; node; i++, node = node->next)
      channel *ch = (channel*)node->data;
      GtkWidget *menuitem = gtk_menu_item_new_with_label(ch->name);
      gtk_menu_shell_insert(GTK_MENU_SHELL(panel_menu), menuitem, i);      
      g_signal_connect(G_OBJECT(menuitem), "activate", (GCallback)channel_menuitem_activate_cb, GINT_TO_POINTER (i));
   gtk_menu_shell_append(GTK_MENU_SHELL(panel_menu), gtk_separator_menu_item_new());

   mute_menuitem = gtk_check_menu_item_new_with_label(_("Muted"));
   gtk_menu_shell_append(GTK_MENU_SHELL(panel_menu), mute_menuitem);

   record_menuitem = gtk_image_menu_item_new_from_stock(GTK_STOCK_MEDIA_RECORD, NULL);
   gtk_menu_shell_append(GTK_MENU_SHELL(panel_menu), record_menuitem);

   gtk_menu_shell_append(GTK_MENU_SHELL(panel_menu), gtk_separator_menu_item_new());
   showwindow_menuitem = gtk_check_menu_item_new_with_label(_("Show Window"));
   gtk_menu_shell_append(GTK_MENU_SHELL(panel_menu), showwindow_menuitem);

   quit_menuitem = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
   gtk_menu_shell_append(GTK_MENU_SHELL(panel_menu), quit_menuitem);


GList* menuitems = gtk_container_get_children(GTK_CONTAINER(panel_menu));

The first entry is channel name witch is added by the users in list of presets and they appear here in panel menu even if they were deleted afterwards in the list store. I want to remove them when list store is empty.


x.png [ 6.85 KiB | Viewed 773 times ]
File comment: SCREENSHOT
x.png [ 12.95 KiB | Viewed 775 times ]
 Post subject: Re: Remove GTK+ container children, repopulate it, then refr
PostPosted: Thu Nov 29, 2012 8:48 am 
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
First a few coding pointers.
#1. Use gtk_menu_shell_prepend rather than insert. It's much more efficient as you are only adding to the beginning each time (do it in reverse order if you care about the ordering).
#2. I think you would be better using gtk_radio_menu_item for the channels, though this is done with the presumption that you are only meant to select one channel at a time.
Even if you don't use the radio_menu_item it would be good to set up a List (GList* or GSList*) and then add or free widgets to that list whenever your channels change. If your change is always to delete the whole list than a GArray will be more efficient for storage. Personally I'd put all your channels in a submenu. Destroying the submenu will then destroy all the contents (If you have RadioMenuItems you will still have to take care of freeing the GList* though).

Here is an example of how to populate such a submenu (I'm labelling them 1,2,...,k):
gchar s[4];
GSList* group;
GtkWidget *mni, *submenu;
for (j=1;j<=k;j++)
  g_snprintf(s, 3, "%d", j);
  mni=gtk_radio_menu_item_new_with_label(group, s);
  g_signal_connect(G_OBJECT(mni), "toggled", G_CALLBACK(upg), NULL);
  gtk_menu_shell_append(GTK_MENU_SHELL(submenu), mni);

here is how I determine which value is selected in the callback function:
while (list)
  if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(list->data))) break;

To destroy the contents of your submenu do: g_slist_free_full(group, gtk_widget_destroy);

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 3 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group