From 8c1c8274963543c45ba3717d1156d9edaa78cdc2 Mon Sep 17 00:00:00 2001 From: Jon Trowbridge Date: Fri, 26 Jan 2001 22:10:51 +0000 Subject: Added; a new test program that demonstrates objects in ETexts. 2001-01-26 Jon Trowbridge * gal/e-text/e-text-model-test.c: Added; a new test program that demonstrates objects in ETexts. * gal/e-text/e-text-model-uri.c: Added; a text model that converts URIs in the text into objects that are passed off to the GNOME URI handler when activated. This is actually still extremely broken; I got it just working enough to test out my EText changes. * gal/e-text/e-text.c: A whole lot of changes, designed to make ETextModel objects render properly. The basic idea of the changes is pretty simple, though. (text_width_with_objects): First of all, this function is an alternative to e_font_utf8_text_width that takes into the account the embedded \1s in the text string and properly accounts for the width of the object strings. (unicode_strlen_with_objects): Next, this function finds the proper strlen of a string, expanding the \1s. (text_draw_with_objects): Finally, this is just a replacement for e_font_draw_utf8_text that does the right thing for objects. I've gone through all of e-text.c and replace calls by those original functions with my new object-enabled alternatives. (split_into_lines): Some tweaking to get line breaking to work properly. Made \1 into a "break character", so that we can break lines between multiple adjacent objects. (Which seemed like the right thing to do, but there may be cases where that is undesireable.) (_get_position_from_xy): Fixed to properly handle embedded objects, and to get the right selection semantics for objects. (Or at least semantics that feel right to me.) Also fixed a bug that caused selection, etc. to not work properly if the text was anchored anywhere other than with GTK_ANCHOR_NORTH*. (_get_position): Hacked to cause objects to activate when they are double-clicked. There is probably a better way to do this. * gal/e-text/e-text-model.c (e_text_model_real_object_count): Provide a default implementation of an object counter. Derived classes might want to override this for efficiency reasons. (e_text_model_strdup_expanded_text): Added. Allocates and returns a string contains the model's text with the objects "expanded" within. * gal/e-text/e-text-model.h: Added obj_count, get_nth_obj, and activate_nth_obj virtual methods to ETextModelClass. svn path=/trunk/; revision=7842 --- widgets/text/e-text-model-uri.c | 174 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 widgets/text/e-text-model-uri.c (limited to 'widgets/text/e-text-model-uri.c') diff --git a/widgets/text/e-text-model-uri.c b/widgets/text/e-text-model-uri.c new file mode 100644 index 0000000000..bd8dbb18e8 --- /dev/null +++ b/widgets/text/e-text-model-uri.c @@ -0,0 +1,174 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ + +/* ETextModelURI - A Text Model w/ clickable URIs + * Copyright (C) 2001 Ximian, Inc. + * + * Author: Jon Trowbridge + * + */ + +#include +#include +#include "e-text-model-uri.h" + +static void e_text_model_uri_class_init (ETextModelURIClass *class); +static void e_text_model_uri_init (ETextModelURI *model); +static void e_text_model_uri_destroy (GtkObject *object); + +static void objectify_uris (ETextModelURI *model); + +static void e_text_model_uri_set_text (ETextModel *model, gchar *text); +static const gchar *e_text_model_uri_get_nth_object (ETextModel *model, gint); +static void e_text_model_uri_activate_nth_object (ETextModel *model, gint); + +static GtkObject *parent_class; + +GtkType +e_text_model_uri_get_type (void) +{ + static GtkType model_uri_type = 0; + + if (!model_uri_type) { + GtkTypeInfo model_uri_info = { + "ETextModelURI", + sizeof (ETextModelURI), + sizeof (ETextModelURIClass), + (GtkClassInitFunc) e_text_model_uri_class_init, + (GtkObjectInitFunc) e_text_model_uri_init, + NULL, /* reserved_1 */ + NULL, /* reserved_2 */ + (GtkClassInitFunc) NULL + }; + + model_uri_type = gtk_type_unique (e_text_model_get_type (), &model_uri_info); + } + + return model_uri_type; +} + +static void +e_text_model_uri_class_init (ETextModelURIClass *klass) +{ + GtkObjectClass *object_class; + ETextModelClass *model_class; + + object_class = (GtkObjectClass *) klass; + model_class = E_TEXT_MODEL_CLASS (klass); + + parent_class = gtk_type_class (e_text_model_get_type ()); + + object_class->destroy = e_text_model_uri_destroy; + + model_class->set_text = e_text_model_uri_set_text; + model_class->get_nth_obj = e_text_model_uri_get_nth_object; + model_class->activate_nth_obj = e_text_model_uri_activate_nth_object; +} + +static void +e_text_model_uri_init (ETextModelURI *model) +{ + +} + +static void +e_text_model_uri_destroy (GtkObject *object) +{ + if (GTK_OBJECT_CLASS (parent_class)->destroy) + (* GTK_OBJECT_CLASS (parent_class)->destroy) (object); + +} + +static gchar * +extract_uri (gchar **in_str) +{ + gchar *s = *in_str; + if (strncmp (s, "http://", 7) == 0) { + gint periods=0; + gchar *uri; + + s += 7; + + while (*s && (isalnum((gint) *s) || (*s == '.' && periods < 2))) { + if (*s == '.') + ++periods; + ++s; + } + + uri = g_strndup (*in_str, s - *in_str); + *in_str = s; + return uri; + + } else { + *in_str = s+1; + return NULL; + } +} + +static void +objectify_uris (ETextModelURI *model_uri) +{ + ETextModel *model = E_TEXT_MODEL (model_uri); + gchar *new_text; + gchar *src, *dest; + GList *uris = NULL; + + if (model->text == NULL) + return; + + new_text = g_new0 (gchar, strlen (model->text)+1); + + src = model->text; + dest = new_text; + + while (*src) { + gchar *uri_str; + gchar *next = src; + if ( (uri_str = extract_uri (&next)) ) { + uris = g_list_append (uris, uri_str); + *dest = '\1'; + } else { + *dest = *src; + } + ++dest; + src = next; + } + + g_free (model->text); + model->text = new_text; + + /* Leaking old list */ + model_uri->uris = uris; +} + +static void +e_text_model_uri_set_text (ETextModel *model, gchar *text) +{ + if (E_TEXT_MODEL_CLASS(parent_class)->set_text) + E_TEXT_MODEL_CLASS(parent_class)->set_text (model, text); + + objectify_uris (E_TEXT_MODEL_URI (model)); +} + +static const gchar * +e_text_model_uri_get_nth_object (ETextModel *model, gint i) +{ + return (const gchar *) g_list_nth_data (E_TEXT_MODEL_URI (model)->uris, i); +} + +static void +e_text_model_uri_activate_nth_object (ETextModel *model, gint i) +{ + const gchar *obj_str; + + obj_str = e_text_model_get_nth_object (model, i); + gnome_url_show (obj_str); +} + +ETextModel * +e_text_model_uri_new (void) +{ + return E_TEXT_MODEL (gtk_type_new (e_text_model_uri_get_type ())); +} + + +/* $Id$ */ -- cgit v1.2.3