calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / pyuno / source / module / pyuno_type.cxx
bloba04b4e26d78ccbc676404bd08064966a7c5f3dcb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include "pyuno_impl.hxx"
21 #include <o3tl/any.hxx>
23 #include <typelib/typedescription.hxx>
26 using com::sun::star::uno::TypeClass;
27 using com::sun::star::uno::Type;
28 using com::sun::star::uno::RuntimeException;
29 using com::sun::star::uno::Any;
30 using com::sun::star::uno::TypeDescription;
32 namespace pyuno
34 const char *typeClassToString( TypeClass t )
36 const char * ret = nullptr;
37 switch (t)
39 case css::uno::TypeClass_VOID:
40 ret = "VOID"; break;
41 case css::uno::TypeClass_CHAR:
42 ret = "CHAR"; break;
43 case css::uno::TypeClass_BOOLEAN:
44 ret = "BOOLEAN"; break;
45 case css::uno::TypeClass_BYTE:
46 ret = "BYTE"; break;
47 case css::uno::TypeClass_SHORT:
48 ret = "SHORT"; break;
49 case css::uno::TypeClass_UNSIGNED_SHORT:
50 ret = "UNSIGNED_SHORT"; break;
51 case css::uno::TypeClass_LONG:
52 ret = "LONG"; break;
53 case css::uno::TypeClass_UNSIGNED_LONG:
54 ret = "UNSIGNED_LONG"; break;
55 case css::uno::TypeClass_HYPER:
56 ret = "HYPER"; break;
57 case css::uno::TypeClass_UNSIGNED_HYPER:
58 ret = "UNSIGNED_HYPER"; break;
59 case css::uno::TypeClass_FLOAT:
60 ret = "FLOAT"; break;
61 case css::uno::TypeClass_DOUBLE:
62 ret = "DOUBLE"; break;
63 case css::uno::TypeClass_STRING:
64 ret = "STRING"; break;
65 case css::uno::TypeClass_TYPE:
66 ret = "TYPE"; break;
67 case css::uno::TypeClass_ANY:
68 ret = "ANY";break;
69 case css::uno::TypeClass_ENUM:
70 ret = "ENUM";break;
71 case css::uno::TypeClass_STRUCT:
72 ret = "STRUCT"; break;
73 case css::uno::TypeClass_EXCEPTION:
74 ret = "EXCEPTION"; break;
75 case css::uno::TypeClass_SEQUENCE:
76 ret = "SEQUENCE"; break;
77 case css::uno::TypeClass_INTERFACE:
78 ret = "INTERFACE"; break;
79 case css::uno::TypeClass_TYPEDEF:
80 ret = "TYPEDEF"; break;
81 case css::uno::TypeClass_SERVICE:
82 ret = "SERVICE"; break;
83 case css::uno::TypeClass_MODULE:
84 ret = "MODULE"; break;
85 case css::uno::TypeClass_INTERFACE_METHOD:
86 ret = "INTERFACE_METHOD"; break;
87 case css::uno::TypeClass_INTERFACE_ATTRIBUTE:
88 ret = "INTERFACE_ATTRIBUTE"; break;
89 default:
90 ret = "UNKNOWN"; break;
92 return ret;
95 static PyRef getClass( const Runtime & r , const char * name)
97 return PyRef( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), name ) );
100 PyRef getTypeClass( const Runtime & r )
102 return getClass( r , "Type" );
105 PyRef getEnumClass( const Runtime & r )
107 return getClass( r , "Enum" );
110 PyRef getCharClass( const Runtime & r )
112 return getClass( r , "Char" );
115 PyRef getByteSequenceClass( const Runtime & r )
117 return getClass( r , "ByteSequence" );
120 PyRef getAnyClass( const Runtime & r )
122 return getClass( r , "Any" );
126 sal_Unicode PyChar2Unicode( PyObject *obj )
128 PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE );
129 if( ! PyUnicode_Check( value.get() ) )
131 throw RuntimeException(
132 "attribute value of uno.Char is not a unicode string" );
135 if( PyUnicode_GetLength( value.get() ) < 1 )
137 throw RuntimeException(
138 "uno.Char contains an empty unicode string");
141 sal_Unicode c = static_cast<sal_Unicode>(PyUnicode_ReadChar( value.get(), 0));
142 return c;
145 Any PyEnum2Enum( PyObject *obj )
147 Any ret;
148 PyRef typeName( PyObject_GetAttrString( obj,"typeName" ), SAL_NO_ACQUIRE);
149 PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE);
150 if( !PyUnicode_Check( typeName.get() ) || ! PyUnicode_Check( value.get() ) )
152 throw RuntimeException(
153 "attributes typeName and/or value of uno.Enum are not strings" );
156 OUString strTypeName( OUString::createFromAscii( PyUnicode_AsUTF8( typeName.get() ) ) );
157 char const *stringValue = PyUnicode_AsUTF8( value.get() );
159 TypeDescription desc( strTypeName );
160 if( !desc.is() )
162 throw RuntimeException( "enum " + OUString::createFromAscii( PyUnicode_AsUTF8(typeName.get()) ) + " is unknown" );
165 if(desc.get()->eTypeClass != typelib_TypeClass_ENUM )
167 throw RuntimeException( "pyuno.checkEnum: " + strTypeName + "is a " +
168 OUString::createFromAscii(typeClassToString( static_cast<css::uno::TypeClass>(desc.get()->eTypeClass))) +
169 ", expected ENUM" );
172 desc.makeComplete();
174 typelib_EnumTypeDescription *pEnumDesc = reinterpret_cast<typelib_EnumTypeDescription*>(desc.get());
175 int i = 0;
176 for( i = 0; i < pEnumDesc->nEnumValues ; i ++ )
178 if( OUString::unacquired(&pEnumDesc->ppEnumNames[i]).equalsAscii( stringValue ) )
180 break;
183 if( i == pEnumDesc->nEnumValues )
185 throw RuntimeException( "value " + OUString::createFromAscii( stringValue ) +
186 "is unknown in enum " +
187 OUString::createFromAscii( PyUnicode_AsUTF8( typeName.get() ) ) );
189 ret = Any( &pEnumDesc->pEnumValues[i], desc.get()->pWeakRef );
191 return ret;
195 Type PyType2Type( PyObject * o )
197 PyRef pyName( PyObject_GetAttrString( o, "typeName" ), SAL_NO_ACQUIRE);
198 if( !PyUnicode_Check( pyName.get() ) )
200 throw RuntimeException(
201 "type object does not have typeName property" );
204 PyRef pyTC( PyObject_GetAttrString( o, "typeClass" ), SAL_NO_ACQUIRE );
205 Any enumValue = PyEnum2Enum( pyTC.get() );
207 OUString name( OUString::createFromAscii( PyUnicode_AsUTF8( pyName.get() ) ) );
208 TypeDescription desc( name );
209 if( ! desc.is() )
211 throw RuntimeException( "type " + name + " is unknown" );
213 css::uno::TypeClass tc = *o3tl::doAccess<css::uno::TypeClass>(enumValue);
214 if( static_cast<css::uno::TypeClass>(desc.get()->eTypeClass) != tc )
216 throw RuntimeException( "pyuno.checkType: " + name + " is a " +
217 OUString::createFromAscii( typeClassToString( static_cast<TypeClass>(desc.get()->eTypeClass)) ) +
218 ", but type got construct with typeclass " +
219 OUString::createFromAscii( typeClassToString( tc ) ) );
221 return desc.get()->pWeakRef;
224 static PyObject* callCtor( const Runtime &r , const char * clazz, const PyRef & args )
226 PyRef code( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), clazz ) );
227 if( ! code.is() )
229 OString buf = OString::Concat("couldn't access uno.") + clazz;
230 PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
231 return nullptr;
233 PyRef instance( PyObject_CallObject( code.get(), args.get() ), SAL_NO_ACQUIRE);
234 Py_XINCREF( instance.get() );
235 return instance.get();
240 PyObject *PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r )
242 PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE, NOT_NULL );
243 PyTuple_SetItem( args.get() , 0 , PyUnicode_FromString( enumBase ) );
244 PyTuple_SetItem( args.get() , 1 , PyUnicode_FromString( enumValue ) );
246 return callCtor( r, "Enum" , args );
250 PyObject* PyUNO_Type_new (const char *typeName , TypeClass t , const Runtime &r )
252 // retrieve type object
253 PyRef args(PyTuple_New( 2 ), SAL_NO_ACQUIRE, NOT_NULL);
255 PyTuple_SetItem( args.get() , 0 , PyUnicode_FromString( typeName ) );
256 PyObject *typeClass = PyUNO_Enum_new( "com.sun.star.uno.TypeClass" , typeClassToString(t), r );
257 if( ! typeClass )
258 return nullptr;
259 PyTuple_SetItem( args.get() , 1 , typeClass);
261 return callCtor( r, "Type" , args );
264 PyObject* PyUNO_char_new ( sal_Unicode val , const Runtime &r )
266 // retrieve type object
267 PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE, NOT_NULL );
268 static_assert(sizeof(sal_Unicode) == sizeof(Py_UCS2), "unexpected size");
269 PyTuple_SetItem( args.get() , 0 , PyUnicode_FromKindAndData( PyUnicode_2BYTE_KIND, &val ,1) );
271 return callCtor( r, "Char" , args );
274 PyObject *PyUNO_ByteSequence_new(
275 const css::uno::Sequence< sal_Int8 > &byteSequence, const Runtime &r )
277 PyRef str(
278 PyBytes_FromStringAndSize( reinterpret_cast<char const *>(byteSequence.getConstArray()), byteSequence.getLength()),
279 SAL_NO_ACQUIRE );
280 PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE, NOT_NULL );
281 PyTuple_SetItem( args.get() , 0 , str.getAcquired() );
282 return callCtor( r, "ByteSequence" , args );
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */