aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/text/e-text-model.c
diff options
context:
space:
mode:
authorChris Toshok <toshok@ximian.com>2003-05-14 08:42:36 +0800
committerChris Toshok <toshok@src.gnome.org>2003-05-14 08:42:36 +0800
commit2579e70339ec9fe523115b654f457b6a07367de8 (patch)
tree0f095610a5d1110f06225cb9833e95ace746d92a /widgets/text/e-text-model.c
parent9c06fd0d7f85ad322ba9e996e64587f60ad19d9d (diff)
downloadgsoc2013-evolution-2579e70339ec9fe523115b654f457b6a07367de8.tar.gz
gsoc2013-evolution-2579e70339ec9fe523115b654f457b6a07367de8.tar.zst
gsoc2013-evolution-2579e70339ec9fe523115b654f457b6a07367de8.zip
[ fixes several utf8/pango related problems, including bugs #41288,
2003-05-13 Chris Toshok <toshok@ximian.com> [ fixes several utf8/pango related problems, including bugs #41288, #42596, #42604 ] * configure.in (GAL_CURRENT): bump to 3, per mkestner. (GAL_REVISION): drop to 0, per mkestner. * gal/e-text/e-text.c (reset_layout_attrs): we need to convert the start/end bounds of the object to byte indices for the attribute. (reset_layout): in the layout == NULL case don't create the layout then immediately set it again with the same text. also, we need to convert selection_start to a byte index before calling pango_layout_get_cursor_pos. (e_text_draw): remove some #ifdef 0'd code, move the calculation of our initial clip_rect below the xpos/ypos assignments so we don't duplicate the expression. Fix the selection drawing in the multiline case so that it actually works, instead of assuming that all ETexts only have 1 line *boggle*. (get_position_from_xy): this needs to return a utf8 offset. (e_text_copy_clipboard): convert sel_start/sel_end to byte indices before copying. (primary_get_cb): same. (paste_received): validate the input here, and drop the length parameter from e_text_insert. (next_word): convert from an utf8 offset on entry to this function, and return a utf8 offset when we're done. also, remove the call the g_unichar_validate. we validate at all points where text is inserted. (find_offset_into_line): new function used in the backward/forward line code. find the utf8 offset into a line (the number of utf8 characters from a prior \n or beginning of the string.) (_get_position): in general there are lots of changes here because text->selection_start/text->selection_end are utf8 offsets, not byte offsets. fix E_TEP_START_OF_LINE so that hitting Ctrl-a when you're at the beginning of a line doesn't take you to the beginning of the previous line. fix E_TEP_END_OF_LINE in an analogous fashion. for E_TEP_FORWARD_CHARACTER we just increment by 1. for E_TEP_BACKWARD_CHARACTER we just decrement by 1. for E_TEP_BACKWARD_WORD we drop the g_unichar_validate call and simplify things a bit. reimplement E_TEP_FORWARD_LINE/E_TEP_BACKWARD_LINE so they find the current offset into the line, then scan forward/backward for the next/prev line, and put us at the right offset on that line. fix E_TEP_SELECT_WORD so double clicking in the space between words doesn't select both words - if you double click on the trailing edge of the space, it selects the next word. leading edge selects the previous one. for E_TEP_SELECT_ALL use g_utf8_strlen. (e_text_insert): everything that calls this passes a \0 terminated string, so we assume it's \0 terminated (the old code did as well, with calls to strlen) and drop the length parameter. also make sure this is all utf8 happy. (capitalize): use g_utf8_offset_to_pointer instead of just adding text->text and start/end, and remove the validate call. also fix the call to e_text_model_delete and use e_text_model_insert_length instead of e_text_model_insert. (e_text_command): for E_TEP_INSERT, validate the input. for E_TEP_CAPS just use MAX instead of the neat little hack. also, fix the scrolling so that it scrolls properly in both X and Y directions (there are still some hiccups but it's much much better than previously). (e_text_commit_cb): validate the input here. * gal/e-text/e-text-model.c (struct _ETextModelPrivate): just use a GString here and get rid of MAX_LENGTH. (e_text_model_dispose): free GString. (e_text_model_real_validate_position): clean this up a bit. (e_text_model_real_get_text): return the contents of the GString. (e_text_model_real_get_text_length): use g_utf8_strlen here. (e_text_model_real_set_text): convert to GString (e_text_model_real_insert): just call e_text_model_insert_length here instead of duplicating the function. (e_text_model_real_insert_length): convert to utf8/gstring. i.e. convert @position and @length to a bytes and use g_string_insert_len. (e_text_model_real_delete): same, with g_string_erase. (e_text_model_get_text_length): use g_utf8_strlen (e_text_model_strdup_nth_object): convert the length of the object to bytes before copying. (e_text_model_get_nth_object_bounds): calculate start/end properly for u svn path=/trunk/; revision=21163
Diffstat (limited to 'widgets/text/e-text-model.c')
-rw-r--r--widgets/text/e-text-model.c144
1 files changed, 56 insertions, 88 deletions
diff --git a/widgets/text/e-text-model.c b/widgets/text/e-text-model.c
index 0233f14aa7..d995d81396 100644
--- a/widgets/text/e-text-model.c
+++ b/widgets/text/e-text-model.c
@@ -32,8 +32,6 @@
#include "e-text-model.h"
#include "gal/util/e-util.h"
-#define MAX_LENGTH (2047)
-
enum {
E_TEXT_MODEL_CHANGED,
E_TEXT_MODEL_REPOSITION,
@@ -45,8 +43,7 @@ enum {
static guint e_text_model_signals[E_TEXT_MODEL_LAST_SIGNAL] = { 0 };
struct _ETextModelPrivate {
- gchar *text;
- gint len;
+ GString *text;
};
static void e_text_model_class_init (ETextModelClass *class);
@@ -157,8 +154,7 @@ static void
e_text_model_init (ETextModel *model)
{
model->priv = g_new0 (struct _ETextModelPrivate, 1);
- model->priv->text = g_strdup ("");
- model->priv->len = 0;
+ model->priv->text = g_string_new ("");
}
/* Dispose handler for the text item */
@@ -173,7 +169,7 @@ e_text_model_dispose (GObject *object)
model = E_TEXT_MODEL (object);
if (model->priv) {
- g_free (model->priv->text);
+ g_string_free (model->priv->text, TRUE);
g_free (model->priv);
model->priv = NULL;
@@ -186,11 +182,11 @@ e_text_model_dispose (GObject *object)
static gint
e_text_model_real_validate_position (ETextModel *model, gint pos)
{
- gint len;
+ gint len = e_text_model_get_text_length (model);
if (pos < 0)
pos = 0;
- else if (pos > ( len = e_text_model_get_text_length (model) ))
+ else if (pos > len)
pos = len;
return pos;
@@ -200,7 +196,7 @@ static const gchar *
e_text_model_real_get_text (ETextModel *model)
{
if (model->priv->text)
- return model->priv->text;
+ return model->priv->text->str;
else
return "";
}
@@ -208,10 +204,7 @@ e_text_model_real_get_text (ETextModel *model)
static gint
e_text_model_real_get_text_length (ETextModel *model)
{
- if (model->priv->len < 0)
- model->priv->len = strlen (e_text_model_get_text (model));
-
- return model->priv->len;
+ return g_utf8_strlen (model->priv->text->str, -1);
}
static void
@@ -221,18 +214,13 @@ e_text_model_real_set_text (ETextModel *model, const gchar *text)
gboolean changed = FALSE;
if (text == NULL) {
+ changed = (*model->priv->text->str != '\0');
- changed = (model->priv->text != NULL);
-
- g_free (model->priv->text);
- model->priv->text = NULL;
- model->priv->len = -1;
+ g_string_set_size (model->priv->text, 0);
- } else if (model->priv->text == NULL || strcmp (model->priv->text, text)) {
+ } else if (*model->priv->text->str == '\0' || strcmp (model->priv->text->str, text)) {
- g_free (model->priv->text);
- model->priv->text = g_strndup (text, MAX_LENGTH);
- model->priv->len = -1;
+ g_string_assign (model->priv->text, text);
changed = TRUE;
}
@@ -248,41 +236,7 @@ e_text_model_real_set_text (ETextModel *model, const gchar *text)
static void
e_text_model_real_insert (ETextModel *model, gint position, const gchar *text)
{
- EReposInsertShift repos;
- gchar *new_text;
- gint length;
-
- if (model->priv->len < 0)
- e_text_model_real_get_text_length (model);
- length = strlen(text);
-
- if (length + model->priv->len > MAX_LENGTH)
- length = MAX_LENGTH - model->priv->len;
- if (length <= 0)
- return;
-
- /* Can't use g_strdup_printf here because on some systems
- printf ("%.*s"); is locale dependent. */
- new_text = e_strdup_append_strings (model->priv->text, position,
- text, length,
- model->priv->text + position, -1,
- NULL);
-
- if (model->priv->text)
- g_free (model->priv->text);
-
- model->priv->text = new_text;
-
- if (model->priv->len >= 0)
- model->priv->len += length;
-
- e_text_model_changed (model);
-
- repos.model = model;
- repos.pos = position;
- repos.len = length;
-
- e_text_model_reposition (model, e_repos_insert_shift, &repos);
+ e_text_model_insert_length (model, position, text, strlen (text));
}
static void
@@ -290,34 +244,31 @@ e_text_model_real_insert_length (ETextModel *model, gint position, const gchar *
{
EReposInsertShift repos;
gchar *new_text;
+ int model_len = e_text_model_real_get_text_length (model);
+ char *offs;
+ const char *p;
+ int byte_length, l;
- if (model->priv->len < 0)
- e_text_model_real_get_text_length (model);
-
- if (length + model->priv->len > MAX_LENGTH)
- length = MAX_LENGTH - model->priv->len;
- if (length <= 0)
+ if (position > model_len)
return;
- /* Can't use g_strdup_printf here because on some systems
- printf ("%.*s"); is locale dependent. */
- new_text = e_strdup_append_strings (model->priv->text, position,
- text, length,
- model->priv->text + position, -1,
- NULL);
+ offs = g_utf8_offset_to_pointer (model->priv->text->str, position);
- if (model->priv->text)
- g_free (model->priv->text);
- model->priv->text = new_text;
+ for (p = text, l = 0;
+ l < length;
+ p = g_utf8_next_char (p), l ++) ;
- if (model->priv->len >= 0)
- model->priv->len += length;
+ byte_length = p - text;
+
+ g_string_insert_len (model->priv->text,
+ offs - model->priv->text->str,
+ text, byte_length);
e_text_model_changed (model);
repos.model = model;
- repos.pos = position;
- repos.len = length;
+ repos.pos = position;
+ repos.len = length;
e_text_model_reposition (model, e_repos_insert_shift, &repos);
}
@@ -326,11 +277,21 @@ static void
e_text_model_real_delete (ETextModel *model, gint position, gint length)
{
EReposDeleteShift repos;
-
- memmove (model->priv->text + position, model->priv->text + position + length, strlen (model->priv->text + position + length) + 1);
-
- if (model->priv->len >= 0)
- model->priv->len -= length;
+ int byte_position, byte_length;
+ char *offs, *p;
+ int l;
+
+ offs = g_utf8_offset_to_pointer (model->priv->text->str, position);
+ byte_position = offs - model->priv->text->str;
+
+ for (p = offs, l = 0;
+ l < length;
+ p = g_utf8_next_char (p), l ++) ;
+
+ byte_length = p - offs;
+
+ g_string_erase (model->priv->text,
+ byte_position, byte_length);
e_text_model_changed (model);
@@ -415,7 +376,7 @@ e_text_model_get_text_length (ETextModel *model)
#ifdef PARANOID_DEBUGGING
const gchar *str = e_text_model_get_text (model);
- gint len2 = str ? strlen (str) : 0;
+ gint len2 = str ? g_utf8_strlen (str, -1) : 0;
if (len != len)
g_error ("\"%s\" length reported as %d, not %d.", str, len, len2);
#endif
@@ -425,7 +386,7 @@ e_text_model_get_text_length (ETextModel *model)
} else {
/* Calculate length the old-fashioned way... */
const gchar *str = e_text_model_get_text (model);
- return str ? strlen (str) : 0;
+ return str ? g_utf8_strlen (str, -1) : 0;
}
}
@@ -548,8 +509,15 @@ e_text_model_strdup_nth_object (ETextModel *model, gint n)
g_return_val_if_fail (E_IS_TEXT_MODEL (model), NULL);
obj = e_text_model_get_nth_object (model, n, &len);
-
- return obj ? g_strndup (obj, n) : NULL;
+
+ if (obj) {
+ gint byte_len;
+ byte_len = g_utf8_offset_to_pointer (obj, len) - obj;
+ return g_strndup (obj, byte_len);
+ }
+ else {
+ return NULL;
+ }
}
void
@@ -567,9 +535,9 @@ e_text_model_get_nth_object_bounds (ETextModel *model, gint n, gint *start, gint
g_return_if_fail (obj != NULL);
if (start)
- *start = obj - txt;
+ *start = g_utf8_pointer_to_offset (txt, obj);
if (end)
- *end = obj - txt + len;
+ *end = *start + len;
}
gint