Re: gtk_table_resize()



Thanks to those who offered suggestions.  In the end, it turned out
that I'd just made a couple of false assumptions about how the table
resizing worked.  I think I've fixed it now.   I've attached my test program 
just in case anyone else is having trouble with table resizing.  

Perhaps there should be something like this in the "tables" section 
of the Gtk Tutorial...?

----8<--snip---------------------------------------------------------------

/* Gtk Table Resizing - David J. Singer, November 2004. */

#include <stdio.h>
#include <gtk/gtk.h>

/* The limit imposed by gtk_table_new() is 65535 */
#define MAX_TABLE_ROWS 16

GtkWidget *window, *table;
GtkWidget *buttons[MAX_TABLE_ROWS];

int num_table_rows = 0;

/* Add a row to the table */
void add_row( void )
{

   GtkWidget *but;

   if (num_table_rows < MAX_TABLE_ROWS) {

      printf("add_row(): Adding row %d\n", num_table_rows+1);

      /* Add a row to the bottom of the table */
      gtk_table_resize( GTK_TABLE(table), num_table_rows+1, 1 );

      /* Create a new button */
      but = gtk_button_new_with_label("Wibble");
      buttons[num_table_rows] = but;

      /* Attach button to new row in table */
      gtk_table_attach(GTK_TABLE(table), but, 0, 1, num_table_rows+1,
                       num_table_rows+2, (GtkAttachOptions)GTK_FILL,
                       (GtkAttachOptions)0, 0, 0);

      /* Make button visible */
      gtk_widget_show(but);

      /* Increment row count */
      num_table_rows++;

   }
   else
      printf("Table is full!\n");

}

/* Remove a row from the table */
void remove_row( void )
{

   GtkWidget *but;

   if (num_table_rows > 0) {

      printf("add_row(): Removing row %d\n", num_table_rows);

      /* Remove bottom button from table */
      but = buttons[num_table_rows-1];
      gtk_widget_destroy(GTK_WIDGET(but));

      /* Remove bottom row from table */
      if (num_table_rows > 1)
         gtk_table_resize( GTK_TABLE(table), num_table_rows-1, 1 );

      /* Decrement row count */
      num_table_rows--;

   }
   else
      printf("Table is empty!\n");

}

main( int argc, char *argv[] )
{

   GtkWidget *vbox, *toolbar, *scrolled_window, *viewport;

   gtk_init(&argc, &argv);

   /* Create the main window */
   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_window_set_title(GTK_WINDOW(window), "Table Resizing");

   /* Create a vbox */
   vbox = gtk_vbox_new(FALSE, 0);

   /* Create a toolbar and add to the vbox */
   toolbar = gtk_toolbar_new();
   gtk_toolbar_append_item( GTK_TOOLBAR(toolbar), " Add Row ",
                            "Add a row to the table",
                            NULL, NULL, add_row, NULL);
   gtk_toolbar_append_item( GTK_TOOLBAR(toolbar), " Remove Row ",
                            "Remove a row from the table",
                            NULL, NULL, remove_row, NULL);
   gtk_box_pack_start(GTK_BOX(vbox), toolbar, FALSE, FALSE, 0);

   /* Create a scrollable window and add to the vbox */
   scrolled_window = gtk_scrolled_window_new(NULL, NULL);
   gtk_container_set_border_width(GTK_CONTAINER(scrolled_window), 2);
   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
                                  GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
   gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 2);

   /* Create a viewport */
   viewport = gtk_viewport_new(NULL, NULL);
   gtk_container_add(GTK_CONTAINER(scrolled_window), viewport);

   /* Create a table with one row and add to the vbox */
   /* Note that there are no child widgets attached to this table yet! */
   table = gtk_table_new( 1, 1, FALSE );
   gtk_container_add(GTK_CONTAINER(viewport), table);

   /* Add vbox to main window */
   gtk_container_add(GTK_CONTAINER(window), vbox);

   /* Show everything */
   gtk_widget_show_all(window);

   gtk_main();

}

/* Local Variables: */
/* mc--compile-command:"gcc -g table_resize.c -o table_resize `pkg-config gtk+-2.0 --cflags --libs`" */
/* End: */


-- 
David J. Singer
doc deadvirgins org uk
"Time flies like an arrow, fruit flies like a banana"




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