6d6bf5de5486bfd2a03b07d588562aeed5d25c14
[platform/core/uifw/at-spi2-atk.git] / libspi / hyperlink.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /* hyperlink.c : implements the Hyperlink interface */
25
26 #include <config.h>
27 #include <stdio.h>
28 #include <libspi/hyperlink.h>
29 #include <libspi/action.h>
30 #include <libspi/accessible.h>
31
32 /* Static function declarations */
33
34 static void
35 spi_hyperlink_class_init (SpiHyperlinkClass *klass);
36 static void
37 spi_hyperlink_init (SpiHyperlink *hyperlink);
38 static CORBA_string
39 impl_getURI (PortableServer_Servant _servant,
40              const CORBA_long i, CORBA_Environment * ev);
41 static CORBA_short
42 impl__get_n_anchors (PortableServer_Servant _servant,
43                      CORBA_Environment * ev);
44 static CORBA_long
45 impl__get_startIndex (PortableServer_Servant _servant,
46                       CORBA_Environment * ev);
47 static CORBA_long
48 impl__get_endIndex (PortableServer_Servant _servant,
49                     CORBA_Environment * ev);
50 static Accessibility_Accessible
51 impl_getObject (PortableServer_Servant _servant,
52                 const CORBA_long i,
53                 CORBA_Environment * ev);
54 static CORBA_boolean
55 impl_isValid (PortableServer_Servant _servant,
56               CORBA_Environment * ev);
57
58
59 BONOBO_TYPE_FUNC_FULL (SpiHyperlink,
60                        Accessibility_Hyperlink,
61                        SPI_TYPE_BASE,
62                        spi_hyperlink)
63
64
65 static void
66 spi_hyperlink_class_init (SpiHyperlinkClass *klass)
67 {
68   POA_Accessibility_Hyperlink__epv *epv = &klass->epv;
69
70   /* Initialize epv table */
71
72   epv->_get_nAnchors = impl__get_n_anchors;
73   epv->getURI = impl_getURI;
74   epv->_get_startIndex = impl__get_startIndex;
75   epv->_get_endIndex = impl__get_endIndex;
76   epv->getObject = impl_getObject;
77   epv->isValid = impl_isValid;
78 }
79
80
81 static void
82 spi_hyperlink_init (SpiHyperlink *hyperlink)
83 {
84 }
85
86
87 SpiHyperlink *
88 spi_hyperlink_new (AtkHyperlink *object)
89 {
90   SpiHyperlink *new_hyperlink = g_object_new (
91           SPI_HYPERLINK_TYPE, NULL);
92
93   spi_base_construct (SPI_BASE (new_hyperlink), G_OBJECT(object));
94
95   /* 
96    * some hyperlinks are actionable... this is an ATK convention 
97    * that seems convenient though possibly poorly documented or unintended.
98    */
99   if (ATK_IS_ACTION (object))
100     {
101       /* 
102        * NOTE: we don't cast 'object' to ATK_OBJECT in the call to 
103        * spi_action_interface_new(), because of the above convention, 
104        * even though it means we may be violating the func prototype.
105        * See discussion in bugzilla bug #120659.
106        * !!!
107        * IMPORTANT! The 'AtkObject' typecast, instead of the cast macro,
108        * is used below, because 'object' may NOT really be an AtkObject;
109        * it will be cast back to a G_OBJECT inside spi_action_interface_new
110        * before use, so this is OK though very ropey coding style.
111        */
112
113         /* Don't aggregate action twice... if this is from AtkHyperlinkImpl */
114         if (!bonobo_object_query_interface (bonobo_object (new_hyperlink), "IDL:Accessibility/Action:1.0",
115                                             NULL))
116             
117             bonobo_object_add_interface (bonobo_object (new_hyperlink),
118                                          BONOBO_OBJECT (spi_action_interface_new ((AtkObject *) object)));
119     }
120   return new_hyperlink;
121 }
122
123 static AtkHyperlink *
124 get_hyperlink_from_servant (PortableServer_Servant servant)
125 {
126   SpiBase *object = SPI_BASE (bonobo_object_from_servant (servant));
127
128   g_return_val_if_fail (object != NULL, NULL);
129   if (ATK_IS_HYPERLINK(object->gobj)) 
130   {
131       return ATK_HYPERLINK (object->gobj);
132   }
133   else if (ATK_IS_HYPERLINK_IMPL(object->gobj))
134   {
135       return atk_hyperlink_impl_get_hyperlink (ATK_HYPERLINK_IMPL (object->gobj));
136   }
137   else
138       return NULL;
139 }
140
141
142 static CORBA_short
143 impl__get_n_anchors (PortableServer_Servant servant,
144                      CORBA_Environment     *ev)
145 {
146   AtkHyperlink *link = get_hyperlink_from_servant (servant);
147
148   g_return_val_if_fail (link != NULL, 0);
149
150   return atk_hyperlink_get_n_anchors (link);
151 }
152
153
154 static CORBA_long
155 impl__get_startIndex (PortableServer_Servant servant,
156                       CORBA_Environment     *ev)
157 {
158   AtkHyperlink *link = get_hyperlink_from_servant (servant);
159
160   g_return_val_if_fail (link != NULL, -1);
161
162   return atk_hyperlink_get_start_index (link);
163 }
164
165
166 static CORBA_long
167 impl__get_endIndex (PortableServer_Servant servant,
168                     CORBA_Environment     *ev)
169 {
170   AtkHyperlink *link = get_hyperlink_from_servant (servant);
171
172   g_return_val_if_fail (link != NULL, -1);
173
174   return atk_hyperlink_get_end_index (link);
175 }
176
177
178 static CORBA_string
179 impl_getURI (PortableServer_Servant servant,
180              const CORBA_long i, CORBA_Environment *ev)
181 {
182   gchar *uri;
183   CORBA_char *rv;
184   AtkHyperlink *link = get_hyperlink_from_servant (servant);
185
186   g_return_val_if_fail (link != NULL, CORBA_string_dup (""));
187
188   uri = atk_hyperlink_get_uri (link, i);
189   if (uri)
190     {
191       rv = CORBA_string_dup (uri);
192       g_free (uri);
193     }
194   else
195     rv = CORBA_string_dup ("");
196
197   return rv;
198
199
200
201 static Accessibility_Accessible
202 impl_getObject (PortableServer_Servant servant,
203                 const CORBA_long       i,
204                 CORBA_Environment     *ev)
205 {
206   AtkObject    *atk_object;
207   AtkHyperlink *link = get_hyperlink_from_servant (servant);
208
209   g_return_val_if_fail (link != NULL, CORBA_OBJECT_NIL);
210
211   atk_object = atk_hyperlink_get_object (link, i);
212
213   return spi_accessible_new_return (atk_object, FALSE, ev);
214 }
215
216
217 static CORBA_boolean
218 impl_isValid (PortableServer_Servant servant,
219               CORBA_Environment     *ev)
220 {
221   AtkHyperlink *link = get_hyperlink_from_servant (servant);
222
223   g_return_val_if_fail (link != NULL, TRUE);
224
225   return atk_hyperlink_is_valid (link);
226 }
227