]> Pileus Git - ~andy/gtk/blob - gtk/gtkentrybuffer.c
Use G_CONST_RETURN for const return values
[~andy/gtk] / gtk / gtkentrybuffer.c
1 /* gtkentrybuffer.c
2  * Copyright (C) 2009  Stefan Walter <stef@memberwebs.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include "gtkentrybuffer.h"
23 #include "gtkintl.h"
24 #include "gtkmarshalers.h"
25 #include "gtkprivate.h"
26 #include "gtkwidget.h"
27 #include "gtkalias.h"
28
29 #include <gdk/gdk.h>
30
31 #include <string.h>
32
33 /**
34  * SECTION:gtkentrybuffer
35  * @title: GtkEntryBuffer
36  * @short_description: Text buffer for GtkEntry
37  *
38  * The #GtkEntryBuffer class contains the actual text displayed in a
39  * #GtkEntry widget.
40  *
41  * A single #GtkEntryBuffer object can be shared by multiple #GtkEntry
42  * widgets which will then share the same text content, but not the cursor
43  * position, visibility attributes, icon etc.
44  *
45  * #GtkEntryBuffer may be derived from. Such a derived class might allow
46  * text to be stored in an alternate location, such as non-pageable memory,
47  * useful in the case of important passwords. Or a derived class could 
48  * integrate with an application's concept of undo/redo.
49  *
50  * Since: 2.18
51  */
52
53 /* Initial size of buffer, in bytes */
54 #define MIN_SIZE 16
55
56 enum {
57   PROP_0,
58   PROP_TEXT,
59   PROP_LENGTH,
60   PROP_MAX_LENGTH,
61 };
62
63 enum {
64   INSERTED_TEXT,
65   DELETED_TEXT,
66   LAST_SIGNAL
67 };
68
69 static guint signals[LAST_SIGNAL] = { 0 };
70
71 struct _GtkEntryBufferPrivate
72 {
73   gint  max_length;
74
75   /* Only valid if this class is not derived */
76   gchar *normal_text;
77   gsize  normal_text_size;
78   gsize  normal_text_bytes;
79   guint  normal_text_chars;
80 };
81
82 G_DEFINE_TYPE (GtkEntryBuffer, gtk_entry_buffer, G_TYPE_OBJECT);
83
84 /* --------------------------------------------------------------------------------
85  * DEFAULT IMPLEMENTATIONS OF TEXT BUFFER
86  *
87  * These may be overridden by a derived class, behavior may be changed etc...
88  * The normal_text and normal_text_xxxx fields may not be valid when
89  * this class is derived from.
90  */
91
92 /* Overwrite a memory that might contain sensitive information. */
93 static void
94 trash_area (gchar *area,
95             gsize  len)
96 {
97   volatile gchar *varea = (volatile gchar *)area;
98   while (len-- > 0)
99     *varea++ = 0;
100 }
101
102 static const gchar*
103 gtk_entry_buffer_normal_get_text (GtkEntryBuffer *buffer,
104                                   gsize          *n_bytes)
105 {
106   if (n_bytes)
107     *n_bytes = buffer->priv->normal_text_bytes;
108   if (!buffer->priv->normal_text)
109       return "";
110   return buffer->priv->normal_text;
111 }
112
113 static guint
114 gtk_entry_buffer_normal_get_length (GtkEntryBuffer *buffer)
115 {
116   return buffer->priv->normal_text_chars;
117 }
118
119 static guint
120 gtk_entry_buffer_normal_insert_text (GtkEntryBuffer *buffer,
121                                      guint           position,
122                                      const gchar    *chars,
123                                      guint           n_chars)
124 {
125   GtkEntryBufferPrivate *pv = buffer->priv;
126   gsize prev_size;
127   gsize n_bytes;
128   gsize at;
129
130   n_bytes = g_utf8_offset_to_pointer (chars, n_chars) - chars;
131
132   /* Need more memory */
133   if (n_bytes + pv->normal_text_bytes + 1 > pv->normal_text_size)
134     {
135       gchar *et_new;
136
137       prev_size = pv->normal_text_size;
138
139       /* Calculate our new buffer size */
140       while (n_bytes + pv->normal_text_bytes + 1 > pv->normal_text_size)
141         {
142           if (pv->normal_text_size == 0)
143             pv->normal_text_size = MIN_SIZE;
144           else
145             {
146               if (2 * pv->normal_text_size < GTK_ENTRY_BUFFER_MAX_SIZE)
147                 pv->normal_text_size *= 2;
148               else
149                 {
150                   pv->normal_text_size = GTK_ENTRY_BUFFER_MAX_SIZE;
151                   if (n_bytes > pv->normal_text_size - pv->normal_text_bytes - 1)
152                     {
153                       n_bytes = pv->normal_text_size - pv->normal_text_bytes - 1;
154                       n_bytes = g_utf8_find_prev_char (chars, chars + n_bytes + 1) - chars;
155                       n_chars = g_utf8_strlen (chars, n_bytes);
156                     }
157                   break;
158                 }
159             }
160         }
161
162       /* Could be a password, so can't leave stuff in memory. */
163       et_new = g_malloc (pv->normal_text_size);
164       memcpy (et_new, pv->normal_text, MIN (prev_size, pv->normal_text_size));
165       trash_area (pv->normal_text, prev_size);
166       g_free (pv->normal_text);
167       pv->normal_text = et_new;
168     }
169
170   /* Actual text insertion */
171   at = g_utf8_offset_to_pointer (pv->normal_text, position) - pv->normal_text;
172   g_memmove (pv->normal_text + at + n_bytes, pv->normal_text + at, pv->normal_text_bytes - at);
173   memcpy (pv->normal_text + at, chars, n_bytes);
174
175   /* Book keeping */
176   pv->normal_text_bytes += n_bytes;
177   pv->normal_text_chars += n_chars;
178   pv->normal_text[pv->normal_text_bytes] = '\0';
179
180   gtk_entry_buffer_emit_inserted_text (buffer, position, chars, n_chars);
181   return n_chars;
182 }
183
184 static guint
185 gtk_entry_buffer_normal_delete_text (GtkEntryBuffer *buffer,
186                                      guint           position,
187                                      guint           n_chars)
188 {
189   GtkEntryBufferPrivate *pv = buffer->priv;
190   gsize start, end;
191
192   if (position > pv->normal_text_chars)
193     position = pv->normal_text_chars;
194   if (position + n_chars > pv->normal_text_chars)
195     n_chars = pv->normal_text_chars - position;
196
197   if (n_chars > 0)
198     {
199       start = g_utf8_offset_to_pointer (pv->normal_text, position) - pv->normal_text;
200       end = g_utf8_offset_to_pointer (pv->normal_text, position + n_chars) - pv->normal_text;
201
202       g_memmove (pv->normal_text + start, pv->normal_text + end, pv->normal_text_bytes + 1 - end);
203       pv->normal_text_chars -= n_chars;
204       pv->normal_text_bytes -= (end - start);
205
206       /*
207        * Could be a password, make sure we don't leave anything sensitive after
208        * the terminating zero.  Note, that the terminating zero already trashed
209        * one byte.
210        */
211       trash_area (pv->normal_text + pv->normal_text_bytes + 1, end - start - 1);
212
213       gtk_entry_buffer_emit_deleted_text (buffer, position, n_chars);
214     }
215
216   return n_chars;
217 }
218
219 /* --------------------------------------------------------------------------------
220  *
221  */
222
223 static void
224 gtk_entry_buffer_real_inserted_text (GtkEntryBuffer *buffer,
225                                      guint           position,
226                                      const gchar    *chars,
227                                      guint           n_chars)
228 {
229   g_object_notify (G_OBJECT (buffer), "text");
230   g_object_notify (G_OBJECT (buffer), "length");
231 }
232
233 static void
234 gtk_entry_buffer_real_deleted_text (GtkEntryBuffer *buffer,
235                                     guint           position,
236                                     guint           n_chars)
237 {
238   g_object_notify (G_OBJECT (buffer), "text");
239   g_object_notify (G_OBJECT (buffer), "length");
240 }
241
242 /* --------------------------------------------------------------------------------
243  *
244  */
245
246 static void
247 gtk_entry_buffer_init (GtkEntryBuffer *buffer)
248 {
249   GtkEntryBufferPrivate *pv;
250
251   pv = buffer->priv = G_TYPE_INSTANCE_GET_PRIVATE (buffer, GTK_TYPE_ENTRY_BUFFER, GtkEntryBufferPrivate);
252
253   pv->normal_text = NULL;
254   pv->normal_text_chars = 0;
255   pv->normal_text_bytes = 0;
256   pv->normal_text_size = 0;
257 }
258
259 static void
260 gtk_entry_buffer_finalize (GObject *obj)
261 {
262   GtkEntryBuffer *buffer = GTK_ENTRY_BUFFER (obj);
263   GtkEntryBufferPrivate *pv = buffer->priv;
264
265   if (pv->normal_text)
266     {
267       trash_area (pv->normal_text, pv->normal_text_size);
268       g_free (pv->normal_text);
269       pv->normal_text = NULL;
270       pv->normal_text_bytes = pv->normal_text_size = 0;
271       pv->normal_text_chars = 0;
272     }
273
274   G_OBJECT_CLASS (gtk_entry_buffer_parent_class)->finalize (obj);
275 }
276
277 static void
278 gtk_entry_buffer_set_property (GObject      *obj,
279                                guint         prop_id,
280                                const GValue *value,
281                                GParamSpec   *pspec)
282 {
283   GtkEntryBuffer *buffer = GTK_ENTRY_BUFFER (obj);
284
285   switch (prop_id)
286     {
287     case PROP_TEXT:
288       gtk_entry_buffer_set_text (buffer, g_value_get_string (value), -1);
289       break;
290     case PROP_MAX_LENGTH:
291       gtk_entry_buffer_set_max_length (buffer, g_value_get_int (value));
292       break;
293     default:
294       G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
295       break;
296     }
297 }
298
299 static void
300 gtk_entry_buffer_get_property (GObject    *obj,
301                                guint       prop_id,
302                                GValue     *value,
303                                GParamSpec *pspec)
304 {
305   GtkEntryBuffer *buffer = GTK_ENTRY_BUFFER (obj);
306
307   switch (prop_id)
308     {
309     case PROP_TEXT:
310       g_value_set_string (value, gtk_entry_buffer_get_text (buffer));
311       break;
312     case PROP_LENGTH:
313       g_value_set_uint (value, gtk_entry_buffer_get_length (buffer));
314       break;
315     case PROP_MAX_LENGTH:
316       g_value_set_int (value, gtk_entry_buffer_get_max_length (buffer));
317       break;
318     default:
319       G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
320       break;
321     }
322 }
323
324 static void
325 gtk_entry_buffer_class_init (GtkEntryBufferClass *klass)
326 {
327   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
328
329   gobject_class->finalize = gtk_entry_buffer_finalize;
330   gobject_class->set_property = gtk_entry_buffer_set_property;
331   gobject_class->get_property = gtk_entry_buffer_get_property;
332
333   klass->get_text = gtk_entry_buffer_normal_get_text;
334   klass->get_length = gtk_entry_buffer_normal_get_length;
335   klass->insert_text = gtk_entry_buffer_normal_insert_text;
336   klass->delete_text = gtk_entry_buffer_normal_delete_text;
337
338   klass->inserted_text = gtk_entry_buffer_real_inserted_text;
339   klass->deleted_text = gtk_entry_buffer_real_deleted_text;
340
341   g_type_class_add_private (gobject_class, sizeof (GtkEntryBufferPrivate));
342
343   /**
344    * GtkEntryBuffer:text:
345    *
346    * The contents of the buffer.
347    *
348    * Since: 2.18
349    */
350   g_object_class_install_property (gobject_class, PROP_TEXT,
351                      g_param_spec_string ("text", P_("Text"),
352                                           P_("The contents of the buffer"),
353                                           "", GTK_PARAM_READWRITE));
354
355   /**
356    * GtkEntryBuffer:length:
357    *
358    * The length (in characters) of the text in buffer.
359    *
360    * Since: 2.18
361    */
362   g_object_class_install_property (gobject_class, PROP_LENGTH,
363                      g_param_spec_uint ("length", P_("Text length"),
364                                         P_("Length of the text currently in the buffer"),
365                                         0, GTK_ENTRY_BUFFER_MAX_SIZE, 0, GTK_PARAM_READABLE));
366
367   /**
368    * GtkEntryBuffer:max-length:
369    *
370    * The maximum length (in characters) of the text in the buffer.
371    *
372    * Since: 2.18
373    */
374   g_object_class_install_property (gobject_class, PROP_MAX_LENGTH,
375                      g_param_spec_uint ("max-length", P_("Maximum length"),
376                                         P_("Maximum number of characters for this entry. Zero if no maximum"),
377                                         0, GTK_ENTRY_BUFFER_MAX_SIZE, 0, GTK_PARAM_READWRITE));
378
379   /**
380    * GtkEntry::inserted-text:
381    * @buffer: a #GtkEntryBuffer
382    * @position: the position the text was inserted at.
383    * @chars: The text that was inserted.
384    * @n_chars: The number of characters that were inserted.
385    *
386    * This signal is emitted after text is inserted into the buffer.
387    *
388    * Since: 2.18
389    */
390   signals[INSERTED_TEXT] = g_signal_new (I_("inserted-text"),
391                                          GTK_TYPE_ENTRY_BUFFER,
392                                          G_SIGNAL_RUN_FIRST,
393                                          G_STRUCT_OFFSET (GtkEntryBufferClass, inserted_text),
394                                          NULL, NULL,
395                                          _gtk_marshal_VOID__UINT_STRING_UINT,
396                                          G_TYPE_NONE, 3,
397                                          G_TYPE_UINT,
398                                          G_TYPE_STRING,
399                                          G_TYPE_UINT);
400
401   /**
402    * GtkEntry::deleted-text:
403    * @buffer: a #GtkEntryBuffer
404    * @position: the position the text was deleted at.
405    * @n_chars: The number of characters that were deleted.
406    *
407    * This signal is emitted after text is deleted from the buffer.
408    *
409    * Since: 2.18
410    */
411   signals[DELETED_TEXT] =  g_signal_new (I_("deleted-text"),
412                                          GTK_TYPE_ENTRY_BUFFER,
413                                          G_SIGNAL_RUN_FIRST,
414                                          G_STRUCT_OFFSET (GtkEntryBufferClass, deleted_text),
415                                          NULL, NULL,
416                                          _gtk_marshal_VOID__UINT_UINT,
417                                          G_TYPE_NONE, 2,
418                                          G_TYPE_UINT,
419                                          G_TYPE_UINT);
420 }
421
422 /* --------------------------------------------------------------------------------
423  *
424  */
425
426 /**
427  * gtk_entry_buffer_new:
428  * @initial_chars: initial buffer text, or %NULL
429  * @n_initial_chars: number of characters in @initial_chars, or -1
430  *
431  * Create a new GtkEntryBuffer object.
432  *
433  * Optionally, specify initial text to set in the buffer.
434  *
435  * Return value: A new GtkEntryBuffer object.
436  *
437  * Since: 2.18
438  **/
439 GtkEntryBuffer*
440 gtk_entry_buffer_new (const gchar *initial_chars,
441                       gint         n_initial_chars)
442 {
443   GtkEntryBuffer *buffer = g_object_new (GTK_TYPE_ENTRY_BUFFER, NULL);
444   if (initial_chars)
445     gtk_entry_buffer_set_text (buffer, initial_chars, n_initial_chars);
446   return buffer;
447 }
448
449 /**
450  * gtk_entry_buffer_get_length:
451  * @buffer: a #GtkEntryBuffer
452  *
453  * Retrieves the length in characters of the buffer.
454  *
455  * Return value: The number of characters in the buffer.
456  *
457  * Since: 2.18
458  **/
459 guint
460 gtk_entry_buffer_get_length (GtkEntryBuffer *buffer)
461 {
462   GtkEntryBufferClass *klass;
463
464   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
465
466   klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
467   g_return_val_if_fail (klass->get_length != NULL, 0);
468
469   return (*klass->get_length) (buffer);
470 }
471
472 /**
473  * gtk_entry_buffer_get_bytes:
474  * @buffer: a #GtkEntryBuffer
475  *
476  * Retrieves the length in bytes of the buffer.
477  * See gtk_entry_buffer_get_length().
478  *
479  * Return value: The byte length of the buffer.
480  *
481  * Since: 2.18
482  **/
483 gsize
484 gtk_entry_buffer_get_bytes (GtkEntryBuffer *buffer)
485 {
486   GtkEntryBufferClass *klass;
487   gsize bytes = 0;
488
489   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
490
491   klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
492   g_return_val_if_fail (klass->get_text != NULL, 0);
493
494   (*klass->get_text) (buffer, &bytes);
495   return bytes;
496 }
497
498 /**
499  * gtk_entry_buffer_get_text:
500  * @buffer: a #GtkEntryBuffer
501  *
502  * Retrieves the contents of the buffer.
503  *
504  * The memory pointer returned by this call will not change
505  * unless this object emits a signal, or is finalized.
506  *
507  * Return value: a pointer to the contents of the widget as a
508  *      string. This string points to internally allocated
509  *      storage in the buffer and must not be freed, modified or
510  *      stored.
511  *
512  * Since: 2.18
513  **/
514 G_CONST_RETURN gchar*
515 gtk_entry_buffer_get_text (GtkEntryBuffer *buffer)
516 {
517   GtkEntryBufferClass *klass;
518
519   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), NULL);
520
521   klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
522   g_return_val_if_fail (klass->get_text != NULL, NULL);
523
524   return (*klass->get_text) (buffer, NULL);
525 }
526
527 /**
528  * gtk_entry_buffer_set_text:
529  * @buffer: a #GtkEntryBuffer
530  * @chars: the new text
531  * @n_chars: the number of characters in @text, or -1
532  *
533  * Sets the text in the buffer.
534  *
535  * This is roughly equivalent to calling gtk_entry_buffer_delete_text()
536  * and gtk_entry_buffer_insert_text().
537  *
538  * Note that @n_chars is in characters, not in bytes.
539  *
540  * Since: 2.18
541  **/
542 void
543 gtk_entry_buffer_set_text (GtkEntryBuffer *buffer,
544                            const gchar    *chars,
545                            gint            n_chars)
546 {
547   g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
548   g_return_if_fail (chars != NULL);
549
550   g_object_freeze_notify (G_OBJECT (buffer));
551   gtk_entry_buffer_delete_text (buffer, 0, -1);
552   gtk_entry_buffer_insert_text (buffer, 0, chars, n_chars);
553   g_object_thaw_notify (G_OBJECT (buffer));
554 }
555
556 /**
557  * gtk_entry_buffer_set_max_length:
558  * @buffer: a #GtkEntryBuffer
559  * @max_length: the maximum length of the entry buffer, or 0 for no maximum.
560  *   (other than the maximum length of entries.) The value passed in will
561  *   be clamped to the range 0-65536.
562  *
563  * Sets the maximum allowed length of the contents of the buffer. If
564  * the current contents are longer than the given length, then they
565  * will be truncated to fit.
566  *
567  * Since: 2.18
568  **/
569 void
570 gtk_entry_buffer_set_max_length (GtkEntryBuffer *buffer,
571                                  gint            max_length)
572 {
573   g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
574
575   max_length = CLAMP (max_length, 0, GTK_ENTRY_BUFFER_MAX_SIZE);
576
577   if (max_length > 0 && gtk_entry_buffer_get_length (buffer) > max_length)
578     gtk_entry_buffer_delete_text (buffer, max_length, -1);
579
580   buffer->priv->max_length = max_length;
581   g_object_notify (G_OBJECT (buffer), "max-length");
582 }
583
584 /**
585  * gtk_entry_buffer_get_max_length:
586  * @buffer: a #GtkEntryBuffer
587  *
588  * Retrieves the maximum allowed length of the text in
589  * @buffer. See gtk_entry_buffer_set_max_length().
590  *
591  * Return value: the maximum allowed number of characters
592  *               in #GtkEntryBuffer, or 0 if there is no maximum.
593  *
594  * Since: 2.18
595  */
596 gint
597 gtk_entry_buffer_get_max_length (GtkEntryBuffer *buffer)
598 {
599   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
600   return buffer->priv->max_length;
601 }
602
603 /**
604  * gtk_entry_buffer_insert_text:
605  * @buffer: a #GtkEntryBuffer
606  * @position: the position at which to insert text.
607  * @chars: the text to insert into the buffer.
608  * @n_chars: the length of the text in characters, or -1
609  *
610  * Inserts @n_chars characters of @chars into the contents of the
611  * buffer, at position @position.
612  *
613  * If @n_chars is negative, then characters from chars will be inserted
614  * until a null-terminator is found. If @position or @n_chars are out of
615  * bounds, or the maximum buffer text length is exceeded, then they are
616  * coerced to sane values.
617  *
618  * Note that the position and length are in characters, not in bytes.
619  *
620  * Returns: The number of characters actually inserted.
621  *
622  * Since: 2.18
623  */
624 guint
625 gtk_entry_buffer_insert_text (GtkEntryBuffer *buffer,
626                               guint           position,
627                               const gchar    *chars,
628                               gint            n_chars)
629 {
630   GtkEntryBufferClass *klass;
631   GtkEntryBufferPrivate *pv;
632   guint length;
633
634   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
635
636   length = gtk_entry_buffer_get_length (buffer);
637   pv = buffer->priv;
638
639   if (n_chars < 0)
640     n_chars = g_utf8_strlen (chars, -1);
641
642   /* Bring position into bounds */
643   if (position > length)
644     position = length;
645
646   /* Make sure not entering too much data */
647   if (pv->max_length > 0)
648     {
649       if (length >= pv->max_length)
650         n_chars = 0;
651       else if (length + n_chars > pv->max_length)
652         n_chars -= (length + n_chars) - pv->max_length;
653     }
654
655   klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
656   g_return_val_if_fail (klass->insert_text != NULL, 0);
657
658   return (*klass->insert_text) (buffer, position, chars, n_chars);
659 }
660
661 /**
662  * gtk_entry_buffer_delete_text:
663  * @buffer: a #GtkEntryBuffer
664  * @position: position at which to delete text
665  * @n_chars: number of characters to delete
666  *
667  * Deletes a sequence of characters from the buffer. @n_chars characters are
668  * deleted starting at @position. If @n_chars is negative, then all characters
669  * until the end of the text are deleted.
670  *
671  * If @position or @n_chars are out of bounds, then they are coerced to sane
672  * values.
673  *
674  * Note that the positions are specified in characters, not bytes.
675  *
676  * Returns: The number of characters deleted.
677  *
678  * Since: 2.18
679  */
680 guint
681 gtk_entry_buffer_delete_text (GtkEntryBuffer *buffer,
682                               guint           position,
683                               gint            n_chars)
684 {
685   GtkEntryBufferClass *klass;
686   guint length;
687
688   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
689
690   length = gtk_entry_buffer_get_length (buffer);
691   if (n_chars < 0)
692     n_chars = length;
693   if (position > length)
694     position = length;
695   if (position + n_chars > length)
696     n_chars = length - position;
697
698   klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
699   g_return_val_if_fail (klass->delete_text != NULL, 0);
700
701   return (*klass->delete_text) (buffer, position, n_chars);
702 }
703
704 /**
705  * gtk_entry_buffer_emit_inserted_text:
706  * @buffer: a #GtkEntryBuffer
707  * @position: position at which text was inserted
708  * @chars: text that was inserted
709  * @n_chars: number of characters inserted
710  *
711  * Used when subclassing #GtkEntryBuffer
712  *
713  * Since: 2.18
714  */
715 void
716 gtk_entry_buffer_emit_inserted_text (GtkEntryBuffer *buffer,
717                                      guint           position,
718                                      const gchar    *chars,
719                                      guint           n_chars)
720 {
721   g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
722   g_signal_emit (buffer, signals[INSERTED_TEXT], 0, position, chars, n_chars);
723 }
724
725 /**
726  * gtk_entry_buffer_emit_deleted_text:
727  * @buffer: a #GtkEntryBuffer
728  * @position: position at which text was deleted
729  * @n_chars: number of characters deleted
730  *
731  * Used when subclassing #GtkEntryBuffer
732  *
733  * Since: 2.18
734  */
735 void
736 gtk_entry_buffer_emit_deleted_text (GtkEntryBuffer *buffer,
737                                     guint           position,
738                                     guint           n_chars)
739 {
740   g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
741   g_signal_emit (buffer, signals[DELETED_TEXT], 0, position, n_chars);
742 }
743
744 #define __GTK_ENTRY_BUFFER_C__
745 #include "gtkaliasdef.c"