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"
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 ("segment has size <= 0");
180 if (strlen (seg->body.chars) != seg->byte_count)
182 g_error ("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 = (GtkTextLineSegmentClass *)>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,
221 GtkTextLineSegment *seg;
223 g_assert (gtk_text_byte_begins_utf8_char (text1));
224 g_assert (gtk_text_byte_begins_utf8_char (text2));
226 seg = g_malloc (CSEG_SIZE (len1+len2));
227 seg->type = >k_text_char_type;
229 seg->byte_count = len1 + len2;
230 memcpy (seg->body.chars, text1, len1);
231 memcpy (seg->body.chars + len1, text2, len2);
232 seg->body.chars[len1+len2] = '\0';
234 seg->char_count = chars1 + chars2;
236 if (gtk_debug_flags & GTK_DEBUG_TEXT)
237 char_segment_self_check (seg);
243 *--------------------------------------------------------------
245 * char_segment_split_func --
247 * This procedure implements splitting for character segments.
250 * The return value is a pointer to a chain of two segments
251 * that have the same characters as segPtr except split
252 * among the two segments.
255 * Storage for segPtr is freed.
257 *--------------------------------------------------------------
260 static GtkTextLineSegment *
261 char_segment_split_func (GtkTextLineSegment *seg, int index)
263 GtkTextLineSegment *new1, *new2;
265 g_assert (index < seg->byte_count);
267 if (gtk_debug_flags & GTK_DEBUG_TEXT)
269 char_segment_self_check (seg);
272 new1 = _gtk_char_segment_new (seg->body.chars, index);
273 new2 = _gtk_char_segment_new (seg->body.chars + index, seg->byte_count - index);
275 g_assert (gtk_text_byte_begins_utf8_char (new1->body.chars));
276 g_assert (gtk_text_byte_begins_utf8_char (new2->body.chars));
277 g_assert (new1->byte_count + new2->byte_count == seg->byte_count);
278 g_assert (new1->char_count + new2->char_count == seg->char_count);
281 new2->next = seg->next;
283 if (gtk_debug_flags & GTK_DEBUG_TEXT)
285 char_segment_self_check (new1);
286 char_segment_self_check (new2);
294 *--------------------------------------------------------------
296 * char_segment_cleanup_func --
298 * This procedure merges adjacent character segments into
299 * a single character segment, if possible.
302 * The return value is a pointer to the first segment in
303 * the (new) list of segments that used to start with segPtr.
306 * Storage for the segments may be allocated and freed.
308 *--------------------------------------------------------------
312 static GtkTextLineSegment *
313 char_segment_cleanup_func (segPtr, line)
314 GtkTextLineSegment *segPtr; /* Pointer to first of two adjacent
315 * segments to join. */
316 GtkTextLine *line; /* Line containing segments (not
319 GtkTextLineSegment *segPtr2, *newPtr;
321 if (gtk_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_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 * Always returns 0 to indicate that the segment was deleted.
359 * Storage for the segment is freed.
361 *--------------------------------------------------------------
366 char_segment_delete_func (segPtr, line, treeGone)
367 GtkTextLineSegment *segPtr; /* Segment to delete. */
368 GtkTextLine *line; /* Line containing segment. */
369 int treeGone; /* Non-zero means the entire tree is
370 * being deleted, so everything must
373 g_free ((char*) segPtr);
378 *--------------------------------------------------------------
380 * char_segment_check_func --
382 * This procedure is invoked to perform consistency checks
383 * on character segments.
389 * If the segment isn't inconsistent then the procedure
392 *--------------------------------------------------------------
397 char_segment_check_func (segPtr, line)
398 GtkTextLineSegment *segPtr; /* Segment to check. */
399 GtkTextLine *line; /* Line containing segment. */
401 char_segment_self_check (segPtr);
403 if (segPtr->next != NULL)
405 if (segPtr->next->type == >k_text_char_type)
407 g_error ("adjacent character segments weren't merged");
413 _gtk_toggle_segment_new (GtkTextTagInfo *info, gboolean on)
415 GtkTextLineSegment *seg;
417 seg = g_malloc (TSEG_SIZE);
419 seg->type = on ? >k_text_toggle_on_type : >k_text_toggle_off_type;
426 seg->body.toggle.info = info;
427 seg->body.toggle.inNodeCounts = 0;
433 *--------------------------------------------------------------
435 * toggle_segment_delete_func --
437 * This procedure is invoked to delete toggle segments.
440 * Returns 1 to indicate that the segment may not be deleted,
441 * unless the entire B-tree is going away.
444 * If the tree is going away then the toggle's memory is
445 * freed; otherwise the toggle counts in GtkTextBTreeNodes above the
446 * segment get updated.
448 *--------------------------------------------------------------
452 toggle_segment_delete_func (segPtr, line, treeGone)
453 GtkTextLineSegment *segPtr; /* Segment to check. */
454 GtkTextLine *line; /* Line containing segment. */
455 int treeGone; /* Non-zero means the entire tree is
456 * being deleted, so everything must
461 g_free ((char *) segPtr);
466 * This toggle is in the middle of a range of characters that's
467 * being deleted. Refuse to die. We'll be moved to the end of
468 * the deleted range and our cleanup procedure will be called
469 * later. Decrement GtkTextBTreeNode toggle counts here, and set a flag
470 * so we'll re-increment them in the cleanup procedure.
473 if (segPtr->body.toggle.inNodeCounts)
475 _gtk_change_node_toggle_count (line->parent,
476 segPtr->body.toggle.info, -1);
477 segPtr->body.toggle.inNodeCounts = 0;
483 *--------------------------------------------------------------
485 * toggle_segment_cleanup_func --
487 * This procedure is called when a toggle is part of a line that's
488 * been modified in some way. It's invoked after the
489 * modifications are complete.
492 * The return value is the head segment in a new list
493 * that is to replace the tail of the line that used to
494 * start at segPtr. This allows the procedure to delete
498 * Toggle counts in the GtkTextBTreeNodes above the new line will be
499 * updated if they're not already. Toggles may be collapsed
500 * if there are duplicate toggles at the same position.
502 *--------------------------------------------------------------
505 static GtkTextLineSegment *
506 toggle_segment_cleanup_func (segPtr, line)
507 GtkTextLineSegment *segPtr; /* Segment to check. */
508 GtkTextLine *line; /* Line that now contains segment. */
510 GtkTextLineSegment *segPtr2, *prevPtr;
514 * If this is a toggle-off segment, look ahead through the next
515 * segments to see if there's a toggle-on segment for the same tag
516 * before any segments with non-zero size. If so then the two
517 * toggles cancel each other; remove them both.
520 if (segPtr->type == >k_text_toggle_off_type)
522 for (prevPtr = segPtr, segPtr2 = prevPtr->next;
523 (segPtr2 != NULL) && (segPtr2->byte_count == 0);
524 prevPtr = segPtr2, segPtr2 = prevPtr->next)
526 if (segPtr2->type != >k_text_toggle_on_type)
530 if (segPtr2->body.toggle.info != segPtr->body.toggle.info)
534 counts = segPtr->body.toggle.inNodeCounts
535 + segPtr2->body.toggle.inNodeCounts;
538 _gtk_change_node_toggle_count (line->parent,
539 segPtr->body.toggle.info, -counts);
541 prevPtr->next = segPtr2->next;
542 g_free ((char *) segPtr2);
543 segPtr2 = segPtr->next;
544 g_free ((char *) segPtr);
549 if (!segPtr->body.toggle.inNodeCounts)
551 _gtk_change_node_toggle_count (line->parent,
552 segPtr->body.toggle.info, 1);
553 segPtr->body.toggle.inNodeCounts = 1;
559 *--------------------------------------------------------------
561 * toggle_segment_line_change_func --
563 * This procedure is invoked when a toggle segment is about
564 * to move from one line to another.
570 * Toggle counts are decremented in the GtkTextBTreeNodes above the line.
572 *--------------------------------------------------------------
576 toggle_segment_line_change_func (segPtr, line)
577 GtkTextLineSegment *segPtr; /* Segment to check. */
578 GtkTextLine *line; /* Line that used to contain segment. */
580 if (segPtr->body.toggle.inNodeCounts)
582 _gtk_change_node_toggle_count (line->parent,
583 segPtr->body.toggle.info, -1);
584 segPtr->body.toggle.inNodeCounts = 0;
593 const GtkTextLineSegmentClass gtk_text_char_type = {
594 "character", /* name */
596 char_segment_split_func, /* splitFunc */
597 char_segment_delete_func, /* deleteFunc */
598 char_segment_cleanup_func, /* cleanupFunc */
599 NULL, /* lineChangeFunc */
600 char_segment_check_func /* checkFunc */
604 * Type record for segments marking the beginning of a tagged
608 const GtkTextLineSegmentClass gtk_text_toggle_on_type = {
609 "toggleOn", /* name */
611 NULL, /* splitFunc */
612 toggle_segment_delete_func, /* deleteFunc */
613 toggle_segment_cleanup_func, /* cleanupFunc */
614 toggle_segment_line_change_func, /* lineChangeFunc */
615 _gtk_toggle_segment_check_func /* checkFunc */
619 * Type record for segments marking the end of a tagged
623 const GtkTextLineSegmentClass gtk_text_toggle_off_type = {
624 "toggleOff", /* name */
626 NULL, /* splitFunc */
627 toggle_segment_delete_func, /* deleteFunc */
628 toggle_segment_cleanup_func, /* cleanupFunc */
629 toggle_segment_line_change_func, /* lineChangeFunc */
630 _gtk_toggle_segment_check_func /* checkFunc */
633 #define __GTK_TEXT_SEGMENT_C__
634 #include "gtkaliasdef.c"