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 Jul 31, 2014 1:30 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: GtkTreeSortable sort with header
PostPosted: Sat Oct 06, 2012 9:44 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Dear friends,
This is a slight altered version of code Tadej thankfully post for me for my one previous query(viewtopic.php?f=3&t=178159)
I am trying to make it sortable. I have managed to get sortable by any one column, using
GtkTreeSortable *sortable=GTK_TREE_SORTABLE(data->store);
Code:
gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortable),
                    ID_YEAR, GTK_SORT_ASCENDING);


which will by default sort by year(or any ID_* that appear last), and treeview will get that view.
What I am looking for, is make my header clickable to get the sorting, with initial view completely unsorted.
I only managed to found this information:

Quote:
Unless you have hidden your tree view column headers or use custom tree view column header widgets, each tree view column's header can be made clickable. Clicking on a tree view column's header will then sort the list according to the data in that column. You need to do two things to make this happen: firstly, you need to tell your model which sort function to use for which sort column ID with gtk_tree_sortable_set_sort_func. Once you have done this, you tell each tree view column which sort column ID should be active if this column's header is clicked. This is done with gtk_tree_view_column_set_sort_column_id.


but I have no idea how.
Can you kindly help a bit,
Code:
typedef struct _Data
{
  GtkWidget *window;
  GtkListStore *store;
}
Data;

enum
{
  C_TYPE = 0,
  C_NAME,
  C_YEAR,
  C_TITLE,
  NO_COLS
};

enum
  {
    ID_TYPE = 0,
    ID_NAME,
    ID_YEAR,
    ID_TITLE
  };

static void
create_gui (Data *data)
{
  GtkWidget *vbox,
            *cbutton,
            *swindow,
            *tree;
  GtkCellRenderer *cell;

  data->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size (GTK_WINDOW (data->window), 400, 400);
  g_signal_connect (data->window, "destroy", gtk_main_quit, NULL);

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
  gtk_container_add (GTK_CONTAINER (data->window), vbox);

  cbutton = gtk_file_chooser_button_new ("Load bib file",
                                         GTK_FILE_CHOOSER_ACTION_OPEN);
  g_signal_connect (cbutton, "file-set", G_CALLBACK (cb_file_set), data);
  gtk_box_pack_start (GTK_BOX (vbox), cbutton, FALSE, FALSE, 0);

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

  data->store = gtk_list_store_new (NO_COLS, G_TYPE_STRING, G_TYPE_STRING,
                                    G_TYPE_STRING, G_TYPE_STRING);

  GtkTreeSortable *sortable=GTK_TREE_SORTABLE(data->store);
/*  gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortable),
                    ID_NAME, GTK_SORT_ASCENDING);
*/
  gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortable),
                    ID_YEAR, GTK_SORT_ASCENDING);

  tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (data->store));
  gtk_container_add (GTK_CONTAINER (swindow), tree);

  cell = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree), -1,
                                               "Type", cell,
                                               "text", C_TYPE,
                                               NULL);

  cell = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree), -1,
                                               "Name", cell,
                                               "text", C_NAME,
                                               NULL);

  cell = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree), -1,
                                               "Year", cell,
                                               "text", C_YEAR,
                                               NULL);

  cell = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree), -1,
                                               "Title", cell,
                                               "text", C_TITLE,
                                               NULL);

  gtk_widget_show_all (vbox);
}


Top
 Profile  
 
 Post subject: Re: GtkTreeSortable sort with header
PostPosted: Sun Oct 07, 2012 8:22 am 
Offline
Never Seen the Sunlight

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

Have a look at this great treeview tutorial: Sorting tree view.

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: GtkTreeSortable sort with header
PostPosted: Thu Oct 11, 2012 4:42 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Dear friends,
I have another query.
As the treeview is loaded from a file, in my program, new entries can also be entered. It writes to the Treeview, as well as in a file using:
Code:
strAuth = gtk_entry_get_text(GTK_ENTRY(e->entryAuth));

/*Entering the data in Treeview */
  gtk_list_store_append(store, &siter);
  gtk_list_store_set(store, &siter,
                      COL_BIB_KEY, strkey,
                      COL_BIB_TYPE, strcombo,
                      COL_BIB_NAME, strAuth,
                      COL_BIB_YEAR, strYear,
                      COL_BIB_PUB, strTitle,
                      -1);
/*Entering the data in file */
if( strlen(strEditor)!=0)
    g_fprintf( fop, "\tEditor=\"%s\",\n", strAuth);
...etc...


Now, my question is, can this to entry be linked such that if I delete an entry from treeview, it will also delete the entry from file(fop).


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google Adsense [Bot] and 5 guests


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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group