sample source of buggy gtk 2.0 function



Hi,

as i mentioned in another post i think that GtkTreeViewColumn is buggy,
but no one helped me... so i have just write this small sample that you
can use to try this problem. In this sample if you press <alt+u> the
button2 work, but if you use the mouse..........

byeZ!

__________________cut here!!!__________________________


#include <gtk/gtk.h>

typedef struct
{
  gint     number;
  gchar   *product;
}
Item;

enum
{
  COLUMN_NUMBER,
  COLUMN_PRODUCT,
  NUM_COLUMNS
};

static GArray *articles = NULL;



int flag = 0;
int flag2 = 1;

GtkWidget *button1;
GtkWidget *button2;
GtkWidget *view;

/* Our new improved callback.  The data passed to this function
 * is printed to stdout. */
void callback( GtkWidget *widget,
               gpointer   data )
{
	if (strcmp(data, "button 1"))
	{
		flag = 1-flag;

		if (flag)
			gtk_widget_hide(button1);
		else gtk_widget_show(button1);
		g_print ("Hello again - %s was pressed\n", (char *) data);
	} else {
		flag2 = 1-flag2;
		gtk_widget_set_sensitive(button2, flag2);
	}
}

gint delete_event( GtkWidget *widget,
                   GdkEvent  *event,
                   gpointer   data )
{
    gtk_main_quit();
    return(FALSE);
}


static void
add_items (void)
{
  Item foo;

  g_return_if_fail (articles != NULL);

  foo.number = 3;
  foo.product = g_strdup ("bottles of coke");
  g_array_append_vals (articles, &foo, 1);

  foo.number = 5;
  foo.product = g_strdup ("packages of noodles");
  g_array_append_vals (articles, &foo, 1);

  foo.number = 2;
  foo.product = g_strdup ("packages of chocolate chip cookies");
  g_array_append_vals (articles, &foo, 1);

  foo.number = 1;
  foo.product = g_strdup ("can vanilla ice cream");
  g_array_append_vals (articles, &foo, 1);

  foo.number = 6;
  foo.product = g_strdup ("eggs");
  g_array_append_vals (articles, &foo, 1);
}

static GtkTreeModel *
create_model (void)
{
  gint i = 0;
  GtkListStore *model;
  GtkTreeIter iter;

  /* create array */
  articles = g_array_sized_new (FALSE, FALSE, sizeof (Item), 1);

  add_items ();

  /* create list store */
  model = gtk_list_store_new (NUM_COLUMNS, G_TYPE_INT, G_TYPE_STRING,
			      G_TYPE_BOOLEAN);

  /* add items */
  for (i = 0; i < articles->len; i++)
    {
      gtk_list_store_append (model, &iter);

      gtk_list_store_set (model, &iter,
			  COLUMN_NUMBER,
			  g_array_index (articles, Item, i).number,
			  COLUMN_PRODUCT,
			  g_array_index (articles, Item, i).product,
			  -1);
    }

  return GTK_TREE_MODEL (model);
}


static void
add_columns (GtkTreeView *treeview)
{
  GtkCellRenderer *renderer;
  GtkTreeModel *model = gtk_tree_view_get_model (treeview);
  GtkTreeViewColumn *column;

  /* number column */
  renderer = gtk_cell_renderer_text_new ();
  g_object_set_data (G_OBJECT (renderer), "column", (gint *)COLUMN_NUMBER);

  column = gtk_tree_view_column_new_with_attributes ("Directory",
          gtk_cell_renderer_text_new (),
          "text", 0,
          NULL);

  gtk_tree_view_column_set_widget (column, button2);
//  gtk_tree_view_column_set_clickable (column, button2);
  gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
  gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);

  /* product column */
  renderer = gtk_cell_renderer_text_new ();
  g_object_set_data (G_OBJECT (renderer), "column", (gint *)COLUMN_PRODUCT);

  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
					       -1, "Product", renderer,
					       "text", COLUMN_PRODUCT,
					       NULL);
}

void crea()
{
    GtkTreeModel *model;

    /* create model */
    model = create_model ();

    view = gtk_tree_view_new_with_model (model);

    g_object_unref (G_OBJECT (model));
    gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (view), TRUE);
    gtk_tree_selection_set_mode (gtk_tree_view_get_selection
(GTK_TREE_VIEW (view)), GTK_SELECTION_SINGLE);

    add_columns (GTK_TREE_VIEW (view));
    
    gtk_widget_show(view);
    
}



int main( int   argc,
          char *argv[] )
{
    GtkWidget *window;
    GtkWidget *box1;

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

    gtk_window_set_title (GTK_WINDOW (window), "Hello Buttons!");

    gtk_signal_connect (GTK_OBJECT (window), "delete_event",
			GTK_SIGNAL_FUNC (delete_event), NULL);

    gtk_container_set_border_width (GTK_CONTAINER (window), 10);

    box1 = gtk_vbox_new(FALSE, 0);

    gtk_container_add (GTK_CONTAINER (window), box1);

    button1 = gtk_button_new_with_mnemonic ("_Button 1");

    gtk_signal_connect (GTK_OBJECT (button1), "clicked",
			GTK_SIGNAL_FUNC (callback), (gpointer) "button 1");

    gtk_box_pack_start(GTK_BOX(box1), button1, TRUE, TRUE, 0);

    gtk_widget_show(button1);

    button2 = gtk_button_new_with_mnemonic("b_utton2");

    gtk_signal_connect (GTK_OBJECT (button2), "clicked",
			GTK_SIGNAL_FUNC (callback), (gpointer) "button 2");

    gtk_widget_show(button2);

    crea();
    gtk_widget_show(view);
    gtk_box_pack_start(GTK_BOX(box1), view, TRUE, TRUE, 0);

    gtk_widget_show(box1);

    gtk_widget_show (window);

    gtk_main ();

    return(0);
}



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