Initial commit

This is the latest version found at
http://scentric.net/tutorial/treeview-tutorial-xml.tar.gz
This commit is contained in:
Gergely Polonkai 2014-07-23 01:28:11 +02:00
commit aa71f8211d
44 changed files with 50184 additions and 0 deletions

111
Makefile Normal file
View File

@ -0,0 +1,111 @@
DOCBASE = treeview-tutorial
DOCEXT = xml
DOCFILE = $(DOCBASE).$(DOCEXT)
SRCBASE = $(DOCBASE)-$(DOCEXT)
default:
@echo
@echo "Makefile targets: html pdf ps force clean check"
@echo
@echo " e.g. make html will create a html version of the tutorial."
@echo
html: $(DOCFILE) docbook-utils-a4.dsl treeview-tutorial.css
db2html --dsl docbook-utils-a4.dsl#html $(DOCFILE) ; \
test -d html && rm -rf ./html/; \
mv $(DOCBASE) html ; \
mkdir html/images; \
cp images/*.png html/images
cp treeview-tutorial.css html/
cat html/treeview-tutorial.html | sed -e 's%</head%<meta name="keywords" content="gtk tree view,treeview,gtk,GtkTreeView,tutorial,documentation,help,introduction,FAQ"><meta name="description" content="Gtk Tree View tutorial"></head%i' > tmp.html
mv tmp.html html/treeview-tutorial.html
pdf: $(DOCFILE)
db2pdf --dsl "docbook-utils-a4.dsl#print" $(DOCFILE)
ps: $(DOCFILE)
db2ps --dsl "docbook-utils-a4.dsl#print" $(DOCFILE)
force:
touch $(DOCFILE) && make html
clean:
rm -rf ./html/ 2>/dev/null
rm -f $(DOCBASE).ps $(DOCBASE).pdf $(DOCBASE).out $(DOCBASE).aux $(DOCBASE).dvi $(DOCBASE).log 2>/dev/null
rm -f examples.tar.gz 2>/dev/null
rm -f *~ 2>/dev/null
rm -rf $(SRCBASE)/ $(SRCBASE).tar.gz 2>/dev/null
check: $(DOCFILE)
xmllint --valid --noout $(DOCFILE)
examples.tar.gz: examples
( test -e examples.tar.gz && rm examples.tar.gz ) || /bin/true
cd examples/ && make clean && cd ..
tar --exclude CVS --exclude .cvsignore -cf examples.tar examples/
gzip examples.tar
src: $(DOCFILE) docbook-utils-a4.dsl treeview-tutorial.css images check
rm -rf $(SRCBASE)/ $(SRCBASE).tar $(SRCBASE).tar.gz 2>/dev/null || /bin/null
mkdir $(SRCBASE)/
cp -R Makefile $(DOCFILE) docbook-utils-a4.dsl treeview-tutorial.css images/ $(SRCBASE)/
tar --exclude CVS --exclude .cvsignore -cf $(SRCBASE).tar $(SRCBASE)/*
gzip $(SRCBASE).tar
dist: html examples
rm -rf treeview-tutorial/ 2>/dev/null || /bin/true
mkdir treeview-tutorial/
rm html/examples.tar.gz 2>/dev/null || /bin/true
cp -R html/ treeview-tutorial/
cp -R examples/ treeview-tutorial/
rm treeview-tutorial.tar.gz 2>/dev/null || /bin/true
tar --exclude CVS --exclude .cvsignore -cf treeview-tutorial.tar treeview-tutorial/
gzip treeview-tutorial.tar
rm -rf treeview-tutorial/
#upload: examples.tar.gz dist html src pdf
# mv examples.tar.gz html/
# mv treeview-tutorial.tar.gz html/
# mv $(SRCBASE).tar.gz html/
# mv $(DOCBASE).pdf html/
# cd html/ && rm -f uploadball.tar && tar cf ../uploadball.tar *html *css examples.tar.gz treeview-tutorial.tar.gz $(SRCBASE).tar.gz $(DOCBASE).pdf images/*png
# rm -f uploadball.tar.bz2
# bzip2 uploadball.tar
# scp uploadball.tar.bz2 uberdork@scentric.sourceforge.net:/home/groups/s/sc/scentric/htdocs/temp/tutorial/
# ssh uberdork@scentric.sourceforge.net "cd /home/groups/s/sc/scentric/htdocs/temp/tutorial && tar xjf uploadball.tar.bz2 && rm uploadball.tar.bz2"
# echo "**** Done. "
upload-html: html
rsync -Cavz \
--rsh="ssh -l uberdork" \
./html/* \
uberdork@shell.sourceforge.net:/home/groups/s/sc/scentric/htdocs/tutorial/
upload: clean upload-html examples.tar.gz src pdf dist
rsync -Cav \
--rsh="ssh -l uberdork" \
./examples.tar.gz \
./$(SRCBASE).tar.gz \
./$(DOCBASE).pdf \
./treeview-tutorial.tar.gz \
uberdork@shell.sourceforge.net:/home/groups/s/sc/scentric/htdocs/tutorial/
#upload: examples.tar.gz dist html src pdf
# mv examples.tar.gz html/
# mv treeview-tutorial.tar.gz html/
# mv $(SRCBASE).tar.gz html/
# mv $(DOCBASE).pdf html/
# cd html/ && rm -f uploadball.tar && tar cf ../uploadball.tar *html *css examples.tar.gz treeview-tutorial.tar.gz $(SRCBASE).tar.gz $(DOCBASE).pdf images/*png
# rm -f uploadball.tar.bz2
# bzip2 uploadball.tar
# scp uploadball.tar.bz2 uberdork@scentric.sourceforge.net:/home/groups/s/sc/scentric/htdocs/temp/tutorial/
# ssh uberdork@scentric.sourceforge.net "cd /home/groups/s/sc/scentric/htdocs/temp/tutorial && tar xjf uploadball.tar.bz2 && rm uploadball.tar.bz2"
# echo "**** Done. "

1067
docbook-utils-a4.dsl Normal file

File diff suppressed because it is too large Load Diff

18
examples/Makefile Normal file
View File

@ -0,0 +1,18 @@
SUBDIRS = cell-renderer-spin \
custom-cell-renderer \
custom-list-model \
custom-list-model-sorted \
hello-world \
simple-list \
treeview-demo
all: $(SUBDIRS)
for dir in $(SUBDIRS); do\
cd $$dir && make && cd ..;\
done; \
clean: $(SUBDIRS)
for dir in $(SUBDIRS); do\
cd $$dir && make clean && cd ..;\
done;
rm -f `find -name "*~"` || /bin/true

View File

@ -0,0 +1,12 @@
CC = gcc
OBJS = main.o cellrendererspin.o
CFLAGS = -g -O2 `pkg-config --cflags gtk+-2.0`
spinbuttonrenderer: $(OBJS)
gcc -o spinbuttonrenderer $(OBJS) `pkg-config --libs gtk+-2.0`
clean:
rm $(OBJS) spinbuttonrenderer 2>/dev/null || /bin/true

View File

@ -0,0 +1,361 @@
/***************************************************************************
cellrendererspin.c
------------------
begin : Tue Oct 21 2003
copyright : (C) 2003 by Tim-Philipp Müller
email : t.i.m at orange dot net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/*
*
* This is a dirty 15-minute hack that tries to
* make editable cells with spin buttons instead
* of the text entry widget.
*
* Modify how you please. At the moment you need
* to hook up your own cell data function to make
* sure that the number of digits is the same in
* editing mode as it is in non-editing mode.
*
* The parameters passed to _new() should probably
* be properties, and probably we don't need most
* of them anyway. Also, it would be good if there
* was a better method to ensure that the number
* of digits is the same without this.
*
* Maybe one should just rip out the whole _render
* stuff from GtkCellRendererText and make a
* whole new specialised GtkCellRenderFloat
* or something.
*
* If anyone ever completes this code to sth useful,
* or achieves sth similar in another way, or has
* any comments on it, please drop me a mail.
*/
#include "cellrendererspin.h"
#include <gtk/gtkadjustment.h>
#include <gtk/gtkspinbutton.h>
#include <stdlib.h>
#define GUI_CELL_RENDERER_SPIN_PATH "gui-cell-renderer-spin-path"
#define GUI_CELL_RENDERER_SPIN_INFO "gui-cell-renderer-spin-info"
/* Some boring function declarations: GObject type system stuff */
static void gui_cell_renderer_spin_init (GuiCellRendererSpin *cellspin);
static void gui_cell_renderer_spin_class_init (GuiCellRendererSpinClass *klass);
static void gui_cell_renderer_spin_finalize (GObject *gobject);
static gpointer parent_class;
static GtkCellEditable *gui_cell_renderer_spin_start_editing (GtkCellRenderer *cell,
GdkEvent *event,
GtkWidget *widget,
const gchar *path,
GdkRectangle *background_area,
GdkRectangle *cell_area,
GtkCellRendererState flags);
struct _GCRSpinInfo
{
gulong focus_out_id;
};
typedef struct _GCRSpinInfo GCRSpinInfo;
/***************************************************************************
*
* gui_cell_renderer_spin_get_type
*
* Here we register our type with the GObject type system if we
* haven't done so yet. Everything else is done in the callbacks.
*
***************************************************************************/
GType
gui_cell_renderer_spin_get_type (void)
{
static GType cell_spin_type = 0;
if (cell_spin_type)
return cell_spin_type;
if (1)
{
static const GTypeInfo cell_spin_info =
{
sizeof (GuiCellRendererSpinClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) gui_cell_renderer_spin_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GuiCellRendererSpin),
0, /* n_preallocs */
(GInstanceInitFunc) gui_cell_renderer_spin_init,
};
/* Derive from GtkCellRenderer */
cell_spin_type = g_type_register_static (GTK_TYPE_CELL_RENDERER_TEXT,
"GuiCellRendererSpin",
&cell_spin_info,
0);
}
return cell_spin_type;
}
/***************************************************************************
*
* gui_cell_renderer_spin_init
*
* Set some default properties of the parent (GtkCellRendererText).
*
***************************************************************************/
static void
gui_cell_renderer_spin_init (GuiCellRendererSpin *cellrendererspin)
{
return;
}
/***************************************************************************
*
* gui_cell_renderer_spin_class_init:
*
***************************************************************************/
static void
gui_cell_renderer_spin_class_init (GuiCellRendererSpinClass *klass)
{
GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(klass);
GObjectClass *object_class = G_OBJECT_CLASS(klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = gui_cell_renderer_spin_finalize;
/* Override the cell renderer's edit-related methods */
cell_class->start_editing = gui_cell_renderer_spin_start_editing;
}
/***************************************************************************
*
* gui_cell_renderer_spin_finalize: free any resources here
*
***************************************************************************/
static void
gui_cell_renderer_spin_finalize (GObject *object)
{
/*
GuiCellRendererSpin *cellrendererspin = GUI_CELL_RENDERER_SPIN(object);
*/
/* Free any dynamically allocated resources here */
/* chain up to parent class to make sure
* they release all their memory as well */
(* G_OBJECT_CLASS (parent_class)->finalize) (object);
}
/***************************************************************************
*
* gui_cell_renderer_spin_new
*
* return a new cell renderer instance
* (all the parameters should really be properties)
*
* Not sure which of all these values are really
* relevant for the spin button - needs checking!
*
***************************************************************************/
GtkCellRenderer *
gui_cell_renderer_spin_new (gdouble lower,
gdouble upper,
gdouble step_inc,
gdouble page_inc,
gdouble page_size,
gdouble climb_rate,
guint digits)
{
GtkCellRenderer *cell;
GuiCellRendererSpin *spincell;
cell = g_object_new(GUI_TYPE_CELL_RENDERER_SPIN, NULL);
spincell = GUI_CELL_RENDERER_SPIN(cell);
spincell->lower = lower;
spincell->upper = upper;
spincell->step_inc = step_inc;
spincell->page_inc = page_inc;
spincell->page_size = page_size;
spincell->climb_rate = climb_rate;
spincell->digits = digits;
return cell;
}
/***************************************************************************
*
* gui_cell_renderer_spin_editing_done
*
***************************************************************************/
static void
gui_cell_renderer_spin_editing_done (GtkCellEditable *spinbutton,
gpointer data)
{
const gchar *path;
const gchar *new_text;
GCRSpinInfo *info;
info = g_object_get_data (G_OBJECT (data), GUI_CELL_RENDERER_SPIN_INFO);
if (info->focus_out_id > 0)
{
g_signal_handler_disconnect (spinbutton, info->focus_out_id);
info->focus_out_id = 0;
}
if (GTK_ENTRY(spinbutton)->editing_canceled)
return;
path = g_object_get_data (G_OBJECT (spinbutton), GUI_CELL_RENDERER_SPIN_PATH);
new_text = gtk_entry_get_text (GTK_ENTRY(spinbutton));
g_signal_emit_by_name(data, "edited", path, new_text);
}
/***************************************************************************
*
* gui_cell_renderer_spin_focus_out_event
*
***************************************************************************/
static gboolean
gui_cell_renderer_spin_focus_out_event (GtkWidget *spinbutton,
GdkEvent *event,
gpointer data)
{
gui_cell_renderer_spin_editing_done (GTK_CELL_EDITABLE (spinbutton), data);
/* entry needs focus-out-event */
return FALSE;
}
/***************************************************************************
*
* gui_cell_renderer_spin_start_editing
*
***************************************************************************/
static gboolean
onButtonPress (GtkWidget *spinbutton, GdkEventButton *bevent, gpointer data)
{
if (bevent->button == 1 && bevent->type == GDK_2BUTTON_PRESS || bevent->type == GDK_3BUTTON_PRESS)
{
g_print ("double or triple click caught and ignored.\n");
return TRUE; /* don't invoke other handlers */
}
return FALSE;
}
/***************************************************************************
*
* gui_cell_renderer_spin_start_editing
*
***************************************************************************/
static GtkCellEditable *
gui_cell_renderer_spin_start_editing (GtkCellRenderer *cell,
GdkEvent *event,
GtkWidget *widget,
const gchar *path,
GdkRectangle *background_area,
GdkRectangle *cell_area,
GtkCellRendererState flags)
{
GtkCellRendererText *celltext;
GuiCellRendererSpin *spincell;
GtkAdjustment *adj;
GtkWidget *spinbutton;
GCRSpinInfo *info;
gdouble curval = 0.0;
celltext = GTK_CELL_RENDERER_TEXT(cell);
spincell = GUI_CELL_RENDERER_SPIN(cell);
/* If the cell isn't editable we return NULL. */
if (celltext->editable == FALSE)
return NULL;
spinbutton = g_object_new (GTK_TYPE_SPIN_BUTTON, "has_frame", FALSE, "numeric", TRUE, NULL);
/* dirty */
if (celltext->text)
curval = atof(celltext->text);
adj = GTK_ADJUSTMENT(gtk_adjustment_new(curval,
spincell->lower,
spincell->upper,
spincell->step_inc,
spincell->page_inc,
spincell->page_size));
gtk_spin_button_configure(GTK_SPIN_BUTTON(spinbutton), adj, spincell->climb_rate, spincell->digits);
g_object_set_data_full (G_OBJECT(spinbutton), GUI_CELL_RENDERER_SPIN_PATH, g_strdup (path), g_free);
gtk_editable_select_region (GTK_EDITABLE (spinbutton), 0, -1);
gtk_widget_show (spinbutton);
g_signal_connect (spinbutton, "editing_done",
G_CALLBACK (gui_cell_renderer_spin_editing_done),
celltext);
/* hack trying to catch the quite annoying effect
* a double click has while editing */
g_signal_connect (spinbutton, "button_press_event",
G_CALLBACK (onButtonPress),
NULL);
info = g_new0(GCRSpinInfo, 1);
info->focus_out_id = g_signal_connect (spinbutton, "focus_out_event",
G_CALLBACK (gui_cell_renderer_spin_focus_out_event),
celltext);
g_object_set_data_full (G_OBJECT (cell), GUI_CELL_RENDERER_SPIN_INFO, info, g_free);
return GTK_CELL_EDITABLE (spinbutton);
}

View File

@ -0,0 +1,70 @@
/***************************************************************************
cellrendererspin.h
------------------
begin : Tue Oct 21 2003
copyright : (C) 2003 by Tim-Philipp Müller
email : t.i.m at orange dot net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef _cellrendererspin_h_included_
#define _cellrendererspin_h_included_
#include <gtk/gtkcellrenderertext.h>
/* Some boilerplate GObject type check and type cast macros */
#define GUI_TYPE_CELL_RENDERER_SPIN (gui_cell_renderer_spin_get_type())
#define GUI_CELL_RENDERER_SPIN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GUI_TYPE_CELL_RENDERER_SPIN, GuiCellRendererSpin))
#define GUI_CELL_RENDERER_SPIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GUI_TYPE_CELL_RENDERER_SPIN, GuiCellRendererSpinClass))
#define GUI_IS_CELL_RENDERER_SPIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GUI_TYPE_CELL_RENDERER_SPIN))
#define GUI_IS_CELL_RENDERER_SPIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GUI_TYPE_CELL_RENDERER_SPIN))
#define GUI_CELL_RENDERER_SPIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GUI_TYPE_CELL_RENDERER_SPIN, GuiCellRendererSpinClass))
typedef struct _GuiCellRendererSpin GuiCellRendererSpin;
typedef struct _GuiCellRendererSpinClass GuiCellRendererSpinClass;
struct _GuiCellRendererSpin
{
GtkCellRendererText parent;
gdouble lower;
gdouble upper;
gdouble step_inc;
gdouble page_inc;
gdouble page_size;
gdouble climb_rate;
guint digits;
};
struct _GuiCellRendererSpinClass
{
GtkCellRendererTextClass parent_class;
};
GType gui_cell_renderer_spin_get_type (void);
GtkCellRenderer *gui_cell_renderer_spin_new (gdouble lower,
gdouble upper,
gdouble step_inc,
gdouble page_inc,
gdouble page_size,
gdouble climb_rate,
guint digits);
#endif /* _spinbar_renderer_h_included_ */

View File

@ -0,0 +1,144 @@
/* a simple test for GuiCellRendererSpin */
#include <gtk/gtk.h>
#include "cellrendererspin.h"
enum
{
COL_NAME = 0,
COL_NUMBER,
NUM_COLS
} ;
static GtkTreeModel *
create_and_fill_model (void)
{
GtkListStore *liststore;
GtkTreeIter iter;
liststore = gtk_list_store_new(NUM_COLS, G_TYPE_STRING, G_TYPE_FLOAT);
gtk_list_store_append(liststore, &iter);
gtk_list_store_set(liststore, &iter, COL_NAME, "M J Smith", COL_NUMBER, 5.0, -1);
gtk_list_store_append(liststore, &iter);
gtk_list_store_set(liststore, &iter, COL_NAME, "K F Hemmingwinski", COL_NUMBER, 3.8, -1);
gtk_list_store_append(liststore, &iter);
gtk_list_store_set(liststore, &iter, COL_NAME, "P C Juanicini", COL_NUMBER, 2.5, -1);
return GTK_TREE_MODEL(liststore);
}
static void
cell_data_func_gpa (GtkTreeViewColumn *col,
GtkCellRenderer *cell,
GtkTreeModel *model,
GtkTreeIter *iter,
gpointer data)
{
gchar buf[32];
GValue val = {0, };
gtk_tree_model_get_value(model, iter, COL_NUMBER, &val);
g_snprintf(buf, sizeof(buf), "%.1f", g_value_get_float(&val));
g_object_set(cell, "text", buf, NULL);
}
static void
on_gpa_edited (GtkCellRendererText *celltext,
const gchar *string_path,
const gchar *new_text,
gpointer data)
{
GtkTreeModel *model = GTK_TREE_MODEL(data);
GtkTreeIter iter;
gchar *name = NULL;
gfloat oldval = 0.0;
gfloat newval = 0.0;
gtk_tree_model_get_iter_from_string(model, &iter, string_path);
gtk_tree_model_get(model, &iter, COL_NAME, &name, COL_NUMBER, &oldval, -1);
if (sscanf(new_text, "%f", &newval) != 1)
g_warning("in %s: problem converting string '%s' into float.\n", __FUNCTION__, new_text);
g_print ("%s: old GPA = %.1f, new GPA = %.1f ('%s')\n", name, oldval, newval, new_text);
gtk_list_store_set(GTK_LIST_STORE(model), &iter, COL_NUMBER, newval, -1);
g_free(name);
}
static GtkWidget *
create_view_and_model (void)
{
GtkTreeViewColumn *col;
GtkCellRenderer *renderer;
GtkWidget *view;
GtkTreeModel *model;
view = gtk_tree_view_new();
model = create_and_fill_model();
gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
g_object_unref(model); /* destroy model automatically with view */
gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), GTK_SELECTION_SINGLE);
/* --- Column #1 --- */
col = gtk_tree_view_column_new();
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_set_title(col, "Name");
gtk_tree_view_column_pack_start(col, renderer, TRUE);
gtk_tree_view_column_add_attribute(col, renderer, "text", COL_NAME);
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
/* --- Column #2 --- */
col = gtk_tree_view_column_new();
renderer = gui_cell_renderer_spin_new(0.0, 5.0, 0.1, 1.0, 1.0, 0.1, 1);
gtk_tree_view_column_set_title(col, "GPA");
gtk_tree_view_column_pack_start(col, renderer, TRUE);
gtk_tree_view_column_set_cell_data_func(col, renderer, cell_data_func_gpa, NULL, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
g_object_set(renderer, "editable", TRUE, NULL);
g_signal_connect(renderer, "edited", G_CALLBACK(on_gpa_edited), model);
return view;
}
int
main (int argc, char **argv)
{
GtkWidget *window;
GtkWidget *view;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "delete_event", gtk_main_quit, NULL); /* dirty */
view = create_view_and_model();
gtk_container_add(GTK_CONTAINER(window), view);
gtk_widget_show_all(window);
gtk_main();
return 0;
}

View File

@ -0,0 +1,12 @@
CC = gcc
OBJS = main.o custom-cell-renderer-progressbar.o
CFLAGS = -g -O2 `pkg-config --cflags gtk+-2.0`
customlist: $(OBJS)
gcc -o customcellrenderer $(OBJS) `pkg-config --libs gtk+-2.0`
clean:
rm $(OBJS) customcellrenderer 2>/dev/null || /bin/true

View File

@ -0,0 +1,353 @@
/***********************************************************
*
* custom-cell-renderer-progressbar.c
*
* part of the Gtk+ tree view tutorial
*
* by Tim-Philipp Mueller < tim at centricular dot net >
*
***********************************************************/
#include "custom-cell-renderer-progressbar.h"
/* This is based mainly on GtkCellRendererProgress
* in GAIM, written and (c) 2002 by Sean Egan
* (Licensed under the GPL), which in turn is
* based on Gtk's GtkCellRenderer[Text|Toggle|Pixbuf]
* implementation by Jonathan Blandford */
/* Some boring function declarations: GObject type system stuff */
static void custom_cell_renderer_progress_init (CustomCellRendererProgress *cellprogress);
static void custom_cell_renderer_progress_class_init (CustomCellRendererProgressClass *klass);
static void custom_cell_renderer_progress_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec);
static void custom_cell_renderer_progress_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec);
static void custom_cell_renderer_progress_finalize (GObject *gobject);
/* These functions are the heart of our custom cell renderer: */
static void custom_cell_renderer_progress_get_size (GtkCellRenderer *cell,
GtkWidget *widget,
GdkRectangle *cell_area,
gint *x_offset,
gint *y_offset,
gint *width,
gint *height);
static void custom_cell_renderer_progress_render (GtkCellRenderer *cell,
GdkWindow *window,
GtkWidget *widget,
GdkRectangle *background_area,
GdkRectangle *cell_area,
GdkRectangle *expose_area,
guint flags);
enum
{
PROP_PERCENTAGE = 1,
};
static gpointer parent_class;
/***************************************************************************
*
* custom_cell_renderer_progress_get_type: here we register our type with
* the GObject type system if we
* haven't done so yet. Everything
* else is done in the callbacks.
*
***************************************************************************/
GType
custom_cell_renderer_progress_get_type (void)
{
static GType cell_progress_type = 0;
if (cell_progress_type)
return cell_progress_type;
if (1)
{
static const GTypeInfo cell_progress_info =
{
sizeof (CustomCellRendererProgressClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) custom_cell_renderer_progress_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (CustomCellRendererProgress),
0, /* n_preallocs */
(GInstanceInitFunc) custom_cell_renderer_progress_init,
};
/* Derive from GtkCellRenderer */
cell_progress_type = g_type_register_static (GTK_TYPE_CELL_RENDERER,
"CustomCellRendererProgress",
&cell_progress_info,
0);
}
return cell_progress_type;
}
/***************************************************************************
*
* custom_cell_renderer_progress_init: set some default properties of the
* parent (GtkCellRenderer).
*
***************************************************************************/
static void
custom_cell_renderer_progress_init (CustomCellRendererProgress *cellrendererprogress)
{
GTK_CELL_RENDERER(cellrendererprogress)->mode = GTK_CELL_RENDERER_MODE_INERT;
GTK_CELL_RENDERER(cellrendererprogress)->xpad = 2;
GTK_CELL_RENDERER(cellrendererprogress)->ypad = 2;
}
/***************************************************************************
*
* custom_cell_renderer_progress_class_init:
*
* set up our own get_property and set_property functions, and
* override the parent's functions that we need to implement.
* And make our new "percentage" property known to the type system.
* If you want cells that can be activated on their own (ie. not
* just the whole row selected) or cells that are editable, you
* will need to override 'activate' and 'start_editing' as well.
*
***************************************************************************/
static void
custom_cell_renderer_progress_class_init (CustomCellRendererProgressClass *klass)
{
GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(klass);
GObjectClass *object_class = G_OBJECT_CLASS(klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = custom_cell_renderer_progress_finalize;
/* Hook up functions to set and get our
* custom cell renderer properties */
object_class->get_property = custom_cell_renderer_progress_get_property;
object_class->set_property = custom_cell_renderer_progress_set_property;
/* Override the two crucial functions that are the heart
* of a cell renderer in the parent class */
cell_class->get_size = custom_cell_renderer_progress_get_size;
cell_class->render = custom_cell_renderer_progress_render;
/* Install our very own properties */
g_object_class_install_property (object_class,
PROP_PERCENTAGE,
g_param_spec_double ("percentage",
"Percentage",
"The fractional progress to display",
0.0, 1.0, 0.0,
G_PARAM_READWRITE));
}
/***************************************************************************
*
* custom_cell_renderer_progress_finalize: free any resources here
*
***************************************************************************/
static void
custom_cell_renderer_progress_finalize (GObject *object)
{
/*
CustomCellRendererProgress *cellrendererprogress = CUSTOM_CELL_RENDERER_PROGRESS(object);
*/
/* Free any dynamically allocated resources here */
(* G_OBJECT_CLASS (parent_class)->finalize) (object);
}
/***************************************************************************
*
* custom_cell_renderer_progress_get_property: as it says
*
***************************************************************************/
static void
custom_cell_renderer_progress_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *psec)
{
CustomCellRendererProgress *cellprogress = CUSTOM_CELL_RENDERER_PROGRESS(object);
switch (param_id)
{
case PROP_PERCENTAGE:
g_value_set_double(value, cellprogress->progress);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, psec);
break;
}
}
/***************************************************************************
*
* custom_cell_renderer_progress_set_property: as it says
*
***************************************************************************/
static void
custom_cell_renderer_progress_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec)
{
CustomCellRendererProgress *cellprogress = CUSTOM_CELL_RENDERER_PROGRESS (object);
switch (param_id)
{
case PROP_PERCENTAGE:
cellprogress->progress = g_value_get_double(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
break;
}
}
/***************************************************************************
*
* custom_cell_renderer_progress_new: return a new cell renderer instance
*
***************************************************************************/
GtkCellRenderer *
custom_cell_renderer_progress_new (void)
{
return g_object_new(CUSTOM_TYPE_CELL_RENDERER_PROGRESS, NULL);
}
/***************************************************************************
*
* custom_cell_renderer_progress_get_size: crucial - calculate the size
* of our cell, taking into account
* padding and alignment properties
* of parent.
*
***************************************************************************/
#define FIXED_WIDTH 100
#define FIXED_HEIGHT 10
static void
custom_cell_renderer_progress_get_size (GtkCellRenderer *cell,
GtkWidget *widget,
GdkRectangle *cell_area,
gint *x_offset,
gint *y_offset,
gint *width,
gint *height)
{
gint calc_width;
gint calc_height;
calc_width = (gint) cell->xpad * 2 + FIXED_WIDTH;
calc_height = (gint) cell->ypad * 2 + FIXED_HEIGHT;
if (width)
*width = calc_width;
if (height)
*height = calc_height;
if (cell_area)
{
if (x_offset)
{
*x_offset = cell->xalign * (cell_area->width - calc_width);
*x_offset = MAX (*x_offset, 0);
}
if (y_offset)
{
*y_offset = cell->yalign * (cell_area->height - calc_height);
*y_offset = MAX (*y_offset, 0);
}
}
}
/***************************************************************************
*
* custom_cell_renderer_progress_render: crucial - do the rendering.
*
***************************************************************************/
static void
custom_cell_renderer_progress_render (GtkCellRenderer *cell,
GdkWindow *window,
GtkWidget *widget,
GdkRectangle *background_area,
GdkRectangle *cell_area,
GdkRectangle *expose_area,
guint flags)
{
CustomCellRendererProgress *cellprogress = CUSTOM_CELL_RENDERER_PROGRESS (cell);
GtkStateType state;
gint width, height;
gint x_offset, y_offset;
custom_cell_renderer_progress_get_size (cell, widget, cell_area,
&x_offset, &y_offset,
&width, &height);
if (GTK_WIDGET_HAS_FOCUS (widget))
state = GTK_STATE_ACTIVE;
else
state = GTK_STATE_NORMAL;
width -= cell->xpad*2;
height -= cell->ypad*2;
gtk_paint_box (widget->style,
window,
GTK_STATE_NORMAL, GTK_SHADOW_IN,
NULL, widget, "trough",
cell_area->x + x_offset + cell->xpad,
cell_area->y + y_offset + cell->ypad,
width - 1, height - 1);
gtk_paint_box (widget->style,
window,
state, GTK_SHADOW_OUT,
NULL, widget, "bar",
cell_area->x + x_offset + cell->xpad,
cell_area->y + y_offset + cell->ypad,
width * cellprogress->progress,
height - 1);
}

View File

@ -0,0 +1,55 @@
/***********************************************************
*
* custom-cell-renderer-progressbar.h
*
* part of the Gtk+ tree view tutorial
*
* by Tim-Philipp Mueller < tim at centricular dot net >
*
***********************************************************/
#ifndef _custom_cell_renderer_progressbar_included_
#define _custom_cell_renderer_progressbar_included_
#include <gtk/gtk.h>
/* Some boilerplate GObject type check and type cast macros.
* 'klass' is used here instead of 'class', because 'class'
* is a c++ keyword */
#define CUSTOM_TYPE_CELL_RENDERER_PROGRESS (custom_cell_renderer_progress_get_type())
#define CUSTOM_CELL_RENDERER_PROGRESS(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), CUSTOM_TYPE_CELL_RENDERER_PROGRESS, CustomCellRendererProgress))
#define CUSTOM_CELL_RENDERER_PROGRESS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CUSTOM_TYPE_CELL_RENDERER_PROGRESS, CustomCellRendererProgressClass))
#define CUSTOM_IS_CELL_PROGRESS_PROGRESS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CUSTOM_TYPE_CELL_RENDERER_PROGRESS))
#define CUSTOM_IS_CELL_PROGRESS_PROGRESS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CUSTOM_TYPE_CELL_RENDERER_PROGRESS))
#define CUSTOM_CELL_RENDERER_PROGRESS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CUSTOM_TYPE_CELL_RENDERER_PROGRESS, CustomCellRendererProgressClass))
typedef struct _CustomCellRendererProgress CustomCellRendererProgress;
typedef struct _CustomCellRendererProgressClass CustomCellRendererProgressClass;
/* CustomCellRendererProgress: Our custom cell renderer
* structure. Extend according to need */
struct _CustomCellRendererProgress
{
GtkCellRenderer parent;
gdouble progress;
};
struct _CustomCellRendererProgressClass
{
GtkCellRendererClass parent_class;
};
GType custom_cell_renderer_progress_get_type (void);
GtkCellRenderer *custom_cell_renderer_progress_new (void);
#endif /* _custom_cell_renderer_progressbar_included_ */

View File

@ -0,0 +1,114 @@
/***********************************************************
*
* main.c - testing CustomCellRendererProgress
*
* part of the Gtk+ tree view tutorial
*
* by Tim-Philipp Mueller < tim at centricular dot net >
*
***********************************************************/
#include "custom-cell-renderer-progressbar.h"
static GtkListStore *liststore;
static gboolean increasing = TRUE; /* direction of progress bar change */
enum
{
COL_PERCENTAGE = 0,
COL_TEXT,
NUM_COLS
};
#define STEP 0.01
gboolean
increase_progress_timeout (GtkCellRenderer *renderer)
{
GtkTreeIter iter;
gfloat perc = 0.0;
gchar buf[20];
gtk_tree_model_get_iter_first(GTK_TREE_MODEL(liststore), &iter); /* first and only row */
gtk_tree_model_get (GTK_TREE_MODEL(liststore), &iter, COL_PERCENTAGE, &perc, -1);
if ( perc > (1.0-STEP) || (perc < STEP && perc > 0.0) )
{
increasing = (!increasing);
}
if (increasing)
perc = perc + STEP;
else
perc = perc - STEP;
g_snprintf(buf, sizeof(buf), "%u %%", (guint)(perc*100));
gtk_list_store_set (liststore, &iter, COL_PERCENTAGE, perc, COL_TEXT, buf, -1);
return TRUE; /* Call again */
}
GtkWidget *
create_view_and_model (void)
{
GtkTreeViewColumn *col;
GtkCellRenderer *renderer;
GtkTreeIter iter;
GtkWidget *view;
liststore = gtk_list_store_new(NUM_COLS, G_TYPE_FLOAT, G_TYPE_STRING);
gtk_list_store_append(liststore, &iter);
gtk_list_store_set (liststore, &iter, COL_PERCENTAGE, 0.5, -1); /* start at 50% */
view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(liststore));
g_object_unref(liststore); /* destroy store automatically with view */
renderer = gtk_cell_renderer_text_new();
col = gtk_tree_view_column_new();
gtk_tree_view_column_pack_start (col, renderer, TRUE);
gtk_tree_view_column_add_attribute (col, renderer, "text", COL_TEXT);
gtk_tree_view_column_set_title (col, "Progress");
gtk_tree_view_append_column(GTK_TREE_VIEW(view),col);
renderer = custom_cell_renderer_progress_new();
col = gtk_tree_view_column_new();
gtk_tree_view_column_pack_start (col, renderer, TRUE);
gtk_tree_view_column_add_attribute (col, renderer, "percentage", COL_PERCENTAGE);
gtk_tree_view_column_set_title (col, "Progress");
gtk_tree_view_append_column(GTK_TREE_VIEW(view),col);
g_timeout_add(50, (GSourceFunc) increase_progress_timeout, NULL);
return view;
}
int
main (int argc, char **argv)
{
GtkWidget *window, *view;
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW(window), 150, 100);
g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
view = create_view_and_model();
gtk_container_add(GTK_CONTAINER(window), view);
gtk_widget_show_all(window);
gtk_main();
return 0;
}

View File

@ -0,0 +1,12 @@
CC = gcc
OBJS = main.o custom-list.o
CFLAGS = -g -O2 `pkg-config --cflags gtk+-2.0`
customlist-sorted: $(OBJS)
gcc -o customlist-sorted $(OBJS) `pkg-config --libs gtk+-2.0`
clean:
rm $(OBJS) customlist-sorted 2>/dev/null || /bin/true

View File

@ -0,0 +1,951 @@
/***********************************************************
*
* custom-list.c
*
* A simple custom list model with sorting
*
* part of the Gtk+ tree view tutorial
*
* by Tim-Philipp Mueller < tim at centricular dot net >
*
***********************************************************/
#include "custom-list.h"
/* boring declarations of local functions */
static void custom_list_init (CustomList *pkg_tree);
static void custom_list_class_init (CustomListClass *klass);
static void custom_list_tree_model_init (GtkTreeModelIface *iface);
static void custom_list_finalize (GObject *object);
static GtkTreeModelFlags custom_list_get_flags (GtkTreeModel *tree_model);
static gint custom_list_get_n_columns (GtkTreeModel *tree_model);
static GType custom_list_get_column_type (GtkTreeModel *tree_model,
gint index);
static gboolean custom_list_get_iter (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreePath *path);
static GtkTreePath *custom_list_get_path (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static void custom_list_get_value (GtkTreeModel *tree_model,
GtkTreeIter *iter,
gint column,
GValue *value);
static gboolean custom_list_iter_next (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gboolean custom_list_iter_children (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent);
static gboolean custom_list_iter_has_child (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gint custom_list_iter_n_children (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gboolean custom_list_iter_nth_child (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent,
gint n);
static gboolean custom_list_iter_parent (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *child);
/* -- GtkTreeSortable interface functions -- */
static void custom_list_sortable_init (GtkTreeSortableIface *iface);
static gboolean custom_list_sortable_get_sort_column_id (GtkTreeSortable *sortable,
gint *sort_col_id,
GtkSortType *order);
static void custom_list_sortable_set_sort_column_id (GtkTreeSortable *sortable,
gint sort_col_id,
GtkSortType order);
static void custom_list_sortable_set_sort_func (GtkTreeSortable *sortable,
gint sort_col_id,
GtkTreeIterCompareFunc sort_func,
gpointer user_data,
GtkDestroyNotify destroy_func);
static void custom_list_sortable_set_default_sort_func (GtkTreeSortable *sortable,
GtkTreeIterCompareFunc sort_func,
gpointer user_data,
GtkDestroyNotify destroy_func);
static gboolean custom_list_sortable_has_default_sort_func (GtkTreeSortable *sortable);
static void custom_list_resort (CustomList *custom_list);
static GObjectClass *parent_class = NULL; /* GObject stuff - nothing to worry about */
/*****************************************************************************
*
* custom_list_get_type: here we register our new type and its interfaces
* with the type system. If you want to implement
* additional interfaces, you will need to do it here.
*
*****************************************************************************/
GType
custom_list_get_type (void)
{
static GType custom_list_type = 0;
if (custom_list_type)
return custom_list_type;
/* Some boilerplate type registration stuff */
if (1)
{
static const GTypeInfo custom_list_info =
{
sizeof (CustomListClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) custom_list_class_init,
NULL, /* class finalize */
NULL, /* class_data */
sizeof (CustomList),
0, /* n_preallocs */
(GInstanceInitFunc) custom_list_init
};
custom_list_type = g_type_register_static (G_TYPE_OBJECT, "CustomList",
&custom_list_info, (GTypeFlags)0);
}
/* Here we register our GtkTreeModel interface with the type system */
if (1)
{
static const GInterfaceInfo tree_model_info =
{
(GInterfaceInitFunc) custom_list_tree_model_init,
NULL,
NULL
};
g_type_add_interface_static (custom_list_type, GTK_TYPE_TREE_MODEL, &tree_model_info);
}
/* Add GtkTreeSortable interface */
if (1)
{
static const GInterfaceInfo tree_sortable_info =
{
(GInterfaceInitFunc) custom_list_sortable_init,
NULL,
NULL
};
g_type_add_interface_static (custom_list_type, GTK_TYPE_TREE_SORTABLE, &tree_sortable_info);
}
return custom_list_type;
}
/*****************************************************************************
*
* custom_list_class_init: more boilerplate GObject/GType stuff.
* Init callback for the type system,
* called once when our new class is created.
*
*****************************************************************************/
static void
custom_list_class_init (CustomListClass *klass)
{
GObjectClass *object_class;
parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
object_class = (GObjectClass*) klass;
object_class->finalize = custom_list_finalize;
}
/*****************************************************************************
*
* custom_list_tree_model_init: init callback for the interface registration
* in custom_list_get_type. Here we override
* the GtkTreeModel interface functions that
* we implement.
*
*****************************************************************************/
static void
custom_list_tree_model_init (GtkTreeModelIface *iface)
{
iface->get_flags = custom_list_get_flags;
iface->get_n_columns = custom_list_get_n_columns;
iface->get_column_type = custom_list_get_column_type;
iface->get_iter = custom_list_get_iter;
iface->get_path = custom_list_get_path;
iface->get_value = custom_list_get_value;
iface->iter_next = custom_list_iter_next;
iface->iter_children = custom_list_iter_children;
iface->iter_has_child = custom_list_iter_has_child;
iface->iter_n_children = custom_list_iter_n_children;
iface->iter_nth_child = custom_list_iter_nth_child;
iface->iter_parent = custom_list_iter_parent;
}
/*****************************************************************************
*
* custom_list_sortable_init: init callback for the interface registration
* in custom_list_get_type. Here we override
* the GtkTreeSortable interface functions that
* we implement.
*
*****************************************************************************/
static void
custom_list_sortable_init (GtkTreeSortableIface *iface)
{
iface->get_sort_column_id = custom_list_sortable_get_sort_column_id;
iface->set_sort_column_id = custom_list_sortable_set_sort_column_id;
iface->set_sort_func = custom_list_sortable_set_sort_func; /* NOT SUPPORTED */
iface->set_default_sort_func = custom_list_sortable_set_default_sort_func; /* NOT SUPPORTED */
iface->has_default_sort_func = custom_list_sortable_has_default_sort_func; /* NOT SUPPORTED */
}
/*****************************************************************************
*
* custom_list_init: this is called everytime a new custom list object
* instance is created (we do that in custom_list_new).
* Initialise your list structure's fields here.
*
*****************************************************************************/
static void
custom_list_init (CustomList *custom_list)
{
custom_list->n_columns = CUSTOM_LIST_N_COLUMNS;
custom_list->column_types[0] = G_TYPE_POINTER; /* CUSTOM_LIST_COL_RECORD */
custom_list->column_types[1] = G_TYPE_STRING; /* CUSTOM_LIST_COL_NAME */
custom_list->column_types[2] = G_TYPE_UINT; /* CUSTOM_LIST_COL_YEAR_BORN */
g_assert ( CUSTOM_LIST_N_COLUMNS == 3 );
custom_list->num_rows = 0;
custom_list->rows = NULL;
custom_list->stamp = g_random_int(); /* Random int to check whether iters belong to out model */
custom_list->sort_id = SORT_ID_NONE;
custom_list->sort_order = GTK_SORT_ASCENDING;
}
/*****************************************************************************
*
* custom_list_finalize: this is called just before a custom list is
* destroyed. Free dynamically allocated memory here.
*
*****************************************************************************/
static void
custom_list_finalize (GObject *object)
{
/* CustomList *custom_list = CUSTOM_LIST(object); */
/* free all records and free all memory used by the list */
#warning IMPLEMENT
/* must chain up - finalize parent */
(* parent_class->finalize) (object);
}
/*****************************************************************************
*
* custom_list_get_flags: tells the rest of the world whether our tree model
* has any special characteristics. In our case,
* we have a list model (instead of a tree), and each
* tree iter is valid as long as the row in question
* exists, as it only contains a pointer to our struct.
*
*****************************************************************************/
static GtkTreeModelFlags
custom_list_get_flags (GtkTreeModel *tree_model)
{
g_return_val_if_fail (CUSTOM_IS_LIST(tree_model), (GtkTreeModelFlags)0);
return (GTK_TREE_MODEL_LIST_ONLY | GTK_TREE_MODEL_ITERS_PERSIST);
}
/*****************************************************************************
*
* custom_list_get_n_columns: tells the rest of the world how many data
* columns we export via the tree model interface
*
*****************************************************************************/
static gint
custom_list_get_n_columns (GtkTreeModel *tree_model)
{
g_return_val_if_fail (CUSTOM_IS_LIST(tree_model), 0);
return CUSTOM_LIST(tree_model)->n_columns;
}
/*****************************************************************************
*
* custom_list_get_column_type: tells the rest of the world which type of
* data an exported model column contains
*
*****************************************************************************/
static GType
custom_list_get_column_type (GtkTreeModel *tree_model,
gint index)
{
g_return_val_if_fail (CUSTOM_IS_LIST(tree_model), G_TYPE_INVALID);
g_return_val_if_fail (index < CUSTOM_LIST(tree_model)->n_columns && index >= 0, G_TYPE_INVALID);
return CUSTOM_LIST(tree_model)->column_types[index];
}
/*****************************************************************************
*
* custom_list_get_iter: converts a tree path (physical position) into a
* tree iter structure (the content of the iter
* fields will only be used internally by our model).
* We simply store a pointer to our CustomRecord
* structure that represents that row in the tree iter.
*
*****************************************************************************/
static gboolean
custom_list_get_iter (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreePath *path)
{
CustomList *custom_list;
CustomRecord *record;
gint *indices, n, depth;
g_assert(CUSTOM_IS_LIST(tree_model));
g_assert(path!=NULL);
custom_list = CUSTOM_LIST(tree_model);
indices = gtk_tree_path_get_indices(path);
depth = gtk_tree_path_get_depth(path);
/* we do not allow children */
g_assert(depth == 1); /* depth 1 = top level; a list only has top level nodes and no children */
n = indices[0]; /* the n-th top level row */
if ( n >= custom_list->num_rows || n < 0 )
return FALSE;
record = custom_list->rows[n];
g_assert(record != NULL);
g_assert(record->pos == n);
/* We simply store a pointer to our custom record in the iter */
iter->stamp = custom_list->stamp;
iter->user_data = record;
iter->user_data2 = NULL; /* unused */
iter->user_data3 = NULL; /* unused */
return TRUE;
}
/*****************************************************************************
*
* custom_list_get_path: converts a tree iter into a tree path (ie. the
* physical position of that row in the list).
*
*****************************************************************************/
static GtkTreePath *
custom_list_get_path (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
GtkTreePath *path;
CustomRecord *record;
CustomList *custom_list;
g_return_val_if_fail (CUSTOM_IS_LIST(tree_model), NULL);
g_return_val_if_fail (iter != NULL, NULL);
g_return_val_if_fail (iter->user_data != NULL, NULL);
custom_list = CUSTOM_LIST(tree_model);
record = (CustomRecord*) iter->user_data;
path = gtk_tree_path_new();
gtk_tree_path_append_index(path, record->pos);
return path;
}
/*****************************************************************************
*
* custom_list_get_value: Returns a row's exported data columns
* (_get_value is what gtk_tree_model_get uses)
*
*****************************************************************************/
static void
custom_list_get_value (GtkTreeModel *tree_model,
GtkTreeIter *iter,
gint column,
GValue *value)
{
CustomRecord *record;
CustomList *custom_list;
g_return_if_fail (CUSTOM_IS_LIST (tree_model));
g_return_if_fail (iter != NULL);
g_return_if_fail (column < CUSTOM_LIST(tree_model)->n_columns);
g_value_init (value, CUSTOM_LIST(tree_model)->column_types[column]);
custom_list = CUSTOM_LIST(tree_model);
record = (CustomRecord*) iter->user_data;
g_return_if_fail ( record != NULL );
if(record->pos >= custom_list->num_rows)
g_return_if_reached();
switch(column)
{
case CUSTOM_LIST_COL_RECORD:
g_value_set_pointer(value, record);
break;
case CUSTOM_LIST_COL_NAME:
g_value_set_string(value, record->name);
break;
case CUSTOM_LIST_COL_YEAR_BORN:
g_value_set_uint(value, record->year_born);
break;
}
}
/*****************************************************************************
*
* custom_list_iter_next: Takes an iter structure and sets it to point
* to the next row.
*
*****************************************************************************/
static gboolean
custom_list_iter_next (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
CustomRecord *record, *nextrecord;
CustomList *custom_list;
g_return_val_if_fail (CUSTOM_IS_LIST (tree_model), FALSE);
if (iter == NULL || iter->user_data == NULL)
return FALSE;
custom_list = CUSTOM_LIST(tree_model);
record = (CustomRecord *) iter->user_data;
/* Is this the last record in the list? */
if ((record->pos + 1) >= custom_list->num_rows)
return FALSE;
nextrecord = custom_list->rows[(record->pos + 1)];
g_assert ( nextrecord != NULL );
g_assert ( nextrecord->pos == (record->pos + 1) );
iter->stamp = custom_list->stamp;
iter->user_data = nextrecord;
return TRUE;
}
/*****************************************************************************
*
* custom_list_iter_children: Returns TRUE or FALSE depending on whether
* the row specified by 'parent' has any children.
* If it has children, then 'iter' is set to
* point to the first child. Special case: if
* 'parent' is NULL, then the first top-level
* row should be returned if it exists.
*
*****************************************************************************/
static gboolean
custom_list_iter_children (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent)
{
CustomList *custom_list;
g_return_val_if_fail (parent == NULL || parent->user_data != NULL, FALSE);
/* this is a list, nodes have no children */
if (parent)
return FALSE;
/* parent == NULL is a special case; we need to return the first top-level row */
g_return_val_if_fail (CUSTOM_IS_LIST (tree_model), FALSE);
custom_list = CUSTOM_LIST(tree_model);
/* No rows => no first row */
if (custom_list->num_rows == 0)
return FALSE;
/* Set iter to first item in list */
iter->stamp = custom_list->stamp;
iter->user_data = custom_list->rows[0];
return TRUE;
}
/*****************************************************************************
*
* custom_list_iter_has_child: Returns TRUE or FALSE depending on whether
* the row specified by 'iter' has any children.
* We only have a list and thus no children.
*
*****************************************************************************/
static gboolean
custom_list_iter_has_child (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
return FALSE;
}
/*****************************************************************************
*
* custom_list_iter_n_children: Returns the number of children the row
* specified by 'iter' has. This is usually 0,
* as we only have a list and thus do not have
* any children to any rows. A special case is
* when 'iter' is NULL, in which case we need
* to return the number of top-level nodes,
* ie. the number of rows in our list.
*
*****************************************************************************/
static gint
custom_list_iter_n_children (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
CustomList *custom_list;
g_return_val_if_fail (CUSTOM_IS_LIST (tree_model), -1);
g_return_val_if_fail (iter == NULL || iter->user_data != NULL, FALSE);
custom_list = CUSTOM_LIST(tree_model);
/* special case: if iter == NULL, return number of top-level rows */
if (!iter)
return custom_list->num_rows;
return 0; /* otherwise, this is easy again for a list */
}
/*****************************************************************************
*
* custom_list_iter_nth_child: If the row specified by 'parent' has any
* children, set 'iter' to the n-th child and
* return TRUE if it exists, otherwise FALSE.
* A special case is when 'parent' is NULL, in
* which case we need to set 'iter' to the n-th
* row if it exists.
*
*****************************************************************************/
static gboolean
custom_list_iter_nth_child (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent,
gint n)
{
CustomRecord *record;
CustomList *custom_list;
g_return_val_if_fail (CUSTOM_IS_LIST (tree_model), FALSE);
custom_list = CUSTOM_LIST(tree_model);
/* a list has only top-level rows */
if(parent)
return FALSE;
/* special case: if parent == NULL, set iter to n-th top-level row */
if( n >= custom_list->num_rows )
return FALSE;
record = custom_list->rows[n];
g_assert( record != NULL );
g_assert( record->pos == n );
iter->stamp = custom_list->stamp;
iter->user_data = record;
return TRUE;
}
/*****************************************************************************
*
* custom_list_iter_parent: Point 'iter' to the parent node of 'child'. As
* we have a list and thus no children and no
* parents of children, we can just return FALSE.
*
*****************************************************************************/
static gboolean
custom_list_iter_parent (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *child)
{
return FALSE;
}
/*****************************************************************************
*
* custom_list_sortable_get_sort_column_id
*
*****************************************************************************/
static gboolean
custom_list_sortable_get_sort_column_id (GtkTreeSortable *sortable,
gint *sort_col_id,
GtkSortType *order)
{
CustomList *custom_list;
g_return_val_if_fail ( sortable != NULL , FALSE );
g_return_val_if_fail ( CUSTOM_IS_LIST(sortable), FALSE );
custom_list = CUSTOM_LIST(sortable);
if (sort_col_id)
*sort_col_id = custom_list->sort_id;
if (order)
*order = custom_list->sort_order;
return TRUE;
}
/*****************************************************************************
*
* custom_list_sortable_set_sort_column_id
*
*****************************************************************************/
static void
custom_list_sortable_set_sort_column_id (GtkTreeSortable *sortable,
gint sort_col_id,
GtkSortType order)
{
CustomList *custom_list;
g_return_if_fail ( sortable != NULL );
g_return_if_fail ( CUSTOM_IS_LIST(sortable) );
custom_list = CUSTOM_LIST(sortable);
if (custom_list->sort_id == sort_col_id && custom_list->sort_order == order)
return;
custom_list->sort_id = sort_col_id;
custom_list->sort_order = order;
custom_list_resort(custom_list);
}
/*****************************************************************************
*
* custom_list_sortable_set_sort_func: We have our own built-in sort function
*
*****************************************************************************/
static void
custom_list_sortable_set_sort_func (GtkTreeSortable *sortable,
gint sort_col_id,
GtkTreeIterCompareFunc sort_func,
gpointer user_data,
GtkDestroyNotify destroy_func)
{
g_warning ("%s is not supported by the CustomList model.\n", __FUNCTION__);
}
/*****************************************************************************
*
* custom_list_sortable_set_default_sort_func: We have our own built-in
* sort function
*
*****************************************************************************/
static void
custom_list_sortable_set_default_sort_func (GtkTreeSortable *sortable,
GtkTreeIterCompareFunc sort_func,
gpointer user_data,
GtkDestroyNotify destroy_func)
{
g_warning ("%s is not supported by the CustomList model.\n", __FUNCTION__);
}
/*****************************************************************************
*
* custom_list_sortable_has_default_sort_func
*
*****************************************************************************/
static gboolean
custom_list_sortable_has_default_sort_func (GtkTreeSortable *sortable)
{
return FALSE;
}
/*****************************************************************************
*
* custom_list_compare_records
*
*****************************************************************************/
static gint
custom_list_compare_records (gint sort_id, CustomRecord *a, CustomRecord *b)
{
switch(sort_id)
{
case SORT_ID_NONE:
return 0;
case SORT_ID_NAME:
{
if ((a->name) && (b->name))
return g_utf8_collate(a->name, b->name);
if (a->name == b->name)
return 0; /* both are NULL */
else
return (a->name == NULL) ? -1 : 1;
}
case SORT_ID_YEAR_BORN:
{
if (a->year_born == b->year_born)
return 0;
return (a->year_born > b->year_born) ? 1 : -1;
}
}
g_return_val_if_reached(0);
}
/*****************************************************************************
*
* custom_list_qsort_compare_func
*
*****************************************************************************/
static gint
custom_list_qsort_compare_func (CustomRecord **a, CustomRecord **b, CustomList *custom_list)
{
gint ret;
g_assert ((a) && (b) && (custom_list));
ret = custom_list_compare_records(custom_list->sort_id, *a, *b);
/* Swap -1 and 1 if sort order is reverse */
if (ret != 0 && custom_list->sort_order == GTK_SORT_DESCENDING)
ret = (ret < 0) ? 1 : -1;
return ret;
}
/*****************************************************************************
*
* custom_list_resort: resort the list. Pay attention to how we emit the
* "rows-reordered" signal after we have resorted the
* list. This function could be optimised to speed up
* mass insertions via a timeout or something.
*
*****************************************************************************/
static void
custom_list_resort (CustomList *custom_list)
{
GtkTreePath *path;
gint *neworder, i;
g_return_if_fail ( custom_list != NULL );
g_return_if_fail ( CUSTOM_IS_LIST(custom_list) );
if (custom_list->sort_id == SORT_ID_NONE)
return;
if (custom_list->num_rows == 0)
return;
/* resort */
g_qsort_with_data(custom_list->rows,
custom_list->num_rows,
sizeof(CustomRecord*),
(GCompareDataFunc) custom_list_qsort_compare_func,
custom_list);
/* let other objects know about the new order */
neworder = g_new0(gint, custom_list->num_rows);
/* NOTE that the API reference for
* gtk_tree_model_rows_reordered()
* might be wrong here, see bug
* number 124790 on bugs.gnome.org.
* Both will work, but one will give
* you 'jumpy' selections after a
* reordering */
for (i = 0; i < custom_list->num_rows; ++i)
{
/* neworder[(custom_list->rows[i])->pos] = i; */
neworder[i] = (custom_list->rows[i])->pos;
(custom_list->rows[i])->pos = i;
}
path = gtk_tree_path_new();
gtk_tree_model_rows_reordered(GTK_TREE_MODEL(custom_list), path, NULL, neworder);
gtk_tree_path_free(path);
g_free(neworder);
}
/*****************************************************************************
*
* custom_list_new: This is what you use in your own code to create a
* new custom list tree model for you to use.
*
*****************************************************************************/
CustomList *
custom_list_new (void)
{
CustomList *newcustomlist;
newcustomlist = (CustomList*) g_object_new (CUSTOM_TYPE_LIST, NULL);
g_assert( newcustomlist != NULL );
return newcustomlist;
}
/*****************************************************************************
*
* custom_list_append_record: Empty lists are boring. This function can
* be used in your own code to add rows to the
* list. Note how we emit the "row-inserted"
* signal after we have appended the row
* internally, so the tree view and other
* interested objects know about the new row.
*
*****************************************************************************/
void
custom_list_append_record (CustomList *custom_list,
const gchar *name,
guint year_born)
{
GtkTreeIter iter;
GtkTreePath *path;
CustomRecord *newrecord;
gulong newsize;
guint pos;
g_return_if_fail (CUSTOM_IS_LIST(custom_list));
g_return_if_fail (name != NULL);
pos = custom_list->num_rows;
custom_list->num_rows++;
newsize = custom_list->num_rows * sizeof(CustomRecord*);
custom_list->rows = g_realloc(custom_list->rows, newsize);
newrecord = g_new0(CustomRecord, 1);
newrecord->name = g_strdup(name);
newrecord->name_collate_key = g_utf8_collate_key(name,-1); /* for fast sorting, used later */
newrecord->year_born = year_born;
custom_list->rows[pos] = newrecord;
newrecord->pos = pos;
/* inform the tree view and other interested objects
* (e.g. tree row references) that we have inserted
* a new row, and where it was inserted */
path = gtk_tree_path_new();
gtk_tree_path_append_index(path, newrecord->pos);
custom_list_get_iter(GTK_TREE_MODEL(custom_list), &iter, path);
gtk_tree_model_row_inserted(GTK_TREE_MODEL(custom_list), path, &iter);
gtk_tree_path_free(path);
}

View File

@ -0,0 +1,107 @@
/***********************************************************
*
* custom-list.h
*
* A simple custom list model with sorting
*
* part of the Gtk+ tree view tutorial
*
* by Tim-Philipp Mueller < tim at centricular dot net >
*
***********************************************************/
#ifndef _custom_list_h_included_
#define _custom_list_h_included_
#include <gtk/gtk.h>
/* Some boilerplate GObject defines. 'klass' is used instead of 'class', because 'class' is a C++ keyword */
#define CUSTOM_TYPE_LIST (custom_list_get_type ())
#define CUSTOM_LIST(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CUSTOM_TYPE_LIST, CustomList))
#define CUSTOM_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CUSTOM_TYPE_LIST, CustomListClass))
#define CUSTOM_IS_LIST(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CUSTOM_TYPE_LIST))
#define CUSTOM_IS_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CUSTOM_TYPE_LIST))
#define CUSTOM_LIST_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CUSTOM_TYPE_LIST, CustomListClass))
/* The data columns that we export via the tree model interface */
enum
{
CUSTOM_LIST_COL_RECORD = 0,
CUSTOM_LIST_COL_NAME,
CUSTOM_LIST_COL_YEAR_BORN,
CUSTOM_LIST_N_COLUMNS,
} ;
enum
{
SORT_ID_NONE = 0,
SORT_ID_NAME,
SORT_ID_YEAR_BORN,
};
typedef struct _CustomRecord CustomRecord;
typedef struct _CustomList CustomList;
typedef struct _CustomListClass CustomListClass;
/* CustomRecord: this structure represents a row */
struct _CustomRecord
{
/* data - you can extend this */
gchar *name;
gchar *name_collate_key;
guint year_born;
/* admin stuff used by the custom list model */
guint pos; /* pos within the array */
};
/* CustomList: this structure contains everything we need for our
* model implementation. You can add extra fields to
* this structure, e.g. hashtables to quickly lookup
* rows or whatever else you might need, but it is
* crucial that 'parent' is the first member of the
* structure. */
struct _CustomList
{
GObject parent; /* this MUST be the first member */
guint num_rows; /* number of rows that we have */
CustomRecord **rows; /* a dynamically allocated array of pointers to the CustomRecord structure for each row */
gint n_columns; /* These two fields are not absolutely necessary, but they */
GType column_types[CUSTOM_LIST_N_COLUMNS]; /* speed things up a bit in our get_value implementation */
gint sort_id;
GtkSortType sort_order;
gint stamp; /* A random integer to check if an iter belongs to our model */
};
/* CustomListClass: more boilerplate GObject stuff */
struct _CustomListClass
{
GObjectClass parent_class;
};
GType custom_list_get_type (void);
CustomList *custom_list_new (void);
void custom_list_append_record (CustomList *custom_list,
const gchar *name,
guint year_born);
#endif /* _custom_list_h_included_ */

View File

@ -0,0 +1,98 @@
/***********************************************************
*
* main.c - testing our sorted CustomList tree model
*
* A simple custom list model with sorting
*
* part of the Gtk+ tree view tutorial
*
* by Tim-Philipp Mueller < tim at centricular dot net >
*
***********************************************************/
#include "custom-list.h"
#include <stdlib.h>
void
fill_model (CustomList *customlist)
{
const gchar *firstnames[] = { "Joe", "Jane", "William", "Hannibal", "Timothy", "Gargamel", NULL } ;
const gchar *surnames[] = { "Grokowich", "Twitch", "Borheimer", "Bork", NULL } ;
const gchar **fname, **sname;
for (sname = surnames; *sname != NULL; sname++)
{
for (fname = firstnames; *fname != NULL; fname++)
{
gchar *name = g_strdup_printf ("%s %s", *fname, *sname);
custom_list_append_record (customlist, name, 1900 + (guint) (103.0*rand()/(RAND_MAX+1900.0)));
g_free(name);
}
}
}
GtkWidget *
create_view_and_model (void)
{
GtkTreeViewColumn *col;
GtkTreeSelection *sel;
GtkCellRenderer *renderer;
CustomList *customlist;
GtkWidget *view;
customlist = custom_list_new();
fill_model(customlist);
view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(customlist));
g_object_unref(customlist); /* destroy model automatically with view */
renderer = gtk_cell_renderer_text_new();
col = gtk_tree_view_column_new();
gtk_tree_view_column_pack_start (col, renderer, TRUE);
gtk_tree_view_column_add_attribute (col, renderer, "text", CUSTOM_LIST_COL_NAME);
gtk_tree_view_column_set_title (col, "Name");
gtk_tree_view_column_set_sort_column_id(col, SORT_ID_NAME);
gtk_tree_view_append_column(GTK_TREE_VIEW(view),col);
renderer = gtk_cell_renderer_text_new();
col = gtk_tree_view_column_new();
gtk_tree_view_column_pack_start (col, renderer, TRUE);
gtk_tree_view_column_add_attribute (col, renderer, "text", CUSTOM_LIST_COL_YEAR_BORN);
gtk_tree_view_column_set_title (col, "Year Born");
gtk_tree_view_column_set_sort_column_id(col, SORT_ID_YEAR_BORN);
gtk_tree_view_append_column(GTK_TREE_VIEW(view),col);
sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
gtk_tree_selection_set_mode(sel, GTK_SELECTION_MULTIPLE);
return view;
}
int
main (int argc, char **argv)
{
GtkWidget *window, *view, *scrollwin;
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW(window), 200, 400);
g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
scrollwin = gtk_scrolled_window_new(NULL,NULL);
view = create_view_and_model();
gtk_container_add(GTK_CONTAINER(scrollwin), view);
gtk_container_add(GTK_CONTAINER(window), scrollwin);
gtk_widget_show_all(window);
gtk_main();
return 0;
}

View File

@ -0,0 +1,12 @@
CC = gcc
OBJS = main.o custom-list.o
CFLAGS = -g -O2 `pkg-config --cflags gtk+-2.0`
customlist: $(OBJS)
gcc -o customlist $(OBJS) `pkg-config --libs gtk+-2.0`
clean:
rm $(OBJS) customlist 2>/dev/null || /bin/true

View File

@ -0,0 +1,664 @@
/***********************************************************
*
* custom-list.c
*
* A simple custom list model
*
* part of the Gtk+ tree view tutorial
*
* by Tim-Philipp Mueller < tim at centricular dot net >
*
***********************************************************/
#include "custom-list.h"
/* boring declarations of local functions */
static void custom_list_init (CustomList *pkg_tree);
static void custom_list_class_init (CustomListClass *klass);
static void custom_list_tree_model_init (GtkTreeModelIface *iface);
static void custom_list_finalize (GObject *object);
static GtkTreeModelFlags custom_list_get_flags (GtkTreeModel *tree_model);
static gint custom_list_get_n_columns (GtkTreeModel *tree_model);
static GType custom_list_get_column_type (GtkTreeModel *tree_model,
gint index);
static gboolean custom_list_get_iter (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreePath *path);
static GtkTreePath *custom_list_get_path (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static void custom_list_get_value (GtkTreeModel *tree_model,
GtkTreeIter *iter,
gint column,
GValue *value);
static gboolean custom_list_iter_next (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gboolean custom_list_iter_children (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent);
static gboolean custom_list_iter_has_child (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gint custom_list_iter_n_children (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gboolean custom_list_iter_nth_child (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent,
gint n);
static gboolean custom_list_iter_parent (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *child);
static GObjectClass *parent_class = NULL; /* GObject stuff - nothing to worry about */
/*****************************************************************************
*
* custom_list_get_type: here we register our new type and its interfaces
* with the type system. If you want to implement
* additional interfaces like GtkTreeSortable, you
* will need to do it here.
*
*****************************************************************************/
GType
custom_list_get_type (void)
{
static GType custom_list_type = 0;
if (custom_list_type)
return custom_list_type;
/* Some boilerplate type registration stuff */
if (1)
{
static const GTypeInfo custom_list_info =
{
sizeof (CustomListClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) custom_list_class_init,
NULL, /* class finalize */
NULL, /* class_data */
sizeof (CustomList),
0, /* n_preallocs */
(GInstanceInitFunc) custom_list_init
};
custom_list_type = g_type_register_static (G_TYPE_OBJECT, "CustomList",
&custom_list_info, (GTypeFlags)0);
}
/* Here we register our GtkTreeModel interface with the type system */
if (1)
{
static const GInterfaceInfo tree_model_info =
{
(GInterfaceInitFunc) custom_list_tree_model_init,
NULL,
NULL
};
g_type_add_interface_static (custom_list_type, GTK_TYPE_TREE_MODEL, &tree_model_info);
}
return custom_list_type;
}
/*****************************************************************************
*
* custom_list_class_init: more boilerplate GObject/GType stuff.
* Init callback for the type system,
* called once when our new class is created.
*
*****************************************************************************/
static void
custom_list_class_init (CustomListClass *klass)
{
GObjectClass *object_class;
parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
object_class = (GObjectClass*) klass;
object_class->finalize = custom_list_finalize;
}
/*****************************************************************************
*
* custom_list_tree_model_init: init callback for the interface registration
* in custom_list_get_type. Here we override
* the GtkTreeModel interface functions that
* we implement.
*
*****************************************************************************/
static void
custom_list_tree_model_init (GtkTreeModelIface *iface)
{
iface->get_flags = custom_list_get_flags;
iface->get_n_columns = custom_list_get_n_columns;
iface->get_column_type = custom_list_get_column_type;
iface->get_iter = custom_list_get_iter;
iface->get_path = custom_list_get_path;
iface->get_value = custom_list_get_value;
iface->iter_next = custom_list_iter_next;
iface->iter_children = custom_list_iter_children;
iface->iter_has_child = custom_list_iter_has_child;
iface->iter_n_children = custom_list_iter_n_children;
iface->iter_nth_child = custom_list_iter_nth_child;
iface->iter_parent = custom_list_iter_parent;
}
/*****************************************************************************
*
* custom_list_init: this is called everytime a new custom list object
* instance is created (we do that in custom_list_new).
* Initialise the list structure's fields here.
*
*****************************************************************************/
static void
custom_list_init (CustomList *custom_list)
{
custom_list->n_columns = CUSTOM_LIST_N_COLUMNS;
custom_list->column_types[0] = G_TYPE_POINTER; /* CUSTOM_LIST_COL_RECORD */
custom_list->column_types[1] = G_TYPE_STRING; /* CUSTOM_LIST_COL_NAME */
custom_list->column_types[2] = G_TYPE_UINT; /* CUSTOM_LIST_COL_YEAR_BORN */
g_assert ( CUSTOM_LIST_N_COLUMNS == 3 );
custom_list->num_rows = 0;
custom_list->rows = NULL;
custom_list->stamp = g_random_int(); /* Random int to check whether iters belong to out model */
}
/*****************************************************************************
*
* custom_list_finalize: this is called just before a custom list is
* destroyed. Free dynamically allocated memory here.
*
*****************************************************************************/
static void
custom_list_finalize (GObject *object)
{
/* CustomList *custom_list = CUSTOM_LIST(object); */
/* free all records and free all memory used by the list */
#warning IMPLEMENT
/* must chain up - finalize parent */
(* parent_class->finalize) (object);
}
/*****************************************************************************
*
* custom_list_get_flags: tells the rest of the world whether our tree model
* has any special characteristics. In our case,
* we have a list model (instead of a tree), and each
* tree iter is valid as long as the row in question
* exists, as it only contains a pointer to our struct.
*
*****************************************************************************/
static GtkTreeModelFlags
custom_list_get_flags (GtkTreeModel *tree_model)
{
g_return_val_if_fail (CUSTOM_IS_LIST(tree_model), (GtkTreeModelFlags)0);
return (GTK_TREE_MODEL_LIST_ONLY | GTK_TREE_MODEL_ITERS_PERSIST);
}
/*****************************************************************************
*
* custom_list_get_n_columns: tells the rest of the world how many data
* columns we export via the tree model interface
*
*****************************************************************************/
static gint
custom_list_get_n_columns (GtkTreeModel *tree_model)
{
g_return_val_if_fail (CUSTOM_IS_LIST(tree_model), 0);
return CUSTOM_LIST(tree_model)->n_columns;
}
/*****************************************************************************
*
* custom_list_get_column_type: tells the rest of the world which type of
* data an exported model column contains
*
*****************************************************************************/
static GType
custom_list_get_column_type (GtkTreeModel *tree_model,
gint index)
{
g_return_val_if_fail (CUSTOM_IS_LIST(tree_model), G_TYPE_INVALID);
g_return_val_if_fail (index < CUSTOM_LIST(tree_model)->n_columns && index >= 0, G_TYPE_INVALID);
return CUSTOM_LIST(tree_model)->column_types[index];
}
/*****************************************************************************
*
* custom_list_get_iter: converts a tree path (physical position) into a
* tree iter structure (the content of the iter
* fields will only be used internally by our model).
* We simply store a pointer to our CustomRecord
* structure that represents that row in the tree iter.
*
*****************************************************************************/
static gboolean
custom_list_get_iter (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreePath *path)
{
CustomList *custom_list;
CustomRecord *record;
gint *indices, n, depth;
g_assert(CUSTOM_IS_LIST(tree_model));
g_assert(path!=NULL);
custom_list = CUSTOM_LIST(tree_model);
indices = gtk_tree_path_get_indices(path);
depth = gtk_tree_path_get_depth(path);
/* we do not allow children */
g_assert(depth == 1); /* depth 1 = top level; a list only has top level nodes and no children */
n = indices[0]; /* the n-th top level row */
if ( n >= custom_list->num_rows || n < 0 )
return FALSE;
record = custom_list->rows[n];
g_assert(record != NULL);
g_assert(record->pos == n);
/* We simply store a pointer to our custom record in the iter */
iter->stamp = custom_list->stamp;
iter->user_data = record;
iter->user_data2 = NULL; /* unused */
iter->user_data3 = NULL; /* unused */
return TRUE;
}
/*****************************************************************************
*
* custom_list_get_path: converts a tree iter into a tree path (ie. the
* physical position of that row in the list).
*
*****************************************************************************/
static GtkTreePath *
custom_list_get_path (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
GtkTreePath *path;
CustomRecord *record;
CustomList *custom_list;
g_return_val_if_fail (CUSTOM_IS_LIST(tree_model), NULL);
g_return_val_if_fail (iter != NULL, NULL);
g_return_val_if_fail (iter->user_data != NULL, NULL);
custom_list = CUSTOM_LIST(tree_model);
record = (CustomRecord*) iter->user_data;
path = gtk_tree_path_new();
gtk_tree_path_append_index(path, record->pos);
return path;
}
/*****************************************************************************
*
* custom_list_get_value: Returns a row's exported data columns
* (_get_value is what gtk_tree_model_get uses)
*
*****************************************************************************/
static void
custom_list_get_value (GtkTreeModel *tree_model,
GtkTreeIter *iter,
gint column,
GValue *value)
{
CustomRecord *record;
CustomList *custom_list;
g_return_if_fail (CUSTOM_IS_LIST (tree_model));
g_return_if_fail (iter != NULL);
g_return_if_fail (column < CUSTOM_LIST(tree_model)->n_columns);
g_value_init (value, CUSTOM_LIST(tree_model)->column_types[column]);
custom_list = CUSTOM_LIST(tree_model);
record = (CustomRecord*) iter->user_data;
g_return_if_fail ( record != NULL );
if(record->pos >= custom_list->num_rows)
g_return_if_reached();
switch(column)
{
case CUSTOM_LIST_COL_RECORD:
g_value_set_pointer(value, record);
break;
case CUSTOM_LIST_COL_NAME:
g_value_set_string(value, record->name);
break;
case CUSTOM_LIST_COL_YEAR_BORN:
g_value_set_uint(value, record->year_born);
break;
}
}
/*****************************************************************************
*
* custom_list_iter_next: Takes an iter structure and sets it to point
* to the next row.
*
*****************************************************************************/
static gboolean
custom_list_iter_next (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
CustomRecord *record, *nextrecord;
CustomList *custom_list;
g_return_val_if_fail (CUSTOM_IS_LIST (tree_model), FALSE);
if (iter == NULL || iter->user_data == NULL)
return FALSE;
custom_list = CUSTOM_LIST(tree_model);
record = (CustomRecord *) iter->user_data;
/* Is this the last record in the list? */
if ((record->pos + 1) >= custom_list->num_rows)
return FALSE;
nextrecord = custom_list->rows[(record->pos + 1)];
g_assert ( nextrecord != NULL );
g_assert ( nextrecord->pos == (record->pos + 1) );
iter->stamp = custom_list->stamp;
iter->user_data = nextrecord;
return TRUE;
}
/*****************************************************************************
*
* custom_list_iter_children: Returns TRUE or FALSE depending on whether
* the row specified by 'parent' has any children.
* If it has children, then 'iter' is set to
* point to the first child. Special case: if
* 'parent' is NULL, then the first top-level
* row should be returned if it exists.
*
*****************************************************************************/
static gboolean
custom_list_iter_children (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent)
{
CustomList *custom_list;
g_return_val_if_fail (parent == NULL || parent->user_data != NULL, FALSE);
/* this is a list, nodes have no children */
if (parent)
return FALSE;
/* parent == NULL is a special case; we need to return the first top-level row */
g_return_val_if_fail (CUSTOM_IS_LIST (tree_model), FALSE);
custom_list = CUSTOM_LIST(tree_model);
/* No rows => no first row */
if (custom_list->num_rows == 0)
return FALSE;
/* Set iter to first item in list */
iter->stamp = custom_list->stamp;
iter->user_data = custom_list->rows[0];
return TRUE;
}
/*****************************************************************************
*
* custom_list_iter_has_child: Returns TRUE or FALSE depending on whether
* the row specified by 'iter' has any children.
* We only have a list and thus no children.
*
*****************************************************************************/
static gboolean
custom_list_iter_has_child (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
return FALSE;
}
/*****************************************************************************
*
* custom_list_iter_n_children: Returns the number of children the row
* specified by 'iter' has. This is usually 0,
* as we only have a list and thus do not have
* any children to any rows. A special case is
* when 'iter' is NULL, in which case we need
* to return the number of top-level nodes,
* ie. the number of rows in our list.
*
*****************************************************************************/
static gint
custom_list_iter_n_children (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
CustomList *custom_list;
g_return_val_if_fail (CUSTOM_IS_LIST (tree_model), -1);
g_return_val_if_fail (iter == NULL || iter->user_data != NULL, FALSE);
custom_list = CUSTOM_LIST(tree_model);
/* special case: if iter == NULL, return number of top-level rows */
if (!iter)
return custom_list->num_rows;
return 0; /* otherwise, this is easy again for a list */
}
/*****************************************************************************
*
* custom_list_iter_nth_child: If the row specified by 'parent' has any
* children, set 'iter' to the n-th child and
* return TRUE if it exists, otherwise FALSE.
* A special case is when 'parent' is NULL, in
* which case we need to set 'iter' to the n-th
* row if it exists.
*
*****************************************************************************/
static gboolean
custom_list_iter_nth_child (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent,
gint n)
{
CustomRecord *record;
CustomList *custom_list;
g_return_val_if_fail (CUSTOM_IS_LIST (tree_model), FALSE);
custom_list = CUSTOM_LIST(tree_model);
/* a list has only top-level rows */
if(parent)
return FALSE;
/* special case: if parent == NULL, set iter to n-th top-level row */
if( n >= custom_list->num_rows )
return FALSE;
record = custom_list->rows[n];
g_assert( record != NULL );
g_assert( record->pos == n );
iter->stamp = custom_list->stamp;
iter->user_data = record;
return TRUE;
}
/*****************************************************************************
*
* custom_list_iter_parent: Point 'iter' to the parent node of 'child'. As
* we have a list and thus no children and no
* parents of children, we can just return FALSE.
*
*****************************************************************************/
static gboolean
custom_list_iter_parent (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *child)
{
return FALSE;
}
/*****************************************************************************
*
* custom_list_new: This is what you use in your own code to create a
* new custom list tree model for you to use.
*
*****************************************************************************/
CustomList *
custom_list_new (void)
{
CustomList *newcustomlist;
newcustomlist = (CustomList*) g_object_new (CUSTOM_TYPE_LIST, NULL);
g_assert( newcustomlist != NULL );
return newcustomlist;
}
/*****************************************************************************
*
* custom_list_append_record: Empty lists are boring. This function can
* be used in your own code to add rows to the
* list. Note how we emit the "row-inserted"
* signal after we have appended the row
* internally, so the tree view and other
* interested objects know about the new row.
*
*****************************************************************************/
void
custom_list_append_record (CustomList *custom_list,
const gchar *name,
guint year_born)
{
GtkTreeIter iter;
GtkTreePath *path;
CustomRecord *newrecord;
gulong newsize;
guint pos;
g_return_if_fail (CUSTOM_IS_LIST(custom_list));
g_return_if_fail (name != NULL);
pos = custom_list->num_rows;
custom_list->num_rows++;
newsize = custom_list->num_rows * sizeof(CustomRecord*);
custom_list->rows = g_realloc(custom_list->rows, newsize);
newrecord = g_new0(CustomRecord, 1);
newrecord->name = g_strdup(name);
newrecord->name_collate_key = g_utf8_collate_key(name,-1); /* for fast sorting, used later */
newrecord->year_born = year_born;
custom_list->rows[pos] = newrecord;
newrecord->pos = pos;
/* inform the tree view and other interested objects
* (e.g. tree row references) that we have inserted
* a new row, and where it was inserted */
path = gtk_tree_path_new();
gtk_tree_path_append_index(path, newrecord->pos);
custom_list_get_iter(GTK_TREE_MODEL(custom_list), &iter, path);
gtk_tree_model_row_inserted(GTK_TREE_MODEL(custom_list), path, &iter);
gtk_tree_path_free(path);
}

View File

@ -0,0 +1,97 @@
/***********************************************************
*
* custom-list.h
*
* A simple custom list model
*
* part of the Gtk+ tree view tutorial
*
* by Tim-Philipp Mueller < tim at centricular dot net >
*
***********************************************************/
#ifndef _custom_list_h_included_
#define _custom_list_h_included_
#include <gtk/gtk.h>
/* Some boilerplate GObject defines. 'klass' is used instead of 'class', because 'class' is a C++ keyword */
#define CUSTOM_TYPE_LIST (custom_list_get_type ())
#define CUSTOM_LIST(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CUSTOM_TYPE_LIST, CustomList))
#define CUSTOM_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CUSTOM_TYPE_LIST, CustomListClass))
#define CUSTOM_IS_LIST(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CUSTOM_TYPE_LIST))
#define CUSTOM_IS_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CUSTOM_TYPE_LIST))
#define CUSTOM_LIST_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CUSTOM_TYPE_LIST, CustomListClass))
/* The data columns that we export via the tree model interface */
enum
{
CUSTOM_LIST_COL_RECORD = 0,
CUSTOM_LIST_COL_NAME,
CUSTOM_LIST_COL_YEAR_BORN,
CUSTOM_LIST_N_COLUMNS,
} ;
typedef struct _CustomRecord CustomRecord;
typedef struct _CustomList CustomList;
typedef struct _CustomListClass CustomListClass;
/* CustomRecord: this structure represents a row */
struct _CustomRecord
{
/* data - you can extend this */
gchar *name;
gchar *name_collate_key;
guint year_born;
/* admin stuff used by the custom list model */
guint pos; /* pos within the array */
};
/* CustomList: this structure contains everything we need for our
* model implementation. You can add extra fields to
* this structure, e.g. hashtables to quickly lookup
* rows or whatever else you might need, but it is
* crucial that 'parent' is the first member of the
* structure. */
struct _CustomList
{
GObject parent; /* this MUST be the first member */
guint num_rows; /* number of rows that we have */
CustomRecord **rows; /* a dynamically allocated array of pointers to the CustomRecord structure for each row */
gint n_columns; /* These two fields are not absolutely necessary, but they */
GType column_types[CUSTOM_LIST_N_COLUMNS]; /* speed things up a bit in our get_value implementation */
gint stamp; /* A random integer to check if an iter belongs to our model */
};
/* CustomListClass: more boilerplate GObject stuff */
struct _CustomListClass
{
GObjectClass parent_class;
};
GType custom_list_get_type (void);
CustomList *custom_list_new (void);
void custom_list_append_record (CustomList *custom_list,
const gchar *name,
guint year_born);
#endif /* _custom_list_h_included_ */

View File

@ -0,0 +1,80 @@
#include "custom-list.h"
#include <stdlib.h>
void
fill_model (CustomList *customlist)
{
const gchar *firstnames[] = { "Joe", "Jane", "William", "Hannibal", "Timothy", "Gargamel", NULL } ;
const gchar *surnames[] = { "Grokowich", "Twitch", "Borheimer", "Bork", NULL } ;
const gchar **fname, **sname;
for (sname = surnames; *sname != NULL; sname++)
{
for (fname = firstnames; *fname != NULL; fname++)
{
gchar *name = g_strdup_printf ("%s %s", *fname, *sname);
custom_list_append_record (customlist, name, 1900 + (guint) (103.0*rand()/(RAND_MAX+1900.0)));
g_free(name);
}
}
}
GtkWidget *
create_view_and_model (void)
{
GtkTreeViewColumn *col;
GtkCellRenderer *renderer;
CustomList *customlist;
GtkWidget *view;
customlist = custom_list_new();
fill_model(customlist);
view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(customlist));
g_object_unref(customlist); /* destroy store automatically with view */
renderer = gtk_cell_renderer_text_new();
col = gtk_tree_view_column_new();
gtk_tree_view_column_pack_start (col, renderer, TRUE);
gtk_tree_view_column_add_attribute (col, renderer, "text", CUSTOM_LIST_COL_NAME);
gtk_tree_view_column_set_title (col, "Name");
gtk_tree_view_append_column(GTK_TREE_VIEW(view),col);
renderer = gtk_cell_renderer_text_new();
col = gtk_tree_view_column_new();
gtk_tree_view_column_pack_start (col, renderer, TRUE);
gtk_tree_view_column_add_attribute (col, renderer, "text", CUSTOM_LIST_COL_YEAR_BORN);
gtk_tree_view_column_set_title (col, "Year Born");
gtk_tree_view_append_column(GTK_TREE_VIEW(view),col);
return view;
}
int
main (int argc, char **argv)
{
GtkWidget *window, *view, *scrollwin;
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW(window), 200, 400);
g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
scrollwin = gtk_scrolled_window_new(NULL,NULL);
view = create_view_and_model();
gtk_container_add(GTK_CONTAINER(scrollwin), view);
gtk_container_add(GTK_CONTAINER(window), scrollwin);
gtk_widget_show_all(window);
gtk_main();
return 0;
}

View File

@ -0,0 +1,14 @@
CC = gcc
CFLAGS = -Wall -g -O2 `pkg-config --cflags gtk+-2.0`
PROG = hello-world
OBJS = $(PROG).o
all: $(OBJS)
gcc -o $(PROG) $(OBJS) `pkg-config --libs gtk+-2.0`
clean:
rm -f $(PROG) $(OBJS) *.c~ *.h~ 2>/dev/null

View File

@ -0,0 +1,105 @@
#include <gtk/gtk.h>
enum
{
COL_NAME = 0,
COL_AGE,
NUM_COLS
} ;
static GtkTreeModel *
create_and_fill_model (void)
{
GtkListStore *store;
GtkTreeIter iter;
store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING, G_TYPE_UINT);
/* Append a row and fill in some data */
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
COL_NAME, "Heinz El-Mann",
COL_AGE, 51,
-1);
/* append another row and fill in some data */
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
COL_NAME, "Jane Doe",
COL_AGE, 23,
-1);
/* ... and a third row */
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
COL_NAME, "Joe Bungop",
COL_AGE, 91,
-1);
return GTK_TREE_MODEL (store);
}
static GtkWidget *
create_view_and_model (void)
{
GtkTreeViewColumn *col;
GtkCellRenderer *renderer;
GtkTreeModel *model;
GtkWidget *view;
view = gtk_tree_view_new ();
/* --- Column #1 --- */
renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
-1,
"Name",
renderer,
"text", COL_NAME,
NULL);
/* --- Column #2 --- */
col = gtk_tree_view_column_new();
renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
-1,
"Age",
renderer,
"text", COL_AGE,
NULL);
model = create_and_fill_model ();
gtk_tree_view_set_model (GTK_TREE_VIEW (view), model);
g_object_unref (model); /* destroy model automatically with view */
return view;
}
int
main (int argc, char **argv)
{
GtkWidget *window;
GtkWidget *view;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "delete_event", gtk_main_quit, NULL); /* dirty */
view = create_view_and_model ();
gtk_container_add (GTK_CONTAINER (window), view);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}

View File

@ -0,0 +1,12 @@
CC = gcc
CFLAGS = -Wall -g -O2 `pkg-config --cflags gtk+-2.0`
all: simple-list
simple-list: list.o
gcc -o simple-list list.o `pkg-config --libs gtk+-2.0`
clean:
rm -f *.o simple-list *.c~ *.h~ 2>/dev/null

231
examples/simple-list/list.c Normal file
View File

@ -0,0 +1,231 @@
#include <gtk/gtk.h>
enum
{
COL_TEXT = 0,
NUM_COLS
};
static GtkWidget *treeview;
static GtkWidget *delbutton;
/**************************************************************************
*
* onAddButtonPress
*
* Button has been clicked, or <enter> has been hit in entry
*
**************************************************************************/
static void
onAddButtonPress (GtkWidget *entry, gpointer data)
{
const gchar *txt;
g_assert(GTK_IS_ENTRY(entry));
txt = gtk_entry_get_text(GTK_ENTRY(entry));
/* ignore if entry is empty */
if (txt && *txt)
{
GtkTreeModel *model;
GtkTreeIter newrow;
model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
gtk_list_store_append(GTK_LIST_STORE(model), &newrow);
gtk_list_store_set(GTK_LIST_STORE(model), &newrow, COL_TEXT, txt, -1);
gtk_entry_set_text(GTK_ENTRY(entry), ""); /* clear entry */
}
}
/**************************************************************************
*
* onDelButtonPress
*
**************************************************************************/
static void
onDelButtonPress (GtkWidget *button, gpointer data)
{
GtkTreeSelection *sel;
GtkTreeModel *model;
GtkTreeIter selected_row;
sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
g_assert(gtk_tree_selection_get_mode(sel) == GTK_SELECTION_SINGLE);
if (gtk_tree_selection_get_selected(sel, &model, &selected_row))
{
gtk_list_store_remove(GTK_LIST_STORE(model), &selected_row);
}
else
{
/* If no row is selected, the button should
* not be clickable in the first place */
g_assert_not_reached();
}
}
/**************************************************************************
*
* onSelectionChanged
*
* Sets 'delete selected row' button active or inactive, depending
* on whether a row is selected or not. This function is called
* whenever the selection changes.
*
**************************************************************************/
static void
onSelectionChanged (GtkTreeSelection *sel, GtkListStore *liststore)
{
GtkTreeIter selected_row;
/* Check if a row is selected or not */
/* This will only work in SINGLE or BROWSE mode! */
if (gtk_tree_selection_get_selected(sel, NULL, &selected_row))
{
gtk_widget_set_sensitive(delbutton, TRUE);
}
else
{
gtk_widget_set_sensitive(delbutton, FALSE);
}
}
/**************************************************************************
*
* create_list_view
*
* sets up the tree view and returns a GtkTreeView widget
*
**************************************************************************/
static GtkWidget *
create_list_view (void)
{
GtkCellRenderer *renderer;
GtkTreeViewColumn *col;
GtkTreeSelection *sel;
GtkListStore *liststore;
GtkWidget *view;
liststore = gtk_list_store_new(NUM_COLS, G_TYPE_STRING); /* NUM_COLS = 1 */
view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(liststore));
renderer = gtk_cell_renderer_text_new();
col = gtk_tree_view_column_new();
gtk_tree_view_column_pack_start(col, renderer, TRUE);
gtk_tree_view_column_add_attribute(col, renderer, "text", COL_TEXT);
gtk_tree_view_column_set_title(col, " Your items here ");
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
g_signal_connect(sel, "changed", G_CALLBACK(onSelectionChanged), liststore);
return view;
}
/**************************************************************************
*
* create_entry_hbox
*
* Returns GtkHBox widget with entry and button inside
*
**************************************************************************/
static GtkWidget *
create_entry_hbox (void)
{
GtkWidget *addbutton;
GtkWidget *entry;
GtkWidget *hbox;
hbox = gtk_hbox_new(FALSE, 2);
/* when 'return' is pressed, add text to list
* just as if the button had been pressed */
entry = gtk_entry_new();
g_signal_connect(entry, "activate", G_CALLBACK(onAddButtonPress), NULL);
gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
addbutton = gtk_button_new_with_label(" Add text to list ");
/* using _swapped, so that the first argument to the callback
* is what we specify as 'data' argument here, ie. the entry,
* and data argument to callback is what is the object to
* connect to here, ie. the button */
g_signal_connect_swapped(addbutton, "clicked", G_CALLBACK(onAddButtonPress), entry);
gtk_box_pack_start(GTK_BOX(hbox), addbutton, FALSE, FALSE, 0);
delbutton = gtk_button_new_with_label(" Remove selected row ");
g_signal_connect(delbutton, "clicked", G_CALLBACK(onDelButtonPress), NULL);
gtk_box_pack_start(GTK_BOX(hbox), delbutton, FALSE, FALSE, 0);
/* at beginning no rows, so nothing to delete */
gtk_widget_set_sensitive(delbutton, FALSE);
return hbox;
}
/**************************************************************************
*
* main
*
**************************************************************************/
int
main (int argc, char **argv)
{
GtkWidget *window;
GtkWidget *scrollwin;
GtkWidget *tophbox;
GtkWidget *vbox;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
g_signal_connect(window, "delete_event", gtk_main_quit, NULL); /* dirty */
vbox = gtk_vbox_new(FALSE, 2);
tophbox = create_entry_hbox();
gtk_box_pack_start(GTK_BOX(vbox), tophbox, FALSE, FALSE, 0);
scrollwin = gtk_scrolled_window_new(NULL,NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
treeview = create_list_view();
gtk_container_add(GTK_CONTAINER(scrollwin), treeview);
gtk_box_pack_start(GTK_BOX(vbox), scrollwin, TRUE, TRUE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_widget_show_all(window);
gtk_main();
return 0;
}

View File

@ -0,0 +1,12 @@
CC = gcc
OBJS = guitreemodelfilter.o main-filter-rows.o
CFLAGS = -g -O2 `pkg-config --cflags gtk+-2.0`
all: $(OBJS)
gcc -o filter-rows $(OBJS) `pkg-config --libs gtk+-2.0`
clean:
rm -f filter-rows $(OBJS) 2>/dev/null

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,104 @@
/* gtktreemodelfilter.h
* Copyright (C) 2000,2001 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
* Copyright (C) 2001-2003 Kristian Rietveld <kris@gtk.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GUI_TREE_MODEL_FILTER_H__
#define __GUI_TREE_MODEL_FILTER_H__
#include <gtk/gtktreemodel.h>
G_BEGIN_DECLS
#define GUI_TYPE_TREE_MODEL_FILTER (gui_tree_model_filter_get_type ())
#define GUI_TREE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GUI_TYPE_TREE_MODEL_FILTER, GuiTreeModelFilter))
#define GUI_TREE_MODEL_FILTER_CLASS(vtable) (G_TYPE_CHECK_CLASS_CAST ((vtable), GUI_TYPE_TREE_MODEL_FILTER, GuiTreeModelFilterClass))
#define GUI_IS_TREE_MODEL_FILTER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GUI_TYPE_TREE_MODEL_FILTER))
#define GUI_IS_TREE_MODEL_FILTER_CLASS(vtable) (G_TYPE_CHECK_CLASS_TYPE ((vtable), GUI_TYPE_TREE_MODEL_FILTER))
#define GUI_TREE_MODEL_FILTER_GET_CLASS(inst) (G_TYPE_INSTANCE_GET_CLASS ((obj), GUI_TYPE_TREE_MODEL_FILTER, GuiTreeModelFilterClass))
typedef gboolean (* GuiTreeModelFilterVisibleFunc) (GtkTreeModel *model,
GtkTreeIter *iter,
gpointer data);
typedef void (* GuiTreeModelFilterModifyFunc) (GtkTreeModel *model,
GtkTreeIter *iter,
GValue *value,
gint column,
gpointer data);
typedef struct _GuiTreeModelFilter GuiTreeModelFilter;
typedef struct _GuiTreeModelFilterClass GuiTreeModelFilterClass;
typedef struct _GuiTreeModelFilterPrivate GuiTreeModelFilterPrivate;
struct _GuiTreeModelFilter
{
GObject parent;
/*< private >*/
GuiTreeModelFilterPrivate *priv;
};
struct _GuiTreeModelFilterClass
{
GObjectClass parent_class;
/* Padding for future expansion */
void (*_gtk_reserved0) (void);
void (*_gtk_reserved1) (void);
void (*_gtk_reserved2) (void);
void (*_gtk_reserved3) (void);
};
/* base */
GType gui_tree_model_filter_get_type (void);
GtkTreeModel *gui_tree_model_filter_new (GtkTreeModel *child_model,
GtkTreePath *root);
void gui_tree_model_filter_set_visible_func (GuiTreeModelFilter *filter,
GuiTreeModelFilterVisibleFunc func,
gpointer data,
GtkDestroyNotify destroy);
void gui_tree_model_filter_set_modify_func (GuiTreeModelFilter *filter,
gint n_columns,
GType *types,
GuiTreeModelFilterModifyFunc func,
gpointer data,
GtkDestroyNotify destroy);
void gui_tree_model_filter_set_visible_column (GuiTreeModelFilter *filter,
gint column);
GtkTreeModel *gui_tree_model_filter_get_model (GuiTreeModelFilter *filter);
/* conversion */
void gui_tree_model_filter_convert_child_iter_to_iter (GuiTreeModelFilter *filter,
GtkTreeIter *filter_iter,
GtkTreeIter *child_iter);
void gui_tree_model_filter_convert_iter_to_child_iter (GuiTreeModelFilter *filter,
GtkTreeIter *child_iter,
GtkTreeIter *filter_iter);
GtkTreePath *gui_tree_model_filter_convert_child_path_to_path (GuiTreeModelFilter *filter,
GtkTreePath *child_path);
GtkTreePath *gui_tree_model_filter_convert_path_to_child_path (GuiTreeModelFilter *path,
GtkTreePath *filter_path);
/* extras */
void gui_tree_model_filter_refilter (GuiTreeModelFilter *filter);
void gui_tree_model_filter_clear_cache (GuiTreeModelFilter *filter);
G_END_DECLS
#endif /* __GUI_TREE_MODEL_FILTER_H__ */

Binary file not shown.

View File

@ -0,0 +1,228 @@
#include <gtk/gtk.h>
#include "guitreemodelfilter.h"
/**************************************************************
* *
* Short demonstration of how to use GtkTreeModelFilter *
* to filter out certain rows of a tree model/tree view *
* *
* Note: we changed the namespace from GtkTreeModelFilter *
* to GuiTreeModelFilter here, in order to make this *
* demo work with Gtk+ versions < 2.3 *
* *
* GtkTreeModelFilter will be in Gtk+-2.4 and is already *
* in the (unstable) Gtk+-2.3 development series *
* *
* part of the GtkTreeView tutorial *
* written by Tim Mueller <tim @at@ centricular .dot. net> *
* *
**************************************************************/
enum
{
COL_NAME = 0,
COL_YEAR,
NUM_COLS
};
static GtkTreeModel *filter; /* NULL */
static guint minyear; /* 0 */
/**************************************************************
*
* filter_visible_function
*
* This is where the action is. Returns TRUE if
* the row should be visible, and FALSE if the
* row should be hidden
*
**************************************************************/
static gboolean
filter_visible_function (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
{
gint year;
gtk_tree_model_get(model, iter, COL_YEAR, &year, -1);
if (year < minyear)
return FALSE;
return TRUE;
}
/**************************************************************
*
* fill_list_store
*
**************************************************************/
static void
fill_list_store (GtkListStore *store)
{
const gchar *names[] = { "John B.", "Jane Z.", "Carl O.", "Owen P.", "Jeremy F.",
"Michael M.", "Ute D.", "Akira T.", "Thomas F.", "Matthew J."};
guint i;
for (i = 0; i < G_N_ELEMENTS(names); ++i)
{
GtkTreeIter iter;
guint randomyear;
randomyear = 1900 + (g_random_int() % 103);
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter,
COL_NAME, names[i],
COL_YEAR, randomyear,
-1);
}
}
/**************************************************************
*
* create_models_and_view
*
**************************************************************/
GtkWidget *
create_models_and_view (void)
{
GtkCellRenderer *renderer;
GtkTreeViewColumn *col;
GtkListStore *store;
GtkWidget *view;
store = gtk_list_store_new(NUM_COLS, G_TYPE_STRING, G_TYPE_INT);
filter = gui_tree_model_filter_new(GTK_TREE_MODEL(store), NULL);
gui_tree_model_filter_set_visible_func(GUI_TREE_MODEL_FILTER(filter),
filter_visible_function,
NULL,
NULL);
fill_list_store(store);
/* set up the view */
view = gtk_tree_view_new_with_model(filter);
/* column 1: name */
renderer = gtk_cell_renderer_text_new();
col = gtk_tree_view_column_new();
gtk_tree_view_column_pack_start(col, renderer, FALSE);
gtk_tree_view_column_add_attribute(col, renderer, "text", COL_NAME);
gtk_tree_view_column_set_title(col, "Name");
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
/* column 2: year of birth - note: COL_YEAR is of G_TYPE_INT,
* but it will automatically converted to a string
* (the "text" property/attribute requires a string) */
renderer = gtk_cell_renderer_text_new();
col = gtk_tree_view_column_new();
gtk_tree_view_column_pack_start(col, renderer, FALSE);
gtk_tree_view_column_add_attribute(col, renderer, "text", COL_YEAR);
gtk_tree_view_column_set_title(col, "Year Born");
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
return view;
}
/**************************************************************
*
* onSpinButtonValueChanged
*
**************************************************************/
static void
onSpinButtonValueChanged (GtkSpinButton *spin, gpointer data)
{
g_assert(filter != NULL);
minyear = gtk_spin_button_get_value_as_int(spin);
gui_tree_model_filter_refilter(GUI_TREE_MODEL_FILTER(filter));
g_print("%s: %d\n", __FUNCTION__, minyear);
}
/**************************************************************
*
* create_spin_button_row
*
**************************************************************/
static GtkWidget *
create_spin_button_row (void)
{
GtkWidget *hbox, *spinbutton, *label;
hbox = gtk_hbox_new(FALSE, 2);
spinbutton = gtk_spin_button_new_with_range(1900.0, 2004.0, 1.0);
g_signal_connect(spinbutton, "value-changed", G_CALLBACK(onSpinButtonValueChanged), NULL);
gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinbutton), 1900.0);
label = gtk_label_new("Show all persons born after ");
gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(hbox), spinbutton, FALSE, FALSE, 0);
return hbox;
}
/**************************************************************
*
* fill_window
*
**************************************************************/
static void
fill_window(GtkWidget *mainwindow)
{
GtkWidget *vbox, *view, *spinrowhbox;
spinrowhbox = create_spin_button_row();
view = create_models_and_view();
vbox = gtk_vbox_new(FALSE,2);
gtk_box_pack_start(GTK_BOX(vbox), spinrowhbox, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), view, TRUE, TRUE, 0);
gtk_container_add(GTK_CONTAINER(mainwindow), vbox);
}
/**************************************************************
*
* main
*
**************************************************************/
gint
main (gint argc, gchar **argv)
{
GtkWidget *mainwindow, *vbox;
gtk_init(&argc, &argv);
mainwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
/* quit when window close button is clicked */
g_signal_connect(mainwindow, "delete_event", gtk_main_quit, NULL); /* dirty */
fill_window(mainwindow);
gtk_widget_show_all(mainwindow);
gtk_main();
return 0;
}

Binary file not shown.

View File

@ -0,0 +1,14 @@
CC = gcc
CFLAGS = -g -O2 `pkg-config --cflags gtk+-2.0`
all: treeview-demo icon-with-text
treeview-demo: main.o
gcc -o treeview-demo main.o `pkg-config --libs gtk+-2.0`
icon-with-text: main-icon-with-text.o
gcc -o icon-with-text main-icon-with-text.o `pkg-config --libs gtk+-2.0`
clean:
rm -f *.o treeview-demo icon-with-text 2>/dev/null

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -0,0 +1,123 @@
#include <gtk/gtk.h>
enum
{
COL_ICON = 0,
COL_TEXT,
COL_TEXT2,
NUM_COLS
};
/**************************************************************
*
* create_liststore
*
**************************************************************/
GtkListStore *
create_liststore(void)
{
GtkListStore *store;
GtkTreeIter iter;
GdkPixbuf *icon;
GError *error = NULL;
store = gtk_list_store_new(3, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING);
icon = gdk_pixbuf_new_from_file("icon.png", &error);
if (error)
{
g_warning ("Could not load icon: %s\n", error->message);
g_error_free(error);
error = NULL;
}
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter,
COL_ICON, icon,
COL_TEXT, "Smile",
COL_TEXT2, "and another column",
-1);
return store;
}
/**************************************************************
*
* create_treeview
*
**************************************************************/
GtkWidget *
create_treeview(void)
{
GtkTreeModel *model;
GtkTreeViewColumn *col;
GtkCellRenderer *renderer;
GtkWidget *view;
model = GTK_TREE_MODEL(create_liststore());
view = gtk_tree_view_new_with_model(model);
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "Column #1");
renderer = gtk_cell_renderer_pixbuf_new();
gtk_tree_view_column_pack_start(col, renderer, FALSE);
gtk_tree_view_column_set_attributes(col, renderer,
"pixbuf", COL_ICON,
NULL);
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start(col, renderer, TRUE);
gtk_tree_view_column_set_attributes(col, renderer,
"text", COL_TEXT,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
/* 2nd column */
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "Column #2");
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start(col, renderer, TRUE);
gtk_tree_view_column_set_attributes(col, renderer,
"text", COL_TEXT2,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
return view;
}
/**************************************************************
*
* main
*
**************************************************************/
gint
main (gint argc, gchar **argv)
{
GtkWidget *mainwindow, *vbox;
gtk_init(&argc, &argv);
mainwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
/* quit when window close button is clicked */
g_signal_connect(mainwindow, "delete_event", gtk_main_quit, NULL); /* dirty */
gtk_container_add(GTK_CONTAINER(mainwindow), create_treeview());
gtk_widget_show_all(mainwindow);
gtk_main();
return 0;
}

View File

@ -0,0 +1,178 @@
#include <gtk/gtk.h>
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); /* NUM_COLS = 3 */
/* Append a top level row and leave it empty */
gtk_tree_store_append(treestore, &toplevel, NULL);
gtk_tree_store_set(treestore, &toplevel,
COL_FIRST_NAME, "Maria",
COL_LAST_NAME, "Incognito",
-1);
/* Append a second top level row, and fill it with some data */
gtk_tree_store_append(treestore, &toplevel, NULL);
gtk_tree_store_set(treestore, &toplevel,
COL_FIRST_NAME, "Jane",
COL_LAST_NAME, "Average",
COL_YEAR_BORN, (guint) 1962,
-1);
/* Append a child to the second top level row, and fill in some data */
gtk_tree_store_append(treestore, &child, &toplevel);
gtk_tree_store_set(treestore, &child,
COL_FIRST_NAME, "Janinita",
COL_LAST_NAME, "Average",
COL_YEAR_BORN, (guint) 1985,
-1);
return GTK_TREE_MODEL(treestore);
}
void
age_cell_data_func (GtkTreeViewColumn *col,
GtkCellRenderer *renderer,
GtkTreeModel *model,
GtkTreeIter *iter,
gpointer user_data)
{
guint year_born;
guint year_now = 2003; /* to save code not relevant for the example */
gchar buf[64];
gtk_tree_model_get(model, iter, COL_YEAR_BORN, &year_born, -1);
if (year_born <= year_now && year_born > 0)
{
guint age = year_now - year_born;
g_snprintf(buf, sizeof(buf), "%u years old", age);
g_object_set(renderer, "foreground-set", FALSE, NULL); /* print this normal */
}
else
{
g_snprintf(buf, sizeof(buf), "age unknown");
g_object_set(renderer, "foreground", "Red", "foreground-set", TRUE, NULL); /* make red */
}
g_object_set(renderer, "text", buf, NULL);
}
static GtkWidget *
create_view_and_model (void)
{
GtkTreeViewColumn *col;
GtkCellRenderer *renderer;
GtkWidget *view;
GtkTreeModel *model;
view = gtk_tree_view_new();
/* --- Column #1 --- */
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "First Name");
/* pack tree view column into tree view */
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
renderer = gtk_cell_renderer_text_new();
/* pack cell renderer into tree view column */
gtk_tree_view_column_pack_start(col, renderer, TRUE);
/* connect 'text' property of the cell renderer to model column that contains the first name */
gtk_tree_view_column_add_attribute(col, renderer, "text", COL_FIRST_NAME);
/* --- Column #2 --- */
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "Last Name");
/* pack tree view column into tree view */
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
renderer = gtk_cell_renderer_text_new();
/* pack cell renderer into tree view column */
gtk_tree_view_column_pack_start(col, renderer, TRUE);
/* connect 'text' property of the cell renderer to model column that contains the last name */
gtk_tree_view_column_add_attribute(col, renderer, "text", COL_LAST_NAME);
/* set 'weight' property of the cell renderer to bold print (we want all last names in bold) */
g_object_set(renderer, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
/* --- Column #3 --- */
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "Age");
/* pack tree view column into tree view */
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
renderer = gtk_cell_renderer_text_new();
/* pack cell renderer into tree view column */
gtk_tree_view_column_pack_start(col, renderer, TRUE);
/* connect a cell data function */
gtk_tree_view_column_set_cell_data_func(col, renderer, age_cell_data_func, NULL, NULL);
model = create_and_fill_model();
gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
g_object_unref(model); /* destroy model automatically with view */
gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), GTK_SELECTION_NONE);
return view;
}
int
main (int argc, char **argv)
{
GtkWidget *window;
GtkWidget *view;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
g_signal_connect(window, "delete_event", gtk_main_quit, NULL); /* dirty */
view = create_view_and_model();
gtk_container_add(GTK_CONTAINER(window), view);
gtk_widget_show_all(window);
gtk_main();
return 0;
}

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 KiB

2398
images/renderer.eps Normal file

File diff suppressed because it is too large Load Diff

BIN
images/renderer.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

14183
images/tree-view-column.eps Normal file

File diff suppressed because it is too large Load Diff

BIN
images/tree-view-column.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

3248
images/treepath.eps Normal file

File diff suppressed because it is too large Load Diff

BIN
images/treepath.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

10
treeview-tutorial.css Normal file
View File

@ -0,0 +1,10 @@
p {
text-align:justify;
width:95%
}
table {
width:95%
}

7849
treeview-tutorial.xml Normal file

File diff suppressed because it is too large Load Diff