I’m a gnome newbie, working on a C application that displays mutliple columns. Created a test program to experiment, consisting of two text columns. That works.
However, failing miserably at adding the sort function for each, using the example in GTK4 api doc as the guide ( Gtk.ColumnView.get_sorter). Haven’t been able to find a good reference example in C of working code …any help?
Output from code so far.
Code compilation.
gcc pkg-config --cflags gtk4
column.view.test.model.c -g -o out pkg-config --libs gtk4
Code so far.
// Use for testing columnview
#include <gtk/gtk.h>
// Setup custom type - each item has two strings which will be displayed in two columns
#define TESTCOL_TYPE_ITEM (testcol_item_get_type())
G_DECLARE_FINAL_TYPE (TestColItem, testcol_item, TESTCOL, ITEM, GObject)
struct _TestColItem
{
GObject parent_instance;
const char *str1;
const char *str2;
};
struct _TestColItemClass
{
GObjectClass parent_class;
};
G_DEFINE_TYPE (TestColItem, testcol_item, G_TYPE_OBJECT)
static void testcol_item_init(TestColItem *item)
{
}
static void testcol_item_class_init(TestColItemClass *class)
{
}
// Set label for factory widget
static void setup_cb(GtkSignalListItemFactory *factory,GObject *listitem)
{
GtkWidget *label =gtk_label_new(NULL);
gtk_list_item_set_child(GTK_LIST_ITEM(listitem),label);
}
// Set text from the column1 passed item for label
static void bind_col1_cb(GtkSignalListItemFactory *factory, GtkListItem *listitem)
{
GtkWidget *label = gtk_list_item_get_child(listitem);
TestColItem *item = gtk_list_item_get_item(GTK_LIST_ITEM(listitem));
const char *string = item->str1;
g_print("value is %s\n",string); // Show on std out
gtk_label_set_text(GTK_LABEL (label), string);
}
// Set text from the column2 passed item for label
static void bind_col2_cb(GtkSignalListItemFactory *factory, GtkListItem *listitem)
{
GtkWidget *label = gtk_list_item_get_child(listitem);
TestColItem *item = gtk_list_item_get_item(GTK_LIST_ITEM(listitem));
const char *string = item->str2;
g_print("value is %s\n",string); // Show on std out
gtk_label_set_text(GTK_LABEL (label), string);
}
// Create the custom object
static TestColItem * testcol_item_new(const char *str1, const char *str2)
{
TestColItem *item = g_object_new(TESTCOL_TYPE_ITEM, NULL);
item->str1 = g_strdup(str1);
item->str2 = g_strdup(str2);
return item;
}
// Activate
static void activate (GtkApplication* app, gpointer user_data)
{
// Create and populate custom list model
GListStore *store = g_list_store_new(G_TYPE_OBJECT);
GListModel *model = G_LIST_MODEL(store);
g_list_store_append(store,testcol_item_new("abc1","ghi1"));
g_list_store_append(store,testcol_item_new("def2","aaa2"));
g_list_store_append(store,testcol_item_new("aaa3","jkl3"));
// Set selection for custom model
GtkSingleSelection *selection = gtk_single_selection_new(G_LIST_MODEL(model));
gtk_single_selection_set_autoselect(selection,TRUE);
// Set Windows in child scrolled window
GtkWidget *window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Test");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
GtkWidget *scrolled_window = gtk_scrolled_window_new ();
gtk_window_set_child (GTK_WINDOW(window), scrolled_window);
// Add column view. Put in scrolled window.
GtkWidget *cv = gtk_column_view_new(GTK_SELECTION_MODEL (selection));
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scrolled_window),cv);
gtk_column_view_set_show_column_separators (GTK_COLUMN_VIEW (cv),TRUE);
// Factory setup and bind for column view
GtkListItemFactory *factory = gtk_signal_list_item_factory_new();
g_signal_connect(factory, "setup", G_CALLBACK(setup_cb),NULL);
g_signal_connect(factory, "bind", G_CALLBACK(bind_col1_cb),NULL);
GtkColumnViewColumn *column = gtk_column_view_column_new("Col1", factory);
gtk_column_view_append_column (GTK_COLUMN_VIEW (cv), column);
// Factory setup and bind for column view
factory = gtk_signal_list_item_factory_new();
g_signal_connect(factory, "setup", G_CALLBACK(setup_cb),NULL);
g_signal_connect(factory, "bind", G_CALLBACK(bind_col2_cb),NULL);
column = gtk_column_view_column_new("Col2", factory);
gtk_column_view_append_column (GTK_COLUMN_VIEW (cv), column);
//From: https://docs.gtk.org/gtk4/method.ColumnView.get_sorter.html
//gtk_column_view_column_set_sorter (column, sorter);
//gtk_column_view_append_column (view, column);
//sorter = g_object_ref (gtk_column_view_get_sorter (view)));
//model = gtk_sort_list_model_new (store, sorter);
//selection = gtk_no_selection_new (model);
//gtk_column_view_set_model (view, selection);
// Sort attempt, fails miserably, clearly don't get it
// First comment out append and then add
//GtkSorter *sorter = gtk_column_view_get_sorter ((GtkColumnView *) cv); // get the sorter for col
//gtk_column_view_column_set_sorter(column, sorter); // set the sorter on column
//gtk_column_view_append_column (GTK_COLUMN_VIEW (cv), column); // put col into colview
//GtkSortListModel *smodel = gtk_sort_list_model_new (model, sorter);
//selection = gtk_single_selection_new(G_LIST_MODEL(smodel));
//gtk_column_view_set_model ((GtkColumnView *)cv, (GtkSelectionModel *)selection);
// Show window
gtk_window_present (GTK_WINDOW (window));
}
// Init application
int main (int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}