]> Pileus Git - ~andy/gtk/blob - gtk/gtktextmark.c
[annotations] Add allow-none
[~andy/gtk] / gtk / gtktextmark.c
1 /* gtktextmark.c - mark segments
2  *
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>
7  *
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.
12  *
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.
22  *
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.
28  *
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.
35  *
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.
47  *
48  */
49
50 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
51 #include "config.h"
52 #include "gtktextbtree.h"
53 #include "gtkprivate.h"
54 #include "gtkintl.h"
55 #include "gtkalias.h"
56
57 static void gtk_text_mark_set_property (GObject         *object,
58                                         guint            prop_id,
59                                         const GValue    *value,
60                                         GParamSpec      *pspec);
61 static void gtk_text_mark_get_property (GObject         *object,
62                                         guint            prop_id,
63                                         GValue          *value,
64                                         GParamSpec      *pspec);
65 static void gtk_text_mark_finalize     (GObject         *object);
66
67 static GtkTextLineSegment *gtk_mark_segment_new (GtkTextMark *mark_obj);
68
69 G_DEFINE_TYPE (GtkTextMark, gtk_text_mark, G_TYPE_OBJECT)
70
71 enum {
72   PROP_0,
73   PROP_NAME,
74   PROP_LEFT_GRAVITY
75 };
76
77 static void
78 gtk_text_mark_class_init (GtkTextMarkClass *klass)
79 {
80   GObjectClass *object_class = G_OBJECT_CLASS (klass);
81
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;
85
86   g_object_class_install_property (object_class,
87                                    PROP_NAME,
88                                    g_param_spec_string ("name",
89                                                         P_("Name"),
90                                                         P_("Mark name"),
91                                                         NULL,
92                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
93
94   g_object_class_install_property (object_class,
95                                    PROP_LEFT_GRAVITY,
96                                    g_param_spec_boolean ("left-gravity",
97                                                          P_("Left gravity"),
98                                                          P_("Whether the mark has left gravity"),
99                                                          FALSE,
100                                                          GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
101 }
102
103 static void
104 gtk_text_mark_init (GtkTextMark *mark)
105 {
106   mark->segment = gtk_mark_segment_new (mark);
107 }
108
109 static void
110 gtk_text_mark_finalize (GObject *obj)
111 {
112   GtkTextMark *mark;
113   GtkTextLineSegment *seg;
114
115   mark = GTK_TEXT_MARK (obj);
116
117   seg = mark->segment;
118
119   if (seg)
120     {
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 "
124                    "impending");
125
126       g_free (seg->body.mark.name);
127       g_free (seg);
128
129       mark->segment = NULL;
130     }
131
132   /* chain parent_class' handler */
133   G_OBJECT_CLASS (gtk_text_mark_parent_class)->finalize (obj);
134 }
135
136 static void
137 gtk_text_mark_set_property (GObject      *object,
138                             guint         prop_id,
139                             const GValue *value,
140                             GParamSpec   *pspec)
141 {
142   gchar *tmp;
143   GtkTextMark *mark = GTK_TEXT_MARK (object);
144   GtkTextLineSegment *seg = mark->segment;
145
146   switch (prop_id)
147     {
148     case PROP_NAME:
149       tmp = seg->body.mark.name;
150       seg->body.mark.name = g_value_dup_string (value);
151       g_free (tmp);
152       break;
153
154     case PROP_LEFT_GRAVITY:
155       if (g_value_get_boolean (value))
156         seg->type = &gtk_text_left_mark_type;
157       else
158         seg->type = &gtk_text_right_mark_type;
159       break;
160
161     default:
162       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163     }
164 }
165
166 static void
167 gtk_text_mark_get_property (GObject    *object,
168                             guint       prop_id,
169                             GValue     *value,
170                             GParamSpec *pspec)
171 {
172   GtkTextMark *mark = GTK_TEXT_MARK (object);
173
174   switch (prop_id)
175     {
176     case PROP_NAME:
177       g_value_set_string (value, gtk_text_mark_get_name (mark));
178       break;
179
180     case PROP_LEFT_GRAVITY:
181       g_value_set_boolean (value, gtk_text_mark_get_left_gravity (mark));
182       break;
183
184     default:
185       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186     }
187 }
188
189 /**
190  * gtk_text_mark_new:
191  * @name: (allow-none): mark name or %NULL
192  * @left_gravity: whether the mark should have left gravity
193  *
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).
203  *
204  * Return value: new #GtkTextMark
205  *
206  * Since: 2.12
207  **/
208 GtkTextMark *
209 gtk_text_mark_new (const gchar *name,
210                    gboolean     left_gravity)
211 {
212   return g_object_new (GTK_TYPE_TEXT_MARK,
213                        "name", name,
214                        "left-gravity", left_gravity,
215                        NULL);
216 }
217
218 /**
219  * gtk_text_mark_get_visible:
220  * @mark: a #GtkTextMark
221  * 
222  * Returns %TRUE if the mark is visible (i.e. a cursor is displayed
223  * for it).
224  * 
225  * Return value: %TRUE if visible
226  **/
227 gboolean
228 gtk_text_mark_get_visible (GtkTextMark *mark)
229 {
230   GtkTextLineSegment *seg;
231
232   seg = mark->segment;
233
234   return seg->body.mark.visible;
235 }
236
237 /**
238  * gtk_text_mark_get_name:
239  * @mark: a #GtkTextMark
240  * 
241  * Returns the mark name; returns NULL for anonymous marks.
242  * 
243  * Return value: mark name
244  **/
245 const char *
246 gtk_text_mark_get_name (GtkTextMark *mark)
247 {
248   GtkTextLineSegment *seg;
249
250   seg = mark->segment;
251
252   return seg->body.mark.name;
253 }
254
255 /**
256  * gtk_text_mark_get_deleted:
257  * @mark: a #GtkTextMark
258  * 
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.
262  * 
263  * Return value: whether the mark is deleted
264  **/
265 gboolean
266 gtk_text_mark_get_deleted (GtkTextMark *mark)
267 {
268   GtkTextLineSegment *seg;
269
270   g_return_val_if_fail (GTK_IS_TEXT_MARK (mark), FALSE);
271
272   seg = mark->segment;
273
274   if (seg == NULL)
275     return TRUE;
276
277   return seg->body.mark.tree == NULL;
278 }
279
280 /**
281  * gtk_text_mark_get_buffer:
282  * @mark: a #GtkTextMark
283  * 
284  * Gets the buffer this mark is located inside,
285  * or %NULL if the mark is deleted.
286  *
287  * Return value: (transfer none): the mark's #GtkTextBuffer
288  **/
289 GtkTextBuffer*
290 gtk_text_mark_get_buffer (GtkTextMark *mark)
291 {
292   GtkTextLineSegment *seg;
293
294   g_return_val_if_fail (GTK_IS_TEXT_MARK (mark), NULL);
295
296   seg = mark->segment;
297
298   if (seg->body.mark.tree == NULL)
299     return NULL;
300   else
301     return _gtk_text_btree_get_buffer (seg->body.mark.tree);
302 }
303
304 /**
305  * gtk_text_mark_get_left_gravity:
306  * @mark: a #GtkTextMark
307  * 
308  * Determines whether the mark has left gravity.
309  * 
310  * Return value: %TRUE if the mark has left gravity, %FALSE otherwise
311  **/
312 gboolean
313 gtk_text_mark_get_left_gravity (GtkTextMark *mark)
314 {
315   GtkTextLineSegment *seg;
316
317   g_return_val_if_fail (GTK_IS_TEXT_MARK (mark), FALSE);
318   
319   seg = mark->segment;
320
321   return seg->type == &gtk_text_left_mark_type;
322 }
323
324 /*
325  * Macro that determines the size of a mark segment:
326  */
327
328 #define MSEG_SIZE ((unsigned) (G_STRUCT_OFFSET (GtkTextLineSegment, body) \
329         + sizeof (GtkTextMarkBody)))
330
331
332 static GtkTextLineSegment *
333 gtk_mark_segment_new (GtkTextMark *mark_obj)
334 {
335   GtkTextLineSegment *mark;
336
337   mark = (GtkTextLineSegment *) g_malloc0 (MSEG_SIZE);
338   mark->body.mark.name = NULL;
339   mark->type = &gtk_text_right_mark_type;
340
341   mark->byte_count = 0;
342   mark->char_count = 0;
343
344   mark->body.mark.obj = mark_obj;
345   mark_obj->segment = mark;
346
347   mark->body.mark.tree = NULL;
348   mark->body.mark.line = NULL;
349   mark->next = NULL;
350
351   mark->body.mark.visible = FALSE;
352   mark->body.mark.not_deleteable = FALSE;
353
354   return mark;
355 }
356
357 void
358 _gtk_mark_segment_set_tree (GtkTextLineSegment *mark,
359                             GtkTextBTree       *tree)
360 {
361   g_assert (mark->body.mark.tree == NULL);
362   g_assert (mark->body.mark.obj != NULL);
363
364   mark->byte_count = 0;
365   mark->char_count = 0;
366
367   mark->body.mark.tree = tree;
368   mark->body.mark.line = NULL;
369   mark->next = NULL;
370
371   mark->body.mark.not_deleteable = FALSE;
372 }
373
374 static int                 mark_segment_delete_func  (GtkTextLineSegment *segPtr,
375                                                       GtkTextLine        *line,
376                                                       int                 treeGone);
377 static GtkTextLineSegment *mark_segment_cleanup_func (GtkTextLineSegment *segPtr,
378                                                       GtkTextLine        *line);
379 static void                mark_segment_check_func   (GtkTextLineSegment *segPtr,
380                                                       GtkTextLine        *line);
381
382
383 /*
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.
388  */
389
390 const GtkTextLineSegmentClass gtk_text_right_mark_type = {
391   "mark",                                               /* name */
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 */
398 };
399
400 const GtkTextLineSegmentClass gtk_text_left_mark_type = {
401   "mark",                                               /* name */
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 */
408 };
409
410 /*
411  *--------------------------------------------------------------
412  *
413  * mark_segment_delete_func --
414  *
415  *      This procedure is invoked by the text B-tree code whenever
416  *      a mark lies in a range of characters being deleted.
417  *
418  * Results:
419  *      Returns 1 to indicate that deletion has been rejected,
420  *      or 0 otherwise
421  *
422  * Side effects:
423  *      Frees mark if tree is going away
424  *
425  *--------------------------------------------------------------
426  */
427
428 static gboolean
429 mark_segment_delete_func (GtkTextLineSegment *seg,
430                           GtkTextLine        *line,
431                           gboolean            tree_gone)
432 {
433   if (tree_gone)
434     {
435       _gtk_text_btree_release_mark_segment (seg->body.mark.tree, seg);
436       return FALSE;
437     }
438   else
439     return TRUE;
440 }
441
442 /*
443  *--------------------------------------------------------------
444  *
445  * mark_segment_cleanup_func --
446  *
447  *      This procedure is invoked by the B-tree code whenever a
448  *      mark segment is moved from one line to another.
449  *
450  * Results:
451  *      None.
452  *
453  * Side effects:
454  *      The line field of the segment gets updated.
455  *
456  *--------------------------------------------------------------
457  */
458
459 static GtkTextLineSegment *
460 mark_segment_cleanup_func (GtkTextLineSegment *seg,
461                            GtkTextLine        *line)
462 {
463   /* not sure why Tk did this here and not in LineChangeFunc */
464   seg->body.mark.line = line;
465   return seg;
466 }
467
468 /*
469  *--------------------------------------------------------------
470  *
471  * mark_segment_check_func --
472  *
473  *      This procedure is invoked by the B-tree code to perform
474  *      consistency checks on mark segments.
475  *
476  * Results:
477  *      None.
478  *
479  * Side effects:
480  *      The procedure panics if it detects anything wrong with
481  *      the mark.
482  *
483  *--------------------------------------------------------------
484  */
485
486 static void
487 mark_segment_check_func (GtkTextLineSegment *seg,
488                          GtkTextLine        *line)
489 {
490   if (seg->body.mark.line != line)
491     g_error ("mark_segment_check_func: seg->body.mark.line bogus");
492 }
493
494 #define __GTK_TEXT_MARK_C__
495 #include "gtkaliasdef.c"