From 4d700b3604fdd19d0d2078e286a2e9f0426d677a Mon Sep 17 00:00:00 2001 From: lecoanet Date: Mon, 2 Sep 2002 12:26:42 +0000 Subject: * 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. --- generic/Field.c | 149 ++++++++++++++++++++++++++++++++++++------------------- generic/Text.c | 151 +++++++++++++++++++++++++++++++------------------------- 2 files changed, 183 insertions(+), 117 deletions(-) diff --git a/generic/Field.c b/generic/Field.c index 3cd5036..286e5fb 100644 --- a/generic/Field.c +++ b/generic/Field.c @@ -587,7 +587,10 @@ ComputeFieldTextLocation(Field field_ptr, Tk_FontMetrics fm; Tk_GetFontMetrics(field_ptr->font, &fm); - w = ZnTextWidth(field_ptr->font, field_ptr->text, strlen(field_ptr->text)); + w = 0; + if (field_ptr->text) { + w = ZnTextWidth(field_ptr->font, field_ptr->text, strlen(field_ptr->text)); + } h = fm.ascent + fm.descent; text_bbox->orig.y = (bbox->orig.y + bbox->corner.y - h) / 2.0; text_bbox->corner.y = text_bbox->orig.y + h; @@ -649,7 +652,7 @@ LeaderToLabel(FieldSet field_set, * its own, don't clip. */ if (ISCLEAR(field_ptr->flags, FIELD_VISIBLE_BIT) || - ((*field_ptr->text == 0) && + (!field_ptr->text && ISCLEAR(field_ptr->flags, FILLED_BIT) && (field_ptr->border_edges == NO_BORDER) && (field_ptr->relief == RELIEF_FLAT) && @@ -665,10 +668,9 @@ LeaderToLabel(FieldSet field_set, /* * Adjust leader on real text, not on field boundaries. This is * important when there are leading and trailing spaces. - * The correct test here is really *field_ptr->text, an empty - * text is represented by an empty string NOT by a NULL pointer. */ - if (*field_ptr->text && ISCLEAR(field_ptr->flags, FILLED_BIT) && + if (field_ptr->text && + ISCLEAR(field_ptr->flags, FILLED_BIT) && (field_ptr->border_edges == NO_BORDER) && (field_ptr->relief == RELIEF_FLAT) && (field_ptr->image == ZnUnspecifiedImage)) { @@ -829,7 +831,7 @@ InitFields(FieldSet field_set) CLEAR(field->flags, FILLED_BIT); CLEAR(field->flags, CACHE_OK); field->fill_pattern = ZnUnspecifiedImage; - field->text = ""; + field->text = NULL; field->image = ZnUnspecifiedImage; field->tile = ZnUnspecifiedImage; field->font = Tk_GetFont(wi->interp, wi->win, Tk_NameOfFont(wi->font)); @@ -910,7 +912,7 @@ CloneFields(FieldSet field_set) field->fill_color = ZnGetGradientByValue(field->fill_color); field->border_color = ZnGetGradientByValue(field->border_color); - if (strlen(field->text) != 0) { + if (field->text) { text = (char *) ZnMalloc((strlen(field->text) + 1) * sizeof(char)); strcpy(text, field->text); field->text = text; @@ -950,14 +952,20 @@ ConfigureField(FieldSet fs, field_ptr = &fs->fields[field]; old_font = field_ptr->font; - old_num_chars = strlen(field_ptr->text); + old_num_chars = 0; + if (field_ptr->text) { + old_num_chars = strlen(field_ptr->text); + } if (ZnConfigureAttributes(wi, field_ptr, field_attrs, argc, argv, flags) == ZN_ERROR) { return ZN_ERROR; } - num_chars = strlen(field_ptr->text); + num_chars = 0; + if (field_ptr->text) { + num_chars = strlen(field_ptr->text); + } if (old_num_chars != num_chars) { TextInfo *ti = &wi->text_info; /* @@ -1086,7 +1094,7 @@ FreeFields(FieldSet field_set) for (i = 0; i < num_fields; i++) { field = &field_set->fields[i]; - if (strlen(field->text) != 0) { + if (field->text) { ZnFree(field->text); } if (field->gradient) { @@ -1149,7 +1157,10 @@ FieldPointToChar(FieldSet fs, int n, dummy; field_ptr = &fs->fields[field]; - num_chars = strlen(field_ptr->text); + num_chars = 0; + if (field_ptr->text) { + num_chars = strlen(field_ptr->text); + } if (num_chars == 0) { return 0; @@ -1198,6 +1209,10 @@ WordMoveFromIndex(char *text, { char *strp; + if (!text) { + return index; + } + if (fwd) { strp = &text[index]; while ((strp[1] == ' ') || (strp[1] == '\n')) { @@ -1241,7 +1256,10 @@ FieldIndex(FieldSet fs, } field_ptr = &fs->fields[field]; - num_chars = strlen(field_ptr->text); + num_chars = 0; + if (field_ptr->text) { + num_chars = strlen(field_ptr->text); + } p = Tcl_GetString(index_spec); c = p[0]; @@ -1346,7 +1364,10 @@ FieldInsertChars(FieldSet fs, } field_ptr = &fs->fields[field]; - num_chars = strlen(field_ptr->text); + num_chars = 0; + if (field_ptr->text) { + num_chars = strlen(field_ptr->text); + } if (*index < 0) { *index = 0; } @@ -1355,10 +1376,19 @@ FieldInsertChars(FieldSet fs, } new = ZnMalloc(num_chars + length + 1); - strncpy(new, field_ptr->text, (size_t) *index); + if (num_chars) { + /* + * Copy the part before and the part after the new + * text (if any). + */ + strncpy(new, field_ptr->text, (size_t) *index); + strcpy(new + *index + length, field_ptr->text + *index); + ZnFree(field_ptr->text); + } + /* + * Insert the new text. + */ strcpy(new + *index, chars); - strcpy(new + *index + length, field_ptr->text + *index); - ZnFree(field_ptr->text); field_ptr->text = new; if (field_ptr->insert_index >= *index) { @@ -1387,7 +1417,10 @@ FieldDeleteChars(FieldSet fs, } field_ptr = &fs->fields[field]; - num_chars = strlen(field_ptr->text); + num_chars = 0; + if (field_ptr->text) { + num_chars = strlen(field_ptr->text); + } if (num_chars == 0) { return False; } @@ -1403,11 +1436,17 @@ FieldDeleteChars(FieldSet fs, } count = *last + 1 - *first; - new = ZnMalloc(num_chars + 1 - count); - strncpy(new, field_ptr->text, (size_t) *first); - strcpy(new + *first, field_ptr->text + *last + 1); - ZnFree(field_ptr->text); - field_ptr->text = new; + if (num_chars - count) { + new = ZnMalloc(num_chars + 1 - count); + strncpy(new, field_ptr->text, (size_t) *first); + strcpy(new + *first, field_ptr->text + *last + 1); + ZnFree(field_ptr->text); + field_ptr->text = new; + } + else { + ZnFree(field_ptr->text); + field_ptr->text = NULL; + } /* * Update the cursor to reflect the new string. @@ -1439,7 +1478,10 @@ FieldCursor(FieldSet fs, } field_ptr = &fs->fields[field]; - num_chars = strlen(field_ptr->text); + num_chars = 0; + if (field_ptr->text) { + num_chars = strlen(field_ptr->text); + } if (index < 0) { field_ptr->insert_index = 0; @@ -1473,7 +1515,10 @@ FieldSelection(FieldSet fs, count = max_chars; } field_ptr = &fs->fields[field]; - num_chars = strlen(field_ptr->text); + num_chars = 0; + if (field_ptr->text) { + num_chars = strlen(field_ptr->text); + } if (count > num_chars) { count = num_chars; } @@ -1597,32 +1642,34 @@ FieldsEngine(FieldSet field_set, restore |= val < 0; cursor = -1; - if ((field_set->item == wi->focus_item) && (wi->focus_field == i) && - wi->got_focus && ti->cursor_on) { - cursor = Tk_TextWidth(field_ptr->font, field_ptr->text, - field_ptr->insert_index); - } - ComputeFieldTextLocation(field_ptr, &bbox, &text_pos, &text_bbox); - num_chars = strlen(field_ptr->text); - if (num_chars) { - if ((field_set->item == ti->sel_item) && (ti->sel_field == i) && - (ti->sel_last >= 0) && (ti->sel_first <= num_chars)) { - sel_start = Tk_TextWidth(field_ptr->font, - field_ptr->text, ti->sel_first); - sel_stop = Tk_TextWidth(field_ptr->font, - field_ptr->text, ti->sel_last); + if (field_ptr->text) { + if ((field_set->item == wi->focus_item) && + (wi->focus_field == i) && wi->got_focus && ti->cursor_on) { + cursor = Tk_TextWidth(field_ptr->font, field_ptr->text, + field_ptr->insert_index); + } + ComputeFieldTextLocation(field_ptr, &bbox, &text_pos, &text_bbox); + num_chars = strlen(field_ptr->text); + if (num_chars) { + if ((field_set->item == ti->sel_item) && (ti->sel_field == i) && + (ti->sel_last >= 0) && (ti->sel_first <= num_chars)) { + sel_start = Tk_TextWidth(field_ptr->font, + field_ptr->text, ti->sel_first); + sel_stop = Tk_TextWidth(field_ptr->font, + field_ptr->text, ti->sel_last); + } + + IntersectBBox(&fclip_bbox, &text_bbox, &tmp_bbox); + + val = tmp_bbox.orig.x - text_bbox.orig.x; + restore |= val > 0; + val = tmp_bbox.orig.y - text_bbox.orig.y; + restore |= val > 0; + val = tmp_bbox.corner.x - text_bbox.corner.x; + restore |= val < 0; + val = tmp_bbox.corner.y - text_bbox.corner.y; + restore |= val < 0; } - - IntersectBBox(&fclip_bbox, &text_bbox, &tmp_bbox); - - val = tmp_bbox.orig.x - text_bbox.orig.x; - restore |= val > 0; - val = tmp_bbox.orig.y - text_bbox.orig.y; - restore |= val > 0; - val = tmp_bbox.corner.x - text_bbox.corner.x; - restore |= val < 0; - val = tmp_bbox.corner.y - text_bbox.corner.y; - restore |= val < 0; } if (field_ptr->image != ZnUnspecifiedImage) { @@ -1761,7 +1808,7 @@ DrawField(WidgetInfo *wi, } } } - else { + else if (field_ptr->text) { /* * Draw the text. */ @@ -1941,7 +1988,7 @@ RenderField(WidgetInfo *wi, &pm_bbox->orig, False); } } - else { + else if (field_ptr->text) { /* * Draw the text. */ 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; -- cgit v1.1