Slightly improved docs in atktext.c
[platform/upstream/atk.git] / atk / atktext.c
1 /* ATK - The Accessibility Toolkit for GTK+
2  * Copyright 2001 Sun Microsystems Inc.
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 "atktext.h"
21 #include "atkmarshal.h"
22
23 enum {
24   TEXT_CHANGED,
25   CARET_MOVED,
26   LAST_SIGNAL
27 };
28
29 struct _AtkTextIfaceClass
30 {
31   GObjectClass parent;
32 };
33
34 typedef struct _AtkTextIfaceClass AtkTextIfaceClass;
35
36 static void atk_text_base_init (gpointer *g_class);
37
38 static guint atk_text_signals[LAST_SIGNAL] = { 0 };
39
40 GType
41 atk_text_get_type ()
42 {
43   static GType type = 0;
44
45   if (!type) 
46     {
47       static const GTypeInfo tinfo =
48       {
49         sizeof (AtkTextIface),
50         (GBaseInitFunc) atk_text_base_init,
51         (GBaseFinalizeFunc) NULL,
52         (GClassInitFunc) NULL /* atk_text_interface_init */ ,
53         (GClassFinalizeFunc) NULL,
54
55       };
56
57       type = g_type_register_static (G_TYPE_INTERFACE, "AtkText", &tinfo, 0);
58     }
59
60   return type;
61 }
62
63 static void
64 atk_text_base_init (gpointer *g_class)
65 {
66   static gboolean initialized = FALSE;
67
68   if (! initialized)
69     {
70    /* 
71     * Note that text_changed signal supports details "insert", "delete", 
72     * possibly "replace". 
73     */
74
75     atk_text_signals[TEXT_CHANGED] =
76       g_signal_newc ("text_changed",
77                      ATK_TYPE_TEXT,
78                      G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
79                      G_STRUCT_OFFSET (AtkTextIface, text_changed), 
80                      (GSignalAccumulator) NULL, NULL,
81                      atk_marshal_VOID__INT_INT,
82                      G_TYPE_NONE,
83                      2, G_TYPE_INT, G_TYPE_INT);
84
85     atk_text_signals[CARET_MOVED] =
86       g_signal_newc ("text_caret_moved",
87                      ATK_TYPE_TEXT,
88                      G_SIGNAL_RUN_LAST,
89                      G_STRUCT_OFFSET (AtkTextIface, caret_changed),
90                      (GSignalAccumulator) NULL, NULL,
91                      g_cclosure_marshal_VOID__INT,
92                      G_TYPE_NONE,
93                      1, G_TYPE_INT);
94
95   initialized = TRUE;
96   }
97 }
98
99 /**
100  * atk_text_get_text:
101  * @text: an #AtkText
102  * @start_offset: start position
103  * @end_offset: end position
104  *
105  * Gets the specified text.
106  *
107  * Returns: the text from @start_offset up to, but not including @end_offset.
108  **/
109 gchar*
110 atk_text_get_text (AtkText      *text,
111                    gint         start_offset,
112                    gint         end_offset)
113 {
114   AtkTextIface *iface;
115
116   g_return_val_if_fail (text != NULL, NULL);
117   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
118
119   iface = ATK_TEXT_GET_IFACE (text);
120
121   if (iface->get_text)
122     return (*(iface->get_text)) (text, start_offset, end_offset);
123   else
124     return NULL;
125 }
126
127 /**
128  * atk_text_get_character_at_offset
129  * @text: an #AtkText
130  * @offset: position
131  *
132  * Gets the specified text.
133  *
134  * Returns: the character at @offset.
135  **/
136 gunichar
137 atk_text_get_character_at_offset (AtkText      *text,
138                                   gint         offset)
139 {
140   AtkTextIface *iface;
141
142   g_return_val_if_fail (text != NULL, (gunichar) 0);
143   g_return_val_if_fail (ATK_IS_TEXT (text), (gunichar) 0);
144
145   iface = ATK_TEXT_GET_IFACE (text);
146
147   if (iface->get_character_at_offset)
148     return (*(iface->get_character_at_offset)) (text, offset);
149   else
150     return (gunichar) 0;
151 }
152
153 /**
154  * atk_text_get_text_after_offset
155  * @text: an #AtkText
156  * @offset: position
157  * @boundary_type: An #AtkTextBoundary
158  *
159  * Gets the specified text.
160  * If the boundary type is ATK_TEXT_BOUNDARY_WORD_START or
161  * ATK_TEXT_BOUNDARY_WORD_END part of a word may be returned.
162  * If the boundary type is ATK_TEXT_BOUNDARY_SENTENCE_START the start point 
163  * will be the offset and will continue to the start of the next sentence. 
164  * The first word may not be a complete word. Similarly for 
165  * ATK_TEXT_BOUNDARY_SENTENCE_END, ATK_TEXT_BOUNDARY_LINE_START and
166  * ATK_TEXT_BOUNDARY_LINE_END
167  *
168  * Returns: the text after @offset up to the specified @boundary_type.
169  **/
170 gchar*
171 atk_text_get_text_after_offset (AtkText          *text,
172                                 gint             offset,
173                                 AtkTextBoundary  boundary_type)
174 {
175   AtkTextIface *iface;
176
177   g_return_val_if_fail (text != NULL, NULL);
178   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
179
180   iface = ATK_TEXT_GET_IFACE (text);
181
182   if (iface->get_text_after_offset)
183     return (*(iface->get_text_after_offset)) (text, offset, boundary_type);
184   else
185     return NULL;
186 }
187
188 /**
189  * atk_text_get_text_at_offset
190  * @text: an #AtkText
191  * @offset: position
192  * @boundary_type: An #AtkTextBoundary
193  *
194  * Gets the specified text.
195  * If the boundary_type is ATK_TEXT_BOUNDARY_WORD_START or 
196  * ATK_TEXT_BOUNDARY_WORD_END a complete word is returned; 
197  * if the boundary type is  ATK_TEXT_BOUNDARY_SENTENCE_START or 
198  * ATK_TEXT_BOUNDARY_SENTENCE_END a complete sentence
199  * is returned; if the boundary type is ATK_TEXT_BOUNDARY_LINE_START or
200  * ATK_TEXT_BOUNDARY_LINE_END a complete line is returned.
201  *
202  * Returns: the text at @offset up to the specified @boundary_type.
203  **/
204 gchar*
205 atk_text_get_text_at_offset (AtkText          *text,
206                              gint             offset,
207                              AtkTextBoundary  boundary_type)
208 {
209   AtkTextIface *iface;
210
211   g_return_val_if_fail (text != NULL, NULL);
212   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
213
214   iface = ATK_TEXT_GET_IFACE (text);
215
216   if (iface->get_text_at_offset)
217     return (*(iface->get_text_at_offset)) (text, offset, boundary_type);
218   else
219     return NULL;
220 }
221
222 /**
223  * atk_text_get_text_before_offset
224  * @text: an #AtkText
225  * @offset: position
226  * @boundary_type: An #AtkTextBoundary
227  *
228  * Gets the specified text.
229  * If the boundary type is ATK_TEXT_BOUNDARY_WORD_START or
230  * ATK_TEXT_BOUNDARY_WORD_END part of a word may be returned.
231  * If the boundary type is ATK_TEXT_BOUNDARY_SENTENCE_START the start point 
232  * will be at the start of the sentence, and will continue to the offset. 
233  * The last word may not be a complete word. Similarly for 
234  * ATK_TEXT_BOUNDARY_SENTENCE_END, ATK_TEXT_BOUNDARY_LINE_START and
235  * ATK_TEXT_BOUNDARY_LINE_END
236  *
237  * Returns: the text before @offset starting from the specified @boundary_type.
238  **/
239 gchar*
240 atk_text_get_text_before_offset (AtkText          *text,
241                                  gint             offset,
242                                  AtkTextBoundary  boundary_type)
243 {
244   AtkTextIface *iface;
245
246   g_return_val_if_fail (text != NULL, NULL);
247   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
248
249   iface = ATK_TEXT_GET_IFACE (text);
250
251   if (iface->get_text_before_offset)
252     return (*(iface->get_text_before_offset)) (text, offset, boundary_type);
253   else
254     return NULL;
255 }
256
257 /**
258  * atk_text_get_caret_offset
259  * @text: an #AtkText
260  *
261  * Gets the offset position of the caret (cursor).
262  *
263  * Returns: the offset position of the caret (cursor), or -1 if
264  *    one does not exist (or is not supported by the widget).
265  **/
266 gint
267 atk_text_get_caret_offset (AtkText *text)
268 {
269   AtkTextIface *iface;
270
271   g_return_val_if_fail (text != NULL, -1);
272   g_return_val_if_fail (ATK_IS_TEXT (text), -1);
273
274   iface = ATK_TEXT_GET_IFACE (text);
275
276   if (iface->get_caret_offset)
277     return (*(iface->get_caret_offset)) (text);
278   else
279     return -1;
280 }
281
282 /**
283  * atk_text_get_range_attributes
284  * @text: an #AtkText
285  * @start_offset: start position
286  * @end_offset: end position
287  *
288  * Gets attributes over the specified range.
289  *
290  * Returns: a #PangoAttrList with the text attributes between the
291  * @start_offset and the @end_offset.
292  **/
293 PangoAttrList*
294 atk_text_get_range_attributes (AtkText *text,
295                                gint start_offset,
296                                gint end_offset)
297 {
298   AtkTextIface *iface;
299
300   g_return_val_if_fail (text != NULL, NULL);
301   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
302
303   iface = ATK_TEXT_GET_IFACE (text);
304
305   if (iface->get_range_attributes)
306     return (*(iface->get_range_attributes)) (text, start_offset, end_offset);
307   else
308     return NULL;
309 }
310
311 /**
312  * atk_text_get_character_extents
313  * @text: an #AtkText
314  * @offset: position
315  * @x: x-position of character
316  * @y: y-position of character
317  * @length: length of character
318  * @width: width of character
319  *
320  * Given an @offset, the @x, @y, @length, and @width values are filled
321  * appropriately.
322  **/
323 void
324 atk_text_get_character_extents (AtkText *text,
325                                 gint offset,
326                                 gint *x,
327                                 gint *y,
328                                 gint *length,
329                                 gint *width)
330 {
331   AtkTextIface *iface;
332
333   g_return_if_fail (text != NULL);
334   g_return_if_fail (ATK_IS_TEXT (text));
335
336   iface = ATK_TEXT_GET_IFACE (text);
337
338   if (iface->get_character_extents)
339     (*(iface->get_character_extents)) (text, offset, x, y, length, width);
340   else
341     {
342       *x = 0;
343       *x = 0;
344       *length = 0;
345       *width = 0;
346     }
347 }
348
349 /**
350  * atk_text_get_character_count
351  * @text: an #AtkText
352  *
353  * Gets the character count.
354  *
355  * Returns: the number of characters.
356  **/
357 gint
358 atk_text_get_character_count (AtkText *text)
359 {
360   AtkTextIface *iface;
361
362   g_return_val_if_fail (text != NULL, -1);
363   g_return_val_if_fail (ATK_IS_TEXT (text), -1);
364
365   iface = ATK_TEXT_GET_IFACE (text);
366
367   if (iface->get_character_count)
368     return (*(iface->get_character_count)) (text);
369   else
370     return -1;
371 }
372
373 /**
374  * atk_text_get_offset_at_point
375  * @text: an #AtkText
376  * @x: screen x-position of character
377  * @y: screen y-position of character
378  *
379  * Gets the x,y screen coordinates of the specified character.
380  *
381  * Returns: the offset to the character which is located at
382  * the specified @x and @y coordinates.
383  **/
384 gint
385 atk_text_get_offset_at_point (AtkText *text,
386                               gint x,
387                               gint y)
388 {
389   AtkTextIface *iface;
390
391   g_return_val_if_fail (text != NULL, -1);
392   g_return_val_if_fail (ATK_IS_TEXT (text), -1);
393
394   iface = ATK_TEXT_GET_IFACE (text);
395
396   if (iface->get_offset_at_point)
397     return (*(iface->get_offset_at_point)) (text, x, y);
398   else
399     return -1;
400 }
401
402 /**
403  * atk_text_get_n_selections
404  * @text: an #AtkText
405  *
406  * Gets the number of selected regions.
407  *
408  * Returns: The number of selected regions, or -1 if a failure
409  *   occurred.
410  **/
411 gint
412 atk_text_get_n_selections (AtkText *text)
413 {
414   AtkTextIface *iface;
415
416   g_return_val_if_fail (text != NULL, -1);
417   g_return_val_if_fail (ATK_IS_TEXT (text), -1);
418
419   iface = ATK_TEXT_GET_IFACE (text);
420
421   if (iface->get_n_selections)
422     return (*(iface->get_n_selections)) (text);
423   else
424     return -1;
425 }
426
427 /**
428  * atk_text_get_selection
429  * @text: an #AtkText
430  * @selection_num: The selection number.  The selected regions are
431  * assigned numbers that corrispond to how far the region is from the
432  * start of the text.  The selected region closest to the beginning
433  * of the text region is assigned the number 0, etc.  Note that adding,
434  * moving or deleting a selected region can change the numbering.
435  * @start_offset: passes back the start position of the selected region
436  * @end_offset: passes back the end position of the selected region
437  *
438  * Gets the text from the specified selection.
439  *
440  * Returns: the selected text.
441  **/
442 gchar*
443 atk_text_get_selection (AtkText *text, gint selection_num,
444    gint *start_offset, gint *end_offset)
445 {
446   AtkTextIface *iface;
447
448   g_return_val_if_fail (text != NULL, NULL);
449   g_return_val_if_fail (ATK_IS_TEXT (text), NULL);
450
451   iface = ATK_TEXT_GET_IFACE (text);
452
453   if (iface->get_selection)
454   {
455     return (*(iface->get_selection)) (text, selection_num,
456        start_offset, end_offset);
457   }
458   else
459     return NULL;
460 }
461
462 /**
463  * atk_text_add_selection
464  * @text: an #AtkText
465  * @start_offset: the start position of the selected region
466  * @end_offset: the end position of the selected region
467  *
468  * Adds a selection bounded by the specified offsets.
469  *
470  * Returns: %TRUE if success, %FALSE otherwise
471  **/
472 gboolean
473 atk_text_add_selection (AtkText *text, gint start_offset,
474    gint end_offset)
475 {
476   AtkTextIface *iface;
477
478   g_return_val_if_fail (text != NULL, FALSE);
479   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
480
481   iface = ATK_TEXT_GET_IFACE (text);
482
483   if (iface->add_selection)
484     return (*(iface->add_selection)) (text, start_offset, end_offset);
485   else
486     return FALSE;
487 }
488
489 /**
490  * atk_text_remove_selection
491  * @text: an #AtkText
492  * @selection_num: The selection number.  The selected regions are
493  * assigned numbers that corrispond to how far the region is from the
494  * start of the text.  The selected region closest to the beginning
495  * of the text region is assigned the number 0, etc.  Note that adding,
496  * moving or deleting a selected region can change the numbering.
497  *
498  * Removes the specified selection
499  *
500  * Returns: %TRUE if success, %FALSE otherwise
501  **/
502 gboolean
503 atk_text_remove_selection (AtkText *text, gint selection_num)
504 {
505   AtkTextIface *iface;
506
507   g_return_val_if_fail (text != NULL, FALSE);
508   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
509
510   iface = ATK_TEXT_GET_IFACE (text);
511
512   if (iface->remove_selection)
513     return (*(iface->remove_selection)) (text, selection_num);
514   else
515     return FALSE;
516 }
517
518 /**
519  * atk_text_set_selection
520  * @text: an #AtkText
521  * @selection_num: The selection number.  The selected regions are
522  * assigned numbers that corrispond to how far the region is from the
523  * start of the text.  The selected region closest to the beginning
524  * of the text region is assigned the number 0, etc.  Note that adding,
525  * moving or deleting a selected region can change the numbering.
526  * @start_offset: the new start position of the selection
527  * @end_offset: the new end position of the selection
528  *
529  * Changes the start and end offset of the specified selection
530  *
531  * Returns: %TRUE if success, %FALSE otherwise
532  **/
533 gboolean
534 atk_text_set_selection (AtkText *text, gint selection_num,
535    gint start_offset, gint end_offset)
536 {
537   AtkTextIface *iface;
538
539   g_return_val_if_fail (text != NULL, FALSE);
540   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
541
542   iface = ATK_TEXT_GET_IFACE (text);
543
544   if (iface->set_selection)
545   {
546     return (*(iface->set_selection)) (text, selection_num,
547        start_offset, end_offset);
548   }
549   else
550     return FALSE;
551 }
552
553 /**
554  * atk_text_set_caret_offset
555  * @text: an #AtkText
556  * @offset: position
557  *
558  * Sets the caret (cursor) position to the specified @offset.
559  *
560  * Returns: %TRUE if success, %FALSE otherwise.
561  **/
562 gboolean
563 atk_text_set_caret_offset (AtkText *text,
564                            gint    offset)
565 {
566   AtkTextIface *iface;
567
568   g_return_val_if_fail (text != NULL, FALSE);
569   g_return_val_if_fail (ATK_IS_TEXT (text), FALSE);
570
571   iface = ATK_TEXT_GET_IFACE (text);
572
573   if (iface->set_caret_offset)
574     {
575       return (*(iface->set_caret_offset)) (text, offset);
576     }
577   else
578     {
579       return FALSE;
580     }
581 }