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