aboutsummaryrefslogtreecommitdiff
path: root/generic/Text.c
diff options
context:
space:
mode:
authorlecoanet2002-09-02 12:26:42 +0000
committerlecoanet2002-09-02 12:26:42 +0000
commit4d700b3604fdd19d0d2078e286a2e9f0426d677a (patch)
tree53db921b7a08c0aa328c7878ea72a37b9dd1f95c /generic/Text.c
parent38519003e425b85df353f723f3073b5a1babcd1e (diff)
downloadtkzinc-4d700b3604fdd19d0d2078e286a2e9f0426d677a.zip
tkzinc-4d700b3604fdd19d0d2078e286a2e9f0426d677a.tar.gz
tkzinc-4d700b3604fdd19d0d2078e286a2e9f0426d677a.tar.bz2
tkzinc-4d700b3604fdd19d0d2078e286a2e9f0426d677a.tar.xz
* Remplacement de la convention qu'un texte vide est repr�sent�
par une chaine vide par un texte vide est repr�sent� par un pointeur NULL.
Diffstat (limited to 'generic/Text.c')
-rw-r--r--generic/Text.c151
1 files changed, 85 insertions, 66 deletions
diff --git a/generic/Text.c b/generic/Text.c
index 998d9ee..f9743bb 100644
--- a/generic/Text.c
+++ b/generic/Text.c
@@ -187,7 +187,7 @@ Init(Item item,
item->priority = DEFAULT_TEXT_PRIORITY;
text->pos.x = text->pos.y = 0.0;
- text->text = "";
+ text->text = NULL;
text->num_chars = 0;
text->fill_pattern = ZnUnspecifiedImage;
text->anchor = ZnAnchorNW;
@@ -225,7 +225,7 @@ Clone(Item item)
WidgetInfo *wi = item->wi;
char *str;
- if (strlen(text->text) != 0) {
+ if (text->text) {
str = ZnMalloc((strlen(text->text) + 1) * sizeof(char));
strcpy(str, text->text);
text->text = str;
@@ -264,7 +264,7 @@ Destroy(Item item)
{
TextItem text = (TextItem) item;
- if (strlen(text->text) != 0) {
+ if (text->text) {
ZnFree(text->text);
}
if (text->fill_pattern != ZnUnspecifiedImage) {
@@ -323,7 +323,10 @@ Configure(Item item,
}
}
#endif
- num_chars = strlen(text->text);
+ num_chars = 0;
+ if (text->text) {
+ num_chars = strlen(text->text);
+ }
if (text->num_chars != num_chars) {
TextInfo *ti = &item->wi->text_info;
/*
@@ -407,7 +410,7 @@ ComputeCoordinates(Item item,
int font_height, height;
ResetBBox(&item->item_bounding_box);
-
+
Tk_GetFontMetrics(text->font, &fm);
font_height = fm.ascent+fm.descent;
@@ -436,53 +439,54 @@ ComputeCoordinates(Item item,
wrap = 100000;
}
- scan = text->text;
- while (*scan) {
- TextLineInfoStruct info;
- char *ptr;
- int num;
-
- line_index = scan - text->text;
- /*
- * Limit the excursion of Tk_MeasureChars to the end
- * of the line. Do not include \n in the measure done.
- */
- ptr = strchr(scan, '\n');
- if (ptr) {
- num = ptr-scan;
- }
- else {
- num = strlen(scan);
- }
- info.num_chars = Tk_MeasureChars(text->font, scan, num, wrap,
- TK_WHOLE_WORDS|TK_AT_LEAST_ONE,
- &info.width);
-
- info.start = scan;
- text->max_width = MAX(info.width, text->max_width);
-
- scan += info.num_chars;
- /*
- * Adjust for the newline at the end of line.
- */
- if (ptr) {
- scan++;
- }
-
- /* Build a line info even for an empty line
- * at the end of text or for an empty text.
- * It is needed to enable selection and cursor
- * insertion to behave correctly.
- */
- ZnListAdd(text->text_info, &info, ZnListTail);
- /*printf("adding a line : %s, num_chars : %d, width : %d\n",
- info.start, info.num_chars, info.width);*/
-
- /*
- * Skip terminating space or return char if any.
- */
- if (isspace(*scan) || *scan == '\n') {
- scan++;
+ if ((scan = text->text) != NULL) {
+ while (*scan) {
+ TextLineInfoStruct info;
+ char *ptr;
+ int num;
+
+ line_index = scan - text->text;
+ /*
+ * Limit the excursion of Tk_MeasureChars to the end
+ * of the line. Do not include \n in the measure done.
+ */
+ ptr = strchr(scan, '\n');
+ if (ptr) {
+ num = ptr-scan;
+ }
+ else {
+ num = strlen(scan);
+ }
+ info.num_chars = Tk_MeasureChars(text->font, scan, num, wrap,
+ TK_WHOLE_WORDS|TK_AT_LEAST_ONE,
+ &info.width);
+
+ info.start = scan;
+ text->max_width = MAX(info.width, text->max_width);
+
+ scan += info.num_chars;
+ /*
+ * Adjust for the newline at the end of line.
+ */
+ if (ptr) {
+ scan++;
+ }
+
+ /* Build a line info even for an empty line
+ * at the end of text or for an empty text.
+ * It is needed to enable selection and cursor
+ * insertion to behave correctly.
+ */
+ ZnListAdd(text->text_info, &info, ZnListTail);
+ /*printf("adding a line : %s, num_chars : %d, width : %d\n",
+ info.start, info.num_chars, info.width);*/
+
+ /*
+ * Skip terminating space or return char if any.
+ */
+ if (isspace(*scan) || *scan == '\n') {
+ scan++;
+ }
}
}
@@ -581,7 +585,7 @@ ToArea(Item item,
ZnBBox line_bbox, *area = ta->area;
ZnPoint o;
- if (!text->text_info) {
+ if (!text->text_info || !text->text) {
return -1;
}
@@ -708,7 +712,7 @@ Draw(Item item)
int sel_first_line=-1, sel_last_line=-1, cursor_line=-1;
int sel_start_offset=0, sel_stop_offset=0, cursor_offset=0;
- if (!text->text_info) {
+ if (!text->text_info || !text->text) {
return;
}
lines = (TextLineInfo) ZnListArray(text->text_info);
@@ -858,7 +862,7 @@ Render(Item item)
int sel_first_line=-1, sel_last_line=-1, cursor_line=-1;
int sel_start_offset=0, sel_stop_offset=0, cursor_offset=0;
- if (!text->text_info || !text->tfi) {
+ if (!text->text_info || !text->text || !text->tfi) {
return;
}
@@ -1037,7 +1041,7 @@ Pick(Item item,
ZnBBox line_bbox;
ZnPoint o, *p = ps->point;
- if (!text->text_info) {
+ if (!text->text_info || !text->text) {
return dist;
}
@@ -1263,7 +1267,7 @@ MoveFromIndex(TextItem text,
TextLineInfo lines, p;
char *strp;
- if (!text->text_info) {
+ if (!text->text_info || !text->text) {
return index;
}
num_lines = ZnListSize(text->text_info);
@@ -1452,10 +1456,12 @@ InsertChars(Item item,
}
new = ZnMalloc((unsigned) (text->num_chars + length + 1));
- strncpy(new, text->text, (size_t) *index);
+ if (text->text) {
+ strncpy(new, text->text, (size_t) *index);
+ strcpy(new + *index + length, text->text + *index);
+ ZnFree(text->text);
+ }
strcpy(new + *index, chars);
- strcpy(new + *index + length, text->text + *index);
- ZnFree(text->text);
text->text = new;
text->num_chars += length;
@@ -1484,6 +1490,9 @@ DeleteChars(Item item,
int count;
char *new;
+ if (!text->text) {
+ return;
+ }
if (*first < 0) {
*first = 0;
}
@@ -1495,12 +1504,19 @@ DeleteChars(Item item,
}
count = *last + 1 - *first;
- new = (char *) ZnMalloc((unsigned) (text->num_chars + 1 - count));
- strncpy(new, text->text, (size_t) *first);
- strcpy(new + *first, text->text + *last + 1);
- ZnFree(text->text);
- text->text = new;
- text->num_chars -= count;
+ if (text->num_chars - count) {
+ new = (char *) ZnMalloc((unsigned) (text->num_chars + 1 - count));
+ strncpy(new, text->text, (size_t) *first);
+ strcpy(new + *first, text->text + *last + 1);
+ ZnFree(text->text);
+ text->text = new;
+ text->num_chars -= count;
+ }
+ else {
+ ZnFree(text->text);
+ text->text = NULL;
+ text->num_chars = 0;
+ }
if (text->insert_index > *first) {
text->insert_index -= count;
@@ -1558,6 +1574,9 @@ Selection(Item item,
TextInfo *ti = &wi->text_info;
int count;
+ if (!text->text) {
+ return 0;
+ }
count = ti->sel_last - ti->sel_first - offset;
if (count > max_chars) {
count = max_chars;