Removed po directory from Makefile.am for now.
[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   if (!object)
40     {
41       return NULL;
42     }
43
44   return ATK_TEXT (object->atko);
45 }
46
47 static CORBA_string
48 impl_getText (PortableServer_Servant servant,
49               const CORBA_long       startOffset,
50               const CORBA_long       endOffset,
51               CORBA_Environment     *ev)
52 {
53   gchar *txt;
54   CORBA_string rv;
55   AtkText *text = get_text_from_servant (servant);
56
57   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
58   
59   txt = atk_text_get_text (text, (gint) startOffset, (gint) endOffset);
60   if (txt)
61     {
62       rv = CORBA_string_dup (txt);
63       g_free (txt);
64     }
65   else
66     rv = CORBA_string_dup ("");
67
68   return rv;
69 }
70
71
72 CORBA_string
73 impl_getTextAfterOffset (PortableServer_Servant servant,
74                          const CORBA_long offset,
75                          const
76                          Accessibility_TEXT_BOUNDARY_TYPE
77                          type, CORBA_long * startOffset,
78                          CORBA_long * endOffset,
79                          CORBA_Environment *ev)
80 {
81   gchar *txt;
82   CORBA_char *rv;
83   gint intStartOffset, intEndOffset;
84   AtkText *text = get_text_from_servant (servant);
85
86   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
87
88   txt = atk_text_get_text_after_offset (text,
89                                         (gint) offset, (AtkTextBoundary) type,
90                                         &intStartOffset, &intEndOffset);
91   *startOffset = (CORBA_long) intStartOffset;
92   *endOffset = (CORBA_long) intEndOffset;
93
94   if (txt)
95     {
96       rv = CORBA_string_dup (txt);
97       g_free (txt);
98     }
99   else
100     rv = CORBA_string_dup ("");
101
102   return rv;
103 }
104
105
106 static CORBA_string
107 impl_getTextAtOffset (PortableServer_Servant servant,
108                       const CORBA_long offset,
109                       const Accessibility_TEXT_BOUNDARY_TYPE type,
110                       CORBA_long * startOffset,
111                       CORBA_long * endOffset,
112                       CORBA_Environment *ev)
113 {
114   CORBA_char *txt;
115   CORBA_char *rv;
116   gint intStartOffset, intEndOffset;
117   AtkText *text = get_text_from_servant (servant);
118
119   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
120
121   txt = (CORBA_char *) atk_text_get_text_at_offset (
122           text,
123           (gint) offset, (AtkTextBoundary) type,
124           &intStartOffset, &intEndOffset);
125
126   *startOffset = (CORBA_long) intStartOffset;
127   *endOffset = (CORBA_long) intEndOffset;
128
129   if (txt)
130     {
131       rv = CORBA_string_dup (txt);
132       g_free (txt);
133     }
134   else
135     rv = CORBA_string_dup ("");
136
137   return rv;
138 }
139
140
141 static CORBA_unsigned_long
142 impl_getCharacterAtOffset (PortableServer_Servant servant,
143                            const CORBA_long offset,
144                            CORBA_Environment *ev)
145 {
146   AtkText *text = get_text_from_servant (servant);
147
148   g_return_val_if_fail (text != NULL, 0);
149
150   return (CORBA_unsigned_long)
151     atk_text_get_character_at_offset (text, (gint) offset);
152 }
153
154
155 static CORBA_string
156 impl_getTextBeforeOffset (PortableServer_Servant servant,
157                           const CORBA_long offset,
158                           const
159                           Accessibility_TEXT_BOUNDARY_TYPE
160                           type, CORBA_long * startOffset,
161                           CORBA_long * endOffset,
162                           CORBA_Environment *ev)
163 {
164   gchar *txt;
165   CORBA_char *rv;
166   gint intStartOffset, intEndOffset;
167   AtkText *text = get_text_from_servant (servant);
168
169   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
170
171   txt = atk_text_get_text_before_offset (text,
172                                          (gint) offset, (AtkTextBoundary) type,
173                                          &intStartOffset, &intEndOffset);
174
175   *startOffset = (CORBA_long) intStartOffset;
176   *endOffset = (CORBA_long) intEndOffset;
177
178   if (txt)
179     {
180       rv = CORBA_string_dup (txt);
181       g_free (txt);
182     }
183   else
184     rv = CORBA_string_dup ("");
185
186   return rv;
187 }
188
189
190 static CORBA_long
191 impl__get_caretOffset (PortableServer_Servant servant,
192                      CORBA_Environment *ev)
193 {
194   AtkText *text = get_text_from_servant (servant);
195
196   g_return_val_if_fail (text != NULL, -1);
197
198   return (CORBA_long) atk_text_get_caret_offset (text);
199 }
200
201
202 static CORBA_string
203 impl_getAttributes (PortableServer_Servant servant,
204                     const CORBA_long offset,
205                     CORBA_long * startOffset,
206                     CORBA_long * endOffset,
207                     CORBA_Environment *ev)
208 {
209   AtkText *text = get_text_from_servant (servant);
210
211   g_return_val_if_fail (text != NULL, CORBA_string_dup (""));
212
213   g_print ("getAttributes not yet implemented.\n");
214
215   return CORBA_string_dup ("");
216 }
217
218
219 static void 
220 impl_getCharacterExtents (PortableServer_Servant servant,
221                           const CORBA_long offset, CORBA_long * x,
222                           CORBA_long * y, CORBA_long * width,
223                           CORBA_long * height,
224                           const CORBA_short coordType,
225                           CORBA_Environment *ev)
226 {
227   AtkText *text = get_text_from_servant (servant);
228
229   g_return_if_fail (text != NULL);
230
231   /* FIXME: Casting a CORBA_long to a gint * is inherantly risky */
232   atk_text_get_character_extents (
233           text, (gint) offset,
234           (gint *) x, (gint *) y, (gint *) width, (gint *) height,
235           (AtkCoordType) coordType);
236 }
237
238
239 static CORBA_long
240 impl__get_characterCount (PortableServer_Servant servant,
241                           CORBA_Environment    *ev)
242 {
243   CORBA_long retval;
244   AtkText *text = get_text_from_servant (servant);
245
246   g_return_val_if_fail (text != NULL, 0);
247
248   retval = (CORBA_long) atk_text_get_character_count (text);
249
250   return retval;
251 }
252
253
254 static CORBA_long
255 impl_getOffsetAtPoint (PortableServer_Servant servant,
256                        const CORBA_long x, const CORBA_long y,
257                        const CORBA_short coordType,
258                        CORBA_Environment *ev)
259 {
260   AtkText *text = get_text_from_servant (servant);
261
262   g_return_val_if_fail (text != NULL, -1);
263
264   return (CORBA_long)
265     atk_text_get_offset_at_point (text,
266                                   (gint) x, (gint) y,
267                                   (AtkCoordType) coordType);
268 }
269
270
271 static CORBA_long
272 impl_getNSelections (PortableServer_Servant servant,
273                      CORBA_Environment *ev)
274 {
275   AtkText *text = get_text_from_servant (servant);
276
277   g_return_val_if_fail (text != NULL, 0);
278
279   return (CORBA_long) atk_text_get_n_selections (text);
280 }
281
282
283 static void 
284 impl_getSelection (PortableServer_Servant servant,
285                    const CORBA_long selectionNum,
286                    CORBA_long * startOffset, CORBA_long * endOffset,
287                    CORBA_Environment *ev)
288 {
289   AtkText *text = get_text_from_servant (servant);
290
291   g_return_if_fail (text != NULL);
292
293   atk_text_get_selection (text, (gint) selectionNum,
294                           (gint *) startOffset, (gint *) endOffset);
295 }
296
297
298 static CORBA_boolean
299 impl_addSelection (PortableServer_Servant servant,
300                    const CORBA_long startOffset,
301                    const CORBA_long endOffset,
302                    CORBA_Environment *ev)
303 {
304   AtkText *text = get_text_from_servant (servant);
305
306   g_return_val_if_fail (text != NULL, FALSE);
307
308   return (CORBA_boolean)
309     atk_text_add_selection (text,
310                             (gint) startOffset, (gint) endOffset);
311 }
312
313
314 static CORBA_boolean
315 impl_removeSelection (PortableServer_Servant servant,
316                       const CORBA_long selectionNum,
317                       CORBA_Environment *ev)
318 {
319   AtkText *text = get_text_from_servant (servant);
320
321   g_return_val_if_fail (text != NULL, FALSE);
322
323   return (CORBA_boolean)
324     atk_text_remove_selection (text, (gint) selectionNum);
325 }
326
327
328 static CORBA_boolean
329 impl_setSelection (PortableServer_Servant servant,
330                    const CORBA_long selectionNum,
331                    const CORBA_long startOffset,
332                    const CORBA_long endOffset,
333                    CORBA_Environment *ev)
334 {
335   AtkText *text = get_text_from_servant (servant);
336
337   g_return_val_if_fail (text != NULL, FALSE);
338
339   return (CORBA_boolean)
340     atk_text_set_selection (text,
341                             (gint) selectionNum, (gint) startOffset, (gint) endOffset);
342 }
343
344
345 static CORBA_boolean
346 impl_setCaretOffset (PortableServer_Servant servant,
347                      const CORBA_long value,
348                      CORBA_Environment *ev)
349 {
350   AtkText *text = get_text_from_servant (servant);
351
352   g_return_val_if_fail (text != NULL, FALSE);
353
354   return (CORBA_boolean)
355     atk_text_set_caret_offset (text, (gint) value);
356 }
357
358
359 static void 
360 impl_getRowColAtOffset (PortableServer_Servant servant,
361                         const CORBA_long offset, CORBA_long * row,
362                         CORBA_long * column, CORBA_Environment *ev)
363 {
364   AtkText *text = get_text_from_servant (servant);
365
366   g_return_if_fail (text != NULL);
367
368   g_print ("getRowColAtOffset not yet implemented\n");
369 }
370
371 static void
372 spi_text_class_init (SpiTextClass *klass)
373 {
374   POA_Accessibility_Text__epv *epv = &klass->epv;
375
376   /* Initialize epv table */
377
378   epv->getText = impl_getText;
379   epv->getTextAfterOffset = impl_getTextAfterOffset;
380   epv->getCharacterAtOffset = impl_getCharacterAtOffset;
381   epv->getTextAtOffset = impl_getTextAtOffset;
382   epv->getTextBeforeOffset = impl_getTextBeforeOffset;
383   epv->_get_caretOffset = impl__get_caretOffset;
384   epv->getAttributes = impl_getAttributes;
385   epv->getCharacterExtents = impl_getCharacterExtents;
386   epv->_get_characterCount = impl__get_characterCount;
387   epv->getOffsetAtPoint = impl_getOffsetAtPoint;
388   epv->getNSelections = impl_getNSelections;
389   epv->getSelection = impl_getSelection;
390   epv->addSelection = impl_addSelection;
391   epv->removeSelection = impl_removeSelection;
392   epv->setSelection = impl_setSelection;
393   epv->setCaretOffset = impl_setCaretOffset;
394 }
395
396 static void
397 spi_text_init (SpiText *text)
398 {
399 }
400
401 BONOBO_TYPE_FUNC_FULL (SpiText,
402                        Accessibility_Text,
403                        PARENT_TYPE,
404                        spi_text);
405
406 void
407 spi_text_construct (SpiText *text, AtkObject *obj)
408 {
409   spi_base_construct (SPI_BASE (text), obj);
410 }
411
412 SpiText *
413 spi_text_interface_new (AtkObject *obj)
414 {
415   SpiText *retval;
416
417   g_return_val_if_fail (ATK_IS_TEXT (obj), NULL);
418
419   retval = g_object_new (SPI_TEXT_TYPE, NULL);
420
421   spi_text_construct (retval, obj);
422
423   return retval;
424 }