]> Pileus Git - ~andy/gtk/blob - gtk/gtkentrybuffer.c
Merge branch 'master' into toolpalette
[~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,
351                                    PROP_TEXT,
352                                    g_param_spec_string ("text",
353                                                         P_("Text"),
354                                                         P_("The contents of the buffer"),
355                                                         "",
356                                                         GTK_PARAM_READWRITE));
357
358   /**
359    * GtkEntryBuffer:length:
360    *
361    * The length (in characters) of the text in buffer.
362    *
363    * Since: 2.18
364    */
365   g_object_class_install_property (gobject_class,
366                                    PROP_LENGTH,
367                                    g_param_spec_uint ("length",
368                                                       P_("Text length"),
369                                                       P_("Length of the text currently in the buffer"),
370                                                       0, GTK_ENTRY_BUFFER_MAX_SIZE, 0,
371                                                       GTK_PARAM_READABLE));
372
373   /**
374    * GtkEntryBuffer:max-length:
375    *
376    * The maximum length (in characters) of the text in the buffer.
377    *
378    * Since: 2.18
379    */
380   g_object_class_install_property (gobject_class,
381                                    PROP_MAX_LENGTH,
382                                    g_param_spec_int ("max-length",
383                                                      P_("Maximum length"),
384                                                      P_("Maximum number of characters for this entry. Zero if no maximum"),
385                                    0, GTK_ENTRY_BUFFER_MAX_SIZE, 0,
386                                    GTK_PARAM_READWRITE));
387
388   /**
389    * GtkEntry::inserted-text:
390    * @buffer: a #GtkEntryBuffer
391    * @position: the position the text was inserted at.
392    * @chars: The text that was inserted.
393    * @n_chars: The number of characters that were inserted.
394    *
395    * This signal is emitted after text is inserted into the buffer.
396    *
397    * Since: 2.18
398    */
399   signals[INSERTED_TEXT] = g_signal_new (I_("inserted-text"),
400                                          GTK_TYPE_ENTRY_BUFFER,
401                                          G_SIGNAL_RUN_FIRST,
402                                          G_STRUCT_OFFSET (GtkEntryBufferClass, inserted_text),
403                                          NULL, NULL,
404                                          _gtk_marshal_VOID__UINT_STRING_UINT,
405                                          G_TYPE_NONE, 3,
406                                          G_TYPE_UINT,
407                                          G_TYPE_STRING,
408                                          G_TYPE_UINT);
409
410   /**
411    * GtkEntry::deleted-text:
412    * @buffer: a #GtkEntryBuffer
413    * @position: the position the text was deleted at.
414    * @n_chars: The number of characters that were deleted.
415    *
416    * This signal is emitted after text is deleted from the buffer.
417    *
418    * Since: 2.18
419    */
420   signals[DELETED_TEXT] =  g_signal_new (I_("deleted-text"),
421                                          GTK_TYPE_ENTRY_BUFFER,
422                                          G_SIGNAL_RUN_FIRST,
423                                          G_STRUCT_OFFSET (GtkEntryBufferClass, deleted_text),
424                                          NULL, NULL,
425                                          _gtk_marshal_VOID__UINT_UINT,
426                                          G_TYPE_NONE, 2,
427                                          G_TYPE_UINT,
428                                          G_TYPE_UINT);
429 }
430
431 /* --------------------------------------------------------------------------------
432  *
433  */
434
435 /**
436  * gtk_entry_buffer_new:
437  * @initial_chars: initial buffer text, or %NULL
438  * @n_initial_chars: number of characters in @initial_chars, or -1
439  *
440  * Create a new GtkEntryBuffer object.
441  *
442  * Optionally, specify initial text to set in the buffer.
443  *
444  * Return value: A new GtkEntryBuffer object.
445  *
446  * Since: 2.18
447  **/
448 GtkEntryBuffer*
449 gtk_entry_buffer_new (const gchar *initial_chars,
450                       gint         n_initial_chars)
451 {
452   GtkEntryBuffer *buffer = g_object_new (GTK_TYPE_ENTRY_BUFFER, NULL);
453   if (initial_chars)
454     gtk_entry_buffer_set_text (buffer, initial_chars, n_initial_chars);
455   return buffer;
456 }
457
458 /**
459  * gtk_entry_buffer_get_length:
460  * @buffer: a #GtkEntryBuffer
461  *
462  * Retrieves the length in characters of the buffer.
463  *
464  * Return value: The number of characters in the buffer.
465  *
466  * Since: 2.18
467  **/
468 guint
469 gtk_entry_buffer_get_length (GtkEntryBuffer *buffer)
470 {
471   GtkEntryBufferClass *klass;
472
473   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
474
475   klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
476   g_return_val_if_fail (klass->get_length != NULL, 0);
477
478   return (*klass->get_length) (buffer);
479 }
480
481 /**
482  * gtk_entry_buffer_get_bytes:
483  * @buffer: a #GtkEntryBuffer
484  *
485  * Retrieves the length in bytes of the buffer.
486  * See gtk_entry_buffer_get_length().
487  *
488  * Return value: The byte length of the buffer.
489  *
490  * Since: 2.18
491  **/
492 gsize
493 gtk_entry_buffer_get_bytes (GtkEntryBuffer *buffer)
494 {
495   GtkEntryBufferClass *klass;
496   gsize bytes = 0;
497
498   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
499
500   klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
501   g_return_val_if_fail (klass->get_text != NULL, 0);
502
503   (*klass->get_text) (buffer, &bytes);
504   return bytes;
505 }
506
507 /**
508  * gtk_entry_buffer_get_text:
509  * @buffer: a #GtkEntryBuffer
510  *
511  * Retrieves the contents of the buffer.
512  *
513  * The memory pointer returned by this call will not change
514  * unless this object emits a signal, or is finalized.
515  *
516  * Return value: a pointer to the contents of the widget as a
517  *      string. This string points to internally allocated
518  *      storage in the buffer and must not be freed, modified or
519  *      stored.
520  *
521  * Since: 2.18
522  **/
523 G_CONST_RETURN gchar*
524 gtk_entry_buffer_get_text (GtkEntryBuffer *buffer)
525 {
526   GtkEntryBufferClass *klass;
527
528   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), NULL);
529
530   klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
531   g_return_val_if_fail (klass->get_text != NULL, NULL);
532
533   return (*klass->get_text) (buffer, NULL);
534 }
535
536 /**
537  * gtk_entry_buffer_set_text:
538  * @buffer: a #GtkEntryBuffer
539  * @chars: the new text
540  * @n_chars: the number of characters in @text, or -1
541  *
542  * Sets the text in the buffer.
543  *
544  * This is roughly equivalent to calling gtk_entry_buffer_delete_text()
545  * and gtk_entry_buffer_insert_text().
546  *
547  * Note that @n_chars is in characters, not in bytes.
548  *
549  * Since: 2.18
550  **/
551 void
552 gtk_entry_buffer_set_text (GtkEntryBuffer *buffer,
553                            const gchar    *chars,
554                            gint            n_chars)
555 {
556   g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
557   g_return_if_fail (chars != NULL);
558
559   g_object_freeze_notify (G_OBJECT (buffer));
560   gtk_entry_buffer_delete_text (buffer, 0, -1);
561   gtk_entry_buffer_insert_text (buffer, 0, chars, n_chars);
562   g_object_thaw_notify (G_OBJECT (buffer));
563 }
564
565 /**
566  * gtk_entry_buffer_set_max_length:
567  * @buffer: a #GtkEntryBuffer
568  * @max_length: the maximum length of the entry buffer, or 0 for no maximum.
569  *   (other than the maximum length of entries.) The value passed in will
570  *   be clamped to the range 0-65536.
571  *
572  * Sets the maximum allowed length of the contents of the buffer. If
573  * the current contents are longer than the given length, then they
574  * will be truncated to fit.
575  *
576  * Since: 2.18
577  **/
578 void
579 gtk_entry_buffer_set_max_length (GtkEntryBuffer *buffer,
580                                  gint            max_length)
581 {
582   g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
583
584   max_length = CLAMP (max_length, 0, GTK_ENTRY_BUFFER_MAX_SIZE);
585
586   if (max_length > 0 && gtk_entry_buffer_get_length (buffer) > max_length)
587     gtk_entry_buffer_delete_text (buffer, max_length, -1);
588
589   buffer->priv->max_length = max_length;
590   g_object_notify (G_OBJECT (buffer), "max-length");
591 }
592
593 /**
594  * gtk_entry_buffer_get_max_length:
595  * @buffer: a #GtkEntryBuffer
596  *
597  * Retrieves the maximum allowed length of the text in
598  * @buffer. See gtk_entry_buffer_set_max_length().
599  *
600  * Return value: the maximum allowed number of characters
601  *               in #GtkEntryBuffer, or 0 if there is no maximum.
602  *
603  * Since: 2.18
604  */
605 gint
606 gtk_entry_buffer_get_max_length (GtkEntryBuffer *buffer)
607 {
608   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
609   return buffer->priv->max_length;
610 }
611
612 /**
613  * gtk_entry_buffer_insert_text:
614  * @buffer: a #GtkEntryBuffer
615  * @position: the position at which to insert text.
616  * @chars: the text to insert into the buffer.
617  * @n_chars: the length of the text in characters, or -1
618  *
619  * Inserts @n_chars characters of @chars into the contents of the
620  * buffer, at position @position.
621  *
622  * If @n_chars is negative, then characters from chars will be inserted
623  * until a null-terminator is found. If @position or @n_chars are out of
624  * bounds, or the maximum buffer text length is exceeded, then they are
625  * coerced to sane values.
626  *
627  * Note that the position and length are in characters, not in bytes.
628  *
629  * Returns: The number of characters actually inserted.
630  *
631  * Since: 2.18
632  */
633 guint
634 gtk_entry_buffer_insert_text (GtkEntryBuffer *buffer,
635                               guint           position,
636                               const gchar    *chars,
637                               gint            n_chars)
638 {
639   GtkEntryBufferClass *klass;
640   GtkEntryBufferPrivate *pv;
641   guint length;
642
643   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
644
645   length = gtk_entry_buffer_get_length (buffer);
646   pv = buffer->priv;
647
648   if (n_chars < 0)
649     n_chars = g_utf8_strlen (chars, -1);
650
651   /* Bring position into bounds */
652   if (position > length)
653     position = length;
654
655   /* Make sure not entering too much data */
656   if (pv->max_length > 0)
657     {
658       if (length >= pv->max_length)
659         n_chars = 0;
660       else if (length + n_chars > pv->max_length)
661         n_chars -= (length + n_chars) - pv->max_length;
662     }
663
664   klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
665   g_return_val_if_fail (klass->insert_text != NULL, 0);
666
667   return (*klass->insert_text) (buffer, position, chars, n_chars);
668 }
669
670 /**
671  * gtk_entry_buffer_delete_text:
672  * @buffer: a #GtkEntryBuffer
673  * @position: position at which to delete text
674  * @n_chars: number of characters to delete
675  *
676  * Deletes a sequence of characters from the buffer. @n_chars characters are
677  * deleted starting at @position. If @n_chars is negative, then all characters
678  * until the end of the text are deleted.
679  *
680  * If @position or @n_chars are out of bounds, then they are coerced to sane
681  * values.
682  *
683  * Note that the positions are specified in characters, not bytes.
684  *
685  * Returns: The number of characters deleted.
686  *
687  * Since: 2.18
688  */
689 guint
690 gtk_entry_buffer_delete_text (GtkEntryBuffer *buffer,
691                               guint           position,
692                               gint            n_chars)
693 {
694   GtkEntryBufferClass *klass;
695   guint length;
696
697   g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
698
699   length = gtk_entry_buffer_get_length (buffer);
700   if (n_chars < 0)
701     n_chars = length;
702   if (position > length)
703     position = length;
704   if (position + n_chars > length)
705     n_chars = length - position;
706
707   klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
708   g_return_val_if_fail (klass->delete_text != NULL, 0);
709
710   return (*klass->delete_text) (buffer, position, n_chars);
711 }
712
713 /**
714  * gtk_entry_buffer_emit_inserted_text:
715  * @buffer: a #GtkEntryBuffer
716  * @position: position at which text was inserted
717  * @chars: text that was inserted
718  * @n_chars: number of characters inserted
719  *
720  * Used when subclassing #GtkEntryBuffer
721  *
722  * Since: 2.18
723  */
724 void
725 gtk_entry_buffer_emit_inserted_text (GtkEntryBuffer *buffer,
726                                      guint           position,
727                                      const gchar    *chars,
728                                      guint           n_chars)
729 {
730   g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
731   g_signal_emit (buffer, signals[INSERTED_TEXT], 0, position, chars, n_chars);
732 }
733
734 /**
735  * gtk_entry_buffer_emit_deleted_text:
736  * @buffer: a #GtkEntryBuffer
737  * @position: position at which text was deleted
738  * @n_chars: number of characters deleted
739  *
740  * Used when subclassing #GtkEntryBuffer
741  *
742  * Since: 2.18
743  */
744 void
745 gtk_entry_buffer_emit_deleted_text (GtkEntryBuffer *buffer,
746                                     guint           position,
747                                     guint           n_chars)
748 {
749   g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
750   g_signal_emit (buffer, signals[DELETED_TEXT], 0, position, n_chars);
751 }
752
753 #define __GTK_ENTRY_BUFFER_C__
754 #include "gtkaliasdef.c"