GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Apr 24, 2014 5:53 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: populate gtk tree view from outside
PostPosted: Wed Sep 12, 2012 1:49 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Dear friends,
Thanks for your help on extracting the data in a array.
I am facing another problem now(please don't get irritated)

I have defined a treeview model as follows:
Code:
enum
{
  COL_FIRST_NAME = 0,
  COL_LAST_NAME,
  COL_YEAR_BORN,
  NUM_COLS
} ;

static GtkTreeModel *
create_and_fill_model (void)
{
  GtkTreeStore  *treestore;
  GtkTreeIter    toplevel, child;

  treestore = gtk_tree_store_new(NUM_COLS,
                                 G_TYPE_STRING,
                                 G_TYPE_STRING,
                                 G_TYPE_UINT);

int i;
FILE *fauth;
char alst[10][500], buffer[500];
fauth=fopen("fauth.dat","r");
if(!fauth){
        printf("fauth failed\n");
}

while(fgets(buffer,500,fauth)){
        strcpy(alst[i],buffer);
        i++;
    }
fclose(fauth);

for (i = 0; i < 10; i++){
  gtk_tree_store_append(treestore, &toplevel, NULL);
  gtk_tree_store_set(treestore, &toplevel,
                     COL_FIRST_NAME, alst[i],
                     COL_LAST_NAME, "Average",
                     COL_YEAR_BORN, (guint) 1962,
                     -1);
}

  return GTK_TREE_MODEL(treestore);
}


static GtkWidget *
create_view_and_model (void)
{
  GtkTreeViewColumn   *col;
  GtkCellRenderer     *renderer;
  GtkWidget           *view;
  GtkTreeModel        *model;
  view = gtk_tree_view_new();
  col = gtk_tree_view_column_new();
  gtk_tree_view_column_set_title(col, "First Name");
  gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
  renderer = gtk_cell_renderer_text_new();
  gtk_tree_view_column_pack_start(col, renderer, TRUE);
  gtk_tree_view_column_add_attribute(col, renderer, "text", COL_FIRST_NAME);
   gtk_tree_view_column_pack_start(col, renderer, TRUE);
  model = create_and_fill_model();
gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
  g_object_unref(model);
  gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),
                              GTK_SELECTION_NONE);
  return view;
}
//For brevity, I have cut out the 2nd and 3rd column


In a different function, I have

Code:
  strAuth = gtk_entry_get_text(GTK_ENTRY(e->entryAuth));
    strEditor = gtk_entry_get_text(GTK_ENTRY(e->entryEditor));

Is it possible to add these strAuth, strEditor in those treeview's 1st and 2nd column?


Top
 Profile  
 
 Post subject: Re: populate gtk tree view from outside
PostPosted: Wed Sep 12, 2012 7:38 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

Below you'll find a small application that demonstrates how to share data between various components of your app. Usually, data in GTK+ apps is shared by passing some common structure to all signals as a last parameter. You can achieve greater data encapsulation if you create custom widgets for various components of your GUI, but that's a bit advanced to do with your current level of knowledge. But in time, I'm sure you'll get there.

So, here is the code:
Code:
#include <gtk/gtk.h>

enum
{
  F_NAME,
  L_NAME,
  NO_COLS
};

typedef struct _Data
{
  GtkListStore *store;
  GtkEntry *fname,
           *lname;
  GtkTreeSelection *sel;
}
Data;


static GtkListStore *
create_store (void)
{
  GtkListStore *store;
  GtkTreeIter iter;

  char const *fnames[] = {"John", "Emma", "Fido"};
  char const *lnames[] = {"King", "Jones", "The Dog"};
  int limit, i;


  limit = G_N_ELEMENTS (fnames);

  store = gtk_list_store_new (NO_COLS, G_TYPE_STRING, G_TYPE_STRING);
  for (i = 0; i < limit; i++)
    {
      gtk_list_store_append (store, &iter);
      gtk_list_store_set (store, &iter, F_NAME, fnames[i],
                          L_NAME, lnames[i], -1);
    }

  return store;
}

static void
sel_changed (GtkTreeSelection *sel,
             Data             *data)
{
  GtkTreeIter iter;
  GtkTreeModel *model;
  char *fname, *lname;

  if (!gtk_tree_selection_get_selected (data->sel, &model, &iter))
    return;

  gtk_tree_model_get (model, &iter, F_NAME, &fname, L_NAME, &lname, -1);
  gtk_entry_set_text (data->fname, fname);
  gtk_entry_set_text (data->lname, lname);
  g_free (fname);
  g_free (lname);
}

static void
update (GtkButton *button,
        Data      *data)
{
  GtkTreeIter iter;

  if (!gtk_tree_selection_get_selected (data->sel, NULL, &iter))
    return;

  gtk_list_store_set (data->store, &iter,
                      F_NAME, gtk_entry_get_text (data->fname),
                      L_NAME, gtk_entry_get_text (data->lname),
                      -1);
}

static void
add (GtkButton *button,
     Data      *data)
{
  GtkTreeIter iter;

  gtk_list_store_append (data->store, &iter);
  gtk_list_store_set (data->store, &iter,
                      F_NAME, gtk_entry_get_text (data->fname),
                      L_NAME, gtk_entry_get_text (data->lname),
                      -1);
}

int
main (int    argc,
      char **argv)
{
  GtkWidget *window,
            *vbox,
            *swindow,
            *tree,
            *hbox,
            *entry,
            *button;
  GtkTreeSelection *sel;
  Data *data;


  gtk_init (&argc, &argv);

  data = g_slice_new (Data);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "destroy", gtk_main_quit, NULL);

  vbox = gtk_vbox_new (FALSE, 5);
  gtk_container_add (GTK_CONTAINER (window), vbox);

  swindow = gtk_scrolled_window_new (NULL, NULL);
  gtk_box_pack_start (GTK_BOX (vbox), swindow, TRUE, TRUE, 0);

  data->store = create_store ();
  tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (data->store));
  g_object_unref (data->store);
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree), -1,
                                               "First name",
                                               gtk_cell_renderer_text_new (),
                                               "text", F_NAME, NULL);
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree), -1,
                                               "Last name",
                                               gtk_cell_renderer_text_new (),
                                               "text", L_NAME, NULL);
  sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree));
  data->sel = sel;
  gtk_tree_selection_set_mode (sel, GTK_SELECTION_BROWSE);
  g_signal_connect (sel, "changed", G_CALLBACK (sel_changed), data);
  gtk_container_add (GTK_CONTAINER (swindow), tree);

  hbox = gtk_hbox_new (FALSE, 5);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

  entry = gtk_entry_new ();
  data->fname = GTK_ENTRY (entry);
  gtk_box_pack_start (GTK_BOX (hbox), entry,  TRUE, TRUE, 0);

  entry = gtk_entry_new ();
  data->lname = GTK_ENTRY (entry);
  gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);

  button = gtk_button_new_with_label ("Update");
  g_signal_connect (button, "clicked", G_CALLBACK (update), data);
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, TRUE, 0);

  button = gtk_button_new_with_label ("Add");
  g_signal_connect (button, "clicked", G_CALLBACK (add), data);
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, TRUE, 0);

  gtk_widget_show_all (window);

  gtk_main ();

  g_slice_free (Data, data);

  return 0;
}


Cheers,
Tadej


Top
 Profile  
 
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 1 guest


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