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
52 #include "gtktextbtree.h"
53 #include "gtkprivate.h"
57 static void gtk_text_mark_set_property (GObject *object,
61 static void gtk_text_mark_get_property (GObject *object,
65 static void gtk_text_mark_finalize (GObject *object);
67 static GtkTextLineSegment *gtk_mark_segment_new (GtkTextMark *mark_obj);
69 G_DEFINE_TYPE (GtkTextMark, gtk_text_mark, G_TYPE_OBJECT)
78 gtk_text_mark_class_init (GtkTextMarkClass *klass)
80 GObjectClass *object_class = G_OBJECT_CLASS (klass);
82 object_class->finalize = gtk_text_mark_finalize;
83 object_class->set_property = gtk_text_mark_set_property;
84 object_class->get_property = gtk_text_mark_get_property;
86 g_object_class_install_property (object_class,
88 g_param_spec_string ("name",
92 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
94 g_object_class_install_property (object_class,
96 g_param_spec_boolean ("left-gravity",
98 P_("Whether the mark has left gravity"),
100 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
104 gtk_text_mark_init (GtkTextMark *mark)
106 mark->segment = gtk_mark_segment_new (mark);
110 gtk_text_mark_finalize (GObject *obj)
113 GtkTextLineSegment *seg;
115 mark = GTK_TEXT_MARK (obj);
121 if (seg->body.mark.tree != NULL)
122 g_warning ("GtkTextMark being finalized while still in the buffer; "
123 "someone removed a reference they didn't own! Crash "
126 g_free (seg->body.mark.name);
129 mark->segment = NULL;
132 /* chain parent_class' handler */
133 G_OBJECT_CLASS (gtk_text_mark_parent_class)->finalize (obj);
137 gtk_text_mark_set_property (GObject *object,
143 GtkTextMark *mark = GTK_TEXT_MARK (object);
144 GtkTextLineSegment *seg = mark->segment;
149 tmp = seg->body.mark.name;
150 seg->body.mark.name = g_value_dup_string (value);
154 case PROP_LEFT_GRAVITY:
155 if (g_value_get_boolean (value))
156 seg->type = >k_text_left_mark_type;
158 seg->type = >k_text_right_mark_type;
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167 gtk_text_mark_get_property (GObject *object,
172 GtkTextMark *mark = GTK_TEXT_MARK (object);
177 g_value_set_string (value, gtk_text_mark_get_name (mark));
180 case PROP_LEFT_GRAVITY:
181 g_value_set_boolean (value, gtk_text_mark_get_left_gravity (mark));
185 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191 * @name: mark name or %NULL
192 * @left_gravity: whether the mark should have left gravity
194 * Creates a text mark. Add it to a buffer using gtk_text_buffer_add_mark().
195 * If @name is %NULL, the mark is anonymous; otherwise, the mark can be
196 * retrieved by name using gtk_text_buffer_get_mark(). If a mark has left
197 * gravity, and text is inserted at the mark's current location, the mark
198 * will be moved to the left of the newly-inserted text. If the mark has
199 * right gravity (@left_gravity = %FALSE), the mark will end up on the
200 * right of newly-inserted text. The standard left-to-right cursor is a
201 * mark with right gravity (when you type, the cursor stays on the right
202 * side of the text you're typing).
204 * Return value: new #GtkTextMark
209 gtk_text_mark_new (const gchar *name,
210 gboolean left_gravity)
212 return g_object_new (GTK_TYPE_TEXT_MARK,
214 "left-gravity", left_gravity,
219 * gtk_text_mark_get_visible:
220 * @mark: a #GtkTextMark
222 * Returns %TRUE if the mark is visible (i.e. a cursor is displayed
225 * Return value: %TRUE if visible
228 gtk_text_mark_get_visible (GtkTextMark *mark)
230 GtkTextLineSegment *seg;
234 return seg->body.mark.visible;
238 * gtk_text_mark_get_name:
239 * @mark: a #GtkTextMark
241 * Returns the mark name; returns NULL for anonymous marks.
243 * Return value: mark name
246 gtk_text_mark_get_name (GtkTextMark *mark)
248 GtkTextLineSegment *seg;
252 return seg->body.mark.name;
256 * gtk_text_mark_get_deleted:
257 * @mark: a #GtkTextMark
259 * Returns %TRUE if the mark has been removed from its buffer
260 * with gtk_text_buffer_delete_mark(). See gtk_text_buffer_add_mark()
261 * for a way to add it to a buffer again.
263 * Return value: whether the mark is deleted
266 gtk_text_mark_get_deleted (GtkTextMark *mark)
268 GtkTextLineSegment *seg;
270 g_return_val_if_fail (GTK_IS_TEXT_MARK (mark), FALSE);
277 return seg->body.mark.tree == NULL;
281 * gtk_text_mark_get_buffer:
282 * @mark: a #GtkTextMark
284 * Gets the buffer this mark is located inside,
285 * or NULL if the mark is deleted.
287 * Return value: the mark's #GtkTextBuffer
290 gtk_text_mark_get_buffer (GtkTextMark *mark)
292 GtkTextLineSegment *seg;
294 g_return_val_if_fail (GTK_IS_TEXT_MARK (mark), NULL);
298 if (seg->body.mark.tree == NULL)
301 return _gtk_text_btree_get_buffer (seg->body.mark.tree);
305 * gtk_text_mark_get_left_gravity:
306 * @mark: a #GtkTextMark
308 * Determines whether the mark has left gravity.
310 * Return value: %TRUE if the mark has left gravity, %FALSE otherwise
313 gtk_text_mark_get_left_gravity (GtkTextMark *mark)
315 GtkTextLineSegment *seg;
317 g_return_val_if_fail (GTK_IS_TEXT_MARK (mark), FALSE);
321 return seg->type == >k_text_left_mark_type;
325 * Macro that determines the size of a mark segment:
328 #define MSEG_SIZE ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
329 + sizeof (GtkTextMarkBody)))
332 static GtkTextLineSegment *
333 gtk_mark_segment_new (GtkTextMark *mark_obj)
335 GtkTextLineSegment *mark;
337 mark = (GtkTextLineSegment *) g_malloc0 (MSEG_SIZE);
338 mark->body.mark.name = NULL;
339 mark->type = >k_text_right_mark_type;
341 mark->byte_count = 0;
342 mark->char_count = 0;
344 mark->body.mark.obj = mark_obj;
345 mark_obj->segment = mark;
347 mark->body.mark.tree = NULL;
348 mark->body.mark.line = NULL;
351 mark->body.mark.visible = FALSE;
352 mark->body.mark.not_deleteable = FALSE;
358 _gtk_mark_segment_set_tree (GtkTextLineSegment *mark,
361 g_assert (mark->body.mark.tree == NULL);
362 g_assert (mark->body.mark.obj != NULL);
364 mark->byte_count = 0;
365 mark->char_count = 0;
367 mark->body.mark.tree = tree;
368 mark->body.mark.line = NULL;
371 mark->body.mark.not_deleteable = FALSE;
374 static int mark_segment_delete_func (GtkTextLineSegment *segPtr,
377 static GtkTextLineSegment *mark_segment_cleanup_func (GtkTextLineSegment *segPtr,
379 static void mark_segment_check_func (GtkTextLineSegment *segPtr,
384 * The following structures declare the "mark" segment types.
385 * There are actually two types for marks, one with left gravity
386 * and one with right gravity. They are identical except for
387 * their gravity property.
390 const GtkTextLineSegmentClass gtk_text_right_mark_type = {
392 FALSE, /* leftGravity */
393 NULL, /* splitFunc */
394 mark_segment_delete_func, /* deleteFunc */
395 mark_segment_cleanup_func, /* cleanupFunc */
396 NULL, /* lineChangeFunc */
397 mark_segment_check_func /* checkFunc */
400 const GtkTextLineSegmentClass gtk_text_left_mark_type = {
402 TRUE, /* leftGravity */
403 NULL, /* splitFunc */
404 mark_segment_delete_func, /* deleteFunc */
405 mark_segment_cleanup_func, /* cleanupFunc */
406 NULL, /* lineChangeFunc */
407 mark_segment_check_func /* checkFunc */
411 *--------------------------------------------------------------
413 * mark_segment_delete_func --
415 * This procedure is invoked by the text B-tree code whenever
416 * a mark lies in a range of characters being deleted.
419 * Returns 1 to indicate that deletion has been rejected,
423 * Frees mark if tree is going away
425 *--------------------------------------------------------------
429 mark_segment_delete_func (GtkTextLineSegment *seg,
435 _gtk_text_btree_release_mark_segment (seg->body.mark.tree, seg);
443 *--------------------------------------------------------------
445 * mark_segment_cleanup_func --
447 * This procedure is invoked by the B-tree code whenever a
448 * mark segment is moved from one line to another.
454 * The line field of the segment gets updated.
456 *--------------------------------------------------------------
459 static GtkTextLineSegment *
460 mark_segment_cleanup_func (GtkTextLineSegment *seg,
463 /* not sure why Tk did this here and not in LineChangeFunc */
464 seg->body.mark.line = line;
469 *--------------------------------------------------------------
471 * mark_segment_check_func --
473 * This procedure is invoked by the B-tree code to perform
474 * consistency checks on mark segments.
480 * The procedure panics if it detects anything wrong with
483 *--------------------------------------------------------------
487 mark_segment_check_func (GtkTextLineSegment *seg,
490 if (seg->body.mark.line != line)
491 g_error ("mark_segment_check_func: seg->body.mark.line bogus");
494 #define __GTK_TEXT_MARK_C__
495 #include "gtkaliasdef.c"