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