API revisions: tweaks to key event API, added some reserved slots for
[platform/core/uifw/at-spi2-atk.git] / libspi / text.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001 Sun Microsystems Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /* text.c : implements the Text interface */
24
25 #include <config.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <atk/atktext.h>
29 #include <libspi/text.h>
30
31 /* Our parent Gtk object type */
32 #define PARENT_TYPE SPI_TYPE_BASE
33
34 static AtkText *
35 get_text_from_servant (PortableServer_Servant servant)
36 {
37   SpiBase *object = SPI_BASE (bonobo_object_from_servant (servant));
38
39   g_return_val_if_fail (object, NULL);
40   g_return_val_if_fail (ATK_IS_OBJECT(object->gobj), NULL);
41   return ATK_TEXT (object->gobj);
42 }
43
44 static CORBA_string
45 impl_getText (PortableServer_Servant servant,
46               const CORBA_long       startOffset,
47               const CORBA_long       endOffset,
48               CORBA_Environment     *ev)
49 {
50   gchar *txt;
51   CORBA_string rv;
52   AtkText *text = get_text_from_servant (servant);
53
54   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
55   
56   txt = atk_text_get_text (text, (gint) startOffset, (gint) endOffset);
57   if (txt)
58     {
59       rv = CORBA_string_dup (txt);
60       g_free (txt);
61     }
62   else
63     rv = CORBA_string_dup ("");
64
65   return rv;
66 }
67
68
69 CORBA_string
70 impl_getTextAfterOffset (PortableServer_Servant servant,
71                          const CORBA_long offset,
72                          const
73                          Accessibility_TEXT_BOUNDARY_TYPE
74                          type, CORBA_long * startOffset,
75                          CORBA_long * endOffset,
76                          CORBA_Environment *ev)
77 {
78   gchar *txt;
79   CORBA_char *rv;
80   gint intStartOffset, intEndOffset;
81   AtkText *text = get_text_from_servant (servant);
82
83   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
84
85   txt = atk_text_get_text_after_offset (text,
86                                         (gint) offset, (AtkTextBoundary) type,
87                                         &intStartOffset, &intEndOffset);
88   *startOffset = (CORBA_long) intStartOffset;
89   *endOffset = (CORBA_long) intEndOffset;
90
91   if (txt)
92     {
93       rv = CORBA_string_dup (txt);
94       g_free (txt);
95     }
96   else
97     rv = CORBA_string_dup ("");
98
99   return rv;
100 }
101
102
103 static CORBA_string
104 impl_getTextAtOffset (PortableServer_Servant servant,
105                       const CORBA_long offset,
106                       const Accessibility_TEXT_BOUNDARY_TYPE type,
107                       CORBA_long * startOffset,
108                       CORBA_long * endOffset,
109                       CORBA_Environment *ev)
110 {
111   CORBA_char *txt;
112   CORBA_char *rv;
113   gint intStartOffset, intEndOffset;
114   AtkText *text = get_text_from_servant (servant);
115
116   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
117
118   txt = (CORBA_char *) atk_text_get_text_at_offset (
119           text,
120           (gint) offset, (AtkTextBoundary) type,
121           &intStartOffset, &intEndOffset);
122
123   *startOffset = (CORBA_long) intStartOffset;
124   *endOffset = (CORBA_long) intEndOffset;
125
126   if (txt)
127     {
128       rv = CORBA_string_dup (txt);
129       g_free (txt);
130     }
131   else
132     rv = CORBA_string_dup ("");
133
134   return rv;
135 }
136
137
138 static CORBA_unsigned_long
139 impl_getCharacterAtOffset (PortableServer_Servant servant,
140                            const CORBA_long offset,
141                            CORBA_Environment *ev)
142 {
143   AtkText *text = get_text_from_servant (servant);
144
145   g_return_val_if_fail (text != NULL, 0);
146
147   return (CORBA_unsigned_long)
148     atk_text_get_character_at_offset (text, (gint) offset);
149 }
150
151
152 static CORBA_string
153 impl_getTextBeforeOffset (PortableServer_Servant servant,
154                           const CORBA_long offset,
155                           const
156                           Accessibility_TEXT_BOUNDARY_TYPE
157                           type, CORBA_long * startOffset,
158                           CORBA_long * endOffset,
159                           CORBA_Environment *ev)
160 {
161   gchar *txt;
162   CORBA_char *rv;
163   gint intStartOffset, intEndOffset;
164   AtkText *text = get_text_from_servant (servant);
165
166   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
167
168   txt = atk_text_get_text_before_offset (text,
169                                          (gint) offset, (AtkTextBoundary) type,
170                                          &intStartOffset, &intEndOffset);
171
172   *startOffset = (CORBA_long) intStartOffset;
173   *endOffset = (CORBA_long) intEndOffset;
174
175   if (txt)
176     {
177       rv = CORBA_string_dup (txt);
178       g_free (txt);
179     }
180   else
181     rv = CORBA_string_dup ("");
182
183   return rv;
184 }
185
186
187 static CORBA_long
188 impl__get_caretOffset (PortableServer_Servant servant,
189                      CORBA_Environment *ev)
190 {
191   AtkText *text = get_text_from_servant (servant);
192
193   g_return_val_if_fail (text != NULL, -1);
194
195   return (CORBA_long) atk_text_get_caret_offset (text);
196 }
197
198
199 static CORBA_string
200 impl_getAttributes (PortableServer_Servant servant,
201                     const CORBA_long offset,
202                     CORBA_long * startOffset,
203                     CORBA_long * endOffset,
204                     CORBA_Environment *ev)
205 {
206   AtkText *text = get_text_from_servant (servant);
207
208   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
209
210   g_print ("getAttributes not yet implemented.\n");
211
212   return CORBA_string_dup ("");
213 }
214
215
216 static void 
217 impl_getCharacterExtents (PortableServer_Servant servant,
218                           const CORBA_long offset, CORBA_long * x,
219                           CORBA_long * y, CORBA_long * width,
220                           CORBA_long * height,
221                           const CORBA_short coordType,
222                           CORBA_Environment *ev)
223 {
224   AtkText *text = get_text_from_servant (servant);
225
226   g_return_if_fail (text != NULL);
227
228   /* FIXME: Casting a CORBA_long to a gint * is inherantly risky */
229   atk_text_get_character_extents (
230           text, (gint) offset,
231           (gint *) x, (gint *) y, (gint *) width, (gint *) height,
232           (AtkCoordType) coordType);
233 }
234
235
236 static CORBA_long
237 impl__get_characterCount (PortableServer_Servant servant,
238                           CORBA_Environment    *ev)
239 {
240   CORBA_long retval;
241   AtkText *text = get_text_from_servant (servant);
242
243   g_return_val_if_fail (text != NULL, 0);
244
245   retval = (CORBA_long) atk_text_get_character_count (text);
246
247   return retval;
248 }
249
250
251 static CORBA_long
252 impl_getOffsetAtPoint (PortableServer_Servant servant,
253                        const CORBA_long x, const CORBA_long y,
254                        const CORBA_short coordType,
255                        CORBA_Environment *ev)
256 {
257   AtkText *text = get_text_from_servant (servant);
258
259   g_return_val_if_fail (text != NULL, -1);
260
261   return (CORBA_long)
262     atk_text_get_offset_at_point (text,
263                                   (gint) x, (gint) y,
264                                   (AtkCoordType) coordType);
265 }
266
267
268 static CORBA_long
269 impl_getNSelections (PortableServer_Servant servant,
270                      CORBA_Environment *ev)
271 {
272   AtkText *text = get_text_from_servant (servant);
273
274   g_return_val_if_fail (text != NULL, 0);
275
276   return (CORBA_long) atk_text_get_n_selections (text);
277 }
278
279
280 static void 
281 impl_getSelection (PortableServer_Servant servant,
282                    const CORBA_long selectionNum,
283                    CORBA_long * startOffset, CORBA_long * endOffset,
284                    CORBA_Environment *ev)
285 {
286   AtkText *text = get_text_from_servant (servant);
287
288   g_return_if_fail (text != NULL);
289
290   atk_text_get_selection (text, (gint) selectionNum,
291                           (gint *) startOffset, (gint *) endOffset);
292 }
293
294
295 static CORBA_boolean
296 impl_addSelection (PortableServer_Servant servant,
297                    const CORBA_long startOffset,
298                    const CORBA_long endOffset,
299                    CORBA_Environment *ev)
300 {
301   AtkText *text = get_text_from_servant (servant);
302
303   g_return_val_if_fail (text != NULL, FALSE);
304
305   return (CORBA_boolean)
306     atk_text_add_selection (text,
307                             (gint) startOffset, (gint) endOffset);
308 }
309
310
311 static CORBA_boolean
312 impl_removeSelection (PortableServer_Servant servant,
313                       const CORBA_long selectionNum,
314                       CORBA_Environment *ev)
315 {
316   AtkText *text = get_text_from_servant (servant);
317
318   g_return_val_if_fail (text != NULL, FALSE);
319
320   return (CORBA_boolean)
321     atk_text_remove_selection (text, (gint) selectionNum);
322 }
323
324
325 static CORBA_boolean
326 impl_setSelection (PortableServer_Servant servant,
327                    const CORBA_long selectionNum,
328                    const CORBA_long startOffset,
329                    const CORBA_long endOffset,
330                    CORBA_Environment *ev)
331 {
332   AtkText *text = get_text_from_servant (servant);
333
334   g_return_val_if_fail (text != NULL, FALSE);
335
336   return (CORBA_boolean)
337     atk_text_set_selection (text,
338                             (gint) selectionNum, (gint) startOffset, (gint) endOffset);
339 }
340
341
342 static CORBA_boolean
343 impl_setCaretOffset (PortableServer_Servant servant,
344                      const CORBA_long value,
345                      CORBA_Environment *ev)
346 {
347   AtkText *text = get_text_from_servant (servant);
348
349   g_return_val_if_fail (text != NULL, FALSE);
350
351   return (CORBA_boolean)
352     atk_text_set_caret_offset (text, (gint) value);
353 }
354
355
356 static void 
357 impl_getRowColAtOffset (PortableServer_Servant servant,
358                         const CORBA_long offset, CORBA_long * row,
359                         CORBA_long * column, CORBA_Environment *ev)
360 {
361   AtkText *text = get_text_from_servant (servant);
362
363   g_return_if_fail (text != NULL);
364
365   g_print ("getRowColAtOffset not yet implemented\n");
366 }
367
368 static void
369 spi_text_class_init (SpiTextClass *klass)
370 {
371   POA_Accessibility_Text__epv *epv = &klass->epv;
372
373   /* Initialize epv table */
374
375   epv->getText = impl_getText;
376   epv->getTextAfterOffset = impl_getTextAfterOffset;
377   epv->getCharacterAtOffset = impl_getCharacterAtOffset;
378   epv->getTextAtOffset = impl_getTextAtOffset;
379   epv->getTextBeforeOffset = impl_getTextBeforeOffset;
380   epv->_get_caretOffset = impl__get_caretOffset;
381   epv->getAttributes = impl_getAttributes;
382   epv->getCharacterExtents = impl_getCharacterExtents;
383   epv->_get_characterCount = impl__get_characterCount;
384   epv->getOffsetAtPoint = impl_getOffsetAtPoint;
385   epv->getNSelections = impl_getNSelections;
386   epv->getSelection = impl_getSelection;
387   epv->addSelection = impl_addSelection;
388   epv->removeSelection = impl_removeSelection;
389   epv->setSelection = impl_setSelection;
390   epv->setCaretOffset = impl_setCaretOffset;
391 }
392
393 static void
394 spi_text_init (SpiText *text)
395 {
396 }
397
398 BONOBO_TYPE_FUNC_FULL (SpiText,
399                        Accessibility_Text,
400                        PARENT_TYPE,
401                        spi_text);
402
403 void
404 spi_text_construct (SpiText *text, AtkObject *obj)
405 {
406   spi_base_construct (SPI_BASE (text), G_OBJECT(obj));
407 }
408
409
410 SpiText *
411 spi_text_interface_new (AtkObject *obj)
412 {
413   SpiText *retval;
414
415   g_return_val_if_fail (ATK_IS_TEXT (obj), NULL);
416
417   retval = g_object_new (SPI_TEXT_TYPE, NULL);
418
419   spi_text_construct (retval, obj);
420
421   return retval;
422 }