xref: /trunk/main/vcl/unx/gtk/a11y/atkhypertext.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 
31 #include "atkwrapper.hxx"
32 
33 #include <com/sun/star/accessibility/XAccessibleHypertext.hpp>
34 
35 #include <stdio.h>
36 
37 using namespace ::com::sun::star;
38 
39 
40 // ---------------------- AtkHyperlink ----------------------
41 
42 typedef struct {
43     AtkHyperlink atk_hyper_link;
44 
45     uno::Reference< accessibility::XAccessibleHyperlink > xLink;
46 } HyperLink;
47 
48 static uno::Reference< accessibility::XAccessibleHyperlink >
49     getHyperlink( AtkHyperlink *pHyperlink )
50 {
51     HyperLink *pLink = (HyperLink *) pHyperlink;
52     return pLink->xLink;
53 }
54 
55 static GObjectClass *hyper_parent_class = NULL;
56 
57 extern "C" {
58 
59 static void
60 hyper_link_finalize (GObject *obj)
61 {
62     HyperLink *hl = (HyperLink *) obj;
63     hl->xLink.clear();
64     hyper_parent_class->finalize (obj);
65 }
66 
67 static gchar *
68 hyper_link_get_uri( AtkHyperlink *pLink,
69                     gint          i )
70 {
71     try {
72         uno::Any aAny = getHyperlink( pLink )->getAccessibleActionObject( i );
73         rtl::OUString aUri = aAny.get< rtl::OUString > ();
74         return OUStringToGChar(aUri);
75     }
76     catch(const uno::Exception& e) {
77         g_warning( "Exception in hyper_link_get_uri" );
78     }
79     return NULL;
80 }
81 
82 static AtkObject *
83 hyper_link_get_object( AtkHyperlink *pLink,
84                        gint          i)
85 {
86     try {
87         uno::Any aAny = getHyperlink( pLink )->getAccessibleActionObject( i );
88         uno::Reference< accessibility::XAccessible > xObj( aAny, uno::UNO_QUERY_THROW );
89         return atk_object_wrapper_ref( xObj );
90     }
91     catch(const uno::Exception& e) {
92         g_warning( "Exception in hyper_link_get_object" );
93     }
94     return NULL;
95 }
96 static gint
97 hyper_link_get_end_index( AtkHyperlink *pLink )
98 {
99     try {
100         return getHyperlink( pLink )->getEndIndex();
101     }
102     catch(const uno::Exception& e) {
103     }
104     return -1;
105 }
106 static gint
107 hyper_link_get_start_index( AtkHyperlink *pLink )
108 {
109     try {
110         return getHyperlink( pLink )->getStartIndex();
111     }
112     catch(const uno::Exception& e) {
113     }
114     return -1;
115 }
116 static gboolean
117 hyper_link_is_valid( AtkHyperlink *pLink )
118 {
119     try {
120         return getHyperlink( pLink )->isValid();
121     }
122     catch(const uno::Exception& e) {
123     }
124     return FALSE;
125 }
126 static gint
127 hyper_link_get_n_anchors( AtkHyperlink *pLink )
128 {
129     try {
130         return getHyperlink( pLink )->getAccessibleActionCount();
131     }
132     catch(const uno::Exception& e) {
133     }
134     return 0;
135 }
136 
137 static guint
138 hyper_link_link_state( AtkHyperlink * )
139 {
140     g_warning( "FIXME: hyper_link_link_state unimplemented" );
141     return 0;
142 }
143 static gboolean
144 hyper_link_is_selected_link( AtkHyperlink * )
145 {
146     g_warning( "FIXME: hyper_link_is_selected_link unimplemented" );
147     return FALSE;
148 }
149 
150 static void
151 hyper_link_class_init (AtkHyperlinkClass *klass)
152 {
153     GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
154 
155     gobject_class->finalize = hyper_link_finalize;
156 
157     hyper_parent_class = (GObjectClass *)g_type_class_peek_parent (klass);
158 
159     klass->get_uri = hyper_link_get_uri;
160     klass->get_object = hyper_link_get_object;
161     klass->get_end_index = hyper_link_get_end_index;
162     klass->get_start_index = hyper_link_get_start_index;
163     klass->is_valid = hyper_link_is_valid;
164     klass->get_n_anchors = hyper_link_get_n_anchors;
165     klass->link_state = hyper_link_link_state;
166     klass->is_selected_link = hyper_link_is_selected_link;
167 }
168 
169 static GType
170 hyper_link_get_type (void)
171 {
172     static GType type = 0;
173 
174     if (!type) {
175         static const GTypeInfo tinfo = {
176             sizeof (AtkHyperlinkClass),
177             NULL,               /* base init */
178             NULL,               /* base finalize */
179             (GClassInitFunc) hyper_link_class_init,
180             NULL,               /* class finalize */
181             NULL,               /* class data */
182             sizeof (HyperLink), /* instance size */
183             0,                  /* nb preallocs */
184             NULL,               /* instance init */
185             NULL                /* value table */
186         };
187 
188         static const GInterfaceInfo atk_action_info = {
189             (GInterfaceInitFunc) actionIfaceInit,
190             (GInterfaceFinalizeFunc) NULL,
191             NULL
192         };
193 
194         type = g_type_register_static (ATK_TYPE_HYPERLINK,
195                                        "OOoAtkObjHyperLink", &tinfo,
196                                        (GTypeFlags)0);
197         g_type_add_interface_static (type, ATK_TYPE_ACTION,
198                                      &atk_action_info);
199     }
200 
201     return type;
202 }
203 
204 // ---------------------- AtkHyperText ----------------------
205 
206 static accessibility::XAccessibleHypertext*
207     getHypertext( AtkHypertext *pHypertext ) throw (uno::RuntimeException)
208 {
209     AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pHypertext );
210     if( pWrap )
211     {
212         if( !pWrap->mpHypertext && pWrap->mpContext )
213         {
214             uno::Any any = pWrap->mpContext->queryInterface( accessibility::XAccessibleHypertext::static_type(NULL) );
215             pWrap->mpHypertext = reinterpret_cast< accessibility::XAccessibleHypertext * > (any.pReserved);
216             pWrap->mpHypertext->acquire();
217         }
218 
219         return pWrap->mpHypertext;
220     }
221 
222     return NULL;
223 }
224 
225 
226 static AtkHyperlink *
227 hypertext_get_link( AtkHypertext *hypertext,
228                     gint          link_index)
229 {
230     try {
231         accessibility::XAccessibleHypertext* pHypertext = getHypertext( hypertext );
232         if( pHypertext )
233         {
234             HyperLink *pLink = (HyperLink *)g_object_new( hyper_link_get_type(), NULL );
235             pLink->xLink = pHypertext->getHyperLink( link_index );
236             if( !pLink->xLink.is() ) {
237                 g_object_unref( G_OBJECT( pLink ) );
238                 pLink = NULL;
239             }
240             return ATK_HYPERLINK( pLink );
241         }
242     }
243     catch(const uno::Exception& e) {
244         g_warning( "Exception in getHyperLink()" );
245     }
246 
247     return NULL;
248 }
249 
250 static gint
251 hypertext_get_n_links( AtkHypertext *hypertext )
252 {
253     try {
254         accessibility::XAccessibleHypertext* pHypertext = getHypertext( hypertext );
255         if( pHypertext )
256             return pHypertext->getHyperLinkCount();
257     }
258     catch(const uno::Exception& e) {
259         g_warning( "Exception in getHyperLinkCount()" );
260     }
261 
262     return 0;
263 }
264 
265 static gint
266 hypertext_get_link_index( AtkHypertext *hypertext,
267                           gint          index)
268 {
269     try {
270         accessibility::XAccessibleHypertext* pHypertext = getHypertext( hypertext );
271         if( pHypertext )
272             return pHypertext->getHyperLinkIndex( index );
273     }
274     catch(const uno::Exception& e) {
275         g_warning( "Exception in getHyperLinkIndex()" );
276     }
277 
278     return 0;
279 }
280 
281 } // extern "C"
282 
283 void
284 hypertextIfaceInit (AtkHypertextIface *iface)
285 {
286   g_return_if_fail (iface != NULL);
287 
288   iface->get_link = hypertext_get_link;
289   iface->get_n_links = hypertext_get_n_links;
290   iface->get_link_index = hypertext_get_link_index;
291 }
292