aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/text/e-text-model.c
diff options
context:
space:
mode:
Diffstat (limited to 'widgets/text/e-text-model.c')
-rw-r--r--widgets/text/e-text-model.c134
1 files changed, 134 insertions, 0 deletions
diff --git a/widgets/text/e-text-model.c b/widgets/text/e-text-model.c
index 6ca650867c..acd08ecb04 100644
--- a/widgets/text/e-text-model.c
+++ b/widgets/text/e-text-model.c
@@ -37,6 +37,11 @@ static void e_text_model_real_insert(ETextModel *model, gint postion, gchar *tex
static void e_text_model_real_insert_length(ETextModel *model, gint postion, gchar *text, gint length);
static void e_text_model_real_delete(ETextModel *model, gint postion, gint length);
+static gint e_text_model_real_object_count(ETextModel *model);
+static const gchar *e_text_model_real_get_nth_object(ETextModel *model, gint n);
+static void e_text_model_real_activate_nth_object(ETextModel *mode, gint n);
+
+
static GtkObject *parent_class;
@@ -99,6 +104,9 @@ e_text_model_class_init (ETextModelClass *klass)
klass->insert = e_text_model_real_insert;
klass->insert_length = e_text_model_real_insert_length;
klass->delete = e_text_model_real_delete;
+ klass->obj_count = e_text_model_real_object_count;
+ klass->get_nth_obj = e_text_model_real_get_nth_object;
+ klass->activate_nth_obj = e_text_model_real_activate_nth_object;
object_class->destroy = e_text_model_destroy;
}
@@ -173,6 +181,34 @@ e_text_model_real_delete(ETextModel *model, gint position, gint length)
e_text_model_changed(model);
}
+static gint
+e_text_model_real_object_count(ETextModel *model)
+{
+ gint count = 0;
+ gchar *c = model->text;
+
+ if (c) {
+ while (*c) {
+ if (*c == '\1')
+ ++count;
+ ++c;
+ }
+ }
+ return count;
+}
+
+static const gchar *
+e_text_model_real_get_nth_object(ETextModel *model, gint n)
+{
+ return "";
+}
+
+static void
+e_text_model_real_activate_nth_object(ETextModel *model, gint n)
+{
+ /* By default, do nothing */
+}
+
void
e_text_model_changed(ETextModel *model)
{
@@ -235,6 +271,104 @@ e_text_model_delete(ETextModel *model, gint position, gint length)
E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->delete(model, position, length);
}
+gint
+e_text_model_object_count(ETextModel *model)
+{
+ g_return_val_if_fail (model != NULL, 0);
+ g_return_val_if_fail (E_IS_TEXT_MODEL (model), 0);
+
+ if ( E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->obj_count)
+ return E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->obj_count(model);
+ else
+ return 0;
+}
+
+const gchar *
+e_text_model_get_nth_object(ETextModel *model, gint n)
+{
+ g_return_val_if_fail (model != NULL, NULL);
+ g_return_val_if_fail (E_IS_TEXT_MODEL (model), NULL);
+ g_return_val_if_fail (n >= 0, NULL);
+
+ if ( E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->get_nth_obj )
+ return E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->get_nth_obj(model, n);
+ else
+ return "";
+}
+
+void
+e_text_model_activate_nth_object(ETextModel *model, gint n)
+{
+ g_return_if_fail (model != NULL);
+ g_return_if_fail (E_IS_TEXT_MODEL (model));
+ g_return_if_fail (n >= 0);
+
+ if ( E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->activate_nth_obj )
+ E_TEXT_MODEL_CLASS(GTK_OBJECT(model)->klass)->activate_nth_obj(model, n);
+}
+
+gchar *
+e_text_model_strdup_expanded_text(ETextModel *model)
+{
+ gint len = 0, i, N;
+ gchar *expanded, *dest;
+ const gchar *src;
+
+ g_return_val_if_fail (model != NULL, NULL);
+ g_return_val_if_fail (E_IS_TEXT_MODEL (model), NULL);
+
+ if (model->text == NULL)
+ return NULL;
+
+ N = e_text_model_object_count (model);
+ if (N == 0)
+ return g_strdup (model->text);
+
+ /* First, compute the length of the expanded string. */
+
+ len = strlen (model->text);
+ len -= N; /* Subtract out the \1s that signify the objects. */
+
+ for (i=0; i<N; ++i)
+ len += strlen (e_text_model_get_nth_object (model ,i));
+
+
+ /* Next, allocate and build the expanded string. */
+ expanded = g_new0 (gchar, len+1);
+
+ src = model->text;
+ dest = expanded;
+ i = 0;
+ while (*src) {
+ if (*src == '\1') {
+ const gchar *src_obj;
+
+ g_assert (i < N);
+ src_obj = e_text_model_get_nth_object (model, i);
+
+ if (src_obj) {
+ while (*src_obj) {
+ *dest = *src_obj;
+ ++src_obj;
+ ++dest;
+ }
+ }
+
+ ++src;
+ ++i;
+
+ } else {
+
+ *dest = *src;
+ ++src;
+ ++dest;
+
+ }
+ }
+
+ return expanded;
+}
+
ETextModel *
e_text_model_new(void)
{