]> Pileus Git - ~andy/gtk/blob - gtk/gtkeditable.c
Doc additions.
[~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       /**
68        * GtkEditable::insert-text:
69        * @editable: the object which received the signal
70        * @new_text: the new text to insert
71        * @new_text_length: the length of the new text, in bytes,
72        *     or -1 if new_text is nul-terminated
73        * @position: the position, in characters, at which to insert
74        *     the new text. this is an in-out parameter.
75        *     After the signal emission is finished, it should
76        *     point after the newly inserted text.
77        *
78        * This signal is emitted when text is inserted into
79        * the widget by the user. The default handler for
80        * this signal will normally be responsible for inserting
81        * the text, so by connecting to this signal and then
82        * stopping the signal with g_signal_stop_emission(), it
83        * is possible to modify the inserted text, or prevent
84        * it from being inserted entirely.
85        */
86       g_signal_new (I_("insert-text"),
87                     GTK_TYPE_EDITABLE,
88                     G_SIGNAL_RUN_LAST,
89                     G_STRUCT_OFFSET (GtkEditableClass, insert_text),
90                     NULL, NULL,
91                     _gtk_marshal_VOID__STRING_INT_POINTER,
92                     G_TYPE_NONE, 3,
93                     G_TYPE_STRING,
94                     G_TYPE_INT,
95                     G_TYPE_POINTER);
96
97       /**
98        * GtkEditable::delete-text:
99        * @editable: the object which received the signal
100        * @start_pos: the starting position
101        * @end_pos: the end position
102        * 
103        * This signal is emitted when text is deleted from
104        * the widget by the user. The default handler for
105        * this signal will normally be responsible for deleting
106        * the text, so by connecting to this signal and then
107        * stopping the signal with g_signal_stop_emission(), it
108        * is possible to modify the range of deleted text, or
109        * prevent it from being deleted entirely. The @start_pos
110        * and @end_pos parameters are interpreted as for
111        * gtk_editable_delete_text().
112        */
113       g_signal_new (I_("delete-text"),
114                     GTK_TYPE_EDITABLE,
115                     G_SIGNAL_RUN_LAST,
116                     G_STRUCT_OFFSET (GtkEditableClass, delete_text),
117                     NULL, NULL,
118                     _gtk_marshal_VOID__INT_INT,
119                     G_TYPE_NONE, 2,
120                     G_TYPE_INT,
121                     G_TYPE_INT);
122       /**
123        * GtkEditable::changed:
124        * @editable: the object which received the signal
125        *
126        * The ::changed signal is emitted at the end of a single
127        * user-visible operation on the contents of the #GtkEditable.
128        *
129        * E.g., a paste operation that replaces the contents of the
130        * selection will cause only one signal emission (even though it
131        * is implemented by first deleting the selection, then inserting
132        * the new content, and may cause multiple ::notify::text signals
133        * to be emitted).
134        */ 
135       g_signal_new (I_("changed"),
136                     GTK_TYPE_EDITABLE,
137                     G_SIGNAL_RUN_LAST,
138                     G_STRUCT_OFFSET (GtkEditableClass, changed),
139                     NULL, NULL,
140                     _gtk_marshal_VOID__VOID,
141                     G_TYPE_NONE, 0);
142
143       initialized = TRUE;
144     }
145 }
146
147 /**
148  * gtk_editable_insert_text:
149  * @editable: a #GtkEditable
150  * @new_text: the text to append
151  * @new_text_length: the length of the text in bytes, or -1
152  * @position: location of the position text will be inserted at
153  *
154  * Inserts @new_text_length bytes of @new_text into the contents of the 
155  * widget, at position @position. 
156  *
157  * Note that the position is in characters, not in bytes. 
158  * The function updates @position to point after the newly inserted text.
159  */
160 void
161 gtk_editable_insert_text (GtkEditable *editable,
162                           const gchar *new_text,
163                           gint         new_text_length,
164                           gint        *position)
165 {
166   g_return_if_fail (GTK_IS_EDITABLE (editable));
167   g_return_if_fail (position != NULL);
168
169   if (new_text_length < 0)
170     new_text_length = strlen (new_text);
171   
172   GTK_EDITABLE_GET_CLASS (editable)->do_insert_text (editable, new_text, new_text_length, position);
173 }
174
175 /**
176  * gtk_editable_delete_text:
177  * @editable: a #GtkEditable
178  * @start_pos: start position
179  * @end_pos: end position
180  *
181  * Deletes a sequence of characters. The characters that are deleted are 
182  * those characters at positions from @start_pos up to, but not including 
183  * @end_pos. If @end_pos is negative, then the the characters deleted
184  * are those from @start_pos to the end of the text.
185  *
186  * Note that the positions are specified in characters, not bytes.
187  */
188 void
189 gtk_editable_delete_text (GtkEditable *editable,
190                           gint         start_pos,
191                           gint         end_pos)
192 {
193   g_return_if_fail (GTK_IS_EDITABLE (editable));
194
195   GTK_EDITABLE_GET_CLASS (editable)->do_delete_text (editable, start_pos, end_pos);
196 }
197
198 /**
199  * gtk_editable_get_chars:
200  * @editable: a #GtkEditable
201  * @start_pos: start of text
202  * @end_pos: end of text
203  *
204  * Retrieves a sequence of characters. The characters that are retrieved 
205  * are those characters at positions from @start_pos up to, but not 
206  * including @end_pos. If @end_pos is negative, then the the characters 
207  * retrieved are those characters from @start_pos to the end of the text.
208  * 
209  * Note that positions are specified in characters, not bytes.
210  *
211  * Return value: a pointer to the contents of the widget as a
212  *      string. This string is allocated by the #GtkEditable
213  *      implementation and should be freed by the caller.
214  */
215 gchar *    
216 gtk_editable_get_chars (GtkEditable *editable,
217                         gint         start_pos,
218                         gint         end_pos)
219 {
220   g_return_val_if_fail (GTK_IS_EDITABLE (editable), NULL);
221
222   return GTK_EDITABLE_GET_CLASS (editable)->get_chars (editable, start_pos, end_pos);
223 }
224
225 /**
226  * gtk_editable_set_position:
227  * @editable: a #GtkEditable
228  * @position: the position of the cursor 
229  *
230  * Sets the cursor position in the editable to the given value.
231  *
232  * The cursor is displayed before the character with the given (base 0) 
233  * index in the contents of the editable. The value must be less than or 
234  * equal to the number of characters in the editable. A value of -1 
235  * indicates that the position should be set after the last character 
236  * of the editable. Note that @position is in characters, not in bytes.
237  */
238 void
239 gtk_editable_set_position (GtkEditable      *editable,
240                            gint              position)
241 {
242   g_return_if_fail (GTK_IS_EDITABLE (editable));
243
244   GTK_EDITABLE_GET_CLASS (editable)->set_position (editable, position);
245 }
246
247 /**
248  * gtk_editable_get_position:
249  * @editable: a #GtkEditable
250  *
251  * Retrieves the current position of the cursor relative to the start
252  * of the content of the editable. 
253  * 
254  * Note that this position is in characters, not in bytes.
255  *
256  * Return value: the cursor position
257  */
258 gint
259 gtk_editable_get_position (GtkEditable *editable)
260 {
261   g_return_val_if_fail (GTK_IS_EDITABLE (editable), 0);
262
263   return GTK_EDITABLE_GET_CLASS (editable)->get_position (editable);
264 }
265
266 /**
267  * gtk_editable_get_selection_bounds:
268  * @editable: a #GtkEditable
269  * @start_pos: location to store the starting position, or %NULL 
270  * @end_pos: location to store the end position, or %NULL 
271  *
272  * Retrieves the selection bound of the editable. start_pos will be filled
273  * with the start of the selection and @end_pos with end. If no text was 
274  * selected both will be identical and %FALSE will be returned. 
275  *
276  * Note that positions are specified in characters, not bytes.
277  *
278  * Return value: %TRUE if an area is selected, %FALSE otherwise
279  */
280 gboolean
281 gtk_editable_get_selection_bounds (GtkEditable *editable,
282                                    gint        *start_pos,
283                                    gint        *end_pos)
284 {
285   gint tmp_start, tmp_end;
286   gboolean result;
287   
288   g_return_val_if_fail (GTK_IS_EDITABLE (editable), FALSE);
289
290   result = GTK_EDITABLE_GET_CLASS (editable)->get_selection_bounds (editable, &tmp_start, &tmp_end);
291
292   if (start_pos)
293     *start_pos = MIN (tmp_start, tmp_end);
294   if (end_pos)
295     *end_pos = MAX (tmp_start, tmp_end);
296
297   return result;
298 }
299
300 /**
301  * gtk_editable_delete_selection:
302  * @editable: a #GtkEditable
303  *
304  * Deletes the currently selected text of the editable.
305  * This call doesn't do anything if there is no selected text.
306  */
307 void
308 gtk_editable_delete_selection (GtkEditable *editable)
309 {
310   gint start, end;
311
312   g_return_if_fail (GTK_IS_EDITABLE (editable));
313
314   if (gtk_editable_get_selection_bounds (editable, &start, &end))
315     gtk_editable_delete_text (editable, start, end);
316 }
317
318 /**
319  * gtk_editable_select_region:
320  * @editable: a #GtkEditable
321  * @start_pos: start of region
322  * @end_pos: end of region
323  *
324  * Selects a region of text. The characters that are selected are 
325  * those characters at positions from @start_pos up to, but not 
326  * including @end_pos. If @end_pos is negative, then the the 
327  * characters selected are those characters from @start_pos to 
328  * the end of the text.
329  * 
330  * Note that positions are specified in characters, not bytes.
331  */
332 void
333 gtk_editable_select_region (GtkEditable *editable,
334                             gint         start_pos,
335                             gint         end_pos)
336 {
337   g_return_if_fail (GTK_IS_EDITABLE (editable));
338   
339   GTK_EDITABLE_GET_CLASS (editable)->set_selection_bounds (editable, start_pos, end_pos);
340 }
341
342 /**
343  * gtk_editable_cut_clipboard:
344  * @editable: a #GtkEditable
345  *
346  * Removes the contents of the currently selected content in the editable and
347  * puts it on the clipboard.
348  */
349 void
350 gtk_editable_cut_clipboard (GtkEditable *editable)
351 {
352   g_return_if_fail (GTK_IS_EDITABLE (editable));
353   
354   g_signal_emit_by_name (editable, "cut-clipboard");
355 }
356
357 /**
358  * gtk_editable_copy_clipboard:
359  * @editable: a #GtkEditable
360  *
361  * Copies the contents of the currently selected content in the editable and
362  * puts it on the clipboard.
363  */
364 void
365 gtk_editable_copy_clipboard (GtkEditable *editable)
366 {
367   g_return_if_fail (GTK_IS_EDITABLE (editable));
368   
369   g_signal_emit_by_name (editable, "copy-clipboard");
370 }
371
372 /**
373  * gtk_editable_paste_clipboard:
374  * @editable: a #GtkEditable
375  *
376  * Pastes the content of the clipboard to the current position of the
377  * cursor in the editable.
378  */
379 void
380 gtk_editable_paste_clipboard (GtkEditable *editable)
381 {
382   g_return_if_fail (GTK_IS_EDITABLE (editable));
383   
384   g_signal_emit_by_name (editable, "paste-clipboard");
385 }
386
387 /**
388  * gtk_editable_set_editable:
389  * @editable: a #GtkEditable
390  * @is_editable: %TRUE if the user is allowed to edit the text
391  *   in the widget
392  *
393  * Determines if the user can edit the text in the editable
394  * widget or not. 
395  */
396 void
397 gtk_editable_set_editable (GtkEditable    *editable,
398                            gboolean        is_editable)
399 {
400   g_return_if_fail (GTK_IS_EDITABLE (editable));
401
402   g_object_set (editable,
403                 "editable", is_editable != FALSE,
404                 NULL);
405 }
406
407 /**
408  * gtk_editable_get_editable:
409  * @editable: a #GtkEditable
410  *
411  * Retrieves whether @editable is editable. See
412  * gtk_editable_set_editable().
413  *
414  * Return value: %TRUE if @editable is editable.
415  */
416 gboolean
417 gtk_editable_get_editable (GtkEditable *editable)
418 {
419   gboolean value;
420
421   g_return_val_if_fail (GTK_IS_EDITABLE (editable), FALSE);
422
423   g_object_get (editable, "editable", &value, NULL);
424
425   return value;
426 }
427
428 #define __GTK_EDITABLE_C__
429 #include "gtkaliasdef.c"