]> Pileus Git - ~andy/gtk/blob - gtk/gtkeditable.c
Bug 559404 – gtk_editable_insert_text counts length in bytes
[~andy/gtk] / gtk / gtkeditable.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser 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 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28 #include <string.h>
29
30 #include "gtkeditable.h"
31 #include "gtkmarshalers.h"
32 #include "gtkintl.h"
33 #include "gtkalias.h"
34
35
36 static void gtk_editable_base_init (gpointer g_class);
37
38
39 GType
40 gtk_editable_get_type (void)
41 {
42   static GType editable_type = 0;
43
44   if (!editable_type)
45     {
46       const GTypeInfo editable_info =
47       {
48         sizeof (GtkEditableClass),  /* class_size */
49         gtk_editable_base_init,     /* base_init */
50         NULL,                       /* base_finalize */
51       };
52
53       editable_type = g_type_register_static (G_TYPE_INTERFACE, I_("GtkEditable"),
54                                               &editable_info, 0);
55     }
56
57   return editable_type;
58 }
59
60 static void
61 gtk_editable_base_init (gpointer g_class)
62 {
63   static gboolean initialized = FALSE;
64
65   if (! initialized)
66     {
67       g_signal_new (I_("insert-text"),
68                     GTK_TYPE_EDITABLE,
69                     G_SIGNAL_RUN_LAST,
70                     G_STRUCT_OFFSET (GtkEditableClass, insert_text),
71                     NULL, NULL,
72                     _gtk_marshal_VOID__STRING_INT_POINTER,
73                     G_TYPE_NONE, 3,
74                     G_TYPE_STRING,
75                     G_TYPE_INT,
76                     G_TYPE_POINTER);
77       g_signal_new (I_("delete-text"),
78                     GTK_TYPE_EDITABLE,
79                     G_SIGNAL_RUN_LAST,
80                     G_STRUCT_OFFSET (GtkEditableClass, delete_text),
81                     NULL, NULL,
82                     _gtk_marshal_VOID__INT_INT,
83                     G_TYPE_NONE, 2,
84                     G_TYPE_INT,
85                     G_TYPE_INT);
86       g_signal_new (I_("changed"),
87                     GTK_TYPE_EDITABLE,
88                     G_SIGNAL_RUN_LAST,
89                     G_STRUCT_OFFSET (GtkEditableClass, changed),
90                     NULL, NULL,
91                     _gtk_marshal_VOID__VOID,
92                     G_TYPE_NONE, 0);
93
94       initialized = TRUE;
95     }
96 }
97
98 /**
99  * gtk_editable_insert_text:
100  * @editable: a #GtkEditable
101  * @new_text: the text to append
102  * @new_text_length: the length of the text in bytes, or -1
103  * @position: position text will be inserted at
104  *
105  * Appends @new_text_length bytes of @new_text to the contents of the widget,
106  * at position @position. Note that this position is in characters, not in bytes.
107  **/
108 void
109 gtk_editable_insert_text (GtkEditable *editable,
110                           const gchar *new_text,
111                           gint         new_text_length,
112                           gint        *position)
113 {
114   g_return_if_fail (GTK_IS_EDITABLE (editable));
115   g_return_if_fail (position != NULL);
116
117   if (new_text_length < 0)
118     new_text_length = strlen (new_text);
119   
120   GTK_EDITABLE_GET_CLASS (editable)->do_insert_text (editable, new_text, new_text_length, position);
121 }
122
123 /**
124  * gtk_editable_delete_text:
125  * @editable: a #GtkEditable
126  * @start_pos: start position
127  * @end_pos: end position
128  *
129  * Deletes the content of the editable between @start_pos and @end_pos.
130  * Note that positions are specified in characters, not bytes.
131  **/
132 void
133 gtk_editable_delete_text (GtkEditable *editable,
134                           gint         start_pos,
135                           gint         end_pos)
136 {
137   g_return_if_fail (GTK_IS_EDITABLE (editable));
138
139   GTK_EDITABLE_GET_CLASS (editable)->do_delete_text (editable, start_pos, end_pos);
140 }
141
142 /**
143  * gtk_editable_get_chars:
144  * @editable: a #GtkEditable
145  * @start: start of text
146  * @end: end of text
147  *
148  * Retreives the content of the editable between @start and @end.
149  * Note that positions are specified in characters, not bytes.
150  *
151  * Return value: a pointer to the contents of the widget as a
152  *      string. This string is allocated by the #GtkEditable
153  *      implementation and should be freed by the caller.
154  **/
155 gchar *    
156 gtk_editable_get_chars (GtkEditable *editable,
157                         gint         start,
158                         gint         end)
159 {
160   g_return_val_if_fail (GTK_IS_EDITABLE (editable), NULL);
161
162   return GTK_EDITABLE_GET_CLASS (editable)->get_chars (editable, start, end);
163 }
164
165 /**
166  * gtk_editable_set_position:
167  * @editable: a #GtkEditable
168  * @position:  the position of the cursor. The cursor is displayed
169  *    before the character with the given (base 0) index in the editable. 
170  *    The value must be less than or equal to the number of characters 
171  *    in the editable. A value of -1 indicates that the position should
172  *    be set after the last character of the editable. Note that this 
173  *    position is in characters, not in bytes.
174  *
175  * Sets the cursor position in the editable to the given value.
176  **/
177 void
178 gtk_editable_set_position (GtkEditable      *editable,
179                            gint              position)
180 {
181   g_return_if_fail (GTK_IS_EDITABLE (editable));
182
183   GTK_EDITABLE_GET_CLASS (editable)->set_position (editable, position);
184 }
185
186 /**
187  * gtk_editable_get_position:
188  * @editable: a #GtkEditable
189  *
190  * Retrieves the current position of the cursor relative to the start
191  * of the content of the editable. Note that this position is in characters,
192  * not in bytes.
193  *
194  * Return value: the cursor position
195  **/
196 gint
197 gtk_editable_get_position (GtkEditable *editable)
198 {
199   g_return_val_if_fail (GTK_IS_EDITABLE (editable), 0);
200
201   return GTK_EDITABLE_GET_CLASS (editable)->get_position (editable);
202 }
203
204 /**
205  * gtk_editable_get_selection_bounds:
206  * @editable: a #GtkEditable
207  * @start_pos: beginning of selection
208  * @end_pos: end of selection
209  *
210  * Retrieves the selection bound of the editable. @start_pos will be filled
211  * with the start of the selection and @end_pos with end. If no text was selected
212  * both will be identical and %FALSE will be returned. Note that positions are
213  * specified in characters, not bytes.
214  *
215  * Return value: %TRUE if an area is selected, %FALSE otherwise
216  *
217  **/
218 gboolean
219 gtk_editable_get_selection_bounds (GtkEditable *editable,
220                                    gint        *start_pos,
221                                    gint        *end_pos)
222 {
223   gint tmp_start, tmp_end;
224   gboolean result;
225   
226   g_return_val_if_fail (GTK_IS_EDITABLE (editable), FALSE);
227
228   result = GTK_EDITABLE_GET_CLASS (editable)->get_selection_bounds (editable, &tmp_start, &tmp_end);
229
230   if (start_pos)
231     *start_pos = MIN (tmp_start, tmp_end);
232   if (end_pos)
233     *end_pos = MAX (tmp_start, tmp_end);
234
235   return result;
236 }
237
238 /**
239  * gtk_editable_delete_selection:
240  * @editable: a #GtkEditable
241  *
242  * Deletes the currently selected text of the editable.
243  * This call will not do anything if there is no selected text.
244  **/
245 void
246 gtk_editable_delete_selection (GtkEditable *editable)
247 {
248   gint start, end;
249
250   g_return_if_fail (GTK_IS_EDITABLE (editable));
251
252   if (gtk_editable_get_selection_bounds (editable, &start, &end))
253     gtk_editable_delete_text (editable, start, end);
254 }
255
256 /**
257  * gtk_editable_select_region:
258  * @editable: a #GtkEditable
259  * @start: start of region
260  * @end: end of region
261  *
262  * Selects the text between @start and @end. Both @start and @end are
263  * relative to the start of the content. Note that positions are specified
264  * in characters, not bytes.
265  **/
266 void
267 gtk_editable_select_region (GtkEditable *editable,
268                             gint         start,
269                             gint         end)
270 {
271   g_return_if_fail (GTK_IS_EDITABLE (editable));
272   
273   GTK_EDITABLE_GET_CLASS (editable)->set_selection_bounds (editable,  start, end);
274 }
275
276 /**
277  * gtk_editable_cut_clipboard:
278  * @editable: a #GtkEditable
279  *
280  * Removes the contents of the currently selected content in the editable and
281  * puts it on the clipboard.
282  **/
283 void
284 gtk_editable_cut_clipboard (GtkEditable *editable)
285 {
286   g_return_if_fail (GTK_IS_EDITABLE (editable));
287   
288   g_signal_emit_by_name (editable, "cut-clipboard");
289 }
290
291 /**
292  * gtk_editable_copy_clipboard:
293  * @editable: a #GtkEditable
294  *
295  * Copies the contents of the currently selected content in the editable and
296  * puts it on the clipboard.
297  **/
298 void
299 gtk_editable_copy_clipboard (GtkEditable *editable)
300 {
301   g_return_if_fail (GTK_IS_EDITABLE (editable));
302   
303   g_signal_emit_by_name (editable, "copy-clipboard");
304 }
305
306 /**
307  * gtk_editable_paste_clipboard:
308  * @editable: a #GtkEditable
309  *
310  * Pastes the content of the clipboard to the current position of the
311  * cursor in the editable.
312  **/
313 void
314 gtk_editable_paste_clipboard (GtkEditable *editable)
315 {
316   g_return_if_fail (GTK_IS_EDITABLE (editable));
317   
318   g_signal_emit_by_name (editable, "paste-clipboard");
319 }
320
321 /**
322  * gtk_editable_set_editable:
323  * @editable: a #GtkEditable
324  * @is_editable: %TRUE if the user is allowed to edit the text
325  *   in the widget
326  *
327  * Determines if the user can edit the text in the editable
328  * widget or not. 
329  *
330  **/
331 void
332 gtk_editable_set_editable (GtkEditable    *editable,
333                            gboolean        is_editable)
334 {
335   g_return_if_fail (GTK_IS_EDITABLE (editable));
336
337   g_object_set (editable,
338                 "editable", is_editable != FALSE,
339                 NULL);
340 }
341
342 /**
343  * gtk_editable_get_editable:
344  * @editable: a #GtkEditable
345  *
346  * Retrieves whether @editable is editable. See
347  * gtk_editable_set_editable().
348  *
349  * Return value: %TRUE if @editable is editable.
350  **/
351 gboolean
352 gtk_editable_get_editable (GtkEditable *editable)
353 {
354   gboolean value;
355
356   g_return_val_if_fail (GTK_IS_EDITABLE (editable), FALSE);
357
358   g_object_get (editable, "editable", &value, NULL);
359
360   return value;
361 }
362
363 #define __GTK_EDITABLE_C__
364 #include "gtkaliasdef.c"