]> Pileus Git - ~andy/gtk/blob - gtk/gtktextsegment.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtktextsegment.c
1 /*
2  * gtktextsegment.c --
3  *
4  * Code for segments in general, and toggle/char segments in particular.
5  *
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>
10  *
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.
15  *
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.
25  *
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.
31  *
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.
38  *
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.
50  *
51  */
52
53 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
54 #include "config.h"
55 #include "gtktextbtree.h"
56 #include <string.h>
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include "gtktexttag.h"
60 #include "gtktexttagtable.h"
61 #include "gtktextlayout.h"
62 #include "gtktextiterprivate.h"
63 #include "gtkdebug.h"
64
65 /*
66  *--------------------------------------------------------------
67  *
68  * split_segment --
69  *
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.
78  *
79  * Results:
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
84  *      value is NULL.
85  *
86  * Side effects:
87  *      The segment referred to by iter is split unless
88  *      iter refers to its first character.
89  *
90  *--------------------------------------------------------------
91  */
92
93 GtkTextLineSegment*
94 gtk_text_line_segment_split (const GtkTextIter *iter)
95 {
96   GtkTextLineSegment *prev, *seg;
97   GtkTextBTree *tree;
98   GtkTextLine *line;
99   int count;
100
101   line = _gtk_text_iter_get_text_line (iter);
102   tree = _gtk_text_iter_get_btree (iter);
103
104   count = gtk_text_iter_get_line_index (iter);
105
106   if (gtk_get_debug_flags () & GTK_DEBUG_TEXT)
107     _gtk_text_iter_check (iter);
108   
109   prev = NULL;
110   seg = line->segments;
111
112   while (seg != NULL)
113     {
114       if (seg->byte_count > count)
115         {
116           if (count == 0)
117             {
118               return prev;
119             }
120           else
121             {
122               g_assert (count != seg->byte_count);
123               g_assert (seg->byte_count > 0);
124
125               _gtk_text_btree_segments_changed (tree);
126
127               seg = (*seg->type->splitFunc)(seg, count);
128
129               if (prev == NULL)
130                 line->segments = seg;
131               else
132                 prev->next = seg;
133
134               return seg;
135             }
136         }
137       else if ((seg->byte_count == 0) && (count == 0)
138                && !seg->type->leftGravity)
139         {
140           return prev;
141         }
142
143       count -= seg->byte_count;
144       prev = seg;
145       seg = seg->next;
146     }
147   g_error ("split_segment reached end of line!");
148   return NULL;
149 }
150
151
152 /*
153  * Macros that determine how much space to allocate for new segments:
154  */
155
156 #define CSEG_SIZE(chars) ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
157         + 1 + (chars)))
158 #define TSEG_SIZE ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
159         + sizeof (GtkTextToggleBody)))
160
161 /*
162  * Type functions
163  */
164
165 static void
166 char_segment_self_check (GtkTextLineSegment *seg)
167 {
168   /* This function checks the segment itself, but doesn't
169      assume the segment has been validly inserted into
170      the btree. */
171
172   g_assert (seg != NULL);
173
174   if (seg->byte_count <= 0)
175     {
176       g_error ("segment has size <= 0");
177     }
178
179   if (strlen (seg->body.chars) != seg->byte_count)
180     {
181       g_error ("segment has wrong size");
182     }
183
184   if (g_utf8_strlen (seg->body.chars, seg->byte_count) != seg->char_count)
185     {
186       g_error ("char segment has wrong character count");
187     }
188 }
189
190 GtkTextLineSegment*
191 _gtk_char_segment_new (const gchar *text, guint len)
192 {
193   GtkTextLineSegment *seg;
194
195   g_assert (gtk_text_byte_begins_utf8_char (text));
196
197   seg = g_malloc (CSEG_SIZE (len));
198   seg->type = (GtkTextLineSegmentClass *)&gtk_text_char_type;
199   seg->next = NULL;
200   seg->byte_count = len;
201   memcpy (seg->body.chars, text, len);
202   seg->body.chars[len] = '\0';
203
204   seg->char_count = g_utf8_strlen (seg->body.chars, seg->byte_count);
205
206   if (gtk_get_debug_flags () & GTK_DEBUG_TEXT)
207     char_segment_self_check (seg);
208
209   return seg;
210 }
211
212 GtkTextLineSegment*
213 _gtk_char_segment_new_from_two_strings (const gchar *text1, 
214                                         guint        len1, 
215                                         guint        chars1,
216                                         const gchar *text2, 
217                                         guint        len2, 
218                                         guint        chars2)
219 {
220   GtkTextLineSegment *seg;
221
222   g_assert (gtk_text_byte_begins_utf8_char (text1));
223   g_assert (gtk_text_byte_begins_utf8_char (text2));
224
225   seg = g_malloc (CSEG_SIZE (len1+len2));
226   seg->type = &gtk_text_char_type;
227   seg->next = NULL;
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';
232
233   seg->char_count = chars1 + chars2;
234
235   if (gtk_get_debug_flags () & GTK_DEBUG_TEXT)
236     char_segment_self_check (seg);
237
238   return seg;
239 }
240
241 /*
242  *--------------------------------------------------------------
243  *
244  * char_segment_split_func --
245  *
246  *      This procedure implements splitting for character segments.
247  *
248  * Results:
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.
252  *
253  * Side effects:
254  *      Storage for segPtr is freed.
255  *
256  *--------------------------------------------------------------
257  */
258
259 static GtkTextLineSegment *
260 char_segment_split_func (GtkTextLineSegment *seg, int index)
261 {
262   GtkTextLineSegment *new1, *new2;
263
264   g_assert (index < seg->byte_count);
265
266   if (gtk_get_debug_flags () & GTK_DEBUG_TEXT)
267     {
268       char_segment_self_check (seg);
269     }
270
271   new1 = _gtk_char_segment_new (seg->body.chars, index);
272   new2 = _gtk_char_segment_new (seg->body.chars + index, seg->byte_count - index);
273
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);
278
279   new1->next = new2;
280   new2->next = seg->next;
281
282   if (gtk_get_debug_flags () & GTK_DEBUG_TEXT)
283     {
284       char_segment_self_check (new1);
285       char_segment_self_check (new2);
286     }
287
288   g_free (seg);
289   return new1;
290 }
291
292 /*
293  *--------------------------------------------------------------
294  *
295  * char_segment_cleanup_func --
296  *
297  *      This procedure merges adjacent character segments into
298  *      a single character segment, if possible.
299  *
300  * Arguments:
301  *      segPtr: Pointer to the first of two adjacent segments to
302  *              join.
303  *      line:   Line containing segments (not used).
304  *
305  * Results:
306  *      The return value is a pointer to the first segment in
307  *      the (new) list of segments that used to start with segPtr.
308  *
309  * Side effects:
310  *      Storage for the segments may be allocated and freed.
311  *
312  *--------------------------------------------------------------
313  */
314
315         /* ARGSUSED */
316 static GtkTextLineSegment *
317 char_segment_cleanup_func (GtkTextLineSegment *segPtr, GtkTextLine *line)
318 {
319   GtkTextLineSegment *segPtr2, *newPtr;
320
321   if (gtk_get_debug_flags () & GTK_DEBUG_TEXT)
322     char_segment_self_check (segPtr);
323
324   segPtr2 = segPtr->next;
325   if ((segPtr2 == NULL) || (segPtr2->type != &gtk_text_char_type))
326     {
327       return segPtr;
328     }
329
330   newPtr =
331     _gtk_char_segment_new_from_two_strings (segPtr->body.chars, 
332                                             segPtr->byte_count,
333                                             segPtr->char_count,
334                                             segPtr2->body.chars, 
335                                             segPtr2->byte_count,
336                                             segPtr2->char_count);
337
338   newPtr->next = segPtr2->next;
339
340   if (gtk_get_debug_flags () & GTK_DEBUG_TEXT)
341     char_segment_self_check (newPtr);
342
343   g_free (segPtr);
344   g_free (segPtr2);
345   return newPtr;
346 }
347
348 /*
349  *--------------------------------------------------------------
350  *
351  * char_segment_delete_func --
352  *
353  *      This procedure is invoked to delete a character segment.
354  *
355  * Arguments:
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.
360  *
361  * Results:
362  *      Always returns 0 to indicate that the segment was deleted.
363  *
364  * Side effects:
365  *      Storage for the segment is freed.
366  *
367  *--------------------------------------------------------------
368  */
369
370         /* ARGSUSED */
371 static int
372 char_segment_delete_func (GtkTextLineSegment *segPtr, GtkTextLine *line, int treeGone)
373 {
374   g_free ((char*) segPtr);
375   return 0;
376 }
377
378 /*
379  *--------------------------------------------------------------
380  *
381  * char_segment_check_func --
382  *
383  *      This procedure is invoked to perform consistency checks
384  *      on character segments.
385  *
386  * Arguments:
387  *      segPtr : Segment to check
388  *      line   : Line containing segment
389  *
390  * Results:
391  *      None.
392  *
393  * Side effects:
394  *      If the segment isn't inconsistent then the procedure
395  *      g_errors.
396  *
397  *--------------------------------------------------------------
398  */
399
400         /* ARGSUSED */
401 static void
402 char_segment_check_func (GtkTextLineSegment *segPtr, GtkTextLine *line)
403 {
404   char_segment_self_check (segPtr);
405
406   if (segPtr->next != NULL)
407     {
408       if (segPtr->next->type == &gtk_text_char_type)
409         {
410           g_error ("adjacent character segments weren't merged");
411         }
412     }
413 }
414
415 GtkTextLineSegment*
416 _gtk_toggle_segment_new (GtkTextTagInfo *info, gboolean on)
417 {
418   GtkTextLineSegment *seg;
419
420   seg = g_malloc (TSEG_SIZE);
421
422   seg->type = on ? &gtk_text_toggle_on_type : &gtk_text_toggle_off_type;
423
424   seg->next = NULL;
425
426   seg->byte_count = 0;
427   seg->char_count = 0;
428
429   seg->body.toggle.info = info;
430   seg->body.toggle.inNodeCounts = 0;
431
432   return seg;
433 }
434
435 /*
436  *--------------------------------------------------------------
437  *
438  * toggle_segment_delete_func --
439  *
440  *      This procedure is invoked to delete toggle segments.
441  *
442  * Arguments:
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
447  *
448  * Results:
449  *      Returns 1 to indicate that the segment may not be deleted,
450  *      unless the entire B-tree is going away.
451  *
452  * Side effects:
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.
456  *
457  *--------------------------------------------------------------
458  */
459
460 static int
461 toggle_segment_delete_func (GtkTextLineSegment *segPtr, GtkTextLine *line, int treeGone)
462 {
463   if (treeGone)
464     {
465       g_free ((char *) segPtr);
466       return 0;
467     }
468
469   /*
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.
475    */
476
477   if (segPtr->body.toggle.inNodeCounts)
478     {
479       _gtk_change_node_toggle_count (line->parent,
480                                      segPtr->body.toggle.info, -1);
481       segPtr->body.toggle.inNodeCounts = 0;
482     }
483   return 1;
484 }
485
486 /*
487  *--------------------------------------------------------------
488  *
489  * toggle_segment_cleanup_func --
490  *
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.
494  *
495  * Arguments:
496  *      segPtr : Segment to check
497  *      line   : Line that now contains segment
498  *
499  * Results:
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
503  *      or modify segPtr.
504  *
505  * Side effects:
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.
509  *
510  *--------------------------------------------------------------
511  */
512
513 static GtkTextLineSegment *
514 toggle_segment_cleanup_func (GtkTextLineSegment *segPtr, GtkTextLine *line)
515 {
516   GtkTextLineSegment *segPtr2, *prevPtr;
517   int counts;
518
519   /*
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.
524    */
525
526   if (segPtr->type == &gtk_text_toggle_off_type)
527     {
528       for (prevPtr = segPtr, segPtr2 = prevPtr->next;
529            (segPtr2 != NULL) && (segPtr2->byte_count == 0);
530            prevPtr = segPtr2, segPtr2 = prevPtr->next)
531         {
532           if (segPtr2->type != &gtk_text_toggle_on_type)
533             {
534               continue;
535             }
536           if (segPtr2->body.toggle.info != segPtr->body.toggle.info)
537             {
538               continue;
539             }
540           counts = segPtr->body.toggle.inNodeCounts
541             + segPtr2->body.toggle.inNodeCounts;
542           if (counts != 0)
543             {
544               _gtk_change_node_toggle_count (line->parent,
545                                              segPtr->body.toggle.info, -counts);
546             }
547           prevPtr->next = segPtr2->next;
548           g_free ((char *) segPtr2);
549           segPtr2 = segPtr->next;
550           g_free ((char *) segPtr);
551           return segPtr2;
552         }
553     }
554
555   if (!segPtr->body.toggle.inNodeCounts)
556     {
557       _gtk_change_node_toggle_count (line->parent,
558                                      segPtr->body.toggle.info, 1);
559       segPtr->body.toggle.inNodeCounts = 1;
560     }
561   return segPtr;
562 }
563
564 /*
565  *--------------------------------------------------------------
566  *
567  * toggle_segment_line_change_func --
568  *
569  *      This procedure is invoked when a toggle segment is about
570  *      to move from one line to another.
571  *
572  * Arguments:
573  *      segPtr : Segment to check
574  *      line   : Line that used to contain segment
575  *
576  * Results:
577  *      None.
578  *
579  * Side effects:
580  *      Toggle counts are decremented in the GtkTextBTreeNodes above the line.
581  *
582  *--------------------------------------------------------------
583  */
584
585 static void
586 toggle_segment_line_change_func (GtkTextLineSegment *segPtr, GtkTextLine *line)
587 {
588   if (segPtr->body.toggle.inNodeCounts)
589     {
590       _gtk_change_node_toggle_count (line->parent,
591                                      segPtr->body.toggle.info, -1);
592       segPtr->body.toggle.inNodeCounts = 0;
593     }
594 }
595
596 /*
597  * Virtual tables
598  */
599
600
601 const GtkTextLineSegmentClass gtk_text_char_type = {
602   "character",                          /* name */
603   0,                                            /* leftGravity */
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 */
609 };
610
611 /*
612  * Type record for segments marking the beginning of a tagged
613  * range:
614  */
615
616 const GtkTextLineSegmentClass gtk_text_toggle_on_type = {
617   "toggleOn",                                   /* name */
618   0,                                            /* leftGravity */
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 */
624 };
625
626 /*
627  * Type record for segments marking the end of a tagged
628  * range:
629  */
630
631 const GtkTextLineSegmentClass gtk_text_toggle_off_type = {
632   "toggleOff",                          /* name */
633   1,                                            /* leftGravity */
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 */
639 };