Up: [Readme.md](../Readme.md), Prev: [Section 27](sec27.md) # GtkColumnView ## GtkColumnView GtkColumnView is like GtkListView, but it has multiple columns. Each column is GtkColumnViewColumn. ![Column View](../image/column_view.png) - GtkColumnView has "model" property. The property points a GtkSelectionModel object. - Each GtkColumnViewColumn has "factory" property. The property points a GtkListItemFactory (GtkSignalListItemFactory or GtkBuilderListItemFactory). - The factory connects GtkListItem, which belongs to GtkColumnViewColumn, and items of GtkSelectionModel. And the factory builds the descendants widgets of GtkColumnView to display the item on the display. This process is the same as the one in GtkListView. The following diagram shows the image how it works. ![ColumnView](../image/column.png) The example in this section is a window that displays information of files in a current directory. The information is the name, size and last modified datetime of files. So, there are three columns. In addition, the example uses GtkSortListModel and GtkSorter to sort the information. ## column.ui Ui file specifies whole widgets and their structure. ~~~xml 1 2 3 4 file list 5 800 6 600 7 8 9 TRUE 10 TRUE 11 12 13 14 15 16 17 18 19 standard::name,standard::icon,standard::size,time::modified 20 21 22 23 columnview 24 25 26 27 28 29 30 31 Name 32 TRUE 33 34 35 37 38 66 67 ]]> 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 Size 83 84 85 87 88 101 102 ]]> 103 104 105 106 107 108 109 110 111 GTK_SORT_ASCENDING 112 113 114 115 116 117 118 Date modified 119 120 121 123 124 137 138 ]]> 139 140 141 142 143 144 145 146 147 GTK_SORT_ASCENDING 148 149 150 151 152 153 154 155 156 157 ~~~ - 3-12: Widget parent-child relationship is GtkApplicationWindow => GtkScrolledWindow => GtkColumnView. - 12-18: GtkColumnView has "model" property. It points GtkSelectionModel interface. In this ui file, GtkSingleSelection is used as GtkSelectionModel. GtkSingleSelection is an object that implements GtkSelectionModel. And again, it has "model" property. It points GtkSortListModel. This list model supports sorting the list. It will be explained in the later subsection. And it also has "model" property. It points GtkDirectoryList. Therefore, the chain is: GtkColumnView => GtkSingleSelection => GtkSortListModel => GtkDirectoryList. - 18-20: GtkDirectoryList. It is a list of GFileInfo, which holds information of files under a directory. It has "attributes" property. It specifies what attributes is kept in each GFileInfo. - "standard::name" is a name of the file. - "standard::icon" is a GIcon object of the file - "standard::size" is the file size. - "time::modified" is the date and time the file was last modified. - 29-79: The first GtkColumnViewColumn object. There are four properties, "title", "expand", factory" and "sorter". - 31: Sets the "title" property with "Name". This is the title on the header of the column. - 32: Sets the "expand" property to TRUE to allow the column to expand as much as possible. (See the image above). - 33- 69: Sets the "factory" property with GtkBuilderListItemFactory. The factory has "bytes" property which holds a ui string to define a template to build GtkListItem composite widget. The CDATA section (line 36-66) is the ui string to put into the "bytes" property. The contents are the same as the ui file `factory_list.ui` in the section 26. - 70-77: Sets the "sorter" property with GtkStringSorter object. This object provides a sorter that compares strings. It has "expression" property which is set with GtkExpression. A closure tag with a string type function `get_file_name` is used here. The function will be explained later. - 80-115: The second GtkColumnViewColumn object. Its "title", "factory" and "sorter" properties are set. GtkNumericSorter is used. - 116-151: The third GtkColumnViewColumn object. Its "title", "factory" and "sorter" properties are set. GtkNumericSorter is used. ## GtkSortListModel and GtkSorter GtkSortListModel is a list model that sorts its elements according to a GtkSorter. It has "sorter" property that is set with GtkSorter. The property is bound to "sorter" property of GtkColumnView in line 22 to 24. ~~~xml ... ... ... columnview ~~~ Therefore, `columnview` determines the way how to sort the list model. The "sorter" property of GtkColumnView is read-only property and it is a special sorter. It reflects the user's sorting choice. If a user clicks the header of a column, then the sorter ("sorter" property) of the column is referenced by "sorter" property of the GtkColumnView. If the user clicks the header of another column, then the "sorter" property of the GtkColumnView refers to the newly clicked column's "sorter" property. The binding above makes a indirect connection between the "sorter" property of GtkSortListModel and the "sorter" property of each column. GtkSorter has several child objects. - GtkStringSorter compares strings. - GtkNumericSorter compares numbers. - GtkCustomSorter uses a callback to compare. - GtkMultiSorter combines multiple sorters. The example uses GtkStringSorter and GtkNumericSorter. GtkStringSorter uses GtkExpression to get the strings from the objects. The GtkExpression is stored in the "expression" property of GtkStringSorter. For example, in the ui file above, the GtkExpression is in the line 71 to 76. ~~~xml ~~~ The GtkExpression calls `get_file_name` function when it is evaluated. ~~~C 1 char * 2 get_file_name (GFileInfo *info) { 3 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL); 4 5 return g_strdup(g_file_info_get_name (info)); 6 } ~~~ The function is given the item (GFileInfo) of the GtkSortListModel as an argument (`this` object). The function retrieves a filename from `info`. The string is owned by `info` so it is necessary to duplicate it. And it returns the copied string. The string will be owned by the expression. GtkNumericSorter compares numbers. It is used in the line 106 to 112 and line 142 to 148. The lines from 106 to 112 is: ~~~xml GTK_SORT_ASCENDING ~~~ The closure tag specifies a callback function `get_file_size`. ~~~C 1 goffset 2 get_file_size (GFileInfo *info) { 3 g_return_val_if_fail (G_IS_FILE_INFO (info), -1); 4 5 return g_file_info_get_size (info); 6 } ~~~ It just returns the size of `info`. The type of the size is `goffset`. The type `goffset` is the same as `gint64`. The lines from 142 to 148 is: ~~~xml GTK_SORT_ASCENDING ~~~ The closure tag specifies a callback function `get_file_unixtime_modified`. ~~~C 1 gint64 2 get_file_unixtime_modified (GFileInfo *info) { 3 g_return_val_if_fail (G_IS_FILE_INFO (info), -1); 4 5 GDateTime *dt; 6 7 dt = g_file_info_get_modification_date_time (info); 8 return g_date_time_to_unix (dt); 9 } ~~~ It gets the modification date and time (GDateTime type) of `info`. Then it gets a unix time from `dt`. Unix time, sometimes called unix epoch, is the number of seconds that have elapsed since 00:00:00 UTC on 1 January 1970. It returns the unix time (gint64 type). ## column.c `column.c` is as follows. ~~~C 1 #include 2 3 /* functions (closures) for GtkBuilderListItemFactory */ 4 GIcon * 5 get_icon_factory (GtkListItem *item, GFileInfo *info) { 6 GIcon *icon; 7 if (! G_IS_FILE_INFO (info)) 8 return NULL; 9 else { 10 icon = g_file_info_get_icon (info); 11 g_object_ref (icon); 12 return icon; 13 } 14 } 15 16 char * 17 get_file_name_factory (GtkListItem *item, GFileInfo *info) { 18 if (! G_IS_FILE_INFO (info)) 19 return NULL; 20 else 21 return g_strdup (g_file_info_get_name (info)); 22 } 23 24 char * 25 get_file_size_factory (GtkListItem *item, GFileInfo *info) { 26 /* goffset is gint64 */ 27 goffset size; 28 29 if (! G_IS_FILE_INFO (info)) 30 return NULL; 31 else { 32 size = g_file_info_get_size (info); 33 return g_strdup_printf ("%ld", (long int) size); 34 } 35 } 36 37 char * 38 get_file_time_modified_factory (GtkListItem *item, GFileInfo *info) { 39 GDateTime *dt; 40 41 if (! G_IS_FILE_INFO (info)) 42 return NULL; 43 else { 44 dt = g_file_info_get_modification_date_time (info); 45 return g_date_time_format (dt, "%F"); 46 } 47 } 48 49 /* Functions (closures) for GtkSorter */ 50 char * 51 get_file_name (GFileInfo *info) { 52 g_return_val_if_fail (G_IS_FILE_INFO (info), NULL); 53 54 return g_strdup(g_file_info_get_name (info)); 55 } 56 57 goffset 58 get_file_size (GFileInfo *info) { 59 g_return_val_if_fail (G_IS_FILE_INFO (info), -1); 60 61 return g_file_info_get_size (info); 62 } 63 64 gint64 65 get_file_unixtime_modified (GFileInfo *info) { 66 g_return_val_if_fail (G_IS_FILE_INFO (info), -1); 67 68 GDateTime *dt; 69 70 dt = g_file_info_get_modification_date_time (info); 71 return g_date_time_to_unix (dt); 72 } 73 74 /* ----- activate, open, startup handlers ----- */ 75 static void 76 app_activate (GApplication *application) { 77 GtkApplication *app = GTK_APPLICATION (application); 78 GFile *file; 79 80 GtkBuilder *build = gtk_builder_new_from_resource ("/com/github/ToshioCP/column/column.ui"); 81 GtkWidget *win = GTK_WIDGET (gtk_builder_get_object (build, "win")); 82 GtkDirectoryList *directorylist = GTK_DIRECTORY_LIST (gtk_builder_get_object (build, "directorylist")); 83 g_object_unref (build); 84 85 gtk_window_set_application (GTK_WINDOW (win), app); 86 87 file = g_file_new_for_path ("."); 88 gtk_directory_list_set_file (directorylist, file); 89 g_object_unref (file); 90 91 gtk_widget_show (win); 92 } 93 94 static void 95 app_startup (GApplication *application) { 96 } 97 98 #define APPLICATION_ID "com.github.ToshioCP.columnview" 99 100 int 101 main (int argc, char **argv) { 102 GtkApplication *app; 103 int stat; 104 105 app = gtk_application_new (APPLICATION_ID, G_APPLICATION_FLAGS_NONE); 106 107 g_signal_connect (app, "startup", G_CALLBACK (app_startup), NULL); 108 g_signal_connect (app, "activate", G_CALLBACK (app_activate), NULL); 109 /* g_signal_connect (app, "open", G_CALLBACK (app_open), NULL);*/ 110 111 stat =g_application_run (G_APPLICATION (app), argc, argv); 112 g_object_unref (app); 113 return stat; 114 } 115 ~~~ - 4-47: Functions for the closure tag in the "bytes" property of GtkBuilderListItemFactory. These are almost same as the functions in section 25 and 26. - 50-72: Functions for the closure in the expression property of GtkStringSorter or GtkNumericSorter. - 75-92: `app_activate` is an "activate" handler of GApplication. - 80-83: Builds objects with ui resource and gets `win` and `directorylist`. - 85: Sets the application of the top level window with `app`. - 87-89: Sets the file of `directorylist` with "." (current directory). - 94-96: Startup handler. - 98-114: `main` function. `exp.c` is simple and short thanks to `exp.ui`. ## Compilation and execution. All the source files are in [src/column](../src/column) directory. Change your current directory to the directory and type the following. ~~~ $ meson _build $ ninja -C _build $ _build/column ~~~ Then, a window appears. ![Column View](../image/column_view.png) If you click the header of a column, then the whole lists are sorted by the column. If you click the header of another column, then the whole lists are sorted by the newly selected column. GtkColumnView is very useful and it can manage very big GListModel. It is possible to use it for file list, application list, database frontend and so on. Up: [Readme.md](../Readme.md), Prev: [Section 27](sec27.md)