gtktextbtree.c 188 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * gtktextbtree.c --
 *
 *      This file contains code that manages the B-tree representation
 *      of text for the text buffer and implements character and
 *      toggle segment types.
 *
 * Copyright (c) 1992-1994 The Regents of the University of California.
 * Copyright (c) 1994-1995 Sun Microsystems, Inc.
 * Copyright (c) 2000      Red Hat, Inc.
 * Tk -> Gtk port by Havoc Pennington <hp@redhat.com>
 *
 * This software is copyrighted by the Regents of the University of
 * California, Sun Microsystems, Inc., and other parties.  The
 * following terms apply to all files associated with the software
 * unless explicitly disclaimed in individual files.
17
 *
18 19 20 21 22 23 24 25 26
 * The authors hereby grant permission to use, copy, modify,
 * distribute, and license this software and its documentation for any
 * purpose, provided that existing copyright notices are retained in
 * all copies and that this notice is included verbatim in any
 * distributions. No written agreement, license, or royalty fee is
 * required for any of the authorized uses.  Modifications to this
 * software may be copyrighted by their authors and need not follow
 * the licensing terms described here, provided that the new terms are
 * clearly indicated on the first page of each file where they apply.
27
 *
28 29 30 31 32
 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
 * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
 * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
33
 *
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
 * NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
 * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * GOVERNMENT USE: If you are acquiring this software on behalf of the
 * U.S. government, the Government shall have only "Restricted Rights"
 * in the software and related documentation as defined in the Federal
 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you
 * are acquiring the software on behalf of the Department of Defense,
 * the software shall be classified as "Commercial Computer Software"
 * and the Government shall have only "Restricted Rights" as defined
 * in Clause 252.227-7013 (c) (1) of DFARs.  Notwithstanding the
 * foregoing, the authors grant the U.S. Government and others acting
 * in its behalf permission to use and distribute the software in
 * accordance with the terms specified in this license.
52
 *
53 54
 */

55
#define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
56
#include <config.h>
57
#include "gtkalias.h"
58 59 60 61 62 63 64 65 66
#include "gtktextbtree.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "gtktexttag.h"
#include "gtktexttagtable.h"
#include "gtktextlayout.h"
#include "gtktextiterprivate.h"
#include "gtkdebug.h"
67
#include "gtktextmarkprivate.h"
68 69 70 71 72 73 74 75

/*
 * Types
 */


/*
 * The structure below is used to pass information between
76
 * _gtk_text_btree_get_tags and inc_count:
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
 */

typedef struct TagInfo {
  int numTags;                  /* Number of tags for which there
                                 * is currently information in
                                 * tags and counts. */
  int arraySize;                        /* Number of entries allocated for
                                         * tags and counts. */
  GtkTextTag **tags;           /* Array of tags seen so far.
                                * Malloc-ed. */
  int *counts;                  /* Toggle count (so far) for each
                                 * entry in tags.  Malloc-ed. */
} TagInfo;


/*
 * This is used to store per-view width/height info at the tree nodes.
 */

typedef struct _NodeData NodeData;

struct _NodeData {
  gpointer view_id;
  NodeData *next;

  /* Height and width of this node */
  gint height;
104
  signed int width : 24;
105

106 107 108 109 110 111
  /* boolean indicating whether the lines below this node are in need of validation.
   * However, width/height should always represent the current total width and
   * max height for lines below this node; the valid flag indicates whether the
   * width/height on the lines needs recomputing, not whether the totals
   * need recomputing.
   */
112
  guint valid : 8;		/* Actually a boolean */
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
};


/*
 * The data structure below keeps summary information about one tag as part
 * of the tag information in a node.
 */

typedef struct Summary {
  GtkTextTagInfo *info;                     /* Handle for tag. */
  int toggle_count;                     /* Number of transitions into or
                                         * out of this tag that occur in
                                         * the subtree rooted at this node. */
  struct Summary *next;         /* Next in list of all tags for same
                                 * node, or NULL if at end of list. */
} Summary;

/*
 * The data structure below defines a node in the B-tree.
 */

struct _GtkTextBTreeNode {
  GtkTextBTreeNode *parent;         /* Pointer to parent node, or NULL if
                                     * this is the root. */
  GtkTextBTreeNode *next;           /* Next in list of siblings with the
                                     * same parent node, or NULL for end
                                     * of list. */
  Summary *summary;             /* First in malloc-ed list of info
                                 * about tags in this subtree (NULL if
                                 * no tag info in the subtree). */
  int level;                            /* Level of this node in the B-tree.
                                         * 0 refers to the bottom of the tree
                                         * (children are lines, not nodes). */
  union {                               /* First in linked list of children. */
    struct _GtkTextBTreeNode *node;         /* Used if level > 0. */
    GtkTextLine *line;         /* Used if level == 0. */
  } children;
  int num_children;                     /* Number of children of this node. */
  int num_lines;                        /* Total number of lines (leaves) in
                                         * the subtree rooted here. */
  int num_chars;                        /* Number of chars below here */

  NodeData *node_data;
};


/*
 * Used to store the list of views in our btree
 */

typedef struct _BTreeView BTreeView;

struct _BTreeView {
  gpointer view_id;
  GtkTextLayout *layout;
  BTreeView *next;
  BTreeView *prev;
};

/*
 * And the tree itself
 */

struct _GtkTextBTree {
  GtkTextBTreeNode *root_node;          /* Pointer to root of B-tree. */
  GtkTextTagTable *table;
  GHashTable *mark_table;
  guint refcount;
181 182
  GtkTextMark *insert_mark;
  GtkTextMark *selection_bound_mark;
183 184 185
  GtkTextBuffer *buffer;
  BTreeView *views;
  GSList *tag_infos;
186
  gulong tag_changed_handler;
187

188
  /* Incremented when a segment with a byte size > 0
189 190 191 192 193
   * is added to or removed from the tree (i.e. the
   * length of a line may have changed, and lines may
   * have been added or removed). This invalidates
   * all outstanding iterators.
   */
194 195
  guint chars_changed_stamp;
  /* Incremented when any segments are added or deleted;
196 197 198
   * this makes outstanding iterators recalculate their
   * pointed-to segment and segment offset.
   */
199
  guint segments_changed_stamp;
200

201 202 203
  /* Cache the last line in the buffer */
  GtkTextLine *last_line;
  guint last_line_stamp;
204

205 206 207 208 209 210 211
  /* Cache the next-to-last line in the buffer,
   * containing the end iterator
   */
  GtkTextLine *end_iter_line;
  GtkTextLineSegment *end_iter_segment;
  int end_iter_segment_byte_index;
  int end_iter_segment_char_offset;
212
  guint end_iter_line_stamp;
213 214
  guint end_iter_segment_stamp;
  
215
  GHashTable *child_anchor_table;
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
};


/*
 * Upper and lower bounds on how many children a node may have:
 * rebalance when either of these limits is exceeded.  MAX_CHILDREN
 * should be twice MIN_CHILDREN and MIN_CHILDREN must be >= 2.
 */

/* Tk used MAX of 12 and MIN of 6. This makes the tree wide and
   shallow. It appears to be faster to locate a particular line number
   if the tree is narrow and deep, since it is more finely sorted.  I
   guess this may increase memory use though, and make it slower to
   walk the tree in order, or locate a particular byte index (which
   is done by walking the tree in order).
231

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
   There's basically a tradeoff here. However I'm thinking we want to
   add pixels, byte counts, and char counts to the tree nodes,
   at that point narrow and deep should speed up all operations,
   not just the line number searches.
*/

#if 1
#define MAX_CHILDREN 12
#define MIN_CHILDREN 6
#else
#define MAX_CHILDREN 6
#define MIN_CHILDREN 3
#endif

/*
 * Prototypes
 */

static BTreeView        *gtk_text_btree_get_view                 (GtkTextBTree     *tree,
                                                                  gpointer          view_id);
static void              gtk_text_btree_rebalance                (GtkTextBTree     *tree,
                                                                  GtkTextBTreeNode *node);
static GtkTextLine     * get_last_line                           (GtkTextBTree     *tree);
static void              post_insert_fixup                       (GtkTextBTree     *tree,
                                                                  GtkTextLine      *insert_line,
                                                                  gint              char_count_delta,
                                                                  gint              line_count_delta);
static void              gtk_text_btree_node_adjust_toggle_count (GtkTextBTreeNode *node,
                                                                  GtkTextTagInfo   *info,
                                                                  gint              adjust);
static gboolean          gtk_text_btree_node_has_tag             (GtkTextBTreeNode *node,
                                                                  GtkTextTag       *tag);

static void             segments_changed                (GtkTextBTree     *tree);
static void             chars_changed                   (GtkTextBTree     *tree);
static void             summary_list_destroy            (Summary          *summary);
static GtkTextLine     *gtk_text_line_new               (void);
static void             gtk_text_line_destroy           (GtkTextBTree     *tree,
                                                         GtkTextLine      *line);
static void             gtk_text_line_set_parent        (GtkTextLine      *line,
                                                         GtkTextBTreeNode *node);
static void             gtk_text_btree_node_remove_data (GtkTextBTreeNode *node,
                                                         gpointer          view_id);


static NodeData         *node_data_new          (gpointer  view_id);
static void              node_data_destroy      (NodeData *nd);
static void              node_data_list_destroy (NodeData *nd);
static NodeData         *node_data_find         (NodeData *nd,
                                                 gpointer  view_id);

static GtkTextBTreeNode     *gtk_text_btree_node_new                  (void);
static void                  gtk_text_btree_node_invalidate_downward  (GtkTextBTreeNode *node);
static void                  gtk_text_btree_node_invalidate_upward    (GtkTextBTreeNode *node,
286
                                                                       gpointer          view_id);
287
static NodeData *            gtk_text_btree_node_check_valid          (GtkTextBTreeNode *node,
288
                                                                       gpointer          view_id);
289
static NodeData *            gtk_text_btree_node_check_valid_downward (GtkTextBTreeNode *node,
290
                                                                       gpointer          view_id);
291
static void                  gtk_text_btree_node_check_valid_upward   (GtkTextBTreeNode *node,
292
                                                                       gpointer          view_id);
293 294 295 296 297 298

static void                  gtk_text_btree_node_remove_view         (BTreeView        *view,
                                                                      GtkTextBTreeNode *node,
                                                                      gpointer          view_id);
static void                  gtk_text_btree_node_destroy             (GtkTextBTree     *tree,
                                                                      GtkTextBTreeNode *node);
Havoc Pennington's avatar
Havoc Pennington committed
299 300
static void                  gtk_text_btree_node_free_empty          (GtkTextBTree *tree,
                                                                      GtkTextBTreeNode *node);
301 302 303 304 305 306 307 308 309
static NodeData         *    gtk_text_btree_node_ensure_data         (GtkTextBTreeNode *node,
                                                                      gpointer          view_id);
static void                  gtk_text_btree_node_remove_data         (GtkTextBTreeNode *node,
                                                                      gpointer          view_id);
static void                  gtk_text_btree_node_get_size            (GtkTextBTreeNode *node,
                                                                      gpointer          view_id,
                                                                      gint             *width,
                                                                      gint             *height);
static GtkTextBTreeNode *    gtk_text_btree_node_common_parent       (GtkTextBTreeNode *node1,
310
                                                                      GtkTextBTreeNode *node2);
311 312 313 314 315 316 317 318 319
static void get_tree_bounds       (GtkTextBTree     *tree,
                                   GtkTextIter      *start,
                                   GtkTextIter      *end);
static void tag_changed_cb        (GtkTextTagTable  *table,
                                   GtkTextTag       *tag,
                                   gboolean          size_changed,
                                   GtkTextBTree     *tree);
static void cleanup_line          (GtkTextLine      *line);
static void recompute_node_counts (GtkTextBTree     *tree,
320
                                   GtkTextBTreeNode *node);
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
static void inc_count             (GtkTextTag       *tag,
                                   int               inc,
                                   TagInfo          *tagInfoPtr);

static void summary_destroy       (Summary          *summary);

static void gtk_text_btree_link_segment   (GtkTextLineSegment *seg,
                                           const GtkTextIter  *iter);
static void gtk_text_btree_unlink_segment (GtkTextBTree       *tree,
                                           GtkTextLineSegment *seg,
                                           GtkTextLine        *line);


static GtkTextTagInfo *gtk_text_btree_get_tag_info          (GtkTextBTree   *tree,
                                                             GtkTextTag     *tag);
static GtkTextTagInfo *gtk_text_btree_get_existing_tag_info (GtkTextBTree   *tree,
                                                             GtkTextTag     *tag);
static void            gtk_text_btree_remove_tag_info       (GtkTextBTree   *tree,
                                                             GtkTextTag     *tag);

341 342 343
static void redisplay_region (GtkTextBTree      *tree,
                              const GtkTextIter *start,
                              const GtkTextIter *end);
344 345 346 347

/* Inline thingies */

static inline void
348
segments_changed (GtkTextBTree *tree)
349 350 351 352 353
{
  tree->segments_changed_stamp += 1;
}

static inline void
354
chars_changed (GtkTextBTree *tree)
355 356 357 358 359 360 361 362 363
{
  tree->chars_changed_stamp += 1;
}

/*
 * BTree operations
 */

GtkTextBTree*
364
_gtk_text_btree_new (GtkTextTagTable *table,
365
                     GtkTextBuffer *buffer)
366 367 368 369 370
{
  GtkTextBTree *tree;
  GtkTextBTreeNode *root_node;
  GtkTextLine *line, *line2;

371 372 373
  g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), NULL);
  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);

374 375 376 377 378 379 380 381
  /*
   * The tree will initially have two empty lines.  The second line
   * isn't actually part of the tree's contents, but its presence
   * makes several operations easier.  The tree will have one GtkTextBTreeNode,
   * which is also the root of the tree.
   */

  /* Create the root node. */
382 383 384 385 386

  root_node = gtk_text_btree_node_new ();

  line = gtk_text_line_new ();
  line2 = gtk_text_line_new ();
387 388 389 390 391 392 393 394 395

  root_node->parent = NULL;
  root_node->next = NULL;
  root_node->summary = NULL;
  root_node->level = 0;
  root_node->children.line = line;
  root_node->num_children = 2;
  root_node->num_lines = 2;
  root_node->num_chars = 2;
396

397 398 399
  line->parent = root_node;
  line->next = line2;

400
  line->segments = _gtk_char_segment_new ("\n", 1);
401 402 403

  line2->parent = root_node;
  line2->next = NULL;
404
  line2->segments = _gtk_char_segment_new ("\n", 1);
405 406

  /* Create the tree itself */
407

408 409 410 411 412 413
  tree = g_new0(GtkTextBTree, 1);
  tree->root_node = root_node;
  tree->table = table;
  tree->views = NULL;

  /* Set these to values that are unlikely to be found
414 415 416 417 418
   * in random memory garbage, and also avoid
   * duplicates between tree instances.
   */
  tree->chars_changed_stamp = g_random_int ();
  tree->segments_changed_stamp = g_random_int ();
419

420 421 422
  tree->last_line_stamp = tree->chars_changed_stamp - 1;
  tree->last_line = NULL;

423
  tree->end_iter_line_stamp = tree->chars_changed_stamp - 1;
424
  tree->end_iter_segment_stamp = tree->segments_changed_stamp - 1;
425
  tree->end_iter_line = NULL;
426 427 428
  tree->end_iter_segment_byte_index = 0;
  tree->end_iter_segment_char_offset = 0;
  
Manish Singh's avatar
Manish Singh committed
429
  g_object_ref (tree->table);
430

Manish Singh's avatar
Manish Singh committed
431
  tree->tag_changed_handler = g_signal_connect (tree->table,
432 433 434 435
						"tag_changed",
						G_CALLBACK (tag_changed_cb),
						tree);

436
  tree->mark_table = g_hash_table_new (g_str_hash, g_str_equal);
437 438
  tree->child_anchor_table = NULL;
  
439
  /* We don't ref the buffer, since the buffer owns us;
440 441 442
   * we'd have some circularity issues. The buffer always
   * lasts longer than the BTree
   */
443
  tree->buffer = buffer;
444

445 446
  {
    GtkTextIter start;
447
    GtkTextLineSegment *seg;
448

449
    _gtk_text_btree_get_iter_at_line_char (tree, &start, 0, 0);
450

451

452
    tree->insert_mark = _gtk_text_btree_set_mark (tree,
453 454 455 456 457 458 459
                                                 NULL,
                                                 "insert",
                                                 FALSE,
                                                 &start,
                                                 FALSE);

    seg = tree->insert_mark->segment;
460

461 462
    seg->body.mark.not_deleteable = TRUE;
    seg->body.mark.visible = TRUE;
463

464
    tree->selection_bound_mark = _gtk_text_btree_set_mark (tree,
465 466 467 468 469 470 471
                                                          NULL,
                                                          "selection_bound",
                                                          FALSE,
                                                          &start,
                                                          FALSE);

    seg = tree->selection_bound_mark->segment;
472

473 474
    seg->body.mark.not_deleteable = TRUE;

Manish Singh's avatar
Manish Singh committed
475 476
    g_object_ref (tree->insert_mark);
    g_object_ref (tree->selection_bound_mark);
477 478 479
  }

  tree->refcount = 1;
480

481 482 483 484
  return tree;
}

void
485
_gtk_text_btree_ref (GtkTextBTree *tree)
486
{
487 488 489
  g_return_if_fail (tree != NULL);
  g_return_if_fail (tree->refcount > 0);

490 491 492 493
  tree->refcount += 1;
}

void
494
_gtk_text_btree_unref (GtkTextBTree *tree)
495
{
496 497
  g_return_if_fail (tree != NULL);
  g_return_if_fail (tree->refcount > 0);
498 499 500 501

  tree->refcount -= 1;

  if (tree->refcount == 0)
502
    {      
Manish Singh's avatar
Manish Singh committed
503
      g_signal_handler_disconnect (tree->table,
504
                                   tree->tag_changed_handler);
505

Manish Singh's avatar
Manish Singh committed
506
      g_object_unref (tree->table);
507 508 509 510 511
      tree->table = NULL;
      
      gtk_text_btree_node_destroy (tree, tree->root_node);
      tree->root_node = NULL;
      
Havoc Pennington's avatar
Havoc Pennington committed
512
      g_assert (g_hash_table_size (tree->mark_table) == 0);
513
      g_hash_table_destroy (tree->mark_table);
514
      tree->mark_table = NULL;
515 516 517 518 519 520
      if (tree->child_anchor_table != NULL) 
	{
	  g_hash_table_destroy (tree->child_anchor_table);
	  tree->child_anchor_table = NULL;
	}

Manish Singh's avatar
Manish Singh committed
521
      g_object_unref (tree->insert_mark);
522
      tree->insert_mark = NULL;
Manish Singh's avatar
Manish Singh committed
523
      g_object_unref (tree->selection_bound_mark);
524
      tree->selection_bound_mark = NULL;
525

526
      g_free (tree);
527 528 529 530
    }
}

GtkTextBuffer*
531
_gtk_text_btree_get_buffer (GtkTextBTree *tree)
532 533 534 535 536
{
  return tree->buffer;
}

guint
537
_gtk_text_btree_get_chars_changed_stamp (GtkTextBTree *tree)
538 539 540 541 542
{
  return tree->chars_changed_stamp;
}

guint
543
_gtk_text_btree_get_segments_changed_stamp (GtkTextBTree *tree)
544 545 546 547 548
{
  return tree->segments_changed_stamp;
}

void
549
_gtk_text_btree_segments_changed (GtkTextBTree *tree)
550
{
551 552
  g_return_if_fail (tree != NULL);
  segments_changed (tree);
553 554 555 556 557 558
}

/*
 * Indexable segment mutation
 */

559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
/*
 *  The following function is responsible for resolving the bidi direction
 *  for the lines between start and end. But it also calculates any
 *  dependent bidi direction for surrounding lines that change as a result
 *  of the bidi direction decisions within the range. The function is
 *  trying to do as little propagation as is needed.
 */
static void
gtk_text_btree_resolve_bidi (GtkTextIter *start,
			     GtkTextIter *end)
{
  GtkTextBTree *tree = _gtk_text_iter_get_btree (start);
  GtkTextLine *start_line, *end_line, *start_line_prev, *end_line_next, *line;
  PangoDirection last_strong, dir_above_propagated, dir_below_propagated;

  /* Resolve the strong bidi direction for all lines between
   * start and end.
  */
  start_line = _gtk_text_iter_get_text_line (start);
  start_line_prev = _gtk_text_line_previous (start_line);
  end_line = _gtk_text_iter_get_text_line (end);
  end_line_next = _gtk_text_line_next (end_line);
  
  line = start_line;
  while (line && line != end_line_next)
    {
      /* Loop through the segments and search for a strong character
       */
      GtkTextLineSegment *seg = line->segments;
      line->dir_strong = PANGO_DIRECTION_NEUTRAL;
      
      while (seg)
        {
          if (seg->byte_count > 0)
            {
	      PangoDirection pango_dir;

              pango_dir = pango_find_base_dir (seg->body.chars,
					       seg->byte_count);
	      
              if (pango_dir != PANGO_DIRECTION_NEUTRAL)
                {
                  line->dir_strong = pango_dir;
                  break;
                }
            }
          seg = seg->next;
        }

      line = _gtk_text_line_next (line);
    }

  /* Sweep forward */

  /* The variable dir_above_propagated contains the forward propagated
   * direction before start. It is neutral if start is in the beginning
   * of the buffer.
   */
  dir_above_propagated = PANGO_DIRECTION_NEUTRAL;
  if (start_line_prev)
    dir_above_propagated = start_line_prev->dir_propagated_forward;

  /* Loop forward and propagate the direction of each paragraph 
   * to all neutral lines.
   */
  line = start_line;
  last_strong = dir_above_propagated;
  while (line != end_line_next)
    {
      if (line->dir_strong != PANGO_DIRECTION_NEUTRAL)
        last_strong = line->dir_strong;
      
      line->dir_propagated_forward = last_strong;
      
      line = _gtk_text_line_next (line);
    }

  /* Continue propagating as long as the previous resolved forward
   * is different from last_strong.
   */
  {
    GtkTextIter end_propagate;
    
    while (line &&
	   line->dir_strong == PANGO_DIRECTION_NEUTRAL &&
	   line->dir_propagated_forward != last_strong)
      {
        GtkTextLine *prev = line;
        line->dir_propagated_forward = last_strong;
        
        line = _gtk_text_line_next(line);
        if (!line)
          {
            line = prev;
            break;
          }
      }

    /* The last line to invalidate is the last line before the
     * line with the strong character. Or in case of the end of the
     * buffer, the last line of the buffer. (There seems to be an
     * extra "virtual" last line in the buffer that must not be used
     * calling _gtk_text_btree_get_iter_at_line (causes crash). Thus the
     * _gtk_text_line_previous is ok in that case as well.)
     */
    line = _gtk_text_line_previous (line);
    _gtk_text_btree_get_iter_at_line (tree, &end_propagate, line, 0);
    _gtk_text_btree_invalidate_region (tree, end, &end_propagate);
  }
  
  /* Sweep backward */

  /* The variable dir_below_propagated contains the backward propagated
   * direction after end. It is neutral if end is at the end of
   * the buffer.
  */
  dir_below_propagated = PANGO_DIRECTION_NEUTRAL;
  if (end_line_next)
    dir_below_propagated = end_line_next->dir_propagated_back;

  /* Loop backward and propagate the direction of each paragraph 
   * to all neutral lines.
   */
  line = end_line;
  last_strong = dir_below_propagated;
  while (line != start_line_prev)
    {
      if (line->dir_strong != PANGO_DIRECTION_NEUTRAL)
        last_strong = line->dir_strong;

      line->dir_propagated_back = last_strong;

      line = _gtk_text_line_previous (line);
    }

  /* Continue propagating as long as the resolved backward dir
   * is different from last_strong.
   */
  {
    GtkTextIter start_propagate;

    while (line &&
	   line->dir_strong == PANGO_DIRECTION_NEUTRAL &&
	   line->dir_propagated_back != last_strong)
      {
        GtkTextLine *prev = line;
        line->dir_propagated_back = last_strong;

        line = _gtk_text_line_previous (line);
        if (!line)
          {
            line = prev;
            break;
          }
      }

    /* We only need to invalidate for backwards propagation if the
     * line we ended up on didn't get a direction from forwards
     * propagation.
     */
    if (line && line->dir_propagated_forward == PANGO_DIRECTION_NEUTRAL)
      {
        _gtk_text_btree_get_iter_at_line (tree, &start_propagate, line, 0);
        _gtk_text_btree_invalidate_region(tree, &start_propagate, start);
      }
  }
}

727
void
728
_gtk_text_btree_delete (GtkTextIter *start,
729
                        GtkTextIter *end)
730 731 732 733 734 735 736 737 738 739 740
{
  GtkTextLineSegment *prev_seg;             /* The segment just before the start
                                             * of the deletion range. */
  GtkTextLineSegment *last_seg;             /* The segment just after the end
                                             * of the deletion range. */
  GtkTextLineSegment *seg, *next;
  GtkTextLine *curline;
  GtkTextBTreeNode *curnode, *node;
  GtkTextBTree *tree;
  GtkTextLine *start_line;
  GtkTextLine *end_line;
741
  GtkTextLine *deleted_lines = NULL;        /* List of lines we've deleted */
742
  gint start_byte_offset;
743 744 745

  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);
746 747
  g_return_if_fail (_gtk_text_iter_get_btree (start) ==
                    _gtk_text_iter_get_btree (end));
748

749
  gtk_text_iter_order (start, end);
750

751
  tree = _gtk_text_iter_get_btree (start);
752 753 754 755
 
  if (gtk_debug_flags & GTK_DEBUG_TEXT)
    _gtk_text_btree_check (tree);
  
756
  /* Broadcast the need for redisplay before we break the iterators */
757
  DV (g_print ("invalidating due to deleting some text (%s)\n", G_STRLOC));
758
  _gtk_text_btree_invalidate_region (tree, start, end);
759 760

  /* Save the byte offset so we can reset the iterators */
761 762
  start_byte_offset = gtk_text_iter_get_line_index (start);

763 764
  start_line = _gtk_text_iter_get_text_line (start);
  end_line = _gtk_text_iter_get_text_line (end);
765

766 767 768 769 770 771 772 773 774
  /*
   * Split the start and end segments, so we have a place
   * to insert our new text.
   *
   * Tricky point:  split at end first;  otherwise the split
   * at end may invalidate seg and/or prev_seg. This allows
   * us to avoid invalidating segments for start.
   */

775
  last_seg = gtk_text_line_segment_split (end);
776 777 778 779 780
  if (last_seg != NULL)
    last_seg = last_seg->next;
  else
    last_seg = end_line->segments;

781
  prev_seg = gtk_text_line_segment_split (start);
782 783 784 785 786 787 788 789 790 791 792 793 794
  if (prev_seg != NULL)
    {
      seg = prev_seg->next;
      prev_seg->next = last_seg;
    }
  else
    {
      seg = start_line->segments;
      start_line->segments = last_seg;
    }

  /* notify iterators that their segments need recomputation,
     just for robustness. */
795 796
  segments_changed (tree);

797 798 799 800 801 802 803 804 805
  /*
   * Delete all of the segments between prev_seg and last_seg.
   */

  curline = start_line;
  curnode = curline->parent;
  while (seg != last_seg)
    {
      gint char_count = 0;
806

807 808 809 810 811 812 813 814 815 816
      if (seg == NULL)
        {
          GtkTextLine *nextline;

          /*
           * We just ran off the end of a line.  First find the
           * next line, then go back to the old line and delete it
           * (unless it's the starting line for the range).
           */

817
          nextline = _gtk_text_line_next (curline);
818 819 820 821 822 823
          if (curline != start_line)
            {
              if (curnode == start_line->parent)
                start_line->next = curline->next;
              else
                curnode->children.line = curline->next;
824

825 826 827
              for (node = curnode; node != NULL;
                   node = node->parent)
                {
828 829 830
                  /* Don't update node->num_chars, because
                   * that was done when we deleted the segments.
                   */
831 832
                  node->num_lines -= 1;
                }
833

834
              curnode->num_children -= 1;
835 836
              curline->next = deleted_lines;
              deleted_lines = curline;
837
            }
838

839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
          curline = nextline;
          seg = curline->segments;

          /*
           * If the GtkTextBTreeNode is empty then delete it and its parents,
           * recursively upwards until a non-empty GtkTextBTreeNode is found.
           */

          while (curnode->num_children == 0)
            {
              GtkTextBTreeNode *parent;

              parent = curnode->parent;
              if (parent->children.node == curnode)
                {
                  parent->children.node = curnode->next;
                }
              else
                {
                  GtkTextBTreeNode *prevnode = parent->children.node;
                  while (prevnode->next != curnode)
                    {
                      prevnode = prevnode->next;
                    }
                  prevnode->next = curnode->next;
                }
              parent->num_children--;
Havoc Pennington's avatar
Havoc Pennington committed
866
              gtk_text_btree_node_free_empty (tree, curnode);
867 868 869 870 871 872 873 874
              curnode = parent;
            }
          curnode = curline->parent;
          continue;
        }

      next = seg->next;
      char_count = seg->char_count;
875

Havoc Pennington's avatar
Havoc Pennington committed
876
      if ((*seg->type->deleteFunc)(seg, curline, FALSE) != 0)
877 878 879 880 881
        {
          /*
           * This segment refuses to die.  Move it to prev_seg and
           * advance prev_seg if the segment has left gravity.
           */
882

883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
          if (prev_seg == NULL)
            {
              seg->next = start_line->segments;
              start_line->segments = seg;
            }
          else
            {
              seg->next = prev_seg->next;
              prev_seg->next = seg;
            }
          if (seg->type->leftGravity)
            {
              prev_seg = seg;
            }
        }
      else
        {
          /* Segment is gone. Decrement the char count of the node and
             all its parents. */
          for (node = curnode; node != NULL;
               node = node->parent)
            {
              node->num_chars -= char_count;
            }
        }
908

909 910 911 912 913 914 915 916 917 918 919 920 921
      seg = next;
    }

  /*
   * If the beginning and end of the deletion range are in different
   * lines, join the two lines together and discard the ending line.
   */

  if (start_line != end_line)
    {
      BTreeView *view;
      GtkTextBTreeNode *ancestor_node;
      GtkTextLine *prevline;
922
      int chars_moved;      
923

924 925
      /* last_seg was appended to start_line up at the top of this function */
      chars_moved = 0;
926 927 928
      for (seg = last_seg; seg != NULL;
           seg = seg->next)
        {
929
          chars_moved += seg->char_count;
930 931 932 933 934
          if (seg->type->lineChangeFunc != NULL)
            {
              (*seg->type->lineChangeFunc)(seg, end_line);
            }
        }
935 936 937 938 939 940 941

      for (node = start_line->parent; node != NULL;
           node = node->parent)
        {
          node->num_chars += chars_moved;
        }
      
942 943 944 945
      curnode = end_line->parent;
      for (node = curnode; node != NULL;
           node = node->parent)
        {
946
          node->num_chars -= chars_moved;
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
          node->num_lines--;
        }
      curnode->num_children--;
      prevline = curnode->children.line;
      if (prevline == end_line)
        {
          curnode->children.line = end_line->next;
        }
      else
        {
          while (prevline->next != end_line)
            {
              prevline = prevline->next;
            }
          prevline->next = end_line->next;
        }
      end_line->next = deleted_lines;
      deleted_lines = end_line;

      /* We now fix up the per-view aggregates. We add all the height and
       * width for the deleted lines to the start line, so that when revalidation
       * occurs, the correct change in size is seen.
       */
      ancestor_node = gtk_text_btree_node_common_parent (curnode, start_line->parent);
      view = tree->views;
      while (view)
973 974 975 976 977 978 979 980 981 982 983
        {
          GtkTextLine *line;
          GtkTextLineData *ld;

          gint deleted_width = 0;
          gint deleted_height = 0;

          line = deleted_lines;
          while (line)
            {
              GtkTextLine *next_line = line->next;
984
              ld = _gtk_text_line_get_data (line, view->view_id);
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

              if (ld)
                {
                  deleted_width = MAX (deleted_width, ld->width);
                  deleted_height += ld->height;
                }

              if (!view->next)
                gtk_text_line_destroy (tree, line);

              line = next_line;
            }

          if (deleted_width > 0 || deleted_height > 0)
            {
1000
              ld = _gtk_text_line_get_data (start_line, view->view_id);
1001 1002 1003 1004 1005 1006 1007 1008
              
              if (ld == NULL)
                {
                  /* This means that start_line has never been validated.
                   * We don't really want to do the validation here but
                   * we do need to store our temporary sizes. So we
                   * create the line data and assume a line w/h of 0.
                   */
1009 1010
                  ld = _gtk_text_line_data_new (view->layout, start_line);
                  _gtk_text_line_add_data (start_line, ld);
1011 1012 1013 1014 1015
                  ld->width = 0;
                  ld->height = 0;
                  ld->valid = FALSE;
                }
              
1016 1017 1018 1019 1020 1021 1022 1023
              ld->width = MAX (deleted_width, ld->width);
              ld->height += deleted_height;
              ld->valid = FALSE;
            }

          gtk_text_btree_node_check_valid_downward (ancestor_node, view->view_id);
          if (ancestor_node->parent)
            gtk_text_btree_node_check_valid_upward (ancestor_node->parent, view->view_id);
1024

1025 1026 1027
          view = view->next;
        }

1028 1029 1030
      /* avoid dangling pointer */
      deleted_lines = NULL;
      
1031
      gtk_text_btree_rebalance (tree, curnode);
1032 1033 1034 1035 1036 1037
    }

  /*
   * Cleanup the segments in the new line.
   */

1038
  cleanup_line (start_line);
1039 1040 1041 1042 1043

  /*
   * Lastly, rebalance the first GtkTextBTreeNode of the range.
   */

1044 1045
  gtk_text_btree_rebalance (tree, start_line->parent);

1046 1047
  /* Notify outstanding iterators that they
     are now hosed */
1048 1049
  chars_changed (tree);
  segments_changed (tree);
1050 1051

  if (gtk_debug_flags & GTK_DEBUG_TEXT)
1052
    _gtk_text_btree_check (tree);
1053 1054

  /* Re-initialize our iterators */
1055
  _gtk_text_btree_get_iter_at_line (tree, start, start_line, start_byte_offset);
1056
  *end = *start;
1057 1058

  gtk_text_btree_resolve_bidi (start, end);
1059 1060 1061
}

void
1062
_gtk_text_btree_insert (GtkTextIter *iter,
1063 1064
                        const gchar *text,
                        gint         len)
1065 1066 1067 1068 1069
{
  GtkTextLineSegment *prev_seg;     /* The segment just before the first
                                     * new segment (NULL means new segment
                                     * is at beginning of line). */
  GtkTextLineSegment *cur_seg;              /* Current segment;  new characters
1070
                                             * are inserted just after this one.
1071 1072 1073 1074 1075 1076
                                             * NULL means insert at beginning of
                                             * line. */
  GtkTextLine *line;           /* Current line (new segments are
                                * added to this line). */
  GtkTextLineSegment *seg;
  GtkTextLine *newline;
Havoc Pennington's avatar
Havoc Pennington committed
1077 1078 1079 1080 1081 1082
  int chunk_len;                        /* # characters in current chunk. */
  gint sol;                           /* start of line */
  gint eol;                           /* Pointer to character just after last
                                       * one in current chunk.
                                       */
  gint delim;                          /* index of paragraph delimiter */
1083
  int line_count_delta;                /* Counts change to total number of
Havoc Pennington's avatar
Havoc Pennington committed
1084 1085
                                        * lines in file.
                                        */
1086 1087 1088 1089 1090

  int char_count_delta;                /* change to number of chars */
  GtkTextBTree *tree;
  gint start_byte_index;
  GtkTextLine *start_line;
1091 1092 1093 1094

  g_return_if_fail (text != NULL);
  g_return_if_fail (iter != NULL);

1095
  if (len < 0)
1096
    len = strlen (text);
1097 1098

  /* extract iterator info */
1099 1100
  tree = _gtk_text_iter_get_btree (iter);
  line = _gtk_text_iter_get_text_line (iter);
1101
  
1102
  start_line = line;
1103
  start_byte_index = gtk_text_iter_get_line_index (iter);
1104

1105 1106 1107 1108 1109
  /* Get our insertion segment split. Note this assumes line allows
   * char insertions, which isn't true of the "last" line. But iter
   * should not be on that line, as we assert here.
   */
  g_assert (!_gtk_text_line_is_last (line, tree));
1110
  prev_seg = gtk_text_line_segment_split (iter);
1111 1112 1113
  cur_seg = prev_seg;

  /* Invalidate all iterators */
1114 1115
  chars_changed (tree);
  segments_changed (tree);
1116
  
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
  /*
   * Chop the text up into lines and create a new segment for
   * each line, plus a new line for the leftovers from the
   * previous line.
   */

  eol = 0;
  sol = 0;
  line_count_delta = 0;
  char_count_delta = 0;
  while (eol < len)
    {
1129 1130
      sol = eol;
      
Havoc Pennington's avatar
Havoc Pennington committed
1131 1132 1133 1134
      pango_find_paragraph_boundary (text + sol,
                                     len - sol,
                                     &delim,
                                     &eol);      
1135

1136 1137 1138
      /* make these relative to the start of the text */
      delim += sol;
      eol += sol;
1139 1140 1141 1142 1143 1144

      g_assert (eol >= sol);
      g_assert (delim >= sol);
      g_assert (eol >= delim);
      g_assert (sol >= 0);
      g_assert (eol <= len);
1145
      
Havoc Pennington's avatar
Havoc Pennington committed
1146
      chunk_len = eol - sol;
1147 1148

      g_assert (g_utf8_validate (&text[sol], chunk_len, NULL));
Havoc Pennington's avatar
Havoc Pennington committed
1149
      seg = _gtk_char_segment_new (&text[sol], chunk_len);
1150 1151

      char_count_delta += seg->char_count;
1152

1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
      if (cur_seg == NULL)
        {
          seg->next = line->segments;
          line->segments = seg;
        }
      else
        {
          seg->next = cur_seg->next;
          cur_seg->next = seg;
        }

Havoc Pennington's avatar
Havoc Pennington committed
1164
      if (delim == eol)
1165 1166 1167 1168 1169
        {
          /* chunk didn't end with a paragraph separator */
          g_assert (eol == len);
          break;
        }
1170 1171 1172 1173 1174 1175

      /*
       * The chunk ended with a newline, so create a new GtkTextLine
       * and move the remainder of the old line to it.
       */

1176 1177
      newline = gtk_text_line_new ();
      gtk_text_line_set_parent (newline, line->parent);
1178 1179 1180 1181 1182 1183
      newline->next = line->next;
      line->next = newline;
      newline->segments = seg->next;
      seg->next = NULL;
      line = newline;
      cur_seg = NULL;
1184
      line_count_delta++;
1185 1186 1187 1188 1189 1190 1191
    }

  /*
   * Cleanup the starting line for the insertion, plus the ending
   * line if it's different.
   */

1192
  cleanup_line (start_line);
1193 1194
  if (line != start_line)
    {
1195
      cleanup_line (line);
1196 1197
    }

1198 1199
  post_insert_fixup (tree, line, line_count_delta, char_count_delta);

1200 1201 1202 1203 1204 1205 1206
  /* Invalidate our region, and reset the iterator the user
     passed in to point to the end of the inserted text. */
  {
    GtkTextIter start;
    GtkTextIter end;


1207
    _gtk_text_btree_get_iter_at_line (tree,
1208 1209 1210
                                      &start,
                                      start_line,
                                      start_byte_index);
1211 1212 1213 1214 1215
    end = start;

    /* We could almost certainly be more efficient here
       by saving the information from the insertion loop
       above. FIXME */
1216
    gtk_text_iter_forward_chars (&end, char_count_delta);
1217

1218
    DV (g_print ("invalidating due to inserting some text (%s)\n", G_STRLOC));
1219
    _gtk_text_btree_invalidate_region (tree,
1220
                                      &start, &end);
1221 1222 1223 1224


    /* Convenience for the user */
    *iter = end;
1225 1226

    gtk_text_btree_resolve_bidi (&start, &end);
1227 1228 1229
  }
}

1230 1231 1232 1233
static void
insert_pixbuf_or_widget_segment (GtkTextIter        *iter,
                                 GtkTextLineSegment *seg)

1234 1235 1236 1237 1238 1239
{
  GtkTextIter start;
  GtkTextLineSegment *prevPtr;
  GtkTextLine *line;
  GtkTextBTree *tree;
  gint start_byte_offset;
1240

1241 1242
  line = _gtk_text_iter_get_text_line (iter);
  tree = _gtk_text_iter_get_btree (iter);
1243 1244 1245
  start_byte_offset = gtk_text_iter_get_line_index (iter);

  prevPtr = gtk_text_line_segment_split (iter);
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
  if (prevPtr == NULL)
    {
      seg->next = line->segments;
      line->segments = seg;
    }
  else
    {
      seg->next = prevPtr->next;
      prevPtr->next = seg;
    }

1257
  post_insert_fixup (tree, line, 0, seg->char_count);
1258

1259 1260
  chars_changed (tree);
  segments_changed (tree);
1261 1262

  /* reset *iter for the user, and invalidate tree nodes */
1263

1264
  _gtk_text_btree_get_iter_at_line (tree, &start, line, start_byte_offset);
1265 1266

  *iter = start;
1267
  gtk_text_iter_forward_char (iter); /* skip forward past the segment */
1268

1269
  DV (g_print ("invalidating due to inserting pixbuf/widget (%s)\n", G_STRLOC));
1270
  _gtk_text_btree_invalidate_region (tree, &start, iter);
1271
}
1272 1273
     
void
1274
_gtk_text_btree_insert_pixbuf (GtkTextIter *iter,
1275 1276 1277 1278 1279 1280 1281 1282 1283
                              GdkPixbuf   *pixbuf)
{
  GtkTextLineSegment *seg;
  
  seg = _gtk_pixbuf_segment_new (pixbuf);

  insert_pixbuf_or_widget_segment (iter, seg);
}

1284 1285 1286
void
_gtk_text_btree_insert_child_anchor (GtkTextIter        *iter,
                                     GtkTextChildAnchor *anchor)
1287 1288 1289
{
  GtkTextLineSegment *seg;
  GtkTextBTree *tree;
1290 1291 1292 1293 1294 1295

  if (anchor->segment != NULL)
    {
      g_warning (G_STRLOC": Same child anchor can't be inserted twice");
      return;
    }
1296
  
1297
  seg = _gtk_widget_segment_new (anchor);
1298

1299
  tree = seg->body.child.tree = _gtk_text_iter_get_btree (iter);
1300
  seg->body.child.line = _gtk_text_iter_get_text_line (iter);
1301
  
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
  insert_pixbuf_or_widget_segment (iter, seg);

  if (tree->child_anchor_table == NULL)
    tree->child_anchor_table = g_hash_table_new (NULL, NULL);

  g_hash_table_insert (tree->child_anchor_table,
                       seg->body.child.obj,
                       seg->body.child.obj);
}

void
1313
_gtk_text_btree_unregister_child_anchor (GtkTextChildAnchor *anchor)
1314 1315
{
  GtkTextLineSegment *seg;
1316

1317 1318 1319 1320 1321
  seg = anchor->segment;
  
  g_hash_table_remove (seg->body.child.tree->child_anchor_table,
                       anchor);
}
1322 1323 1324 1325 1326 1327

/*
 * View stuff
 */

static GtkTextLine*
1328 1329 1330
find_line_by_y (GtkTextBTree *tree, BTreeView *view,
                GtkTextBTreeNode *node, gint y, gint *line_top,
                GtkTextLine *last_line)
1331 1332 1333 1334
{
  gint current_y = 0;

  if (gtk_debug_flags & GTK_DEBUG_TEXT)
1335
    _gtk_text_btree_check (tree);
1336

1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
  if (node->level == 0)
    {
      GtkTextLine *line;

      line = node->children.line;

      while (line != NULL && line != last_line)
        {
          GtkTextLineData *ld;

1347
          ld = _gtk_text_line_get_data (line, view->view_id);
1348

1349 1350 1351 1352 1353 1354 1355 1356
          if (ld)
            {
              if (y < (current_y + (ld ? ld->height : 0)))
                return line;

              current_y += ld->height;
              *line_top += ld->height;
            }
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372

          line = line->next;
        }
      return NULL;
    }
  else
    {
      GtkTextBTreeNode *child;

      child = node->children.node;

      while (child != NULL)
        {
          gint width;
          gint height;

1373 1374
          gtk_text_btree_node_get_size (child, view->view_id,
                                        &width, &height);
1375

1376 1377 1378 1379
          if (y < (current_y + height))
            return find_line_by_y (tree, view, child,
                                   y - current_y, line_top,
                                   last_line);
1380 1381 1382

          current_y += height;
          *line_top += height;
1383

1384 1385
          child = child->next;
        }
1386

1387 1388 1389 1390 1391
      return NULL;
    }
}

GtkTextLine *
1392
_gtk_text_btree_find_line_by_y (GtkTextBTree *tree,
1393 1394 1395
                                gpointer      view_id,
                                gint          ypixel,
                                gint         *line_top_out)
1396 1397 1398 1399 1400
{
  GtkTextLine *line;
  BTreeView *view;
  GtkTextLine *last_line;
  gint line_top = 0;
1401

1402 1403 1404 1405
  view = gtk_text_btree_get_view (tree, view_id);
  g_return_val_if_fail (view != NULL, NULL);

  last_line = get_last_line (tree);
1406

1407
  line = find_line_by_y (tree, view, tree->root_node, ypixel, &line_top,
1408
                         last_line);
1409 1410 1411 1412 1413 1414 1415 1416

  if (line_top_out)
    *line_top_out = line_top;

  return line;
}

static gint
1417 1418 1419 1420 1421
find_line_top_in_line_list (GtkTextBTree *tree,
                            BTreeView *view,
                            GtkTextLine *line,
                            GtkTextLine *target_line,
                            gint y)
1422 1423 1424 1425
{
  while (line != NULL)
    {
      GtkTextLineData *ld;
1426

1427
      if (line == target_line)
1428
        return y;
1429

1430
      ld = _gtk_text_line_get_data (line, view->view_id);
1431
      if (ld)
1432 1433
        y += ld->height;

1434 1435
      line = line->next;
    }
1436 1437 1438 1439

  g_assert_not_reached (); /* If we get here, our
                              target line didn't exist
                              under its parent node */
1440 1441 1442 1443
  return 0;
}

gint
1444
_gtk_text_btree_find_line_top (GtkTextBTree *tree,
1445 1446 1447 1448 1449 1450 1451 1452 1453
                              GtkTextLine *target_line,
                              gpointer view_id)
{
  gint y = 0;
  BTreeView *view;
  GSList *nodes;
  GSList *iter;
  GtkTextBTreeNode *node;

1454 1455 1456
  view = gtk_text_btree_get_view (tree, view_id);

  g_return_val_if_fail (view != NULL, 0);
1457 1458 1459 1460 1461

  nodes = NULL;
  node = target_line->parent;
  while (node != NULL)
    {
1462
      nodes = g_slist_prepend (nodes, node);
1463 1464 1465 1466 1467
      node = node->parent;
    }

  iter = nodes;
  while (iter != NULL)
1468
    {
1469
      node = iter->data;
1470

1471 1472
      if (node->level == 0)
        {
1473 1474 1475 1476
          g_slist_free (nodes);
          return find_line_top_in_line_list (tree, view,
                                             node->children.line,
                                             target_line, y);
1477 1478 1479 1480 1481 1482
        }
      else
        {
          GtkTextBTreeNode *child;
          GtkTextBTreeNode *target_node;

1483
          g_assert (iter->next != NULL); /* not at level 0 */
1484
          target_node = iter->next->data;
1485

1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
          child = node->children.node;

          while (child != NULL)
            {
              gint width;
              gint height;

              if (child == target_node)
                break;
              else
                {
1497 1498
                  gtk_text_btree_node_get_size (child, view->view_id,
                                                &width, &height);
1499 1500 1501 1502
                  y += height;
                }
              child = child->next;
            }
1503 1504
          g_assert (child != NULL); /* should have broken out before we
                                       ran out of nodes */
1505 1506
        }

1507
      iter = g_slist_next (iter);
1508 1509
    }

1510 1511
  g_assert_not_reached (); /* we return when we find the target line */
  return 0;
1512 1513 1514
}

void
1515
_gtk_text_btree_add_view (GtkTextBTree *tree,
1516 1517 1518 1519 1520 1521
                         GtkTextLayout *layout)
{
  BTreeView *view;
  GtkTextLine *last_line;
  GtkTextLineData *line_data;

1522
  g_return_if_fail (tree != NULL);
Havoc Pennington's avatar
Havoc Pennington committed
1523
  
1524
  view = g_new (BTreeView, 1);
1525 1526 1527

  view->view_id = layout;
  view->layout = layout;
1528

1529 1530 1531
  view->next = tree->views;
  view->prev = NULL;

1532 1533 1534 1535 1536 1537
  if (tree->views)
    {
      g_assert (tree->views->prev == NULL);
      tree->views->prev = view;
    }
  
1538 1539 1540 1541 1542 1543 1544
  tree->views = view;

  /* The last line in the buffer has identity values for the per-view
   * data so that we can avoid special case checks for it in a large
   * number of loops
   */
  last_line = get_last_line (tree);
1545

1546 1547 1548 1549 1550 1551
  line_data = g_new (GtkTextLineData, 1);
  line_data->view_id = layout;
  line_data->next = NULL;
  line_data->width = 0;
  line_data->height = 0;
  line_data->valid = TRUE;
1552

1553
  _gtk_text_line_add_data (last_line, line_data);
1554 1555 1556
}

void
1557
_gtk_text_btree_remove_view (GtkTextBTree *tree,
Havoc Pennington's avatar
Havoc Pennington committed
1558
                             gpointer      view_id)
1559 1560 1561 1562 1563
{
  BTreeView *view;
  GtkTextLine *last_line;
  GtkTextLineData *line_data;

1564
  g_return_if_fail (tree != NULL);
Havoc Pennington's avatar
Havoc Pennington committed
1565
  
1566
  view = tree->views;
1567

1568 1569 1570 1571 1572 1573 1574
  while (view != NULL)
    {
      if (view->view_id == view_id)
        break;

      view = view->next;
    }
1575 1576

  g_return_if_fail (view != NULL);
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590

  if (view->next)
    view->next->prev = view->prev;

  if (view->prev)
    view->prev->next = view->next;

  if (view == tree->views)
    tree->views = view->next;

  /* Remove the line data for the last line which we added ourselves.
   * (Do this first, so that we don't try to call the view's line data destructor on it.)
   */
  last_line = get_last_line (tree);
1591
  line_data = _gtk_text_line_remove_data (last_line, view_id);
1592
  g_free (line_data);
1593 1594 1595

  gtk_text_btree_node_remove_view (view, tree->root_node, view_id);

1596 1597 1598
  view->layout = (gpointer) 0xdeadbeef;
  view->view_id = (gpointer) 0xdeadbeef;
  
1599
  g_free (view);
1600 1601 1602
}

void
1603 1604 1605
_gtk_text_btree_invalidate_region (GtkTextBTree      *tree,
                                   const GtkTextIter *start,
                                   const GtkTextIter *end)
1606 1607
{
  BTreeView *view;
1608

1609 1610 1611 1612
  view = tree->views;

  while (view != NULL)
    {
1613
      gtk_text_layout_invalidate (view->layout, start, end);
1614 1615 1616 1617 1618 1619

      view = view->next;
    }
}

void
1620
_gtk_text_btree_get_view_size (GtkTextBTree *tree,
1621 1622 1623 1624
                              gpointer view_id,
                              gint *width,
                              gint *height)
{
1625 1626 1627
  g_return_if_fail (tree != NULL);
  g_return_if_fail (view_id != NULL);

1628
  gtk_text_btree_node_get_size (tree->root_node, view_id,
1629
                                width, height);
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
}

/*
 * Tag
 */

typedef struct {
  GtkTextIter *iters;
  guint count;
  guint alloced;
} IterStack;

static IterStack*
1643
iter_stack_new (void)
1644 1645
{
  IterStack *stack;
1646
  stack = g_new (IterStack, 1);
1647 1648 1649 1650 1651 1652 1653
  stack->iters = NULL;
  stack->count = 0;
  stack->alloced = 0;
  return stack;
}

static void
1654
iter_stack_push (IterStack *stack, const GtkTextIter *iter)
1655 1656 1657 1658 1659
{
  stack->count += 1;
  if (stack->count > stack->alloced)
    {
      stack->alloced = stack->count*2;
1660 1661
      stack->iters = g_realloc (stack->iters,
                                stack->alloced*sizeof (GtkTextIter));
1662 1663 1664 1665 1666
    }
  stack->iters[stack->count-1] = *iter;
}

static gboolean
1667
iter_stack_pop (IterStack *stack, GtkTextIter *iter)