4 * Code for segments in general, and toggle/char segments in particular.
6 * Copyright (c) 1992-1994 The Regents of the University of California.
7 * Copyright (c) 1994-1995 Sun Microsystems, Inc.
8 * Copyright (c) 2000 Red Hat, Inc.
9 * Tk -> Gtk port by Havoc Pennington <hp@redhat.com>
11 * This software is copyrighted by the Regents of the University of
12 * California, Sun Microsystems, Inc., and other parties. The
13 * following terms apply to all files associated with the software
14 * unless explicitly disclaimed in individual files.
16 * The authors hereby grant permission to use, copy, modify,
17 * distribute, and license this software and its documentation for any
18 * purpose, provided that existing copyright notices are retained in
19 * all copies and that this notice is included verbatim in any
20 * distributions. No written agreement, license, or royalty fee is
21 * required for any of the authorized uses. Modifications to this
22 * software may be copyrighted by their authors and need not follow
23 * the licensing terms described here, provided that the new terms are
24 * clearly indicated on the first page of each file where they apply.
26 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
27 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
28 * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
29 * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
32 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
33 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
35 * NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
36 * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
37 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
39 * GOVERNMENT USE: If you are acquiring this software on behalf of the
40 * U.S. government, the Government shall have only "Restricted Rights"
41 * in the software and related documentation as defined in the Federal
42 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you
43 * are acquiring the software on behalf of the Department of Defense,
44 * the software shall be classified as "Commercial Computer Software"
45 * and the Government shall have only "Restricted Rights" as defined
46 * in Clause 252.227-7013 (c) (1) of DFARs. Notwithstanding the
47 * foregoing, the authors grant the U.S. Government and others acting
48 * in its behalf permission to use and distribute the software in
49 * accordance with the terms specified in this license.
53 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
56 #include "gtktextbtree.h"
60 #include "gtktexttag.h"
61 #include "gtktexttagtable.h"
62 #include "gtktextlayout.h"
63 #include "gtktextiterprivate.h"
67 *--------------------------------------------------------------
71 * This procedure is called before adding or deleting
72 * segments. It does three things: (a) it finds the segment
73 * containing iter; (b) if there are several such
74 * segments (because some segments have zero length) then
75 * it picks the first segment that does not have left
76 * gravity; (c) if the index refers to the middle of
77 * a segment then it splits the segment so that the
78 * index now refers to the beginning of a segment.
81 * The return value is a pointer to the segment just
82 * before the segment corresponding to iter (as
83 * described above). If the segment corresponding to
84 * iter is the first in its line then the return
88 * The segment referred to by iter is split unless
89 * iter refers to its first character.
91 *--------------------------------------------------------------
95 gtk_text_line_segment_split (const GtkTextIter *iter)
97 GtkTextLineSegment *prev, *seg;
102 line = _gtk_text_iter_get_text_line (iter);
103 tree = _gtk_text_iter_get_btree (iter);
105 count = gtk_text_iter_get_line_index (iter);
107 if (gtk_debug_flags & GTK_DEBUG_TEXT)
108 _gtk_text_iter_check (iter);
111 seg = line->segments;
115 if (seg->byte_count > count)
123 g_assert (count != seg->byte_count);
124 g_assert (seg->byte_count > 0);
126 _gtk_text_btree_segments_changed (tree);
128 seg = (*seg->type->splitFunc)(seg, count);
131 line->segments = seg;
138 else if ((seg->byte_count == 0) && (count == 0)
139 && !seg->type->leftGravity)
144 count -= seg->byte_count;
148 g_error ("split_segment reached end of line!");
154 * Macros that determine how much space to allocate for new segments:
157 #define CSEG_SIZE(chars) ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
159 #define TSEG_SIZE ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
160 + sizeof (GtkTextToggleBody)))
167 char_segment_self_check (GtkTextLineSegment *seg)
169 /* This function checks the segment itself, but doesn't
170 assume the segment has been validly inserted into
173 g_assert (seg != NULL);
175 if (seg->byte_count <= 0)
177 g_error ("char_segment_check_func: segment has size <= 0");
180 if (strlen (seg->body.chars) != seg->byte_count)
182 g_error ("char_segment_check_func: segment has wrong size");
185 if (g_utf8_strlen (seg->body.chars, seg->byte_count) != seg->char_count)
187 g_error ("char segment has wrong character count");
192 _gtk_char_segment_new (const gchar *text, guint len)
194 GtkTextLineSegment *seg;
196 g_assert (gtk_text_byte_begins_utf8_char (text));
198 seg = g_malloc (CSEG_SIZE (len));
199 seg->type = >k_text_char_type;
201 seg->byte_count = len;
202 memcpy (seg->body.chars, text, len);
203 seg->body.chars[len] = '\0';
205 seg->char_count = g_utf8_strlen (seg->body.chars, seg->byte_count);
207 if (gtk_debug_flags & GTK_DEBUG_TEXT)
208 char_segment_self_check (seg);
214 _gtk_char_segment_new_from_two_strings (const gchar *text1, guint len1,
215 const gchar *text2, guint len2)
217 GtkTextLineSegment *seg;
219 g_assert (gtk_text_byte_begins_utf8_char (text1));
220 g_assert (gtk_text_byte_begins_utf8_char (text2));
222 seg = g_malloc (CSEG_SIZE (len1+len2));
223 seg->type = >k_text_char_type;
225 seg->byte_count = len1 + len2;
226 memcpy (seg->body.chars, text1, len1);
227 memcpy (seg->body.chars + len1, text2, len2);
228 seg->body.chars[len1+len2] = '\0';
230 /* In principle this function could probably take chars1 and chars2
231 as args, since it's typically used to merge two char segments */
232 seg->char_count = g_utf8_strlen (seg->body.chars, seg->byte_count);
234 if (gtk_debug_flags & GTK_DEBUG_TEXT)
235 char_segment_self_check (seg);
241 *--------------------------------------------------------------
243 * char_segment_split_func --
245 * This procedure implements splitting for character segments.
248 * The return value is a pointer to a chain of two segments
249 * that have the same characters as segPtr except split
250 * among the two segments.
253 * Storage for segPtr is freed.
255 *--------------------------------------------------------------
258 static GtkTextLineSegment *
259 char_segment_split_func (GtkTextLineSegment *seg, int index)
261 GtkTextLineSegment *new1, *new2;
263 g_assert (index < seg->byte_count);
265 if (gtk_debug_flags & GTK_DEBUG_TEXT)
267 char_segment_self_check (seg);
270 new1 = _gtk_char_segment_new (seg->body.chars, index);
271 new2 = _gtk_char_segment_new (seg->body.chars + index, seg->byte_count - index);
273 g_assert (gtk_text_byte_begins_utf8_char (new1->body.chars));
274 g_assert (gtk_text_byte_begins_utf8_char (new2->body.chars));
275 g_assert (new1->byte_count + new2->byte_count == seg->byte_count);
276 g_assert (new1->char_count + new2->char_count == seg->char_count);
279 new2->next = seg->next;
281 if (gtk_debug_flags & GTK_DEBUG_TEXT)
283 char_segment_self_check (new1);
284 char_segment_self_check (new2);
292 *--------------------------------------------------------------
294 * char_segment_cleanup_func --
296 * This procedure merges adjacent character segments into
297 * a single character segment, if possible.
300 * The return value is a pointer to the first segment in
301 * the (new) list of segments that used to start with segPtr.
304 * Storage for the segments may be allocated and freed.
306 *--------------------------------------------------------------
310 static GtkTextLineSegment *
311 char_segment_cleanup_func (segPtr, line)
312 GtkTextLineSegment *segPtr; /* Pointer to first of two adjacent
313 * segments to join. */
314 GtkTextLine *line; /* Line containing segments (not
317 GtkTextLineSegment *segPtr2, *newPtr;
319 if (gtk_debug_flags & GTK_DEBUG_TEXT)
320 char_segment_self_check (segPtr);
322 segPtr2 = segPtr->next;
323 if ((segPtr2 == NULL) || (segPtr2->type != >k_text_char_type))
329 _gtk_char_segment_new_from_two_strings (segPtr->body.chars, segPtr->byte_count,
330 segPtr2->body.chars, segPtr2->byte_count);
332 newPtr->next = segPtr2->next;
334 if (gtk_debug_flags & GTK_DEBUG_TEXT)
335 char_segment_self_check (newPtr);
343 *--------------------------------------------------------------
345 * char_segment_delete_func --
347 * This procedure is invoked to delete a character segment.
350 * Always returns 0 to indicate that the segment was deleted.
353 * Storage for the segment is freed.
355 *--------------------------------------------------------------
360 char_segment_delete_func (segPtr, line, treeGone)
361 GtkTextLineSegment *segPtr; /* Segment to delete. */
362 GtkTextLine *line; /* Line containing segment. */
363 int treeGone; /* Non-zero means the entire tree is
364 * being deleted, so everything must
367 g_free ((char*) segPtr);
372 *--------------------------------------------------------------
374 * char_segment_check_func --
376 * This procedure is invoked to perform consistency checks
377 * on character segments.
383 * If the segment isn't inconsistent then the procedure
386 *--------------------------------------------------------------
391 char_segment_check_func (segPtr, line)
392 GtkTextLineSegment *segPtr; /* Segment to check. */
393 GtkTextLine *line; /* Line containing segment. */
395 char_segment_self_check (segPtr);
397 if (segPtr->next != NULL)
399 if (segPtr->next->type == >k_text_char_type)
401 g_error ("char_segment_check_func: adjacent character segments weren't merged");
407 _gtk_toggle_segment_new (GtkTextTagInfo *info, gboolean on)
409 GtkTextLineSegment *seg;
411 seg = g_malloc (TSEG_SIZE);
413 seg->type = on ? >k_text_toggle_on_type : >k_text_toggle_off_type;
420 seg->body.toggle.info = info;
421 seg->body.toggle.inNodeCounts = 0;
427 *--------------------------------------------------------------
429 * toggle_segment_delete_func --
431 * This procedure is invoked to delete toggle segments.
434 * Returns 1 to indicate that the segment may not be deleted,
435 * unless the entire B-tree is going away.
438 * If the tree is going away then the toggle's memory is
439 * freed; otherwise the toggle counts in GtkTextBTreeNodes above the
440 * segment get updated.
442 *--------------------------------------------------------------
446 toggle_segment_delete_func (segPtr, line, treeGone)
447 GtkTextLineSegment *segPtr; /* Segment to check. */
448 GtkTextLine *line; /* Line containing segment. */
449 int treeGone; /* Non-zero means the entire tree is
450 * being deleted, so everything must
455 g_free ((char *) segPtr);
460 * This toggle is in the middle of a range of characters that's
461 * being deleted. Refuse to die. We'll be moved to the end of
462 * the deleted range and our cleanup procedure will be called
463 * later. Decrement GtkTextBTreeNode toggle counts here, and set a flag
464 * so we'll re-increment them in the cleanup procedure.
467 if (segPtr->body.toggle.inNodeCounts)
469 _gtk_change_node_toggle_count (line->parent,
470 segPtr->body.toggle.info, -1);
471 segPtr->body.toggle.inNodeCounts = 0;
477 *--------------------------------------------------------------
479 * toggle_segment_cleanup_func --
481 * This procedure is called when a toggle is part of a line that's
482 * been modified in some way. It's invoked after the
483 * modifications are complete.
486 * The return value is the head segment in a new list
487 * that is to replace the tail of the line that used to
488 * start at segPtr. This allows the procedure to delete
492 * Toggle counts in the GtkTextBTreeNodes above the new line will be
493 * updated if they're not already. Toggles may be collapsed
494 * if there are duplicate toggles at the same position.
496 *--------------------------------------------------------------
499 static GtkTextLineSegment *
500 toggle_segment_cleanup_func (segPtr, line)
501 GtkTextLineSegment *segPtr; /* Segment to check. */
502 GtkTextLine *line; /* Line that now contains segment. */
504 GtkTextLineSegment *segPtr2, *prevPtr;
508 * If this is a toggle-off segment, look ahead through the next
509 * segments to see if there's a toggle-on segment for the same tag
510 * before any segments with non-zero size. If so then the two
511 * toggles cancel each other; remove them both.
514 if (segPtr->type == >k_text_toggle_off_type)
516 for (prevPtr = segPtr, segPtr2 = prevPtr->next;
517 (segPtr2 != NULL) && (segPtr2->byte_count == 0);
518 prevPtr = segPtr2, segPtr2 = prevPtr->next)
520 if (segPtr2->type != >k_text_toggle_on_type)
524 if (segPtr2->body.toggle.info != segPtr->body.toggle.info)
528 counts = segPtr->body.toggle.inNodeCounts
529 + segPtr2->body.toggle.inNodeCounts;
532 _gtk_change_node_toggle_count (line->parent,
533 segPtr->body.toggle.info, -counts);
535 prevPtr->next = segPtr2->next;
536 g_free ((char *) segPtr2);
537 segPtr2 = segPtr->next;
538 g_free ((char *) segPtr);
543 if (!segPtr->body.toggle.inNodeCounts)
545 _gtk_change_node_toggle_count (line->parent,
546 segPtr->body.toggle.info, 1);
547 segPtr->body.toggle.inNodeCounts = 1;
553 *--------------------------------------------------------------
555 * toggle_segment_line_change_func --
557 * This procedure is invoked when a toggle segment is about
558 * to move from one line to another.
564 * Toggle counts are decremented in the GtkTextBTreeNodes above the line.
566 *--------------------------------------------------------------
570 toggle_segment_line_change_func (segPtr, line)
571 GtkTextLineSegment *segPtr; /* Segment to check. */
572 GtkTextLine *line; /* Line that used to contain segment. */
574 if (segPtr->body.toggle.inNodeCounts)
576 _gtk_change_node_toggle_count (line->parent,
577 segPtr->body.toggle.info, -1);
578 segPtr->body.toggle.inNodeCounts = 0;
587 GtkTextLineSegmentClass gtk_text_char_type = {
588 "character", /* name */
590 char_segment_split_func, /* splitFunc */
591 char_segment_delete_func, /* deleteFunc */
592 char_segment_cleanup_func, /* cleanupFunc */
593 NULL, /* lineChangeFunc */
594 char_segment_check_func /* checkFunc */
598 * Type record for segments marking the beginning of a tagged
602 GtkTextLineSegmentClass gtk_text_toggle_on_type = {
603 "toggleOn", /* name */
605 NULL, /* splitFunc */
606 toggle_segment_delete_func, /* deleteFunc */
607 toggle_segment_cleanup_func, /* cleanupFunc */
608 toggle_segment_line_change_func, /* lineChangeFunc */
609 _gtk_toggle_segment_check_func /* checkFunc */
613 * Type record for segments marking the end of a tagged
617 GtkTextLineSegmentClass gtk_text_toggle_off_type = {
618 "toggleOff", /* name */
620 NULL, /* splitFunc */
621 toggle_segment_delete_func, /* deleteFunc */
622 toggle_segment_cleanup_func, /* cleanupFunc */
623 toggle_segment_line_change_func, /* lineChangeFunc */
624 _gtk_toggle_segment_check_func /* checkFunc */