gtkeditable.c 9.49 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

27
#include "config.h"
28
#include <string.h>
Owen Taylor's avatar
Owen Taylor committed
29

30
#include "gtkeditable.h"
31
#include "gtkmarshalers.h"
Matthias Clasen's avatar
Matthias Clasen committed
32
#include "gtkintl.h"
33
#include "gtkalias.h"
34

35

Matthias Clasen's avatar
Matthias Clasen committed
36
static void gtk_editable_base_init (gpointer g_class);
37 38


Manish Singh's avatar
Manish Singh committed
39
GType
40
gtk_editable_get_type (void)
41
{
Manish Singh's avatar
Manish Singh committed
42
  static GType editable_type = 0;
43 44 45

  if (!editable_type)
    {
46
      const GTypeInfo editable_info =
47
      {
Owen Taylor's avatar
Owen Taylor committed
48
	sizeof (GtkEditableClass),  /* class_size */
49
	gtk_editable_base_init,	    /* base_init */
Owen Taylor's avatar
Owen Taylor committed
50
	NULL,			    /* base_finalize */
51 52
      };

Matthias Clasen's avatar
Matthias Clasen committed
53
      editable_type = g_type_register_static (G_TYPE_INTERFACE, I_("GtkEditable"),
Manish Singh's avatar
Manish Singh committed
54
					      &editable_info, 0);
55 56 57 58 59
    }

  return editable_type;
}

60 61 62 63 64 65 66
static void
gtk_editable_base_init (gpointer g_class)
{
  static gboolean initialized = FALSE;

  if (! initialized)
    {
67
      g_signal_new (I_("insert-text"),
68 69 70 71
		    GTK_TYPE_EDITABLE,
		    G_SIGNAL_RUN_LAST,
		    G_STRUCT_OFFSET (GtkEditableClass, insert_text),
		    NULL, NULL,
72
		    _gtk_marshal_VOID__STRING_INT_POINTER,
73
		    G_TYPE_NONE, 3,
Manish Singh's avatar
Manish Singh committed
74 75 76
		    G_TYPE_STRING,
		    G_TYPE_INT,
		    G_TYPE_POINTER);
77
      g_signal_new (I_("delete-text"),
78 79 80 81
		    GTK_TYPE_EDITABLE,
		    G_SIGNAL_RUN_LAST,
		    G_STRUCT_OFFSET (GtkEditableClass, delete_text),
		    NULL, NULL,
82
		    _gtk_marshal_VOID__INT_INT,
Manish Singh's avatar
Manish Singh committed
83 84 85
		    G_TYPE_NONE, 2,
		    G_TYPE_INT,
		    G_TYPE_INT);
Matthias Clasen's avatar
Matthias Clasen committed
86
      g_signal_new (I_("changed"),
87 88 89 90
		    GTK_TYPE_EDITABLE,
		    G_SIGNAL_RUN_LAST,
		    G_STRUCT_OFFSET (GtkEditableClass, changed),
		    NULL, NULL,
91
		    _gtk_marshal_VOID__VOID,
Manish Singh's avatar
Manish Singh committed
92
		    G_TYPE_NONE, 0);
93 94 95 96 97

      initialized = TRUE;
    }
}

98 99 100 101 102 103 104
/**
 * gtk_editable_insert_text:
 * @editable: a #GtkEditable
 * @new_text: the text to append
 * @new_text_length: the text to append
 * @position: position text will be inserted at
 *
105
 * Appends @new_text_length characters of @new_text to the contents of the widget,
106 107
 * at position @position. Note that this position is in characters, not in bytes.
 **/
108 109 110
void
gtk_editable_insert_text (GtkEditable *editable,
			  const gchar *new_text,
111 112
			  gint         new_text_length,
			  gint        *position)
113 114
{
  g_return_if_fail (GTK_IS_EDITABLE (editable));
Owen Taylor's avatar
Owen Taylor committed
115
  g_return_if_fail (position != NULL);
Owen Taylor's avatar
Owen Taylor committed
116

117 118 119
  if (new_text_length < 0)
    new_text_length = strlen (new_text);
  
120
  GTK_EDITABLE_GET_CLASS (editable)->do_insert_text (editable, new_text, new_text_length, position);
121 122
}

123 124 125 126 127 128 129 130 131
/**
 * gtk_editable_delete_text:
 * @editable: a #GtkEditable
 * @start_pos: start position
 * @end_pos: end position
 *
 * Deletes the content of the editable between @start_pos and @end_pos.
 * Note that positions are specified in characters, not bytes.
 **/
132 133
void
gtk_editable_delete_text (GtkEditable *editable,
134 135
			  gint         start_pos,
			  gint         end_pos)
136 137 138
{
  g_return_if_fail (GTK_IS_EDITABLE (editable));

139
  GTK_EDITABLE_GET_CLASS (editable)->do_delete_text (editable, start_pos, end_pos);
140 141
}

142 143 144 145 146 147 148 149 150 151 152 153 154
/**
 * gtk_editable_get_chars:
 * @editable: a #GtkEditable
 * @start: start of text
 * @end: end of text
 *
 * Retreives the content of the editable between @start and @end.
 * Note that positions are specified in characters, not bytes.
 *
 * Return value: a pointer to the contents of the widget as a
 *      string. This string is allocated by the #GtkEditable
 *      implementation and should be freed by the caller.
 **/
155
gchar *    
Owen Taylor's avatar
Owen Taylor committed
156 157 158
gtk_editable_get_chars (GtkEditable *editable,
			gint         start,
			gint         end)
159
{
160
  g_return_val_if_fail (GTK_IS_EDITABLE (editable), NULL);
161

Owen Taylor's avatar
Owen Taylor committed
162
  return GTK_EDITABLE_GET_CLASS (editable)->get_chars (editable, start, end);
163 164
}

165 166 167 168 169 170 171 172 173 174 175 176
/**
 * gtk_editable_set_position:
 * @editable: a #GtkEditable
 * @position:  the position of the cursor. The cursor is displayed
 *    before the character with the given (base 0) index in the editable. 
 *    The value must be less than or equal to the number of characters 
 *    in the editable. A value of -1 indicates that the position should
 *    be set after the last character of the editable. Note that this 
 *    position is in characters, not in bytes.
 *
 * Sets the cursor position in the editable to the given value.
 **/
Owen Taylor's avatar
Owen Taylor committed
177
void
Tim Janik's avatar
Tim Janik committed
178 179
gtk_editable_set_position (GtkEditable      *editable,
			   gint              position)
Owen Taylor's avatar
Owen Taylor committed
180 181 182
{
  g_return_if_fail (GTK_IS_EDITABLE (editable));

Owen Taylor's avatar
Owen Taylor committed
183
  GTK_EDITABLE_GET_CLASS (editable)->set_position (editable, position);
Owen Taylor's avatar
Owen Taylor committed
184 185
}

186 187 188 189 190 191 192 193 194 195
/**
 * gtk_editable_get_position:
 * @editable: a #GtkEditable
 *
 * Retrieves the current position of the cursor relative to the start
 * of the content of the editable. Note that this position is in characters,
 * not in bytes.
 *
 * Return value: the cursor position
 **/
Owen Taylor's avatar
Owen Taylor committed
196
gint
197
gtk_editable_get_position (GtkEditable *editable)
Owen Taylor's avatar
Owen Taylor committed
198
{
Owen Taylor's avatar
Owen Taylor committed
199
  g_return_val_if_fail (GTK_IS_EDITABLE (editable), 0);
Owen Taylor's avatar
Owen Taylor committed
200

Owen Taylor's avatar
Owen Taylor committed
201
  return GTK_EDITABLE_GET_CLASS (editable)->get_position (editable);
202 203
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217
/**
 * gtk_editable_get_selection_bounds:
 * @editable: a #GtkEditable
 * @start_pos: beginning of selection
 * @end_pos: end of selection
 *
 * Retrieves the selection bound of the editable. @start_pos will be filled
 * with the start of the selection and @end_pos with end. If no text was selected
 * both will be identical and %FALSE will be returned. Note that positions are
 * specified in characters, not bytes.
 *
 * Return value: %TRUE if an area is selected, %FALSE otherwise
 *
 **/
Owen Taylor's avatar
Owen Taylor committed
218 219 220 221
gboolean
gtk_editable_get_selection_bounds (GtkEditable *editable,
				   gint        *start_pos,
				   gint        *end_pos)
222
{
Owen Taylor's avatar
Owen Taylor committed
223 224
  gint tmp_start, tmp_end;
  gboolean result;
225
  
Owen Taylor's avatar
Owen Taylor committed
226
  g_return_val_if_fail (GTK_IS_EDITABLE (editable), FALSE);
227

Owen Taylor's avatar
Owen Taylor committed
228
  result = GTK_EDITABLE_GET_CLASS (editable)->get_selection_bounds (editable, &tmp_start, &tmp_end);
229

Owen Taylor's avatar
Owen Taylor committed
230 231 232 233
  if (start_pos)
    *start_pos = MIN (tmp_start, tmp_end);
  if (end_pos)
    *end_pos = MAX (tmp_start, tmp_end);
234

Owen Taylor's avatar
Owen Taylor committed
235
  return result;
236 237
}

238 239 240 241 242 243 244
/**
 * gtk_editable_delete_selection:
 * @editable: a #GtkEditable
 *
 * Deletes the currently selected text of the editable.
 * This call will not do anything if there is no selected text.
 **/
245 246 247
void
gtk_editable_delete_selection (GtkEditable *editable)
{
Owen Taylor's avatar
Owen Taylor committed
248
  gint start, end;
249

250 251
  g_return_if_fail (GTK_IS_EDITABLE (editable));

Owen Taylor's avatar
Owen Taylor committed
252 253
  if (gtk_editable_get_selection_bounds (editable, &start, &end))
    gtk_editable_delete_text (editable, start, end);
254 255
}

256 257 258 259 260 261 262 263 264 265
/**
 * gtk_editable_select_region:
 * @editable: a #GtkEditable
 * @start: start of region
 * @end: end of region
 *
 * Selects the text between @start and @end. Both @start and @end are
 * relative to the start of the content. Note that positions are specified
 * in characters, not bytes.
 **/
266 267
void
gtk_editable_select_region (GtkEditable *editable,
268 269
			    gint         start,
			    gint         end)
270
{
271 272
  g_return_if_fail (GTK_IS_EDITABLE (editable));
  
Owen Taylor's avatar
Owen Taylor committed
273
  GTK_EDITABLE_GET_CLASS (editable)->set_selection_bounds (editable,  start, end);
274 275
}

276 277 278 279 280 281 282
/**
 * gtk_editable_cut_clipboard:
 * @editable: a #GtkEditable
 *
 * Removes the contents of the currently selected content in the editable and
 * puts it on the clipboard.
 **/
283
void
284
gtk_editable_cut_clipboard (GtkEditable *editable)
285
{
286 287
  g_return_if_fail (GTK_IS_EDITABLE (editable));
  
288
  g_signal_emit_by_name (editable, "cut-clipboard");
289 290
}

291 292 293 294 295 296 297
/**
 * gtk_editable_copy_clipboard:
 * @editable: a #GtkEditable
 *
 * Copies the contents of the currently selected content in the editable and
 * puts it on the clipboard.
 **/
298 299 300
void
gtk_editable_copy_clipboard (GtkEditable *editable)
{
301 302
  g_return_if_fail (GTK_IS_EDITABLE (editable));
  
303
  g_signal_emit_by_name (editable, "copy-clipboard");
304 305
}

306 307 308 309 310 311 312
/**
 * gtk_editable_paste_clipboard:
 * @editable: a #GtkEditable
 *
 * Pastes the content of the clipboard to the current position of the
 * cursor in the editable.
 **/
313
void
314
gtk_editable_paste_clipboard (GtkEditable *editable)
315
{
316 317
  g_return_if_fail (GTK_IS_EDITABLE (editable));
  
318
  g_signal_emit_by_name (editable, "paste-clipboard");
319 320
}

321 322 323 324 325 326 327 328 329 330
/**
 * gtk_editable_set_editable:
 * @editable: a #GtkEditable
 * @is_editable: %TRUE if the user is allowed to edit the text
 *   in the widget
 *
 * Determines if the user can edit the text in the editable
 * widget or not. 
 *
 **/
331 332 333 334 335 336
void
gtk_editable_set_editable (GtkEditable    *editable,
			   gboolean        is_editable)
{
  g_return_if_fail (GTK_IS_EDITABLE (editable));

337
  g_object_set (editable,
Manish Singh's avatar
Manish Singh committed
338 339
		"editable", is_editable != FALSE,
		NULL);
340
}
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

/**
 * gtk_editable_get_editable:
 * @editable: a #GtkEditable
 *
 * Retrieves whether @editable is editable. See
 * gtk_editable_set_editable().
 *
 * Return value: %TRUE if @editable is editable.
 **/
gboolean
gtk_editable_get_editable (GtkEditable *editable)
{
  gboolean value;

  g_return_val_if_fail (GTK_IS_EDITABLE (editable), FALSE);

358
  g_object_get (editable, "editable", &value, NULL);
359 360 361

  return value;
}
362 363 364

#define __GTK_EDITABLE_C__
#include "gtkaliasdef.c"