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