RE: 3000 toggle buttons in a table?



The radio buttons are only to improve the look and feel, as the checkbox
with a tick looks bit awkward the application. Here is the code.

----------------------------------------------------------------------------

static void
toggle_cell(GtkCellRendererToggle *renderer,
              gchar *path,
            GtkListStore *store)
{
   GtkTreeModel *model;
    GtkTreeIter iter;
    int x, bit = 1;
    int val;
    int i=0;


    char str[15];

    model = GTK_TREE_MODEL(store);
                
    gtk_tree_model_iter_nth_child(model, &iter, NULL, atoi(path));
     
    int cur = GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(renderer), "id"));

    g_object_set(renderer, "activatable", TRUE, NULL);
        
    gtk_tree_model_get(model, &iter, cur, &x, -1);

gtk_cell_renderer_toggle_set_radio(GTK_CELL_RENDERER_TOGGLE(renderer),TRUE);

    g_object_get_data(G_OBJECT(renderer), "id");

    g_object_set(G_OBJECT(renderer), "active", (gboolean)((x >> cur) & 1),
NULL);
                        
 bit = bit << GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(renderer), "id"));
                
 if (gtk_cell_renderer_toggle_get_active(renderer))
      x &= ~bit;
 else
      x |= bit;
                
 gtk_list_store_set(store, &iter, cur, x, -1);
                 
 for(i=1;i<=16;i++)
 {
    str[i] = !gtk_cell_renderer_toggle_get_active(renderer) ? '1' : '0';
 }
        
  val = binstring2hex( str);

  char str2[15];
         
  sprintf(str2,"%x",val);

  printf("hex value %s", str2);
    
  gtk_list_store_set (store, &iter,17, str2, -1);

   
}

int main(int argc, char* argv[])
{
  GtkWidget* window, *treeWidget;
  GtkTreeView* treeView;
  GtkTreeViewColumn* column;
  GtkListStore* store;
  GtkCellRenderer* renderer;
  GtkCellRenderer* text;
  GtkCellLayout* layout;
  GtkTreeSelection* selection;
  GtkTreeIter iter;
  GtkWidget* scroll;
  int i;

  fclose(stderr);

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  gtk_container_set_border_width(GTK_CONTAINER(window), 10);

  gtk_widget_set_usize(window, 500, 300);

  g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL);

  scroll = gtk_scrolled_window_new(NULL, NULL);

  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
GTK_POLICY_ALWAYS, GTK_POLICY_ALWAYS);

   gtk_container_add (GTK_CONTAINER(window), scroll);

   gtk_widget_show(scroll);
  store=gtk_list_store_new
    (18,G_TYPE_STRING,G_TYPE_UINT,G_TYPE_UINT,
     G_TYPE_UINT,G_TYPE_UINT,G_TYPE_UINT,
     G_TYPE_UINT,G_TYPE_UINT,G_TYPE_UINT,
     G_TYPE_UINT,G_TYPE_UINT,G_TYPE_UINT,
     G_TYPE_UINT,G_TYPE_UINT,G_TYPE_UINT,
     G_TYPE_UINT,G_TYPE_UINT,G_TYPE_STRING);

  treeWidget = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));

  treeView = GTK_TREE_VIEW(treeWidget);
 
  gtk_widget_show(treeWidget);

// Column #1

  text = gtk_cell_renderer_text_new();

  
  column = gtk_tree_view_column_new();

  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(treeView), 
                                                            -1,
                                                     "DRP ADDRESS",
                                                     text,
                                                           "text",0,
                                                            NULL);      


  int k=0;

//Columns 2-17

  for(k=16;k>=1;k--)
  {
   printf("in k\n");

    renderer = gtk_cell_renderer_toggle_new();

    column = gtk_tree_view_column_new();

&&&    g_object_set(renderer, "activatable", TRUE, NULL);
 
gtk_cell_renderer_toggle_set_radio(GTK_CELL_RENDERER_TOGGLE(renderer),TRUE);

    g_signal_connect(renderer, "toggled", G_CALLBACK(toggle_cell), store); 
         
    g_object_set_data(G_OBJECT(renderer), "id", GUINT_TO_POINTER(k));

    int temp=GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(renderer),
"id"));&&&

Note : &&&---I didn't wanted to place those lines there, but without them,
getting some non-sense results&&&---

    char str[1];

    sprintf(str,"%d",k);

    gtk_tree_view_insert_column_with_attributes 
            (GTK_TREE_VIEW(treeView),
             -1,
             str,
             renderer,
             "radio",
             1,
             NULL);

  }
  
//Column# 18

  column = gtk_tree_view_column_new();

  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(treeView),
                                                                -1,
"Hex Value",
                                                               text,
"text",
                                                                 17,
                                                               NULL);   
  

  layout = GTK_CELL_LAYOUT(column);
 
  int num;

  int ctr = 0;
    
   for(ctr =1; ctr <=178;ctr++)
    {
      num = ctr;
      
      column = gtk_tree_view_column_new();
            
      gtk_list_store_append (store, &iter);
      
      gtk_list_store_set (store, &iter,0, "0x001", -1);
   
      char strnum[16];

      int2binstring(num,strnum);

      printf("bin value is %s\n", strnum);

      for(i = 16; i >=1; i--)
      {
        renderer = gtk_cell_renderer_toggle_new();

        column = gtk_tree_view_column_new();

        g_object_set_data(G_OBJECT(renderer), "id", GUINT_TO_POINTER(ctr));

        int temp=GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(renderer),
"id"));
           gtk_cell_renderer_toggle_set_radio(
                        GTK_CELL_RENDERER_TOGGLE(renderer),
                        TRUE);

         g_signal_connect(renderer, "toggled",
                                 G_CALLBACK(toggle_cell), store);

         if(strnum[i] == '1')
                                        
                 g_object_set(G_OBJECT(renderer), "active", TRUE, NULL);
                 
    }//end of for i

 
    column = gtk_tree_view_column_new();

    text = gtk_cell_renderer_text_new();
 
    gtk_list_store_set (store, &iter,18,0, -1);

  }
  gtk_tree_view_append_column(treeView, column);

  selection = gtk_tree_view_get_selection(treeView);

  gtk_tree_selection_set_mode(selection, GTK_SELECTION_NONE);

  gtk_container_add (GTK_CONTAINER(scroll), treeWidget);

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}

Pl. note that I'm new to GTK programming, so if you find some blunders pl.
excuse me.

Regards
Laxmi

-----Original Message-----
From: David Necas (Yeti) [mailto:yeti physics muni cz] 
Sent: Monday, April 10, 2006 12:49 PM
To: Sailaxmi korada
Cc: gtk-app-devel-list gnome org
Subject: Re: 3000 toggle buttons in a table?

On Mon, Apr 10, 2006 at 12:40:12PM +0530, Sailaxmi korada  wrote:

Address(label)  1-16 radio buttons(represent the binary value) hex value

Data of 178 rows is filled in the above structure. But now when I'm
selecting a radio button, all the radio buttons in the same column of 178
rows are getting selected. Can anyone help me out

Do you really want *radiobuttons* i.e. only one bit being
always set?  Anyway, this can be hardly answered without the
code (moreover your description is pretty unclear -- do you
describe the model, the view, or what?).

You have a complete working code sample which sets bits in
some integers using treeview checkbuttons, I sent it in my
first reply (it only misses the hex value representation
part), so compare it with your code.

Yeti


--
That's enough.







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