1 /* gtktextmark.c - mark segments
3 * Copyright (c) 1994 The Regents of the University of California.
4 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
5 * Copyright (c) 2000 Red Hat, Inc.
6 * Tk -> Gtk port by Havoc Pennington <hp@redhat.com>
8 * This software is copyrighted by the Regents of the University of
9 * California, Sun Microsystems, Inc., and other parties. The
10 * following terms apply to all files associated with the software
11 * unless explicitly disclaimed in individual files.
13 * The authors hereby grant permission to use, copy, modify,
14 * distribute, and license this software and its documentation for any
15 * purpose, provided that existing copyright notices are retained in
16 * all copies and that this notice is included verbatim in any
17 * distributions. No written agreement, license, or royalty fee is
18 * required for any of the authorized uses. Modifications to this
19 * software may be copyrighted by their authors and need not follow
20 * the licensing terms described here, provided that the new terms are
21 * clearly indicated on the first page of each file where they apply.
23 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
24 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
25 * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
26 * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
30 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
32 * NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
33 * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
34 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
36 * GOVERNMENT USE: If you are acquiring this software on behalf of the
37 * U.S. government, the Government shall have only "Restricted Rights"
38 * in the software and related documentation as defined in the Federal
39 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you
40 * are acquiring the software on behalf of the Department of Defense,
41 * the software shall be classified as "Commercial Computer Software"
42 * and the Government shall have only "Restricted Rights" as defined
43 * in Clause 252.227-7013 (c) (1) of DFARs. Notwithstanding the
44 * foregoing, the authors grant the U.S. Government and others acting
45 * in its behalf permission to use and distribute the software in
46 * accordance with the terms specified in this license.
50 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
53 #include "gtktextbtree.h"
55 static void gtk_text_mark_init (GtkTextMark *mark);
56 static void gtk_text_mark_class_init (GtkTextMarkClass *klass);
57 static void gtk_text_mark_finalize (GObject *obj);
60 static gpointer parent_class = NULL;
63 gtk_text_mark_get_type (void)
65 static GType object_type = 0;
69 static const GTypeInfo object_info =
71 sizeof (GtkTextMarkClass),
73 (GBaseFinalizeFunc) NULL,
74 (GClassInitFunc) gtk_text_mark_class_init,
75 NULL, /* class_finalize */
76 NULL, /* class_data */
79 (GInstanceInitFunc) gtk_text_mark_init,
82 object_type = g_type_register_static (G_TYPE_OBJECT, "GtkTextMark",
90 gtk_text_mark_init (GtkTextMark *mark)
96 gtk_text_mark_class_init (GtkTextMarkClass *klass)
98 GObjectClass *object_class = G_OBJECT_CLASS (klass);
100 parent_class = g_type_class_peek_parent (klass);
102 object_class->finalize = gtk_text_mark_finalize;
106 gtk_text_mark_finalize (GObject *obj)
109 GtkTextLineSegment *seg;
111 mark = GTK_TEXT_MARK (obj);
117 if (seg->body.mark.tree != NULL)
118 g_warning ("GtkTextMark being finalized while still in the buffer; "
119 "someone removed a reference they didn't own! Crash "
122 g_free (seg->body.mark.name);
125 mark->segment = NULL;
128 /* chain parent_class' handler */
129 G_OBJECT_CLASS (parent_class)->finalize (obj);
133 * gtk_text_mark_get_visible:
134 * @mark: a #GtkTextMark
136 * Returns %TRUE if the mark is visible (i.e. a cursor is displayed
139 * Return value: %TRUE if visible
142 gtk_text_mark_get_visible (GtkTextMark *mark)
144 GtkTextLineSegment *seg;
148 return seg->body.mark.visible;
152 * gtk_text_mark_get_name:
153 * @mark: a #GtkTextMark
155 * Returns the mark name; returns NULL for anonymous marks.
157 * Return value: mark name
160 gtk_text_mark_get_name (GtkTextMark *mark)
162 GtkTextLineSegment *seg;
166 return seg->body.mark.name;
170 * gtk_text_mark_get_deleted:
171 * @mark: a #GtkTextMark
173 * Returns %TRUE if the mark has been removed from its buffer
174 * with gtk_text_buffer_delete_mark(). Marks can't be used
177 * Return value: whether the mark is deleted
180 gtk_text_mark_get_deleted (GtkTextMark *mark)
182 GtkTextLineSegment *seg;
184 g_return_val_if_fail (GTK_IS_TEXT_MARK (mark), FALSE);
191 return seg->body.mark.tree == NULL;
195 * gtk_text_mark_get_buffer:
196 * @mark: a #GtkTextMark
198 * Gets the buffer this mark is located inside,
199 * or NULL if the mark is deleted.
201 * Return value: the mark's #GtkTextBuffer
204 gtk_text_mark_get_buffer (GtkTextMark *mark)
206 GtkTextLineSegment *seg;
208 g_return_val_if_fail (GTK_IS_TEXT_MARK (mark), NULL);
212 if (seg->body.mark.tree == NULL)
215 return _gtk_text_btree_get_buffer (seg->body.mark.tree);
219 * gtk_text_mark_get_left_gravity:
220 * @mark: a #GtkTextMark
222 * Determines whether the mark has left gravity.
224 * Return value: %TRUE if the mark has left gravity, %FALSE otherwise
227 gtk_text_mark_get_left_gravity (GtkTextMark *mark)
229 GtkTextLineSegment *seg;
231 g_return_val_if_fail (GTK_IS_TEXT_MARK (mark), FALSE);
235 return seg->type == >k_text_left_mark_type;
239 * Macro that determines the size of a mark segment:
242 #define MSEG_SIZE ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
243 + sizeof (GtkTextMarkBody)))
247 _gtk_mark_segment_new (GtkTextBTree *tree,
248 gboolean left_gravity,
251 GtkTextLineSegment *mark;
253 mark = (GtkTextLineSegment *) g_malloc0 (MSEG_SIZE);
254 mark->body.mark.name = g_strdup (name);
257 mark->type = >k_text_left_mark_type;
259 mark->type = >k_text_right_mark_type;
261 mark->byte_count = 0;
262 mark->char_count = 0;
264 mark->body.mark.obj = g_object_new (GTK_TYPE_TEXT_MARK, NULL);
265 mark->body.mark.obj->segment = mark;
267 mark->body.mark.tree = tree;
268 mark->body.mark.line = NULL;
271 mark->body.mark.visible = FALSE;
272 mark->body.mark.not_deleteable = FALSE;
277 static int mark_segment_delete_func (GtkTextLineSegment *segPtr,
280 static GtkTextLineSegment *mark_segment_cleanup_func (GtkTextLineSegment *segPtr,
282 static void mark_segment_check_func (GtkTextLineSegment *segPtr,
287 * The following structures declare the "mark" segment types.
288 * There are actually two types for marks, one with left gravity
289 * and one with right gravity. They are identical except for
290 * their gravity property.
293 GtkTextLineSegmentClass gtk_text_right_mark_type = {
295 FALSE, /* leftGravity */
296 NULL, /* splitFunc */
297 mark_segment_delete_func, /* deleteFunc */
298 mark_segment_cleanup_func, /* cleanupFunc */
299 NULL, /* lineChangeFunc */
300 mark_segment_check_func /* checkFunc */
303 GtkTextLineSegmentClass gtk_text_left_mark_type = {
305 TRUE, /* leftGravity */
306 NULL, /* splitFunc */
307 mark_segment_delete_func, /* deleteFunc */
308 mark_segment_cleanup_func, /* cleanupFunc */
309 NULL, /* lineChangeFunc */
310 mark_segment_check_func /* checkFunc */
314 *--------------------------------------------------------------
316 * mark_segment_delete_func --
318 * This procedure is invoked by the text B-tree code whenever
319 * a mark lies in a range of characters being deleted.
322 * Returns 1 to indicate that deletion has been rejected,
326 * Frees mark if tree is going away
328 *--------------------------------------------------------------
332 mark_segment_delete_func (GtkTextLineSegment *seg,
338 _gtk_text_btree_release_mark_segment (seg->body.mark.tree, seg);
346 *--------------------------------------------------------------
348 * mark_segment_cleanup_func --
350 * This procedure is invoked by the B-tree code whenever a
351 * mark segment is moved from one line to another.
357 * The line field of the segment gets updated.
359 *--------------------------------------------------------------
362 static GtkTextLineSegment *
363 mark_segment_cleanup_func (GtkTextLineSegment *seg,
366 /* not sure why Tk did this here and not in LineChangeFunc */
367 seg->body.mark.line = line;
372 *--------------------------------------------------------------
374 * mark_segment_check_func --
376 * This procedure is invoked by the B-tree code to perform
377 * consistency checks on mark segments.
383 * The procedure panics if it detects anything wrong with
386 *--------------------------------------------------------------
390 mark_segment_check_func (GtkTextLineSegment *seg,
393 if (seg->body.mark.line != line)
394 g_error ("mark_segment_check_func: seg->body.mark.line bogus");