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;