Mixed GtkTreeView Demo


/* mixed-tree-view-demo.c
 * Heiher <admin@heiher.info>
 * gcc -o demo mixed-tree-view-demo.c `pkg-config --cflags --libs gtk+-2.0`
 */
 
#include <gtk/gtk.h>
 
enum
{
	COLUMN_NAME,
	COLUMN_VALUE_TEXT_VISIBLE,
	COLUMN_VALUE_TEXT_TEXT,
	COLUMN_VALUE_PROGRESS_VISIBLE,
	COLUMN_VALUE_PROGRESS_VALUE,
	COLUMN_VALUE_PROGRESS_TEXT,
	N_COLUMNS
};
 
int main(int argc, char * argv[])
{
	GtkWidget * window = NULL;
	GtkWidget * scrolled_window = NULL;
	GtkWidget * tree_view = NULL;
	GtkListStore * list_store = NULL;
	GtkCellRenderer * renderer = NULL;
	GtkTreeViewColumn * column = NULL;
	GtkTreeIter iter;
 
	gtk_init(&argc, &argv);
 
	/* Window */
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_resize(GTK_WINDOW(window), 400, 300);
	gtk_window_set_title(GTK_WINDOW(window), "Mixed TreeView Demo");
	gtk_container_set_border_width(GTK_CONTAINER(window), 4);
	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
 
	/* Scrolled Window */
	scrolled_window = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
				GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
	gtk_container_add(GTK_CONTAINER(window), scrolled_window);
 
	/* Tree View */
	tree_view = gtk_tree_view_new();
	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_window),
				tree_view);
 
	/* Append 'Name' column */
	column = gtk_tree_view_column_new();
	gtk_tree_view_column_set_title(column, "Name");
	renderer = gtk_cell_renderer_text_new();
	gtk_tree_view_column_pack_start(column, renderer, FALSE);
	gtk_tree_view_column_set_attributes(column, renderer,
				"text", COLUMN_NAME, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);
 
	/* Append 'Value' column */
	column = gtk_tree_view_column_new();
	gtk_tree_view_column_set_title(column, "Value");
	/* Type 'text' */
	renderer = gtk_cell_renderer_text_new();
	gtk_tree_view_column_pack_start(column, renderer, FALSE);
	gtk_tree_view_column_set_attributes(column, renderer,
				"visible", COLUMN_VALUE_TEXT_VISIBLE,
				"text", COLUMN_VALUE_TEXT_TEXT, NULL);
	/* Type 'progressbar' */
	renderer = gtk_cell_renderer_progress_new();
	gtk_tree_view_column_pack_start(column, renderer, TRUE);
	gtk_tree_view_column_set_attributes(column, renderer,
				"visible", COLUMN_VALUE_PROGRESS_VISIBLE,
				"value", COLUMN_VALUE_PROGRESS_VALUE,
				"text", COLUMN_VALUE_PROGRESS_TEXT, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);
 
	/* List Store */
	list_store = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING,
				G_TYPE_BOOLEAN, G_TYPE_STRING,
				G_TYPE_BOOLEAN, G_TYPE_INT,
				G_TYPE_STRING);
	gtk_tree_view_set_model(GTK_TREE_VIEW(tree_view), GTK_TREE_MODEL(list_store));
 
	/* Append test data */
	gtk_list_store_append(list_store, &iter);
	gtk_list_store_set(list_store, &iter,
				COLUMN_NAME, "Heiher",
				COLUMN_VALUE_TEXT_VISIBLE, TRUE,
				COLUMN_VALUE_TEXT_TEXT, "HAM",
				COLUMN_VALUE_PROGRESS_VISIBLE, FALSE, -1);
	gtk_list_store_append(list_store, &iter);
	gtk_list_store_set(list_store, &iter,
				COLUMN_NAME, "Jack",
				COLUMN_VALUE_TEXT_VISIBLE, FALSE,
				COLUMN_VALUE_PROGRESS_VISIBLE, TRUE,
				COLUMN_VALUE_PROGRESS_VALUE, 28,
				COLUMN_VALUE_PROGRESS_TEXT, "28GB / 100GB", -1);
 
	gtk_widget_show_all(window);
 
	gtk_main();
 
	return 0;
}

Over!

Leave a Reply

Your email address will not be published. Required fields are marked *