Permalink
Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
7310 lines (6079 sloc) 190 KB
/*
* 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.
*
* 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.
*
* 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.
*
* 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.
*
*/
#include "config.h"
#include "gtktextbtree.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "gtktextbufferprivate.h"
#include "gtktexttag.h"
#include "gtktexttagprivate.h"
#include "gtktexttagtable.h"
#include "gtktextlayoutprivate.h"
#include "gtktextiterprivate.h"
#include "gtkdebug.h"
#include "gtktextmarkprivate.h"
#include "gtktextsegment.h"
/*
* Types
*/
/*
* The structure below is used to pass information between
* _gtk_text_btree_get_tags and inc_count:
*/
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;
signed int width : 24;
/* 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.
*/
guint valid : 8; /* Actually a boolean */
};
/*
* 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). */
int num_lines; /* Total number of lines (leaves) in
* the subtree rooted here. */
int num_chars; /* Number of chars below here */
int num_children; /* Number of children of this node. */
union { /* First in linked list of children. */
struct _GtkTextBTreeNode *node; /* Used if level > 0. */
GtkTextLine *line; /* Used if level == 0. */
} children;
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;
GtkTextMark *insert_mark;
GtkTextMark *selection_bound_mark;
GtkTextBuffer *buffer;
BTreeView *views;
GSList *tag_infos;
gulong tag_changed_handler;
/* Incremented when a segment with a byte size > 0
* 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.
*/
guint chars_changed_stamp;
/* Incremented when any segments are added or deleted;
* this makes outstanding iterators recalculate their
* pointed-to segment and segment offset.
*/
guint segments_changed_stamp;
/* Cache the last line in the buffer */
GtkTextLine *last_line;
guint last_line_stamp;
/* 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;
guint end_iter_line_stamp;
guint end_iter_segment_stamp;
GHashTable *child_anchor_table;
};
/*
* 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).
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);
#if 0
static void gtk_text_btree_node_invalidate_downward (GtkTextBTreeNode *node);
#endif
static void gtk_text_btree_node_invalidate_upward (GtkTextBTreeNode *node,
gpointer view_id);
static NodeData * gtk_text_btree_node_check_valid (GtkTextBTreeNode *node,
gpointer view_id);
static NodeData * gtk_text_btree_node_check_valid_downward (GtkTextBTreeNode *node,
gpointer view_id);
static void gtk_text_btree_node_check_valid_upward (GtkTextBTreeNode *node,
gpointer view_id);
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);
static void gtk_text_btree_node_free_empty (GtkTextBTree *tree,
GtkTextBTreeNode *node);
static NodeData * gtk_text_btree_node_ensure_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,
GtkTextBTreeNode *node2);
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,
GtkTextBTreeNode *node);
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);
static void redisplay_region (GtkTextBTree *tree,
const GtkTextIter *start,
const GtkTextIter *end,
gboolean cursors_only);
/* Inline thingies */
static inline void
segments_changed (GtkTextBTree *tree)
{
tree->segments_changed_stamp += 1;
}
static inline void
chars_changed (GtkTextBTree *tree)
{
tree->chars_changed_stamp += 1;
}
/*
* BTree operations
*/
GtkTextBTree*
_gtk_text_btree_new (GtkTextTagTable *table,
GtkTextBuffer *buffer)
{
GtkTextBTree *tree;
GtkTextBTreeNode *root_node;
GtkTextLine *line, *line2;
g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), NULL);
g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
/*
* 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. */
root_node = gtk_text_btree_node_new ();
line = gtk_text_line_new ();
line2 = gtk_text_line_new ();
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;
line->parent = root_node;
line->next = line2;
line->segments = _gtk_char_segment_new ("\n", 1);
line2->parent = root_node;
line2->next = NULL;
line2->segments = _gtk_char_segment_new ("\n", 1);
/* Create the tree itself */
tree = g_slice_new0 (GtkTextBTree);
tree->root_node = root_node;
tree->table = table;
tree->views = NULL;
/* Set these to values that are unlikely to be found
* 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 ();
tree->last_line_stamp = tree->chars_changed_stamp - 1;
tree->last_line = NULL;
tree->end_iter_line_stamp = tree->chars_changed_stamp - 1;
tree->end_iter_segment_stamp = tree->segments_changed_stamp - 1;
tree->end_iter_line = NULL;
tree->end_iter_segment_byte_index = 0;
tree->end_iter_segment_char_offset = 0;
g_object_ref (tree->table);
tree->tag_changed_handler = g_signal_connect (tree->table,
"tag-changed",
G_CALLBACK (tag_changed_cb),
tree);
tree->mark_table = g_hash_table_new (g_str_hash, g_str_equal);
tree->child_anchor_table = NULL;
/* We don't ref the buffer, since the buffer owns us;
* we'd have some circularity issues. The buffer always
* lasts longer than the BTree
*/
tree->buffer = buffer;
{
GtkTextIter start;
GtkTextLineSegment *seg;
_gtk_text_btree_get_iter_at_line_char (tree, &start, 0, 0);
tree->insert_mark = _gtk_text_btree_set_mark (tree,
NULL,
"insert",
FALSE,
&start,
FALSE);
seg = tree->insert_mark->segment;
seg->body.mark.not_deleteable = TRUE;
seg->body.mark.visible = TRUE;
tree->selection_bound_mark = _gtk_text_btree_set_mark (tree,
NULL,
"selection_bound",
FALSE,
&start,
FALSE);
seg = tree->selection_bound_mark->segment;
seg->body.mark.not_deleteable = TRUE;
g_object_ref (tree->insert_mark);
g_object_ref (tree->selection_bound_mark);
}
tree->refcount = 1;
return tree;
}
void
_gtk_text_btree_ref (GtkTextBTree *tree)
{
g_return_if_fail (tree != NULL);
g_return_if_fail (tree->refcount > 0);
tree->refcount += 1;
}
void
_gtk_text_btree_unref (GtkTextBTree *tree)
{
g_return_if_fail (tree != NULL);
g_return_if_fail (tree->refcount > 0);
tree->refcount -= 1;
if (tree->refcount == 0)
{
g_signal_handler_disconnect (tree->table,
tree->tag_changed_handler);
g_object_unref (tree->table);
tree->table = NULL;
gtk_text_btree_node_destroy (tree, tree->root_node);
tree->root_node = NULL;
g_assert (g_hash_table_size (tree->mark_table) == 0);
g_hash_table_destroy (tree->mark_table);
tree->mark_table = NULL;
if (tree->child_anchor_table != NULL)
{
g_hash_table_destroy (tree->child_anchor_table);
tree->child_anchor_table = NULL;
}
g_object_unref (tree->insert_mark);
tree->insert_mark = NULL;
g_object_unref (tree->selection_bound_mark);
tree->selection_bound_mark = NULL;
g_slice_free (GtkTextBTree, tree);
}
}
GtkTextBuffer*
_gtk_text_btree_get_buffer (GtkTextBTree *tree)
{
return tree->buffer;
}
guint
_gtk_text_btree_get_chars_changed_stamp (GtkTextBTree *tree)
{
return tree->chars_changed_stamp;
}
guint
_gtk_text_btree_get_segments_changed_stamp (GtkTextBTree *tree)
{
return tree->segments_changed_stamp;
}
void
_gtk_text_btree_segments_changed (GtkTextBTree *tree)
{
g_return_if_fail (tree != NULL);
segments_changed (tree);
}
/*
* Indexable segment mutation
*/
/*
* 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->type == &gtk_text_char_type && 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, FALSE);
}
/* 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, FALSE);
}
}
}
void
_gtk_text_btree_delete (GtkTextIter *start,
GtkTextIter *end)
{
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, *next2;
GtkTextLine *curline;
GtkTextBTreeNode *curnode, *node;
GtkTextBTree *tree;
GtkTextLine *start_line;
GtkTextLine *end_line;
GtkTextLine *line;
GtkTextLine *deleted_lines = NULL; /* List of lines we've deleted */
gint start_byte_offset;
g_return_if_fail (start != NULL);
g_return_if_fail (end != NULL);
g_return_if_fail (_gtk_text_iter_get_btree (start) ==
_gtk_text_iter_get_btree (end));
gtk_text_iter_order (start, end);
tree = _gtk_text_iter_get_btree (start);
#ifdef G_ENABLE_DEBUG
if (GTK_DEBUG_CHECK (TEXT))
_gtk_text_btree_check (tree);
#endif
/* Broadcast the need for redisplay before we break the iterators */
DV (g_print ("invalidating due to deleting some text (%s)\n", G_STRLOC));
_gtk_text_btree_invalidate_region (tree, start, end, FALSE);
/* Save the byte offset so we can reset the iterators */
start_byte_offset = gtk_text_iter_get_line_index (start);
start_line = _gtk_text_iter_get_text_line (start);
end_line = _gtk_text_iter_get_text_line (end);
/*
* 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.
*/
last_seg = gtk_text_line_segment_split (end);
if (last_seg != NULL)
last_seg = last_seg->next;
else
last_seg = end_line->segments;
prev_seg = gtk_text_line_segment_split (start);
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. */
segments_changed (tree);
/*
* 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;
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).
*/
nextline = _gtk_text_line_next (curline);
if (curline != start_line)
{
if (curnode == start_line->parent)
start_line->next = curline->next;
else
curnode->children.line = curline->next;
for (node = curnode; node != NULL;
node = node->parent)
{
/* Don't update node->num_chars, because
* that was done when we deleted the segments.
*/
node->num_lines -= 1;
}
curnode->num_children -= 1;
curline->next = deleted_lines;
deleted_lines = curline;
}
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--;
gtk_text_btree_node_free_empty (tree, curnode);
curnode = parent;
}
curnode = curline->parent;
continue;
}
next = seg->next;
char_count = seg->char_count;
if ((*seg->type->deleteFunc)(seg, curline, FALSE) != 0)
{
/*
* This segment refuses to die. Move it to prev_seg and
* advance prev_seg if the segment has left gravity.
*/
if (prev_seg == NULL)
{
seg->next = start_line->segments;
start_line->segments = seg;
}
else if (prev_seg->next &&
prev_seg->next != last_seg &&
seg->type == &gtk_text_toggle_off_type &&
prev_seg->next->type == &gtk_text_toggle_on_type &&
seg->body.toggle.info == prev_seg->next->body.toggle.info)
{
/* Try to match an off toggle with the matching on toggle
* if it immediately follows. This is a common case, and
* handling it here prevents quadratic blowup in
* cleanup_line() below. See bug 317125.
*/
next2 = prev_seg->next->next;
_gtk_toggle_segment_free (prev_seg->next);
prev_seg->next = next2;
_gtk_toggle_segment_free (seg);
seg = NULL;
}
else
{
seg->next = prev_seg->next;
prev_seg->next = seg;
}
if (seg && 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;
}
}
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;
int chars_moved;
/* last_seg was appended to start_line up at the top of this function */
chars_moved = 0;
for (seg = last_seg; seg != NULL;
seg = seg->next)
{
chars_moved += seg->char_count;
if (seg->type->lineChangeFunc != NULL)
{
(*seg->type->lineChangeFunc)(seg, end_line);
}
}
for (node = start_line->parent; node != NULL;
node = node->parent)
{
node->num_chars += chars_moved;
}
curnode = end_line->parent;
for (node = curnode; node != NULL;
node = node->parent)
{
node->num_chars -= chars_moved;
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)
{
GtkTextLineData *ld;
gint deleted_width = 0;
gint deleted_height = 0;
line = deleted_lines;
while (line)
{
GtkTextLine *next_line = line->next;
ld = _gtk_text_line_get_data (line, view->view_id);
if (ld)
{
deleted_width = MAX (deleted_width, ld->width);
deleted_height += ld->height;
}
line = next_line;
}
if (deleted_width > 0 || deleted_height > 0)
{
ld = _gtk_text_line_get_data (start_line, view->view_id);
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.
*/
ld = _gtk_text_line_data_new (view->layout, start_line);
_gtk_text_line_add_data (start_line, ld);
ld->width = 0;
ld->height = 0;
ld->valid = FALSE;
}
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);
view = view->next;
}
line = deleted_lines;
while (line)
{
GtkTextLine *next_line = line->next;
gtk_text_line_destroy (tree, line);
line = next_line;
}
/* avoid dangling pointer */
deleted_lines = NULL;
gtk_text_btree_rebalance (tree, curnode);
}
/*
* Cleanup the segments in the new line.
*/
cleanup_line (start_line);
/*
* Lastly, rebalance the first GtkTextBTreeNode of the range.
*/
gtk_text_btree_rebalance (tree, start_line->parent);
/* Notify outstanding iterators that they
are now hosed */
chars_changed (tree);
segments_changed (tree);
#ifdef G_ENABLE_DEBUG
if (GTK_DEBUG_CHECK (TEXT))
_gtk_text_btree_check (tree);
#endif
/* Re-initialize our iterators */
_gtk_text_btree_get_iter_at_line (tree, start, start_line, start_byte_offset);
*end = *start;
gtk_text_btree_resolve_bidi (start, end);
}
void
_gtk_text_btree_insert (GtkTextIter *iter,
const gchar *text,
gint len)
{
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
* are inserted just after this one.
* NULL means insert at beginning of
* line. */
GtkTextLine *line; /* Current line (new segments are
* added to this line). */
GtkTextLineSegment *seg;
GtkTextLine *newline;
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 */
int line_count_delta; /* Counts change to total number of
* lines in file.
*/
int char_count_delta; /* change to number of chars */
GtkTextBTree *tree;
gint start_byte_index;
GtkTextLine *start_line;
g_return_if_fail (text != NULL);
g_return_if_fail (iter != NULL);
if (len < 0)
len = strlen (text);
/* extract iterator info */
tree = _gtk_text_iter_get_btree (iter);
line = _gtk_text_iter_get_text_line (iter);
start_line = line;
start_byte_index = gtk_text_iter_get_line_index (iter);
/* 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));
prev_seg = gtk_text_line_segment_split (iter);
cur_seg = prev_seg;
/* Invalidate all iterators */
chars_changed (tree);
segments_changed (tree);
/*
* 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)
{
sol = eol;
pango_find_paragraph_boundary (text + sol,
len - sol,
&delim,
&eol);
/* make these relative to the start of the text */
delim += sol;
eol += sol;
g_assert (eol >= sol);
g_assert (delim >= sol);
g_assert (eol >= delim);
g_assert (sol >= 0);
g_assert (eol <= len);
chunk_len = eol - sol;
g_assert (g_utf8_validate (&text[sol], chunk_len, NULL));
seg = _gtk_char_segment_new (&text[sol], chunk_len);
char_count_delta += seg->char_count;
if (cur_seg == NULL)
{
seg->next = line->segments;
line->segments = seg;
}
else
{
seg->next = cur_seg->next;
cur_seg->next = seg;
}
if (delim == eol)
{
/* chunk didn't end with a paragraph separator */
g_assert (eol == len);
break;
}
/*
* The chunk ended with a newline, so create a new GtkTextLine
* and move the remainder of the old line to it.
*/
newline = gtk_text_line_new ();
gtk_text_line_set_parent (newline, line->parent);
newline->next = line->next;
line->next = newline;
newline->segments = seg->next;
seg->next = NULL;
line = newline;
cur_seg = NULL;
line_count_delta++;
}
/*
* Cleanup the starting line for the insertion, plus the ending
* line if it's different.
*/
cleanup_line (start_line);
if (line != start_line)
{
cleanup_line (line);
}
post_insert_fixup (tree, line, line_count_delta, char_count_delta);
/* Invalidate our region, and reset the iterator the user
passed in to point to the end of the inserted text. */
{
GtkTextIter start;
GtkTextIter end;
_gtk_text_btree_get_iter_at_line (tree,
&start,
start_line,
start_byte_index);
end = start;
/* We could almost certainly be more efficient here
by saving the information from the insertion loop
above. FIXME */
gtk_text_iter_forward_chars (&end, char_count_delta);
DV (g_print ("invalidating due to inserting some text (%s)\n", G_STRLOC));
_gtk_text_btree_invalidate_region (tree, &start, &end, FALSE);
/* Convenience for the user */
*iter = end;
gtk_text_btree_resolve_bidi (&start, &end);
}
}
static void
insert_texture_or_widget_segment (GtkTextIter *iter,
GtkTextLineSegment *seg)
{
GtkTextIter start;
GtkTextLineSegment *prevPtr;
GtkTextLine *line;
GtkTextBTree *tree;
gint start_byte_offset;
line = _gtk_text_iter_get_text_line (iter);
tree = _gtk_text_iter_get_btree (iter);
start_byte_offset = gtk_text_iter_get_line_index (iter);
prevPtr = gtk_text_line_segment_split (iter);
if (prevPtr == NULL)
{
seg->next = line->segments;
line->segments = seg;
}
else
{
seg->next = prevPtr->next;
prevPtr->next = seg;
}
post_insert_fixup (tree, line, 0, seg->char_count);
chars_changed (tree);
segments_changed (tree);
/* reset *iter for the user, and invalidate tree nodes */
_gtk_text_btree_get_iter_at_line (tree, &start, line, start_byte_offset);
*iter = start;
gtk_text_iter_forward_char (iter); /* skip forward past the segment */
DV (g_print ("invalidating due to inserting texture/widget (%s)\n", G_STRLOC));
_gtk_text_btree_invalidate_region (tree, &start, iter, FALSE);
}
void
_gtk_text_btree_insert_texture (GtkTextIter *iter,
GdkTexture *texture)
{
GtkTextLineSegment *seg;
seg = _gtk_texture_segment_new (texture);
insert_texture_or_widget_segment (iter, seg);
}
void
_gtk_text_btree_insert_child_anchor (GtkTextIter *iter,
GtkTextChildAnchor *anchor)
{
GtkTextLineSegment *seg;
GtkTextBTree *tree;
if (anchor->segment != NULL)
{
g_warning (G_STRLOC": Same child anchor can't be inserted twice");
return;
}
seg = _gtk_widget_segment_new (anchor);
tree = seg->body.child.tree = _gtk_text_iter_get_btree (iter);
seg->body.child.line = _gtk_text_iter_get_text_line (iter);
insert_texture_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
_gtk_text_btree_unregister_child_anchor (GtkTextChildAnchor *anchor)
{
GtkTextLineSegment *seg;
seg = anchor->segment;
g_hash_table_remove (seg->body.child.tree->child_anchor_table,
anchor);
}
/*
* View stuff
*/
static GtkTextLine*
find_line_by_y (GtkTextBTree *tree, BTreeView *view,
GtkTextBTreeNode *node, gint y, gint *line_top,
GtkTextLine *last_line)
{
gint current_y = 0;
#ifdef G_ENABLE_DEBUG
if (GTK_DEBUG_CHECK (TEXT))
_gtk_text_btree_check (tree);
#endif
if (node->level == 0)
{
GtkTextLine *line;
line = node->children.line;
while (line != NULL && line != last_line)
{
GtkTextLineData *ld;
ld = _gtk_text_line_get_data (line, view->view_id);
if (ld)
{
if (y < (current_y + ld->height))
return line;
current_y += ld->height;
*line_top += ld->height;
}
line = line->next;
}
return NULL;
}
else
{
GtkTextBTreeNode *child;
child = node->children.node;
while (child != NULL)
{
gint width;
gint height;
gtk_text_btree_node_get_size (child, view->view_id,
&width, &height);
if (y < (current_y + height))
return find_line_by_y (tree, view, child,
y - current_y, line_top,
last_line);
current_y += height;
*line_top += height;
child = child->next;
}
return NULL;
}
}
GtkTextLine *
_gtk_text_btree_find_line_by_y (GtkTextBTree *tree,
gpointer view_id,
gint ypixel,
gint *line_top_out)
{
GtkTextLine *line;
BTreeView *view;
GtkTextLine *last_line;
gint line_top = 0;
view = gtk_text_btree_get_view (tree, view_id);
g_return_val_if_fail (view != NULL, NULL);
last_line = get_last_line (tree);
line = find_line_by_y (tree, view, tree->root_node, ypixel, &line_top,
last_line);
if (line_top_out)
*line_top_out = line_top;
return line;
}
static gint
find_line_top_in_line_list (GtkTextBTree *tree,
BTreeView *view,
GtkTextLine *line,
GtkTextLine *target_line,
gint y)
{
while (line != NULL)
{
GtkTextLineData *ld;
if (line == target_line)
return y;
ld = _gtk_text_line_get_data (line, view->view_id);
if (ld)
y += ld->height;
line = line->next;
}
g_assert_not_reached (); /* If we get here, our
target line didn't exist
under its parent node */
return 0;
}
gint
_gtk_text_btree_find_line_top (GtkTextBTree *tree,
GtkTextLine *target_line,
gpointer view_id)
{
gint y = 0;
BTreeView *view;
GSList *nodes;
GSList *iter;
GtkTextBTreeNode *node;
view = gtk_text_btree_get_view (tree, view_id);
g_return_val_if_fail (view != NULL, 0);
nodes = NULL;
node = target_line->parent;
while (node != NULL)
{
nodes = g_slist_prepend (nodes, node);
node = node->parent;
}
iter = nodes;
while (iter != NULL)
{
node = iter->data;
if (node->level == 0)
{
g_slist_free (nodes);
return find_line_top_in_line_list (tree, view,
node->children.line,
target_line, y);
}
else
{
GtkTextBTreeNode *child;
GtkTextBTreeNode *target_node;
g_assert (iter->next != NULL); /* not at level 0 */
target_node = iter->next->data;
child = node->children.node;
while (child != NULL)
{
gint width;
gint height;
if (child == target_node)
break;
else
{
gtk_text_btree_node_get_size (child, view->view_id,
&width, &height);
y += height;
}
child = child->next;
}
g_assert (child != NULL); /* should have broken out before we
ran out of nodes */
}
iter = iter->next;
}
g_assert_not_reached (); /* we return when we find the target line */
return 0;
}
void
_gtk_text_btree_add_view (GtkTextBTree *tree,
GtkTextLayout *layout)
{
BTreeView *view;
GtkTextLine *last_line;
GtkTextLineData *line_data;
g_return_if_fail (tree != NULL);
view = g_slice_new (BTreeView);
view->view_id = layout;
view->layout = layout;
view->next = tree->views;
view->prev = NULL;
if (tree->views)
{
g_assert (tree->views->prev == NULL);
tree->views->prev = view;
}
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);
line_data = g_slice_new (GtkTextLineData);
line_data->view_id = layout;
line_data->next = NULL;
line_data->width = 0;
line_data->height = 0;
line_data->valid = TRUE;
_gtk_text_line_add_data (last_line, line_data);
}
void
_gtk_text_btree_remove_view (GtkTextBTree *tree,
gpointer view_id)
{
BTreeView *view;
GtkTextLine *last_line;
GtkTextLineData *line_data;
g_return_if_fail (tree != NULL);
view = tree->views;
while (view != NULL)
{
if (view->view_id == view_id)
break;
view = view->next;
}
g_return_if_fail (view != NULL);
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);
line_data = _gtk_text_line_remove_data (last_line, view_id);
g_slice_free (GtkTextLineData, line_data);
gtk_text_btree_node_remove_view (view, tree->root_node, view_id);
view->layout = (gpointer) 0xdeadbeef;
view->view_id = (gpointer) 0xdeadbeef;
g_slice_free (BTreeView, view);
}
void
_gtk_text_btree_invalidate_region (GtkTextBTree *tree,
const GtkTextIter *start,
const GtkTextIter *end,
gboolean cursors_only)
{
BTreeView *view;
view = tree->views;
while (view != NULL)
{
if (cursors_only)
gtk_text_layout_invalidate_cursors (view->layout, start, end);
else
gtk_text_layout_invalidate (view->layout, start, end);
view = view->next;
}
}
void
_gtk_text_btree_get_view_size (GtkTextBTree *tree,
gpointer view_id,
gint *width,
gint *height)
{
g_return_if_fail (tree != NULL);
g_return_if_fail (view_id != NULL);
gtk_text_btree_node_get_size (tree->root_node, view_id,
width, height);
}
/*
* Tag
*/
typedef struct {
GtkTextIter *iters;
guint count;
guint alloced;
} IterStack;
static IterStack*
iter_stack_new (void)
{
IterStack *stack;
stack = g_slice_new (IterStack);
stack->iters = NULL;
stack->count = 0;
stack->alloced = 0;
return stack;
}
static void
iter_stack_push (IterStack *stack,
const GtkTextIter *iter)
{
stack->count += 1;
if (stack->count > stack->alloced)
{
stack->alloced = stack->count*2;
stack->iters = g_realloc (stack->iters,
stack->alloced * sizeof (GtkTextIter));
}
stack->iters[stack->count-1] = *iter;
}
static gboolean
iter_stack_pop (IterStack *stack,
GtkTextIter *iter)
{
if (stack->count == 0)
return FALSE;
else
{
stack->count -= 1;
*iter = stack->iters[stack->count];
return TRUE;
}
}
static void
iter_stack_free (IterStack *stack)
{
g_free (stack->iters);
g_slice_free (IterStack, stack);
}
static void
iter_stack_invert (IterStack *stack)
{
if (stack->count > 0)
{
guint i = 0;
guint j = stack->count - 1;
while (i < j)
{
GtkTextIter tmp;
tmp = stack->iters[i];
stack->iters[i] = stack->iters[j];
stack->iters[j] = tmp;
++i;
--j;
}
}
}
static void
queue_tag_redisplay (GtkTextBTree *tree,
GtkTextTag *tag,
const GtkTextIter *start,
const GtkTextIter *end)
{
if (_gtk_text_tag_affects_size (tag))
{
DV (g_print ("invalidating due to size-affecting tag (%s)\n", G_STRLOC));
_gtk_text_btree_invalidate_region (tree, start, end, FALSE);
}
else if (_gtk_text_tag_affects_nonsize_appearance (tag))
{
/* We only need to queue a redraw, not a relayout */
redisplay_region (tree, start, end, FALSE);
}
/* We don't need to do anything if the tag doesn't affect display */
}
void
_gtk_text_btree_tag (const GtkTextIter *start_orig,
const GtkTextIter *end_orig,
GtkTextTag *tag,
gboolean add)
{
GtkTextLineSegment *seg, *prev;
GtkTextLine *cleanupline;
gboolean toggled_on;
GtkTextLine *start_line;
GtkTextLine *end_line;
GtkTextIter iter;
GtkTextIter start, end;
GtkTextBTree *tree;
IterStack *stack;
GtkTextTagInfo *info;
g_return_if_fail (start_orig != NULL);
g_return_if_fail (end_orig != NULL);
g_return_if_fail (GTK_IS_TEXT_TAG (tag));
g_return_if_fail (_gtk_text_iter_get_btree (start_orig) ==
_gtk_text_iter_get_btree (end_orig));
g_return_if_fail (tag->priv->table == _gtk_text_iter_get_btree (start_orig)->table);
#if 0
printf ("%s tag %s from %d to %d\n",
add ? "Adding" : "Removing",
tag->name,
gtk_text_buffer_get_offset (start_orig),
gtk_text_buffer_get_offset (end_orig));
#endif
if (gtk_text_iter_equal (start_orig, end_orig))
return;
start = *start_orig;
end = *end_orig;
gtk_text_iter_order (&start, &end);
tree = _gtk_text_iter_get_btree (&start);
queue_tag_redisplay (tree, tag, &start, &end);
info = gtk_text_btree_get_tag_info (tree, tag);
start_line = _gtk_text_iter_get_text_line (&start);
end_line = _gtk_text_iter_get_text_line (&end);
/* Find all tag toggles in the region; we are going to delete them.
We need to find them in advance, because
forward_find_tag_toggle () won't work once we start playing around
with the tree. */
stack = iter_stack_new ();
iter = start;
/* forward_to_tag_toggle() skips a toggle at the start iterator,
* which is deliberate - we don't want to delete a toggle at the
* start.
*/
while (gtk_text_iter_forward_to_tag_toggle (&iter, tag))
{
if (gtk_text_iter_compare (&iter, &end) >= 0)
break;
else
iter_stack_push (stack, &iter);
}
/* We need to traverse the toggles in order. */
iter_stack_invert (stack);
/*
* See whether the tag is present at the start of the range. If
* the state doesn't already match what we want then add a toggle
* there.
*/
toggled_on = gtk_text_iter_has_tag (&start, tag);
if ( (add && !toggled_on) ||
(!add && toggled_on) )
{
/* This could create a second toggle at the start position;
cleanup_line () will remove it if so. */
seg = _gtk_toggle_segment_new (info, add);
prev = gtk_text_line_segment_split (&start);
if (prev == NULL)
{
seg->next = start_line->segments;
start_line->segments = seg;
}
else
{
seg->next = prev->next;
prev->next = seg;
}
/* cleanup_line adds the new toggle to the node counts. */
#if 0
printf ("added toggle at start\n");
#endif
/* we should probably call segments_changed, but in theory
any still-cached segments in the iters we are about to
use are still valid, since they're in front
of this spot. */
}
/*
*
* Scan the range of characters and delete any internal tag
* transitions. Keep track of what the old state was at the end
* of the range, and add a toggle there if it's needed.
*
*/
cleanupline = start_line;
while (iter_stack_pop (stack, &iter))
{
GtkTextLineSegment *indexable_seg;
GtkTextLine *line;
line = _gtk_text_iter_get_text_line (&iter);
seg = _gtk_text_iter_get_any_segment (&iter);
indexable_seg = _gtk_text_iter_get_indexable_segment (&iter);
g_assert (seg != NULL);
g_assert (indexable_seg != NULL);
g_assert (seg != indexable_seg);
prev = line->segments;
/* Find the segment that actually toggles this tag. */
while (seg != indexable_seg)
{
g_assert (seg != NULL);
g_assert (indexable_seg != NULL);
g_assert (seg != indexable_seg);
if ( (seg->type == &gtk_text_toggle_on_type ||
seg->type == &gtk_text_toggle_off_type) &&
(seg->body.toggle.info == info) )
break;
else
seg = seg->next;
}
g_assert (seg != NULL);
g_assert (indexable_seg != NULL);
g_assert (seg != indexable_seg); /* If this happens, then
forward_to_tag_toggle was
full of shit. */
g_assert (seg->body.toggle.info->tag == tag);
/* If this happens, when previously tagging we didn't merge
overlapping tags. */
g_assert ( (toggled_on && seg->type == &gtk_text_toggle_off_type) ||
(!toggled_on && seg->type == &gtk_text_toggle_on_type) );
toggled_on = !toggled_on;
#if 0
printf ("deleting %s toggle\n",
seg->type == &gtk_text_toggle_on_type ? "on" : "off");
#endif
/* Remove toggle segment from the list. */
if (prev == seg)
{
line->segments = seg->next;
}
else
{
while (prev->next != seg)
{
prev = prev->next;
}
prev->next = seg->next;
}
/* Inform iterators we've hosed them. This actually reflects a
bit of inefficiency; if you have the same tag toggled on and
off a lot in a single line, we keep having the rescan from
the front of the line. Of course we have to do that to get
"prev" anyway, but here we are doing it an additional
time. FIXME */
segments_changed (tree);
/* Update node counts */
if (seg->body.toggle.inNodeCounts)
{
_gtk_change_node_toggle_count (line->parent,
info, -1);
seg->body.toggle.inNodeCounts = FALSE;
}
_gtk_toggle_segment_free (seg);
/* We only clean up lines when we're done with them, saves some
gratuitous line-segment-traversals */
if (cleanupline != line)
{
cleanup_line (cleanupline);
cleanupline = line;
}
}
iter_stack_free (stack);
/* toggled_on now reflects the toggle state _just before_ the
end iterator. The end iterator could already have a toggle
on or a toggle off. */
if ( (add && !toggled_on) ||
(!add && toggled_on) )
{
/* This could create a second toggle at the start position;
cleanup_line () will remove it if so. */
seg = _gtk_toggle_segment_new (info, !add);
prev = gtk_text_line_segment_split (&end);
if (prev == NULL)
{
seg->next = end_line->segments;
end_line->segments = seg;
}
else
{
seg->next = prev->next;
prev->next = seg;
}
/* cleanup_line adds the new toggle to the node counts. */
g_assert (seg->body.toggle.inNodeCounts == FALSE);
#if 0
printf ("added toggle at end\n");
#endif
}
/*
* Cleanup cleanupline and the last line of the range, if
* these are different.
*/
cleanup_line (cleanupline);
if (cleanupline != end_line)
{
cleanup_line (end_line);
}
segments_changed (tree);
queue_tag_redisplay (tree, tag, &start, &end);
#ifdef G_ENABLE_DEBUG
if (GTK_DEBUG_CHECK (TEXT))
_gtk_text_btree_check (tree);
#endif
}
/*
* "Getters"
*/
static GtkTextLine*
get_line_internal (GtkTextBTree *tree,
gint line_number,
gint *real_line_number,
gboolean include_last)
{
GtkTextBTreeNode *node;
GtkTextLine *line;
int lines_left;
int line_count;
line_count = _gtk_text_btree_line_count (tree);
if (!include_last)
line_count -= 1;
if (line_number < 0)
{
line_number = line_count;
}
else if (line_number > line_count)
{
line_number = line_count;
}
if (real_line_number)
*real_line_number = line_number;
node = tree->root_node;
lines_left = line_number;
/*
* Work down through levels of the tree until a GtkTextBTreeNode is found at
* level 0.
*/
while (node->level != 0)
{
for (node = node->children.node;
node->num_lines <= lines_left;
node = node->next)
{
#if 0
if (node == NULL)
{
g_error ("gtk_text_btree_find_line ran out of GtkTextBTreeNodes");
}
#endif
lines_left -= node->num_lines;
}
}
/*
* Work through the lines attached to the level-0 GtkTextBTreeNode.
*/
for (line = node->children.line; lines_left > 0;
line = line->next)
{
#if 0
if (line == NULL)
{
g_error ("gtk_text_btree_find_line ran out of lines");
}
#endif
lines_left -= 1;
}
return line;
}
GtkTextLine*
_gtk_text_btree_get_end_iter_line (GtkTextBTree *tree)
{
return
_gtk_text_btree_get_line (tree,
_gtk_text_btree_line_count (tree) - 1,
NULL);
}
GtkTextLine*
_gtk_text_btree_get_line (GtkTextBTree *tree,
gint line_number,
gint *real_line_number)
{
return get_line_internal (tree, line_number, real_line_number, TRUE);
}
GtkTextLine*
_gtk_text_btree_get_line_no_last (GtkTextBTree *tree,
gint line_number,
gint *real_line_number)
{
return get_line_internal (tree, line_number, real_line_number, FALSE);
}
GtkTextLine*
_gtk_text_btree_get_line_at_char (GtkTextBTree *tree,
gint char_index,
gint *line_start_index,
gint *real_char_index)
{
GtkTextBTreeNode *node;
GtkTextLine *line;
GtkTextLineSegment *seg;
int chars_left;
int chars_in_line;
node = tree->root_node;
/* Clamp to valid indexes (-1 is magic for "highest index"),
* node->num_chars includes the two newlines that aren't really
* in the buffer.
*/
if (char_index < 0 || char_index >= (node->num_chars - 1))
{
char_index = node->num_chars - 2;
}
*real_char_index = char_index;
/*
* Work down through levels of the tree until a GtkTextBTreeNode is found at
* level 0.
*/
chars_left = char_index;
while (node->level != 0)
{
for (node = node->children.node;
chars_left >= node->num_chars;
node = node->next)
{
chars_left -= node->num_chars;
g_assert (chars_left >= 0);
}
}
if (chars_left == 0)
{
/* Start of a line */
*line_start_index = char_index;
return node->children.line;
}
/*
* Work through the lines attached to the level-0 GtkTextBTreeNode.
*/
chars_in_line = 0;
seg = NULL;
for (line = node->children.line; line != NULL; line = line->next)
{
seg = line->segments;
while (seg != NULL)
{
if (chars_in_line + seg->char_count > chars_left)
goto found; /* found our line/segment */
chars_in_line += seg->char_count;
seg = seg->next;
}
chars_left -= chars_in_line;
chars_in_line = 0;
seg = NULL;
}
found:
g_assert (line != NULL); /* hosage, ran out of lines */
g_assert (seg != NULL);
*line_start_index = char_index - chars_left;
return line;
}
/* It returns an array sorted by tags priority, ready to pass to
* _gtk_text_attributes_fill_from_tags() */
GtkTextTag**
_gtk_text_btree_get_tags (const GtkTextIter *iter,
gint *num_tags)
{
GtkTextBTreeNode *node;
GtkTextLine *siblingline;
GtkTextLineSegment *seg;
int src, dst, index;
TagInfo tagInfo;
GtkTextLine *line;
gint byte_index;
#define NUM_TAG_INFOS 10
line = _gtk_text_iter_get_text_line (iter);
byte_index = gtk_text_iter_get_line_index (iter);
tagInfo.numTags = 0;
tagInfo.arraySize = NUM_TAG_INFOS;
tagInfo.tags = g_new (GtkTextTag*, NUM_TAG_INFOS);
tagInfo.counts = g_new (int, NUM_TAG_INFOS);
/*
* Record tag toggles within the line of indexPtr but preceding
* indexPtr. Note that if this loop segfaults, your
* byte_index probably points past the sum of all
* seg->byte_count */
for (index = 0, seg = line->segments;
(index + seg->byte_count) <= byte_index;
index += seg->byte_count, seg = seg->next)
{
if ((seg->type == &gtk_text_toggle_on_type)
|| (seg->type == &gtk_text_toggle_off_type))
{
inc_count (seg->body.toggle.info->tag, 1, &tagInfo);
}
}
/*
* Record toggles for tags in lines that are predecessors of
* line but under the same level-0 GtkTextBTreeNode.
*/
for (siblingline = line->parent->children.line;
siblingline != line;
siblingline = siblingline->next)
{
for (seg = siblingline->segments; seg != NULL;
seg = seg->next)
{
if ((seg->type == &gtk_text_toggle_on_type)
|| (seg->type == &gtk_text_toggle_off_type))
{
inc_count (seg->body.toggle.info->tag, 1, &tagInfo);
}
}
}
/*
* For each GtkTextBTreeNode in the ancestry of this line, record tag
* toggles for all siblings that precede that GtkTextBTreeNode.
*/
for (node = line->parent; node->parent != NULL;
node = node->parent)
{
GtkTextBTreeNode *siblingPtr;
Summary *summary;
for (siblingPtr = node->parent->children.node;
siblingPtr != node; siblingPtr = siblingPtr->next)
{
for (summary = siblingPtr->summary; summary != NULL;
summary = summary->next)
{
if (summary->toggle_count & 1)
{
inc_count (summary->info->tag, summary->toggle_count,
&tagInfo);
}
}
}
}
/*
* Go through the tag information and squash out all of the tags
* that have even toggle counts (these tags exist before the point
* of interest, but not at the desired character itself).
*/
for (src = 0, dst = 0; src < tagInfo.numTags; src++)
{
if (tagInfo.counts[src] & 1)
{
g_assert (GTK_IS_TEXT_TAG (tagInfo.tags[src]));
tagInfo.tags[dst] = tagInfo.tags[src];
dst++;
}
}
*num_tags = dst;
g_free (tagInfo.counts);
if (dst == 0)
{
g_free (tagInfo.tags);
return NULL;
}
/* Sort tags in ascending order of priority */
_gtk_text_tag_array_sort (tagInfo.tags, dst);
return tagInfo.tags;
}
static void
copy_segment (GString *string,
gboolean include_hidden,
gboolean include_nonchars,
const GtkTextIter *start,
const GtkTextIter *end)
{
GtkTextLineSegment *end_seg;
GtkTextLineSegment *seg;
if (gtk_text_iter_equal (start, end))
return;
seg = _gtk_text_iter_get_indexable_segment (start);
end_seg = _gtk_text_iter_get_indexable_segment (end);
if (seg->type == &gtk_text_char_type)
{
gboolean copy = TRUE;
gint copy_bytes = 0;
gint copy_start = 0;
/* Don't copy if we're invisible; segments are invisible/not
as a whole, no need to check each char */
if (!include_hidden &&
_gtk_text_btree_char_is_invisible (start))
{
copy = FALSE;
/* printf (" <invisible>\n"); */
}
copy_start = _gtk_text_iter_get_segment_byte (start);
if (seg == end_seg)
{
/* End is in the same segment; need to copy fewer bytes. */
gint end_byte = _gtk_text_iter_get_segment_byte (end);
copy_bytes = end_byte - copy_start;
}
else
copy_bytes = seg->byte_count - copy_start;
g_assert (copy_bytes != 0); /* Due to iter equality check at
front of this function. */
if (copy)
{
g_assert ((copy_start + copy_bytes) <= seg->byte_count);
g_string_append_len (string,
seg->body.chars + copy_start,
copy_bytes);
}
/* printf (" :%s\n", string->str); */
}
else if (seg->type == &gtk_text_texture_type ||
seg->type == &gtk_text_child_type)
{
gboolean copy = TRUE;
if (!include_nonchars)
{
copy = FALSE;
}
else if (!include_hidden &&
_gtk_text_btree_char_is_invisible (start))
{
copy = FALSE;
}
if (copy)
{
g_string_append_len (string,
_gtk_text_unknown_char_utf8,
GTK_TEXT_UNKNOWN_CHAR_UTF8_LEN);
}
}
}
gchar*
_gtk_text_btree_get_text (const GtkTextIter *start_orig,
const GtkTextIter *end_orig,
gboolean include_hidden,
gboolean include_nonchars)
{
GtkTextLineSegment *seg;
GtkTextLineSegment *end_seg;
GString *retval;
gchar *str;
GtkTextIter iter;
GtkTextIter start;
GtkTextIter end;
g_return_val_if_fail (start_orig != NULL, NULL);
g_return_val_if_fail (end_orig != NULL, NULL);
g_return_val_if_fail (_gtk_text_iter_get_btree (start_orig) ==
_gtk_text_iter_get_btree (end_orig), NULL);
start = *start_orig;
end = *end_orig;
gtk_text_iter_order (&start, &end);
retval = g_string_new (NULL);
end_seg = _gtk_text_iter_get_indexable_segment (&end);
iter = start;
seg = _gtk_text_iter_get_indexable_segment (&iter);
while (seg != end_seg)
{
copy_segment (retval, include_hidden, include_nonchars,
&iter, &end);
_gtk_text_iter_forward_indexable_segment (&iter);
seg = _gtk_text_iter_get_indexable_segment (&iter);
}
copy_segment (retval, include_hidden, include_nonchars, &iter, &end);
str = retval->str;
g_string_free (retval, FALSE);
return str;
}
gint
_gtk_text_btree_line_count (GtkTextBTree *tree)
{
/* Subtract bogus line at the end; we return a count
of usable lines. */
return tree->root_node->num_lines - 1;
}
gint
_gtk_text_btree_char_count (GtkTextBTree *tree)
{
/* Exclude newline in bogus last line and the
* one in the last line that is after the end iterator
*/
return tree->root_node->num_chars - 2;
}
#define LOTSA_TAGS 1000
gboolean
_gtk_text_btree_char_is_invisible (const GtkTextIter *iter)
{
gboolean invisible = FALSE; /* if nobody says otherwise, it's visible */
int deftagCnts[LOTSA_TAGS] = { 0, };
int *tagCnts = deftagCnts;
GtkTextTag *deftags[LOTSA_TAGS];
GtkTextTag **tags = deftags;
int numTags;
GtkTextBTreeNode *node;
GtkTextLine *siblingline;
GtkTextLineSegment *seg;
GtkTextTag *tag;
int i, index;
GtkTextLine *line;
GtkTextBTree *tree;
gint byte_index;
line = _gtk_text_iter_get_text_line (iter);
tree = _gtk_text_iter_get_btree (iter);
byte_index = gtk_text_iter_get_line_index (iter);
numTags = gtk_text_tag_table_get_size (tree->table);
/* almost always avoid malloc, so stay out of system calls */
if (LOTSA_TAGS < numTags)
{
tagCnts = g_new0 (int, numTags);
tags = g_new (GtkTextTag*, numTags);
}
/*
* Record tag toggles within the line of indexPtr but preceding
* indexPtr.
*/
for (index = 0, seg = line->segments;
(index + seg->byte_count) <= byte_index; /* segfault here means invalid index */
index += seg->byte_count, seg = seg->next)
{
if ((seg->type == &gtk_text_toggle_on_type)
|| (seg->type == &gtk_text_toggle_off_type))
{
tag = seg->body.toggle.info->tag;
if (tag->priv->invisible_set)
{
tags[tag->priv->priority] = tag;
tagCnts[tag->priv->priority]++;
}
}
}
/*
* Record toggles for tags in lines that are predecessors of
* line but under the same level-0 GtkTextBTreeNode.
*/
for (siblingline = line->parent->children.line;
siblingline != line;
siblingline = siblingline->next)
{
for (seg = siblingline->segments; seg != NULL;
seg = seg->next)
{
if ((seg->type == &gtk_text_toggle_on_type)
|| (seg->type == &gtk_text_toggle_off_type))
{
tag = seg->body.toggle.info->tag;
if (tag->priv->invisible_set)
{
tags[tag->priv->priority] = tag;
tagCnts[tag->priv->priority]++;
}
}
}
}
/*
* For each GtkTextBTreeNode in the ancestry of this line, record tag toggles
* for all siblings that precede that GtkTextBTreeNode.
*/
for (node = line->parent; node->parent != NULL;
node = node->parent)
{
GtkTextBTreeNode *siblingPtr;
Summary *summary;
for (siblingPtr = node->parent->children.node;
siblingPtr != node; siblingPtr = siblingPtr->next)
{
for (summary = siblingPtr->summary; summary != NULL;
summary = summary->next)
{
if (summary->toggle_count & 1)
{
tag = summary->info->tag;
if (tag->priv->invisible_set)
{
tags[tag->priv->priority] = tag;
tagCnts[tag->priv->priority] += summary->toggle_count;
}
}
}
}
}
/*
* Now traverse from highest priority to lowest,
* take invisible value from first odd count (= on)
*/
for (i = numTags-1; i >=0; i--)
{
if (tagCnts[i] & 1)
{
/* FIXME not sure this should be if 0 */
#if 0
#ifndef ALWAYS_SHOW_SELECTION
/* who would make the selection invisible? */
if ((tag == tkxt->seltag)
&& !(tkxt->flags & GOT_FOCUS))
{
continue;
}
#endif
#endif
invisible = tags[i]->priv->values->invisible;
break;
}
}
if (LOTSA_TAGS < numTags)
{
g_free (tagCnts);
g_free (tags);
}
return invisible;
}
/*
* Manipulate marks
*/
static void
redisplay_region (GtkTextBTree *tree,
const GtkTextIter *start,
const GtkTextIter *end,
gboolean cursors_only)
{
BTreeView *view;
GtkTextLine *start_line, *end_line;
if (gtk_text_iter_compare (start, end) > 0)
{
const GtkTextIter *tmp = start;
start = end;
end = tmp;
}
start_line = _gtk_text_iter_get_text_line (start);
end_line = _gtk_text_iter_get_text_line (end);
view = tree->views;
while (view != NULL)
{
gint start_y, end_y;
GtkTextLineData *ld;
start_y = _gtk_text_btree_find_line_top (tree, start_line, view->view_id);
if (end_line == start_line)
end_y = start_y;
else
end_y = _gtk_text_btree_find_line_top (tree, end_line, view->view_id);
ld = _gtk_text_line_get_data (start_line, view->view_id);
if (ld)
start_y -= ld->top_ink;
ld = _gtk_text_line_get_data (end_line, view->view_id);
if (ld)
end_y += ld->height + ld->bottom_ink;
if (cursors_only)
gtk_text_layout_cursors_changed (view->layout, start_y,
end_y - start_y,
end_y - start_y);
else
gtk_text_layout_changed (view->layout, start_y,
end_y - start_y,
end_y - start_y);
view = view->next;
}
}
static void
redisplay_mark (GtkTextLineSegment *mark)
{
GtkTextIter iter;
GtkTextIter end;
gboolean cursor_only;
_gtk_text_btree_get_iter_at_mark (mark->body.mark.tree,
&iter,
mark->body.mark.obj);
end = iter;
gtk_text_iter_forward_char (&end);
DV (g_print ("invalidating due to moving visible mark (%s)\n", G_STRLOC));
cursor_only = mark == mark->body.mark.tree-&g