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
55 #include "gtktextbtree.h"
59 #include "gtktexttag.h"
60 #include "gtktexttagtable.h"
61 #include "gtktextlayout.h"
62 #include "gtktextiterprivate.h"
66 *--------------------------------------------------------------
70 * This procedure is called before adding or deleting
71 * segments. It does three things: (a) it finds the segment
72 * containing iter; (b) if there are several such
73 * segments (because some segments have zero length) then
74 * it picks the first segment that does not have left
75 * gravity; (c) if the index refers to the middle of
76 * a segment then it splits the segment so that the
77 * index now refers to the beginning of a segment.
80 * The return value is a pointer to the segment just
81 * before the segment corresponding to iter (as
82 * described above). If the segment corresponding to
83 * iter is the first in its line then the return
87 * The segment referred to by iter is split unless
88 * iter refers to its first character.
90 *--------------------------------------------------------------
94 gtk_text_line_segment_split (const GtkTextIter *iter)
96 GtkTextLineSegment *prev, *seg;
101 line = _gtk_text_iter_get_text_line (iter);
102 tree = _gtk_text_iter_get_btree (iter);
104 count = gtk_text_iter_get_line_index (iter);
106 if (gtk_get_debug_flags () & GTK_DEBUG_TEXT)
107 _gtk_text_iter_check (iter);
110 seg = line->segments;
114 if (seg->byte_count > count)
122 g_assert (count != seg->byte_count);
123 g_assert (seg->byte_count > 0);
125 _gtk_text_btree_segments_changed (tree);
127 seg = (*seg->type->splitFunc)(seg, count);
130 line->segments = seg;
137 else if ((seg->byte_count == 0) && (count == 0)
138 && !seg->type->leftGravity)
143 count -= seg->byte_count;
147 g_error ("split_segment reached end of line!");
153 * Macros that determine how much space to allocate for new segments:
156 #define CSEG_SIZE(chars) ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
158 #define TSEG_SIZE ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
159 + sizeof (GtkTextToggleBody)))
166 char_segment_self_check (GtkTextLineSegment *seg)
168 /* This function checks the segment itself, but doesn't
169 assume the segment has been validly inserted into
172 g_assert (seg != NULL);
174 if (seg->byte_count <= 0)
176 g_error ("segment has size <= 0");
179 if (strlen (seg->body.chars) != seg->byte_count)
181 g_error ("segment has wrong size");
184 if (g_utf8_strlen (seg->body.chars, seg->byte_count) != seg->char_count)
186 g_error ("char segment has wrong character count");
191 _gtk_char_segment_new (const gchar *text, guint len)
193 GtkTextLineSegment *seg;
195 g_assert (gtk_text_byte_begins_utf8_char (text));
197 seg = g_malloc (CSEG_SIZE (len));
198 seg->type = (GtkTextLineSegmentClass *)>k_text_char_type;
200 seg->byte_count = len;
201 memcpy (seg->body.chars, text, len);
202 seg->body.chars[len] = '\0';
204 seg->char_count = g_utf8_strlen (seg->body.chars, seg->byte_count);
206 if (gtk_get_debug_flags () & GTK_DEBUG_TEXT)
207 char_segment_self_check (seg);
213 _gtk_char_segment_new_from_two_strings (const gchar *text1,
220 GtkTextLineSegment *seg;
222 g_assert (gtk_text_byte_begins_utf8_char (text1));
223 g_assert (gtk_text_byte_begins_utf8_char (text2));
225 seg = g_malloc (CSEG_SIZE (len1+len2));
226 seg->type = >k_text_char_type;
228 seg->byte_count = len1 + len2;
229 memcpy (seg->body.chars, text1, len1);
230 memcpy (seg->body.chars + len1, text2, len2);
231 seg->body.chars[len1+len2] = '\0';
233 seg->char_count = chars1 + chars2;
235 if (gtk_get_debug_flags () & GTK_DEBUG_TEXT)
236 char_segment_self_check (seg);
242 *--------------------------------------------------------------
244 * char_segment_split_func --
246 * This procedure implements splitting for character segments.
249 * The return value is a pointer to a chain of two segments
250 * that have the same characters as segPtr except split
251 * among the two segments.
254 * Storage for segPtr is freed.
256 *--------------------------------------------------------------
259 static GtkTextLineSegment *
260 char_segment_split_func (GtkTextLineSegment *seg, int index)
262 GtkTextLineSegment *new1, *new2;
264 g_assert (index < seg->byte_count);
266 if (gtk_get_debug_flags () & GTK_DEBUG_TEXT)
268 char_segment_self_check (seg);
271 new1 = _gtk_char_segment_new (seg->body.chars, index);
272 new2 = _gtk_char_segment_new (seg->body.chars + index, seg->byte_count - index);
274 g_assert (gtk_text_byte_begins_utf8_char (new1->body.chars));
275 g_assert (gtk_text_byte_begins_utf8_char (new2->body.chars));
276 g_assert (new1->byte_count + new2->byte_count == seg->byte_count);
277 g_assert (new1->char_count + new2->char_count == seg->char_count);
280 new2->next = seg->next;
282 if (gtk_get_debug_flags () & GTK_DEBUG_TEXT)
284 char_segment_self_check (new1);
285 char_segment_self_check (new2);
293 *--------------------------------------------------------------
295 * char_segment_cleanup_func --
297 * This procedure merges adjacent character segments into
298 * a single character segment, if possible.
301 * segPtr: Pointer to the first of two adjacent segments to
303 * line: Line containing segments (not used).
306 * The return value is a pointer to the first segment in
307 * the (new) list of segments that used to start with segPtr.
310 * Storage for the segments may be allocated and freed.
312 *--------------------------------------------------------------
316 static GtkTextLineSegment *
317 char_segment_cleanup_func (GtkTextLineSegment *segPtr, GtkTextLine *line)
319 GtkTextLineSegment *segPtr2, *newPtr;
321 if (gtk_get_debug_flags () & GTK_DEBUG_TEXT)
322 char_segment_self_check (segPtr);
324 segPtr2 = segPtr->next;
325 if ((segPtr2 == NULL) || (segPtr2->type != >k_text_char_type))
331 _gtk_char_segment_new_from_two_strings (segPtr->body.chars,
336 segPtr2->char_count);
338 newPtr->next = segPtr2->next;
340 if (gtk_get_debug_flags () & GTK_DEBUG_TEXT)
341 char_segment_self_check (newPtr);
349 *--------------------------------------------------------------
351 * char_segment_delete_func --
353 * This procedure is invoked to delete a character segment.
356 * segPtr : Segment to delete
357 * line : Line containing segment
358 * treeGone : Non-zero means the entire tree is being
359 * deleted, so everything must get cleaned up.
362 * Always returns 0 to indicate that the segment was deleted.
365 * Storage for the segment is freed.
367 *--------------------------------------------------------------
372 char_segment_delete_func (GtkTextLineSegment *segPtr, GtkTextLine *line, int treeGone)
374 g_free ((char*) segPtr);
379 *--------------------------------------------------------------
381 * char_segment_check_func --
383 * This procedure is invoked to perform consistency checks
384 * on character segments.
387 * segPtr : Segment to check
388 * line : Line containing segment
394 * If the segment isn't inconsistent then the procedure
397 *--------------------------------------------------------------
402 char_segment_check_func (GtkTextLineSegment *segPtr, GtkTextLine *line)
404 char_segment_self_check (segPtr);
406 if (segPtr->next != NULL)
408 if (segPtr->next->type == >k_text_char_type)
410 g_error ("adjacent character segments weren't merged");
416 _gtk_toggle_segment_new (GtkTextTagInfo *info, gboolean on)
418 GtkTextLineSegment *seg;
420 seg = g_malloc (TSEG_SIZE);
422 seg->type = on ? >k_text_toggle_on_type : >k_text_toggle_off_type;
429 seg->body.toggle.info = info;
430 seg->body.toggle.inNodeCounts = 0;
436 *--------------------------------------------------------------
438 * toggle_segment_delete_func --
440 * This procedure is invoked to delete toggle segments.
443 * segPtr : Segment to check
444 * line : Line containing segment
445 * treeGone : Non-zero means the entire tree is being
446 * deleted so everything must get cleaned up
449 * Returns 1 to indicate that the segment may not be deleted,
450 * unless the entire B-tree is going away.
453 * If the tree is going away then the toggle's memory is
454 * freed; otherwise the toggle counts in GtkTextBTreeNodes above the
455 * segment get updated.
457 *--------------------------------------------------------------
461 toggle_segment_delete_func (GtkTextLineSegment *segPtr, GtkTextLine *line, int treeGone)
465 g_free ((char *) segPtr);
470 * This toggle is in the middle of a range of characters that's
471 * being deleted. Refuse to die. We'll be moved to the end of
472 * the deleted range and our cleanup procedure will be called
473 * later. Decrement GtkTextBTreeNode toggle counts here, and set a flag
474 * so we'll re-increment them in the cleanup procedure.
477 if (segPtr->body.toggle.inNodeCounts)
479 _gtk_change_node_toggle_count (line->parent,
480 segPtr->body.toggle.info, -1);
481 segPtr->body.toggle.inNodeCounts = 0;
487 *--------------------------------------------------------------
489 * toggle_segment_cleanup_func --
491 * This procedure is called when a toggle is part of a line that's
492 * been modified in some way. It's invoked after the
493 * modifications are complete.
496 * segPtr : Segment to check
497 * line : Line that now contains segment
500 * The return value is the head segment in a new list
501 * that is to replace the tail of the line that used to
502 * start at segPtr. This allows the procedure to delete
506 * Toggle counts in the GtkTextBTreeNodes above the new line will be
507 * updated if they're not already. Toggles may be collapsed
508 * if there are duplicate toggles at the same position.
510 *--------------------------------------------------------------
513 static GtkTextLineSegment *
514 toggle_segment_cleanup_func (GtkTextLineSegment *segPtr, GtkTextLine *line)
516 GtkTextLineSegment *segPtr2, *prevPtr;
520 * If this is a toggle-off segment, look ahead through the next
521 * segments to see if there's a toggle-on segment for the same tag
522 * before any segments with non-zero size. If so then the two
523 * toggles cancel each other; remove them both.
526 if (segPtr->type == >k_text_toggle_off_type)
528 for (prevPtr = segPtr, segPtr2 = prevPtr->next;
529 (segPtr2 != NULL) && (segPtr2->byte_count == 0);
530 prevPtr = segPtr2, segPtr2 = prevPtr->next)
532 if (segPtr2->type != >k_text_toggle_on_type)
536 if (segPtr2->body.toggle.info != segPtr->body.toggle.info)
540 counts = segPtr->body.toggle.inNodeCounts
541 + segPtr2->body.toggle.inNodeCounts;
544 _gtk_change_node_toggle_count (line->parent,
545 segPtr->body.toggle.info, -counts);
547 prevPtr->next = segPtr2->next;
548 g_free ((char *) segPtr2);
549 segPtr2 = segPtr->next;
550 g_free ((char *) segPtr);
555 if (!segPtr->body.toggle.inNodeCounts)
557 _gtk_change_node_toggle_count (line->parent,
558 segPtr->body.toggle.info, 1);
559 segPtr->body.toggle.inNodeCounts = 1;
565 *--------------------------------------------------------------
567 * toggle_segment_line_change_func --
569 * This procedure is invoked when a toggle segment is about
570 * to move from one line to another.
573 * segPtr : Segment to check
574 * line : Line that used to contain segment
580 * Toggle counts are decremented in the GtkTextBTreeNodes above the line.
582 *--------------------------------------------------------------
586 toggle_segment_line_change_func (GtkTextLineSegment *segPtr, GtkTextLine *line)
588 if (segPtr->body.toggle.inNodeCounts)
590 _gtk_change_node_toggle_count (line->parent,
591 segPtr->body.toggle.info, -1);
592 segPtr->body.toggle.inNodeCounts = 0;
601 const GtkTextLineSegmentClass gtk_text_char_type = {
602 "character", /* name */
604 char_segment_split_func, /* splitFunc */
605 char_segment_delete_func, /* deleteFunc */
606 char_segment_cleanup_func, /* cleanupFunc */
607 NULL, /* lineChangeFunc */
608 char_segment_check_func /* checkFunc */
612 * Type record for segments marking the beginning of a tagged
616 const GtkTextLineSegmentClass gtk_text_toggle_on_type = {
617 "toggleOn", /* name */
619 NULL, /* splitFunc */
620 toggle_segment_delete_func, /* deleteFunc */
621 toggle_segment_cleanup_func, /* cleanupFunc */
622 toggle_segment_line_change_func, /* lineChangeFunc */
623 _gtk_toggle_segment_check_func /* checkFunc */
627 * Type record for segments marking the end of a tagged
631 const GtkTextLineSegmentClass gtk_text_toggle_off_type = {
632 "toggleOff", /* name */
634 NULL, /* splitFunc */
635 toggle_segment_delete_func, /* deleteFunc */
636 toggle_segment_cleanup_func, /* cleanupFunc */
637 toggle_segment_line_change_func, /* lineChangeFunc */
638 _gtk_toggle_segment_check_func /* checkFunc */