Re: gtk_toggle_button_set_active()



Hi Richard,

On 7/24/05, riboaz xs4all nl <riboaz xs4all nl> wrote:
> Yesterday I wrote about how I was suspecting problems related to hiding
> and unhiding widgets and trying to use gtk_toggle_button_set_active().

I had some time free so I wrote you a tiny test program. I think it
does roughly what you wanted (I hope I've not misunderstood!), and it
seems to work.

It displays a window containing a couple of notebooks. The notebooks
have a number of pages, and each page has a set of radio buttons. If
you click on a radio button, it also toggles (though without emitting
any signals) the radio buttons on the corresponding pages on all the
other notebooks.

John
#include <gtk/gtk.h>

#define NUM_BUTTONS (10)
#define NUM_PAGES (10)
#define NUM_BOOKS (2)

typedef struct _Button Button;
typedef struct _Book Book;
typedef struct _Page Page;
typedef struct _Application Application;

struct _Button
{
  Page *page;

  GtkWidget *radio;
  int n;			/* This is (eg.) button 2 on the page */
};

struct _Page
{
  Book *book;

  Button button[NUM_BUTTONS];
  int n;			/* This is (eg.) page 2 */
};

struct _Book
{
  Application *app;

  Page page[NUM_PAGES];
  int n;			/* This is (eg.) book 4 */
};

struct _Application
{
  Book book[NUM_BOOKS];
};

void
toggled_cb (GtkWidget * widget, Button * button)
{
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
    {
      Page *page = button->page;
      Book *book = page->book;
      Application *app = book->app;
      int i;

      printf ("button %d on page %d on book %d turns on\n",
	      button->n, page->n, book->n);

      /* Now set the radios on this page on all books to the same
       * state.
       */
      for (i = 0; i < NUM_BOOKS; i++)
	{
	  Button *target_button =
	    &app->book[i].page[page->n].button[button->n];

	  g_signal_handlers_block_by_func (target_button->radio,
					   toggled_cb, target_button);
	  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
					(target_button->radio), TRUE);
	  g_signal_handlers_unblock_by_func (target_button->radio, toggled_cb,
					     target_button);
	}
    }
}

GtkWidget *
make_book (Book * book)
{
  GtkWidget *notebook;
  int i;

  notebook = gtk_notebook_new ();

  for (i = 0; i < NUM_PAGES; i++)
    {
      Page *page = &book->page[i];

      char buf[256];
      GtkWidget *label;
      GtkWidget *vbox;
      GtkWidget *radio;
      int j;

      snprintf (buf, 256, "page %d", i);
      label = gtk_label_new (buf);

      page->book = book;
      page->n = i;
      vbox = gtk_vbox_new (FALSE, 2);
      for (radio = NULL, j = 0; j < NUM_BUTTONS; j++)
	{
	  Button *button = &page->button[j];

	  button->page = page;
	  button->n = j;
	  snprintf (buf, 256, "button %d on page %d", j, i);
	  radio =
	    gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON
							 (radio), buf);
	  button->radio = radio;
	  g_signal_connect (radio, "toggled",
			    G_CALLBACK (toggled_cb), button);
	  gtk_box_pack_end_defaults (GTK_BOX (vbox), radio);
	}

      gtk_notebook_append_page (GTK_NOTEBOOK (notebook), vbox, label);
    }

  return (notebook);
}

int
main (int argc, char **argv)
{
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *notebook;
  Application app;
  int i;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  vbox = gtk_vbox_new (FALSE, 2);
  gtk_container_add (GTK_CONTAINER (window), vbox);

  for (i = 0; i < NUM_BOOKS; i++)
    {
      Book *book = &app.book[i];

      book->app = &app;
      book->n = i;
      notebook = make_book (book);
      gtk_box_pack_end_defaults (GTK_BOX (vbox), notebook);
    }

  gtk_widget_show_all (window);

  gtk_main ();

  return (0);
}


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