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