2007-11-15 Neil J. Patel <njp@o-hand.com>

* clutter/Makefile.am:
	* clutter/clutter-model.c: 
	* clutter/clutter-model.h:
	* clutter/clutter.h:
	* tests/Makefile.am:
	* tests/test-model.c:
	Merged ClutterModel, which closes #443.
This commit is contained in:
Neil J. Patel 2007-11-15 10:02:25 +00:00
parent baad0f5b0d
commit 70e09ab9df
11 changed files with 2584 additions and 1 deletions

View File

@ -1,3 +1,13 @@
2007-11-15 Neil J. Patel <njp@o-hand.com>
* clutter/Makefile.am:
* clutter/clutter-model.c:
* clutter/clutter-model.h:
* clutter/clutter.h:
* tests/Makefile.am:
* tests/test-model.c:
Merged ClutterModel, which closes #443.
2007-11-14 Emmanuele Bassi <ebassi@openedhand.com>
* clutter/clutter-clone-texture.c (set_parent_texture): Hide the

View File

@ -67,6 +67,7 @@ source_h = \
$(srcdir)/clutter-layout.h \
$(srcdir)/clutter-main.h \
$(srcdir)/clutter-media.h \
$(srcdir)/clutter-model.h \
$(srcdir)/clutter-rectangle.h \
$(srcdir)/clutter-score.h \
$(srcdir)/clutter-script.h \
@ -153,6 +154,7 @@ source_c = \
clutter-main.c \
clutter-marshal.c \
clutter-media.c \
clutter-model.c \
clutter-rectangle.c \
clutter-score.c \
clutter-script.c \

1991
clutter/clutter-model.c Normal file

File diff suppressed because it is too large Load Diff

302
clutter/clutter-model.h Normal file
View File

@ -0,0 +1,302 @@
/*
* Clutter.
*
* An OpenGL based 'interactive canvas' library.
*
* Authored By Matthew Allum <mallum@openedhand.com>
* Neil Jagdish Patel <njp@o-hand.com>
* Emmanuele Bassi <ebassi@openedhand.com>
*
* Copyright (C) 2006 OpenedHand
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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 __CLUTTER_MODEL_H__
#define __CLUTTER_MODEL_H__
#include <glib.h>
#include <glib-object.h>
G_BEGIN_DECLS
#define CLUTTER_TYPE_MODEL (clutter_model_get_type ())
#define CLUTTER_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CLUTTER_TYPE_MODEL, ClutterModel))
#define CLUTTER_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CLUTTER_TYPE_MODEL, ClutterModelClass))
#define CLUTTER_IS_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CLUTTER_TYPE_MODEL))
#define CLUTTER_IS_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLUTTER_TYPE_MODEL))
#define CLUTTER_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CLUTTER_TYPE_MODEL, ClutterModelClass))
typedef struct _ClutterModel ClutterModel;
typedef struct _ClutterModelClass ClutterModelClass;
typedef struct _ClutterModelPrivate ClutterModelPrivate;
typedef struct _ClutterModelIter ClutterModelIter;
typedef struct _ClutterModelIterClass ClutterModelIterClass;
typedef struct _ClutterModelIterPrivate ClutterModelIterPrivate;
/**
* ClutterModelFilterFunc:
* @model: a #ClutterModel
* @iter: the iterator for the row
* @user_data: data passed to clutter_model_set_filter()
*
* Filters the content of a row in the model.
*
* Return value: If the row should be displayed, return %TRUE
*
* Since: 0.6
*/
typedef gboolean (*ClutterModelFilterFunc) (ClutterModel *model,
ClutterModelIter *iter,
gpointer user_data);
/**
* ClutterModelSortFunc:
* @model: a #ClutterModel
* @a: a #GValue representing the contents of the row
* @b: a #GValue representing the contents of the second row
* @user_data: data passed to clutter_model_set_sort()
*
* Compares the content of two rows in the model.
*
* Return value: a positive integer if @a is after @b, a negative integer if
* @a is before @b, or 0 if the rows are the same
*
* Since: 0.6
*/
typedef gboolean (*ClutterModelSortFunc) (ClutterModel *model,
const GValue *a,
const GValue *b,
gpointer user_data);
/**
* ClutterModelForeachFunc:
* @model: a #ClutterModel
* @iter: the iterator for the row
* @user_data: data passed to clutter_model_foreach()
*
* Iterates on the content of a row in the model
*
* Return value: %TRUE if the iteration should continue, %FALSE otherwise
*
* Since: 0.6
*/
typedef gboolean (*ClutterModelForeachFunc) (ClutterModel *model,
ClutterModelIter *iter,
gpointer user_data);
/**
* ClutterModel:
*
* Base class for list models. The #ClutterModel structure contains
* only private data and should be manipulated using the provided
* API.
*
* Since: 0.6
*/
struct _ClutterModel
{
/*< private >*/
GObject parent_instance;
ClutterModelPrivate *priv;
};
/**
* ClutterModelClass:
* @row_added: signal class handler for ClutterModel::row-added
* @row_removed: signal class handler for ClutterModel::row-removed
* @row_changed: signal class handler for ClutterModel::row-changed
* @sort_changed: signal class handler for ClutterModel::sort-changed
* @filter_changed: signal class handler for ClutterModel::filter-changed
*
* Class for #ClutterModel instances.
*
* Since: 0.6
*/
struct _ClutterModelClass
{
/*< private >*/
GObjectClass parent_class;
/*< public >*/
/* vtable */
ClutterModelIter *(* get_iter_at_row) (ClutterModel *model,
guint index_);
/* signals */
void (* row_added) (ClutterModel *model,
ClutterModelIter *iter);
void (* row_removed) (ClutterModel *model,
ClutterModelIter *iter);
void (* row_changed) (ClutterModel *model,
ClutterModelIter *iter);
void (* sort_changed) (ClutterModel *model);
void (* filter_changed) (ClutterModel *model);
/*< private >*/
/* padding for future */
void (*_clutter_model_1) (void);
void (*_clutter_model_2) (void);
void (*_clutter_model_3) (void);
void (*_clutter_model_4) (void);
};
GType clutter_model_get_type (void) G_GNUC_CONST;
ClutterModel * clutter_model_new (guint n_columns,
...);
ClutterModel * clutter_model_newv (guint n_columns,
GType *types);
void clutter_model_set_types (ClutterModel *model,
guint n_columns,
GType *types);
gboolean clutter_model_append (ClutterModel *model,
...);
gboolean clutter_model_prepend (ClutterModel *model,
...);
gboolean clutter_model_insert (ClutterModel *model,
guint index_,
...);
gboolean clutter_model_insert_value (ClutterModel *model,
guint index_,
guint column,
const GValue *value);
void clutter_model_remove (ClutterModel *model,
guint index_);
guint clutter_model_get_n_rows (ClutterModel *model);
ClutterModelIter *clutter_model_get_first_iter (ClutterModel *model);
ClutterModelIter *clutter_model_get_last_iter (ClutterModel *model);
ClutterModelIter *clutter_model_get_iter_at_row (ClutterModel *model,
guint index_);
void clutter_model_set_sorting_column (ClutterModel *model,
guint column);
guint clutter_model_get_sorting_column (ClutterModel *model);
void clutter_model_foreach (ClutterModel *model,
ClutterModelForeachFunc func,
gpointer user_data);
void clutter_model_set_sort (ClutterModel *model,
guint column,
ClutterModelSortFunc func,
gpointer user_data,
GDestroyNotify notify);
void clutter_model_set_filter (ClutterModel *model,
ClutterModelFilterFunc func,
gpointer user_data,
GDestroyNotify notify);
/*
* ClutterModelIter
*/
#define CLUTTER_TYPE_MODEL_ITER (clutter_model_iter_get_type ())
#define CLUTTER_MODEL_ITER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CLUTTER_TYPE_MODEL_ITER, ClutterModelIter))
#define CLUTTER_MODEL_ITER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CLUTTER_TYPE_MODEL_ITER, ClutterModelIterClass))
#define CLUTTER_IS_MODEL_ITER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CLUTTER_TYPE_MODEL_ITER))
#define CLUTTER_IS_MODEL_ITER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLUTTER_TYPE_MODEL_ITER))
#define CLUTTER_MODEL_ITER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CLUTTER_TYPE_MODEL_ITER, ClutterModelIterClass))
/**
* ClutterModelIter:
*
* Base class for list models iters. The #ClutterModelIter structure contains
* only private data and should be manipulated using the provided
* API.
*
* Since: 0.6
*/
struct _ClutterModelIter
{
/*< private >*/
GObject parent_instance;
ClutterModelIterPrivate *priv;
};
/**
* ClutterModelIterClass:
*
* Class for #ClutterModelIter instances.
*
* Since: 0.6
*/
struct _ClutterModelIterClass
{
/*< private >*/
GObjectClass parent_class;
/*< public >*/
/* vtable not signals */
void (* get_value) (ClutterModelIter *iter,
guint column,
GValue *value);
void (* set_value) (ClutterModelIter *iter,
guint column,
const GValue *value);
gboolean (* is_first) (ClutterModelIter *iter);
gboolean (* is_last) (ClutterModelIter *iter);
ClutterModelIter *(* next) (ClutterModelIter *iter);
ClutterModelIter *(* prev) (ClutterModelIter *iter);
ClutterModel* (* get_model) (ClutterModelIter *iter);
guint (* get_row) (ClutterModelIter *iter);
/*< private >*/
/* padding for future */
void (*_clutter_model_iter_1) (void);
void (*_clutter_model_iter_2) (void);
void (*_clutter_model_iter_3) (void);
void (*_clutter_model_iter_4) (void);
};
GType clutter_model_iter_get_type (void) G_GNUC_CONST;
void clutter_model_iter_get (ClutterModelIter *iter,
...);
void clutter_model_iter_get_valist (ClutterModelIter *iter,
va_list args);
void clutter_model_iter_get_value (ClutterModelIter *iter,
guint column,
GValue *value);
void clutter_model_iter_set (ClutterModelIter *iter,
...);
void clutter_model_iter_set_valist (ClutterModelIter *iter,
va_list args);
void clutter_model_iter_set_value (ClutterModelIter *iter,
guint column,
const GValue *value);
gboolean clutter_model_iter_is_first (ClutterModelIter *iter);
gboolean clutter_model_iter_is_last (ClutterModelIter *iter);
ClutterModelIter *clutter_model_iter_next (ClutterModelIter *iter);
ClutterModelIter *clutter_model_iter_prev (ClutterModelIter *iter);
ClutterModel * clutter_model_iter_get_model (ClutterModelIter *iter);
guint clutter_model_iter_get_row (ClutterModelIter *iter);
G_END_DECLS
#endif /* __CLUTTER_MODEL_H__ */

View File

@ -54,6 +54,7 @@
#include "clutter-layout.h"
#include "clutter-main.h"
#include "clutter-media.h"
#include "clutter-model.h"
#include "clutter-stage.h"
#include "clutter-texture.h"
#include "clutter-timeout-pool.h"

View File

@ -1,3 +1,10 @@
2007-11-15 Neil J. Patel <njp@o-hand.com>
* clutter-docs.sgml:
* clutter-sections.txt:
* clutter.types:
Added support for ClutterModel.
2007-11-14 Emmanuele Bassi <ebassi@openedhand.com>
* clutter-sections.txt: Update with the ClutterScriptable changes

View File

@ -145,6 +145,8 @@
<xi:include href="xml/clutter-color.xml"/>
<xi:include href="xml/clutter-util.xml"/>
<xi:include href="xml/clutter-fixed.xml"/>
<xi:include href="xml/clutter-model.xml"/>
<xi:include href="xml/clutter-model-iter.xml"/>
<xi:include href="xml/clutter-version.xml"/>
<xi:include href="xml/clutter-units.xml"/>
</chapter>

View File

@ -1133,3 +1133,70 @@ CLUTTER_SCRIPTABLE_GET_IFACE
ClutterScriptable
clutter_scriptable_get_type
</SECTION>
<SECTION>
<FILE>clutter-model</FILE>
<TITLE>ClutterModel</TITLE>
ClutterModel
ClutterModelClass
ClutterModelFilterFunc
ClutterModelSortFunc
ClutterModelForeachFunc
clutter_model_new
clutter_model_newv
clutter_model_set_types
clutter_model_append
clutter_model_prepend
clutter_model_insert
clutter_model_insert_value
clutter_model_remove
clutter_model_get_n_rows
clutter_model_get_first_iter
clutter_model_get_last_iter
clutter_model_get_iter_at_row
clutter_model_set_sorting_column
clutter_model_get_sorting_column
clutter_model_foreach
clutter_model_set_sort
clutter_model_set_filter
<SUBSECTION Standard>
CLUTTER_TYPE_MODEL
CLUTTER_MODEL
CLUTTER_IS_MODEL
CLUTTER_MODEL_CLASS
CLUTTER_IS_MODEL_CLASS
CLUTTER_MODEL_GET_CLASS
<SUBSECTION Private>
ClutterModelPrivate
</SECTION>
<SECTION>
<FILE>clutter-model-iter</FILE>
<TITLE>ClutterModelIter</TITLE>
ClutterModelIter
ClutterModelIterClass
clutter_model_iter_get
clutter_model_iter_get_valist
clutter_model_iter_get_value
clutter_model_iter_set
clutter_model_iter_set_valist
clutter_model_iter_set_value
clutter_model_iter_is_first
clutter_model_iter_is_last
clutter_model_iter_next
clutter_model_iter_prev
clutter_model_iter_get_model
clutter_model_iter_get_row
<SUBSECTION Standard>
CLUTTER_TYPE_MODEL_ITER
CLUTTER_MODEL_ITER
CLUTTER_IS_MODEL_ITER
CLUTTER_MODEL_ITER_CLASS
CLUTER_IS_MODEL_ITER_CLASS
CLUTTER_MODEL_GET_CLASS
<SUBSECTION Private>
ClutterModelIterPrivate
</SECTION>

View File

@ -26,3 +26,5 @@ clutter_hbox_get_type
clutter_vbox_get_type
clutter_script_get_type
clutter_scriptable_get_type
clutter_model_get_type
clutter_model_iter_get_type

View File

@ -1,7 +1,7 @@
noinst_PROGRAMS = test-textures test-events test-offscreen test-scale \
test-actors test-behave test-text test-entry test-project \
test-boxes test-perspective test-rotate test-depth \
test-threads test-timeline test-score test-script
test-threads test-timeline test-score test-script test-model
INCLUDES = -I$(top_srcdir)/
LDADD = $(top_builddir)/clutter/libclutter-@CLUTTER_FLAVOUR@-@CLUTTER_MAJORMINOR@.la
@ -25,5 +25,6 @@ test_threads_SOURCES = test-threads.c
test_timeline_SOURCES = test-timeline.c
test_score_SOURCES = test-score.c
test_script_SOURCES = test-script.c
test_model_SOURCES = test-model.c
EXTRA_DIST = redhand.png test-script.json

198
tests/test-model.c Normal file
View File

@ -0,0 +1,198 @@
#include <clutter/clutter.h>
#include <string.h>
static void
print_iter (ClutterModelIter *iter, const gchar *text)
{
gint i;
gchar *string;
clutter_model_iter_get (iter, 0, &i, 1, &string, -1);
g_print ("%s: %d, %s\n", text, i, string);
g_free (string);
}
static gboolean
foreach_func (ClutterModel *model, ClutterModelIter *iter, gpointer null)
{
gint i;
gchar *string;
clutter_model_iter_get (iter, 0, &i, 1, &string, -1);
g_print ("Foreach: %d: %s\n", i, string);
g_free (string);
return TRUE;
}
static gboolean
filter_func (ClutterModel *model, ClutterModelIter *iter, gpointer null)
{
gint i = 0;
clutter_model_iter_get (iter, 0, &i, -1);
return !(i % 2);
}
static gint
sort_func (ClutterModel *model,
const GValue *a,
const GValue *b,
gpointer null)
{
return -1 * strcmp (g_value_get_string (a), g_value_get_string (b));
}
static void
on_row_changed (ClutterModel *model, ClutterModelIter *iter)
{
print_iter (iter, "Changed");
}
static void
filter_model (ClutterModel *model)
{
ClutterModelIter *iter;
clutter_model_set_filter (model, filter_func, NULL, NULL);
iter = clutter_model_get_first_iter (model);
while (!clutter_model_iter_is_last (iter))
{
print_iter (iter, "Filtered Forward Iteration");
iter = clutter_model_iter_next (iter);
}
g_object_unref (iter);
clutter_model_set_sort (model, 1, sort_func, NULL, NULL);
g_signal_connect (model, "row-changed",
G_CALLBACK (on_row_changed), NULL);
iter = clutter_model_get_iter_at_row (model, 0);
clutter_model_iter_set (iter, 1, "Changed string of 0th row, automatically"
" gets sorted", -1);
g_object_unref (iter);
clutter_model_foreach (model, foreach_func, NULL);
clutter_model_set_filter (model, NULL, NULL, NULL);
while (clutter_model_get_n_rows (model))
{
clutter_model_remove (model, 0);
}
clutter_main_quit ();
}
static void
iterate (ClutterModel *model)
{
ClutterModelIter *iter;
iter = clutter_model_get_first_iter (model);
while (!clutter_model_iter_is_last (iter))
{
print_iter (iter, "Forward Iteration");
iter = clutter_model_iter_next (iter);
}
g_object_unref (iter);
iter = clutter_model_get_last_iter (model);
do
{
print_iter (iter, "Reverse Iteration");
iter = clutter_model_iter_prev (iter);
}
while (!clutter_model_iter_is_first (iter));
print_iter (iter, "Reverse Iteration");
g_object_unref (iter);
filter_model (model);
}
static gboolean
populate_model (ClutterModel *model)
{
gint i;
for (i = 0; i < 10; i++)
{
gchar *string = g_strdup_printf ("String %d", i);
clutter_model_append (model,
0, i,
1, string,
-1);
g_free (string);
}
clutter_model_foreach (model, foreach_func, NULL);
iterate (model);
return FALSE;
}
static void
on_row_added (ClutterModel *model, ClutterModelIter *iter, gpointer null)
{
gint i;
gchar *string;
clutter_model_iter_get (iter, 0, &i, 1, &string, -1);
g_print ("Added: %d, %s\n", i, string);
g_free (string);
}
static void
on_row_removed (ClutterModel *model, ClutterModelIter *iter, gpointer null)
{
print_iter (iter, "Removed");
}
static void
on_sort_changed (ClutterModel *model)
{
g_print ("\nSort Changed\n\n");
clutter_model_foreach (model, foreach_func, NULL);
}
static void
on_filter_changed (ClutterModel *model)
{
g_print ("\nFilter Changed\n\n");
}
int
main (int argc, char *argv[])
{
ClutterModel *model;
clutter_init (&argc, &argv);
model = clutter_model_new (2, G_TYPE_INT, G_TYPE_STRING);
g_timeout_add (1000, (GSourceFunc)populate_model, model);
g_signal_connect (model, "row-added",
G_CALLBACK (on_row_added), NULL);
g_signal_connect (model, "row-removed",
G_CALLBACK (on_row_removed), NULL);
g_signal_connect (model, "sort-changed",
G_CALLBACK (on_sort_changed), NULL);
g_signal_connect (model, "filter-changed",
G_CALLBACK (on_filter_changed), NULL);
clutter_main();
g_object_unref (model);
return 0;
}