Difference between revisions of "IDL Files and Cpp zh"

From Apache OpenOffice Wiki
Jump to: navigation, search
(通过IDL文件获取UNO信息)
(use ZH categories for ZH content)
 
(19 intermediate revisions by 2 users not shown)
Line 250: Line 250:
 
有多种方法可以获的UNO内的信息。
 
有多种方法可以获的UNO内的信息。
 
   
 
   
一个方法是使用OOoBasic和Bernard Marcelly的Xray工具(访问 [http://www.ooomacros.org/dev.php#101416 这里]). '''Problem''' : OooBasic doesn't show exact methods and properties.
+
一个方法是使用OOoBasic和Bernard Marcelly的Xray工具(访问 [http://www.ooomacros.org/dev.php#101416 这里]). '''Problem''' : OooBasic 不能精确的表现方法和属性。
 
   
 
   
An other way is to use web service at : [http://api.openoffice.org/docs/common/ref/index-files/index-1.html General OOoWEB Index]
+
另一方法就是使用[http://api.openoffice.org/docs/common/ref/index-files/index-1.html General OOoWEB Index]
  
We have provided examples in previous chapters (particularly [[Programming_OooWriter|programming OOoWriter in C++]]) and then normally reader has skills on the subject. We recall however the points (from Danny Brewer's post in OOoForum).
+
在前面的章节我们已经提供了相应的例子(特别[[Programming_OooWriter|programming OOoWriter in C++]])。相信读者已经有了这方面的经验。但是我们还是重复一下Danny Brewer在OOoForum的观点。
  
== [[CppSDKAuthors|Danny Brewer 's]] Point of View ==
+
== [[CppSDKAuthors|Danny Brewer]] 的观点 ==
  
Nonetheless, it is important to understand the concept of a "service" in the API docs. A service is just a way to group....
+
然而,在API文档中,理解“服务”的概念是非常重要的。服务是聚合一下内容的唯一方法:
# more services
+
#更多服务
# interfaces
+
#更多接口
The (1) more services part, of course, means that there are even more interfaces available at the original service.
+
当然,对(1)来说,服务中的服务也同样意味着这些原始服务包含更多的接口。
Let me give a different example.
+
让我来看一个不同的例子。
SpreadsheetDocument is a service.
+
SpreadsheetDocument是一个服务。
SpreadsheetDocument includes the service OfficeDocument. The correponding IDL file shows us this point :
+
SpreadsheetDocument包括服务OfficeDocument。对应的IDL文件如下:
 
<pre>
 
<pre>
 
//Listing 7 Interface Specification
 
//Listing 7 Interface Specification
Line 286: Line 286:
 
//-------------------------------------------------------------------------  
 
//-------------------------------------------------------------------------  
 
</pre>
 
</pre>
Therefore, the combined set of interfaces from both SpreadsheetDocument and OfficeDocument are available at a Spreadsheet document.
+
因此,对服务SpreadsheetDocument-一个电子表格文档,就集合了接口XSpreadsheetDocument和服务OfficeDocument中的接口。所以,尽管loadComponentFromURL返回的是一些接口,但是这些接口的都是基于一个基础的服务,这个服务就是SpreadsheetDocument。参考API文档,你就可以知道查询(query)哪些接口是有效的。
So, even though loadComponentFromURL returns some interface, that interface represents an underlying service which is a SpreadsheetDocument. By looking at the API docs, you can tell what valid interfaces you may query.
+
SpreadsheetDocument有服务(?)XSpreadsheetDocument。
SpreadsheetDocument has service XSpreadsheetDocument.  
+
OfficeDocument拥有服务(?)XPrintable和XStorable.  
OfficeDocument has service XPrintable and XStorable.
+
因为SpreadsheetDocument包含OfficeDocument,所以从SpreadsheetDocument查询接口XPrintable或者XStorable都是有效的。
Since SpreadsheetDocument includes OfficeDocument, it is therefore valid to queryInterface for either XPrintable or XStorable from a SpreadsheetDocument.
+
  
Now in the above discussion, substitute TextDocument for SpreadsheetDocument.
+
对上面的讨论,如果使用TextDocument取代SpreadsheetDocument。
How to tell what is the underlying service?
+
那么怎么知道什么是基础的服务呢?
  
There is no magic formula for that. Generally it is obvious. If you load a Spreadsheet, then you have the SpreadsheetDocument service.
+
这里没有捷径,通常的方法就是观察。如果你装载了一个电子表格,那么你就有了SpreadsheetDocument服务。
  
Similarly, if I create an object by its service name.....
+
相同的,如果通过服务的名称创建一个对象,例如:
 
oShape = oDoc.createUnoService( "com.sun.star.drawing.EllipseShape" )  
 
oShape = oDoc.createUnoService( "com.sun.star.drawing.EllipseShape" )  
then I know what service it is, because I gave the service name.
+
那么知道是什么服务可用了,因为我给出了服务的名字。
  
You can always tell what interfaces are available if you know the name of the service -- just by looking at the API docs. No guesswork. No need for Xray. It is an absolute science. The collection of valid interfaces are the interfaces of the service itself, and of all of the services which the service includes, recursively.
+
如果你知道服务的名字,那么就通常知道什么接口是可用的--通过查看API文档,不需要猜测,不需要Xray工具,这绝对科学。这些可用接口的集合就是,服务本身的接口和这个服务包含的所有服务,并对这个概念递归使用的到的所有接口。
  
In some cases, it is fairly clear what service you have.
+
在下来的案例中,都很明确的知道拥有什么服务。
  
If you call getCellRangeByName(), then you have a SheetCellRange. If you call getCellByPosition(), then you only have a SheetCell. Many of the same things are available on both a SheetCell and a SheetCellRange, but they are different animals. A SheetCellRange does represent a rectangular group of cells, while a SheetCell represents only a single cell. There are things you can do to a single cell that you cannot do to a group of cells together.
+
如果你可以调用getCellRangeByName(),那么你就有个SheetCellRange服务。如果可以调用getCellByPosition(),那么就只有SheetCell可以使用。对于SheetCell和SheetCellRange,有许多相同的内容可用,但是,他们却是不同的东西。 SheetCellRange指的是一个矩形区域内单元格的集合,而SheetCell只表示一个单元格。有些操作,对一个单元格可以,但把单元格集合在一起就不可以对之操作。
  
There are other issues as well. If you get a com.sun.star.table.CellRange from a table, it is just that. But if you get one from a spreadsheet, it is not only that, but it is really a com.sun.star.sheet.SheetCellRange. The SheetCellRange is merely an extended version of the CellRange. Which of the two services you have depends on whether you called getCellRangeByName() on a table, or on a spreadsheet.
+
对于这两个服务,还有其他的问题。如果这是从表格得到一个com.sun.star.table.CellRange,那么它就只是一个CellRange。但是,如果是从电子表格得到的,那么它就不只是一个CellRange,而是个真实的com.sun.star.sheet.SheetCellRange。 SheetCellRange只是CellRange的扩展版。而这两个服务通过对一个表格或者电子表格调用getCellRangeByName()得到。
  
Sometimes, especially in Writer, only the Developer's Guide helps clear up exactly what underlying service you get back from some other API method call. Writer is especially bad about that. But it is always the case that when you know what service you have, you can directly deduce, only from the API docs, exactly all of the interfaces that are valid to query. No guesswork. It is an exact science.
+
有时,特别对字处理,对某些API方法的调用,只有通过开发手册才能准确知道返回了哪些基础服务。这点对字处理特别严重。但是,通常来说,当你知道你有什么服务,那就可以通过API文档准确推断出查询哪些接口是有效的。不需要猜测,这是科学的方法。
  
== A concrete Example ==
+
== 一个具体的例子 ==
  
The IDL files are available with SDK in <OpenOffice.org1.1_SDK>/idl/ directory. Of course, the problem with this way is we only see the SDK's UNO IDL documentation, which can be slightly different from the Openoffice.org binary : remember the SDK comes months after a new version of Openoffice.org (it's not the case now). However, we will explain this way after just mentioning an other way : registry exploring. In fact, at the moment, I have no idea what kind of information can be derived from registery (see [[UNO_registery_and_Bootstrapping|UNO registery and Bootstrapping]])
+
对SDK,这些IDL文件可以在 <OpenOffice.org1.1_SDK>/idl/目录找到。当然,有个问题就是,这些SDK中的UNO IDL文件和二进制的OpenOffice.org库是有些区别的。这是因为SDK通常是在新版的OpenOffice.org发布后几个月才出现(现在不是这样)。然而,对于这个改变,我们将会在介绍另外一种方法:注册表浏览(registry exploring)后给予解释。事实上,此刻我并不清楚通过注册表可以得到什么样的信息。(具体请参考 [[UNO_registery_and_Bootstrapping|UNO registery and Bootstrapping]])
  
We want now to show an example : we start from a ::com::sun::star::document::OfficeDocument and then examine <OpenOffice.org1.1_SDK>/idl/com/sun/star/document/OfficeDocument.idl presented below, (with removing all comments) :
+
我们现在来看一个例子:我们首先来看::com::sun::star::document::OfficeDocument,查看<OpenOffice.org1.1_SDK>/idl/com/sun/star/document/OfficeDocument.idl(下面呈现的,并且去除了所有注释):
 
<pre>
 
<pre>
 
//Listing 8 Interface Specification
 
//Listing 8 Interface Specification
Line 334: Line 333:
 
};
 
};
 
</pre>
 
</pre>
Remember we have already tackled Interfaces in [[IDL_Files_and_Cpp#IDL_specification|this section]].
+
[[IDL_Files_and_Cpp_zh#IDL_.E8.AF.A6.E8.BF.B0|这个章节]],我们已经涉及了接口。
This is shown with the  [http://api.openoffice.org/docs/DevelopersGuide/FirstSteps/TextDocumentWithMethods.png Developper's Guide Figure]. If we now examine the
+
[http://api.openoffice.org/docs/DevelopersGuide/FirstSteps/TextDocumentWithMethods.png 开发手册图标]更明确的表示了这个结构。如果我们需要查看::com::sun::star::text::TextDocument,请参考<OpenOffice.org1.1_SDK>/idl/com/sun/star/text/TextDocument.idl。
::com::sun::star::text::TextDocument we find in  <OpenOffice.org1.1_SDK>/idl/com/sun/star/text/TextDocument.idl
+
 
<pre>
 
<pre>
 
service TextDocument
 
service TextDocument
Line 347: Line 345:
 
};
 
};
 
</pre>
 
</pre>
As can be seen TextDocument service and OfficeDocument service are together.
+
可以看到,TextDocument服务和OfficeDocument服务都在这里。
  
It's time to go deeper in our example and provide code with comments. For this purpose, we shall take code already tackled (see [[Programming_OooWriter#The_XTextCursor_Interface|the Corresponding Writer Code]]).
+
现在,我们深入这个例子,并且增加注释。为了这个目的,我们使用以前处理过的代码。(请查看[[Programming_OooWriter#The_XTextCursor_Interface|the Corresponding Writer Code]]).
 
<code>[cpp]  
 
<code>[cpp]  
 
//Listing 9 Writer Example
 
//Listing 9 Writer Example
Line 772: Line 770:
  
  
[[Category:Development]]
+
 
[[Category:Cpp]]
+
[[Category:中文]]
[[Category:Uno]]
+
[[Category:Tutorial]]
+

Latest revision as of 13:06, 28 March 2010

en

介绍性的文章,请参阅component tutorial.

服务(Services)和接口(Interfaces)

简介

参考 tutorial (in pdf) chapter 2. 对于 OpenOffice API, 服务是一个抽象的概念,它提供了一些特定的接口和特性/属性(properties/attributes)。就像开发手册(Developer's Guide)中所说:“属性是对象(an object)的数据。对象的通用接口(generic interface)可以使用属性的名字来实现对属性的访问,这些接口包括方法 getPropertyValue 和 setPropertyValue。” 这个定义也恰好区分了特性和属性的含义。服务是属性的集合,接口是方法的集合,而这些方法则提供了改变属性的途径(means)。

Danny Brewer的规则

服务类似Java对象。

1. 服务可以继承其他服务或者什么都不继承。(每个服务都递归的遵循这个规则。)

2. 服务可以含有(include)其他的服务或者什么都不包含。(每个服务都递归的遵循这个规则。)

3. 服务可以公开(实现)接口。

4. 服务拥有属性。

5. 接口可以继承其他的接口或者什么也不继承。(每个接口都递归的遵循这个规则。)

6. 接口可以含有(include)其他的接口或者什么都不包含。(每个接口都递归的遵循这个规则。)

7. 接口能够执行/实现(implement)方法。

8. 接口名称通常有一个大写X。

通过这些规则,就可以推断,方法通常(ALWAYS)在接口中,而属性通常在服务中。

另请参阅Introducing the OpenOffice.org_APIOOoBasic Introspection.

IDL 详述

接口使用接口定义语言(IDL)描述。UNO使用UNO-IDL作为其接口定义语言。IDL是描述性语言(不是编程语言),用来描述对象实现的接口。通过IDL,你可以定义接口的名字和每个特性、方法的名字等等。如果你创建了IDL文件,就可以使用IDL编译器创建C++头文件。

开始的例子,我们只是使用IDL直接描述一些简单的模块(modules)。

描述一个接口

我们选择的例子,接口XdrivingDirection (不包括在OOo):

// IDL
interface XdrivingDirection
	{
		void turnLeft();
		void turnRight();
	};

描述一个服务

继续使用上面的例子,下面就是对应的IDL描述。

// IDL
interface XdrivingDirection
	{
		void turnLeft();
		void turnRight();
	};
interface XaccelerationControl
	{
		void speedUp();
		void slowDown();
	};
service car
	{
		// 公开的接口:
		interface XdrivingDirection;
		interface XaccelerationControl;
		[attribute] float speed; 
		[attribute] float angle;
	};

在这里,我们看到一个float的类型。其它的IDL类型会在后面逐步引入。

描述一个模块

下来这个IDL描述,我们仍然使用相同的例子。

// IDL
module my_module
	{
	interface Xsomething
		{
			void methodone();
		};
	service my_service1
		{
			// exported interfaces:
			interface Xsomething;
		};
	interface XsomethingElse
		{
			void methodTwo();
			void methodThree();
		};
	service my_service2
		{
			// exported interfaces:
			interface XsomethingElse;
		};
};

深入IDL

IDL类型 :

char

16-bit unicode character type

boolean

boolean type; true and false

byte

8-bit ordinal integer type

short

signed 16-bit ordinal integer type

unsigned short

unsigned 16-bit ordinal integer type

long

signed 32-bit ordinal integer type

unsigned long

unsigned 32-bit integer type

hyper

signed 64-bit ordinal integer type

unsigned hyper

unsigned 64-bit ordinal integer type

float

processor dependent float

double

processor dependent double

string

string of 16-bit unicode characters

any

universal type, takes every fundamental or compound UNO type, similar to Variant in other environments or Object in Java

void

Indicates that a method does not provide a return value

方法可以有参数。参数列表中的每个参数必须以方向标志(direction flags)开始,也就是在一个知名的类型和参数标识符之前,必须有[in],[out]或者[inout]。下面给出的例子我们通常会在这个文档(the counter example)中遇到。

接口可以衍生自基接口。一个衍生接口可以声明自身的特性和方法。但是,在衍生的接口中,特性和方法不能被重定义。 一个继承接口的例子:

//IDL
interface animal {
	attribute long age;
};
interface dog : animal {
	attribute short leg;
}

也可以多重继承:

// IDL
interface dog : animal, friend {

服务的继承,将在下个章节进行介绍。

在OOo对象的术语中,特性和属性是有些区别的。而仅有的区别就是访问的方法不同。属性使用set/getPropertyValue(PropertyName,Any)访问,然而,特性是通过set/get(AttributeName)方法。. 在文档 shapes 中就会碰到属性处理。

通过IDL文件获取UNO信息

有多种方法可以获的UNO内的信息。

一个方法是使用OOoBasic和Bernard Marcelly的Xray工具(访问 这里). Problem : OooBasic 不能精确的表现方法和属性。

另一方法就是使用General OOoWEB Index

在前面的章节我们已经提供了相应的例子(特别programming OOoWriter in C++)。相信读者已经有了这方面的经验。但是我们还是重复一下Danny Brewer在OOoForum的观点。

Danny Brewer 的观点

然而,在API文档中,理解“服务”的概念是非常重要的。服务是聚合一下内容的唯一方法:

  1. 更多服务
  2. 更多接口

当然,对(1)来说,服务中的服务也同样意味着这些原始服务包含更多的接口。 让我来看一个不同的例子。 SpreadsheetDocument是一个服务。 SpreadsheetDocument包括服务OfficeDocument。对应的IDL文件如下:

//Listing 7 Interface Specification
//IDL
service SpreadsheetDocument
{
	//-------------------------------------------------------------------------

	/** common service for all types of documents.
	 */
	service com::sun::star::document::OfficeDocument;

	..........

	//-------------------------------------------------------------------------

	/** provides access to the collection of spreadsheets.
	 */
	interface com::sun::star::sheet::XSpreadsheetDocument;

	//------------------------------------------------------------------------- 

因此,对服务SpreadsheetDocument-一个电子表格文档,就集合了接口XSpreadsheetDocument和服务OfficeDocument中的接口。所以,尽管loadComponentFromURL返回的是一些接口,但是这些接口的都是基于一个基础的服务,这个服务就是SpreadsheetDocument。参考API文档,你就可以知道查询(query)哪些接口是有效的。 SpreadsheetDocument有服务(?)XSpreadsheetDocument。 OfficeDocument拥有服务(?)XPrintable和XStorable. 因为SpreadsheetDocument包含OfficeDocument,所以从SpreadsheetDocument查询接口XPrintable或者XStorable都是有效的。

对上面的讨论,如果使用TextDocument取代SpreadsheetDocument。 那么怎么知道什么是基础的服务呢?

这里没有捷径,通常的方法就是观察。如果你装载了一个电子表格,那么你就有了SpreadsheetDocument服务。

相同的,如果通过服务的名称创建一个对象,例如: oShape = oDoc.createUnoService( "com.sun.star.drawing.EllipseShape" ) 那么知道是什么服务可用了,因为我给出了服务的名字。

如果你知道服务的名字,那么就通常知道什么接口是可用的--通过查看API文档,不需要猜测,不需要Xray工具,这绝对科学。这些可用接口的集合就是,服务本身的接口和这个服务包含的所有服务,并对这个概念递归使用的到的所有接口。

在下来的案例中,都很明确的知道拥有什么服务。

如果你可以调用getCellRangeByName(),那么你就有个SheetCellRange服务。如果可以调用getCellByPosition(),那么就只有SheetCell可以使用。对于SheetCell和SheetCellRange,有许多相同的内容可用,但是,他们却是不同的东西。 SheetCellRange指的是一个矩形区域内单元格的集合,而SheetCell只表示一个单元格。有些操作,对一个单元格可以,但把单元格集合在一起就不可以对之操作。

对于这两个服务,还有其他的问题。如果这是从表格得到一个com.sun.star.table.CellRange,那么它就只是一个CellRange。但是,如果是从电子表格得到的,那么它就不只是一个CellRange,而是个真实的com.sun.star.sheet.SheetCellRange。 SheetCellRange只是CellRange的扩展版。而这两个服务通过对一个表格或者电子表格调用getCellRangeByName()得到。

有时,特别对字处理,对某些API方法的调用,只有通过开发手册才能准确知道返回了哪些基础服务。这点对字处理特别严重。但是,通常来说,当你知道你有什么服务,那就可以通过API文档准确推断出查询哪些接口是有效的。不需要猜测,这是科学的方法。

一个具体的例子

对SDK,这些IDL文件可以在 <OpenOffice.org1.1_SDK>/idl/目录找到。当然,有个问题就是,这些SDK中的UNO IDL文件和二进制的OpenOffice.org库是有些区别的。这是因为SDK通常是在新版的OpenOffice.org发布后几个月才出现(现在不是这样)。然而,对于这个改变,我们将会在介绍另外一种方法:注册表浏览(registry exploring)后给予解释。事实上,此刻我并不清楚通过注册表可以得到什么样的信息。(具体请参考 UNO registery and Bootstrapping)

我们现在来看一个例子:我们首先来看::com::sun::star::document::OfficeDocument,查看<OpenOffice.org1.1_SDK>/idl/com/sun/star/document/OfficeDocument.idl(下面呈现的,并且去除了所有注释):

//Listing 8 Interface Specification
//IDL
service OfficeDocument
{
	interface com::sun::star::frame::XModel;
    interface com::sun::star::util::XModifiable;
	interface com::sun::star::frame::XStorable;
	interface com::sun::star::view::XPrintable;
    [optional] interface XEventBroadcaster;
	[optional] interface XEventsSupplier;
    [optional] interface XDocumentInfoSupplier;
    [optional] interface XViewDataSupplier;
    [optional] interface com::sun::star::view::XPrintJobBroadcaster;
	[property, optional] boolean AutomaticControlFocus;
	[property, optional] boolean ApplyFormDesignMode;
};

这个章节,我们已经涉及了接口。 开发手册图标更明确的表示了这个结构。如果我们需要查看::com::sun::star::text::TextDocument,请参考<OpenOffice.org1.1_SDK>/idl/com/sun/star/text/TextDocument.idl。

service TextDocument
{
	service com::sun::star::document::OfficeDocument;
	interface com::sun::star::text::XTextDocument;
	interface com::sun::star::util::XSearchable;
	interface com::sun::star::util::XRefreshable;
	....
};

可以看到,TextDocument服务和OfficeDocument服务都在这里。

现在,我们深入这个例子,并且增加注释。为了这个目的,我们使用以前处理过的代码。(请查看the Corresponding Writer Code). [cpp] //Listing 9 Writer Example //C++

   OUString sDocUrl;
   osl::FileBase::getFileURLFromSystemPath(
                OUString::createFromAscii("/home/smoutou/Documents/demo.sxw"),sDocUrl);
   Reference< XComponent > xWriterComponent = 
                   rComponentLoader->loadComponentFromURL(

sDocUrl,

       OUString::createFromAscii("_blank"),
       0,
       Sequence < ::com::sun::star::beans::PropertyValue >());
  Reference < XTextDocument > xTextDocument (xWriterComponent,UNO_QUERY);

xWriterComponent gives us TextDocument service and then OfficeDocument service as already said. XTextDocument is an Interface of TextDocument service. XTextDocument.idl shows us the getText method but we don't know what new services are available. Only two ways to know that : Internet XText interfacegives the answer : the service is com.sun.star.text.Text. The other way is to use Reflection as described later in this section. [cpp] //Listing 10 Writer Example (continuation) //C++

   Reference< XText > xText = xTextDocument->getText();

We try to get a XTextCursor Interface [cpp] //Listing 11 Writer Example (continuation) //C++ Reference< XTextCursor> xTextCursor = xText->createTextCursor(); But how to explain this code ? Looking in XText.idl files only shows two methods :

//Listing 12 XText Interface IDL file
// IDL
module com {  module sun {  module star {  module text {  
interface XText: com::sun::star::text::XSimpleText
{ 
	void insertTextContent( [in] com::sun::star::text::XTextRange xRange, 
			 [in] com::sun::star::text::XTextContent xContent, 
			 [in] boolean bAbsorb ) 
			raises( com::sun::star::lang::IllegalArgumentException ); 
	void removeTextContent( [in] com::sun::star::text::XTextContent xContent ) 
			raises( com::sun::star::container::NoSuchElementException ); 
 
}; 
}; }; }; };

But if you have a closer look at the IDL file, you will see the inheritance from XSimpleText. Is the createTextCursor method here ? Yes, as you can see.

  
//Listing 13 XSimpleText Interface IDL file
// IDL
module com {  module sun {  module star {  module text {  
interface XSimpleText: com::sun::star::text::XTextRange
{ 
	com::sun::star::text::XTextCursor createTextCursor(); 
	com::sun::star::text::XTextCursor createTextCursorByRange( [in] com::sun::star::text::XTextRange aTextPosition ); 
    [oneway] void insertString( [in] com::sun::star::text::XTextRange xRange, 
			 [in] string aString, 
			 [in] boolean bAbsorb ); 
 	void insertControlCharacter( [in] com::sun::star::text::XTextRange xRange, 
			 [in] short nControlCharacter, 
			 [in] boolean bAbsorb ) 
			raises( com::sun::star::lang::IllegalArgumentException ); 
 
};
}; }; }; };

What are the services available here ? I have found them with a reflection tool :

******** Services : 8
com.sun.star.text.TextSortable
com.sun.star.style.ParagraphPropertiesComplex
com.sun.star.style.ParagraphPropertiesAsian
com.sun.star.style.ParagraphProperties
com.sun.star.style.CharacterPropertiesComplex
com.sun.star.style.CharacterPropertiesAsian
com.sun.star.style.CharacterProperties
com.sun.star.text.TextCursor

You can probably find all of them with internet but I think it will be a long task : a good reason for explaining reflection services later in this chapter.

Yes you have to walk through such ways to find what you can do when programming. Fortunately there is a shorter way : the SDK examples. Most of them are in Java but I have found many ideas with them.

IDL and C++

This problem is tackled in Developer's guide

Getting an interface in C++

In C++, you only have variables of an Interface type. One of the most often encountered problem in C++ programming is querying an interface. This problem occurs very rarely in OooBasic but very often in C++/Java. We can find an example in "SDK C++ language" and many other in Office UNO automation. As an example, we suppose we have to get a com::sun::star::document::officeDocument service. This is done in general by something like: [cpp] //Listing 2 C++ code to get a service //C++ Reference<com::sun::star::document::officeDocument> OfDoc = something_to_get_this_service(); or [cpp] Listing 3 using namespace to simplify the code //C++ using namespace com::sun::star::document; .... Reference<officeDocument> OfDoc = something_to_get_this_service(); and we want to query the XStorable interface to save our document. Three steps are involved in this query :

  1. Add your code's line as UNO's query

[cpp] //Listing 4 The UNO query in C++ code // C++ // query from com::sun::star::frame::XStorable interface Reference< XStorable > oToStore (OfDoc, UNO_QUERY);

  1. Add the hpp file in an include statement. Add the corresponding namespace in an using namespace statement. Including is done with :

[cpp] //Listing 5 The corresponding include statement //C++ #include <com/sun/star/frame/XStorable.hpp> and the corresponding namespace statement is : [cpp] //Listing 6 The corresponding namespace statement //C++ using namespace com::sun::star::frame;

  1. Add the corresponding type in the makefile as shown below in red :
# makefile
# added com.sun.star.frame.XStorable
TYPES := \
	com.sun.star.uno.XNamingService \
	....
	com.sun.star.uno.XAggregation \
	com.sun.star.frame.XStorable \
	com.sun.star.lang.XMain \ 
    ...
    com.sun.star.container.XHierarchicalNameAccess

Learn to realize these three steps : you can not program in C++ without encountering an interface's query. This problem of asking an interface is already tackled in chapters 3 and 4.

You have to learn to get interface in an other way.

Getting an Interface through XMultiServiceFactory

We have already encountered such a thing, so it should be quite familiar to us by now. But, because we have not discussed every step in detail, I want to illustrate how it works.

The previous section only deals with getting interfaces directely available because the corresponding service is available. For instance the code below deals with only one arrow of the Figure below and getting a Xmodel inteface starting from XtextDocument is a simple UNO_QUERY call (last line of the listing). [cpp] //Listing 19 Simple UNO_QUERY call // C++ OUString sDocUrl;

   osl::FileBase::getFileURLFromSystemPath(
                OUString::createFromAscii("/home/smoutou/Documents/demo.sxw"),sDocUrl);
   Reference< XComponent > xWriterComponent = rComponentLoader->loadComponentFromURL(

sDocUrl,

       OUString::createFromAscii("_blank"),
       0,
       Sequence < ::com::sun::star::beans::PropertyValue >());

   Reference < XTextDocument > xTextDocument (xWriterComponent,UNO_QUERY);

// Don't forget to include #include <com/sun/star/frame/XModel.hpp>

   Reference < XModel > xModel (xTextDocument,UNO_QUERY);

SimpleUNOQUERY.png

But how can you get an interface if the corresponding service is not directly available ?

First case : a method of your interface allows to get the new interface. It was the case of the code of Listing 11. In this case the corresponding hdl file is not necessary and I have not found a way to retrieve the new services available witout a reflection tool !

Second case : no interface's method can help you. You have to query a service before getting the interface. This can be done with two or three instructions style. I have already used both styles. Have a look into this listing :

[cpp] //Listing 20 Getting an Interface with XMultiServiceFacory // C++

   Reference < XTextDocument > xTextDocument (xWriterComponent,UNO_QUERY);
   Reference<XMultiServiceFactory> oDocMSF (xTextDocument,UNO_QUERY);

// Don't forget to add #include <com/sun/star/text/XTextTable.hpp>

   Reference <XTextTable> xTable (oDocMSF->createInstance(

OUString::createFromAscii("com.sun.star.text.TextTable")),UNO_QUERY);

which uses two instruction style. With three instructions style, the same code is shown in the listing above :

[cpp] //Listing 21 Inserting a Table in a OOoWriter Document // C++

   Reference<XMultiServiceFactory> oDocMSF (xTextDocument,UNO_QUERY);

Reference< XInterface > textTable = oDocMSF->createInstance( OUString::createFromAscii("com.sun.star.text.TextTable") ); // Don't forget to add #include <com/sun/star/text/XTextTable.hpp> Reference< XTextTable > xTable(textTable, UNO_QUERY);

Note that both method require to include (and then construct) the corresponding hpp file (here XtextTable.hpp) but not for the service because you get it with a method (named « createInstance » in this case). We give a schematic to summarize what we talked about.

ComplexUNOQUERY.png

Last question : is it possible to start from any XMultiServiceFactory interface ? The answer to this question is no. Have a look to the code below where I take rOfficeServiceManager instead of oDocMSF because it's a XmultiServiceFactory Interface too.

[cpp] //Listing 22 Wrong code // C++ // Reference<XMultiServiceFactory> oDocMSF (xTextDocument,UNO_QUERY); // using rOfficeServiceManager instead oDocMSF Reference< XInterface > textTable = rOfficeServiceManager->createInstance( OUString::createFromAscii("com.sun.star.text.TextTable") ); Reference< XTextTable > xTable(textTable, UNO_QUERY); if (!xTable.is()) { printf("Erreur creation XTextTable interface !\n"); return 1;

   }

This code compiles but gives a runtime error. This only means you cannot use every XMultiServiceFactory interface to get every Interface. If you are working with a document ask the corresponding XMultiServiceFactory interface. At this point only experience can help you.

Note before going further that a code used many times so far, can find an explanation even if here the XMultiServiceFactory interface is not obtained in a standard way :

[cpp] //Listing 23 Classic code // C++ int main( ) { //retrieve an instance of the remote service manager

   Reference< XMultiServiceFactory > rOfficeServiceManager;
   rOfficeServiceManager = ooConnect();
   

//get the desktop service using createInstance returns an XInterface type

   Reference< XInterface  > Desktop = rOfficeServiceManager->createInstance(
   OUString::createFromAscii( "com.sun.star.frame.Desktop" ));

//query for the XComponentLoader interface

   Reference< XComponentLoader > rComponentLoader (Desktop, UNO_QUERY);

Easy to retreive the three instructions style to get the XcomponentLoader, isn't it ?

An other more technical problem is the mapping : you have an IDL file, how is it compiled in C++ ? The next chapter tackle the subject : you can skip it in a first reading and go on into the chapter 1.5.

Mapping for Modules and Interfaces

For instance:

// IDL 
module M 
{ struct E { long L; 
  }; 
}; 

is mapped into: [cpp] // C++ namespace M { struct E

 { Long L; }; 

} and E can be referred outside of M as M::E. Alternatively, a C++ using statement for namespace M can be used so that E can be referred to simply as E: [cpp] // C++ using namespace M; E e;

 e.L = 3;

Core reflection service and its Interfaces

Many entry points for reflection are descibed in the SDK. We give again any of them here and show a way to use them with C++.

XIdlReflection interface

This interface is described here

The XIntrospection Interface

This interface is described here with code's example. Other code is given in Constructing Helpers section. See also Introducing the OpenOffice.org_API and OOoBasic Introspection.

Using Java Inspector

For an introduction of using Java with OOo, see Java and Eclipse tutorial. This point is not tackled here : we are only interested by using a Java component with C++.

It is possible in principle, to use the Java inspector tools from every programming languages because it's a component. The way to do that is very simple :

1°) Compile the java example in <OpenOffice.org_SDK>/examples/java/Inspector after adding SDK_AUTO_DEPLOYMENT = YES in the beginning of makefile.

2°) Create a OOoBasic example, for instance : [oobas] 'Listing 17 Simple OOoBasic example to call the Java Inspector

REM ***** BASIC ***** Sub JavaInspector o = createUnoService("org.OpenOffice.InstanceInspector") 'XRay.XRay o oReflection = createUnoService( "com.sun.star.reflection.CoreReflection" ) o.inspect(oReflection) End Sub If java is not properly installed when running this program a dialog box will print out. Follow the instructions to install it (you have to know where your virtual machine (JRE) is installed. We see below in Figure what is the result of this program.

Inspector3.png

Our problem is now to use the Java Inspector from C++. But I encounter a problem when I try to do that. Here is the way I choose :

  1. construct a urd file starting from IDL (in makefile)
  2. don't forget the registry of urd file in makefile
  3. add org.OpenOffice.XInstanceInspector in the TYPES macro in the makefile
  4. copy IDL file from <OpenOffice.org1.1_SDK>/examples/java/Inspector/XInstanceInspector.idl into <OpenOffice.org1.1_SDK>/idl/org/OpenOffice/ XInstanceInspector.idl
  5. add the #include <org/OpenOffice/XInstanceInspector.hpp> and using namespace org::OpenOffice;
  6. add this code

[cpp] // C++ Any toInspect; toInspect <<= rDesktop; Reference< XInstanceInspector > xinspect (rOfficeServiceManager->createInstance(

   OUString::createFromAscii( "org.OpenOffice.InstanceInspector" )),UNO_QUERY);
   xinspect->inspect(toInspect);

And it works like in OOoBasic. The corresponding Linux makefile is :

#Listing 18 Simple Makefile to compile a Java Inspector C++ call
# very simple makefile
HELPER = ReflectionHelper
CXXFILE = office_connect.cxx
OBJFILE = office_connect.o
OUTBIN = office_connect
OUT_COMP_INC = ../../../../LINUXexample.out/inc
OUT_COMP_OBJ = ../../../../LINUXexample.out/obj
OUT_COMP_BIN = ../../../../LINUXexample.out/bin
# added for Inspector
OUT_COMP_URD = $(OUT_COMP_BIN)
# end added
COMPONENT_RDB = $(OUT_COMP_BIN)/office_connect.rdb
CC_FLAGS = -c -O -fpic -fno-rtti
CC_DEFINES = -DUNX -DGCC -DLINUX -DCPPU_ENV=gcc3
PS = /
TYPES := \
	com.sun.star.uno.XNamingService \
	....
	com.sun.star.container.XHierarchicalNameAccess \
	org.OpenOffice.XInstanceInspector
# last line added

TYPESLIST = $(foreach t,$(TYPES),-T$(t))
GENHPPFILES = $(foreach t,$(TYPES),$(OUT_COMP_INC)/$(subst .,/,$(t)).hpp)

ALL : \
    ProUNOCppBindingExample

# added for Inspector
#building urd file
$(OUT_COMP_URD)/XInstanceInspector.urd : ../../../../idl/org/OpenOffice/XInstanceInspector.idl
	-mkdir -p $(OUT_COMP_URD)
	idlc -I. -I../../../../idl -O$(OUT_COMP_URD)  ../../../../idl/org/OpenOffice/XInstanceInspector.idl

# end added

#office_connectrc is provided with SDK
$(OUT_COMP_BIN)/office_connectrc : office_connectrc
	-mkdir -p $(OUT_COMP_BIN)
	cp office_connectrc $(OUT_COMP_BIN)/office_connectrc

$(COMPONENT_RDB) : $(OUT_COMP_URD)/XInstanceInspector.urd
	-mkdir -p $(OUT_COMP_BIN)
# added for Inspector
	regmerge $(COMPONENT_RDB) /UCR $(OUT_COMP_URD)/XInstanceInspector.urd
# end added
	regmerge $(COMPONENT_RDB) / "/usr/lib/openoffice/program/types.rdb"
	@echo --------------------------------------------------------------------------------
	@echo   Register necessary runtime components in $(COMPONENT_RDB)
	@echo --------------------------------------------------------------------------------
	regcomp -register -r $(COMPONENT_RDB) -c connector.uno.so
	regcomp -register -r $(COMPONENT_RDB) -c remotebridge.uno.so
	regcomp -register -r $(COMPONENT_RDB) -c bridgefac.uno.so
	regcomp -register -r $(COMPONENT_RDB) -c uuresolver.uno.so
#	@echo bla > $@

$(GENHPPFILES) :  $(subst /,$(PS),$(@D))
	mkdir -p $(subst /,$(PS),$(@D))
# modified for Inspector	cppumaker -Gc -BUCR -O$(OUT_COMP_INC) $(TYPESLIST) "/usr/lib/openoffice/program/types.rdb"
	cppumaker -Gc -BUCR -O$(OUT_COMP_INC) $(TYPESLIST) $(COMPONENT_RDB)

$(OUT_COMP_OBJ)/$(OBJFILE) : $(CXXFILE) $(GENHPPFILES) $(HELPER).hpp
	-mkdir -p $(subst /,$(PS),$(@D))
	gcc $(CC_FLAGS) $(CC_INCLUDES) -I. -I/usr/include -I$(OUT_COMP_INC)/examples \
	-I../../../../include -I$(OUT_COMP_INC) $(CC_DEFINES) -o$(OUT_COMP_OBJ)/$(OBJFILE) $(CXXFILE)

$(OUT_COMP_OBJ)/$(HELPER).o : $(HELPER).cxx $(HELPER).hpp
	-mkdir -p $(OUT_COMP_OBJ)/
	gcc $(CC_FLAGS) $(CC_INCLUDES) -I. -I/usr/include -I$(OUT_COMP_INC)/examples \
	-I../../../../include -I$(OUT_COMP_INC) $(CC_DEFINES) -o$(OUT_COMP_OBJ)/$(HELPER).o $(HELPER).cxx


$(OUT_COMP_BIN)/$(OUTBIN) : $(OUT_COMP_OBJ)/$(OBJFILE) $(OUT_COMP_OBJ)/$(HELPER).o
	-mkdir -p $(OUT_COMP_BIN)
	gcc -Wl -export-dynamic -L../../../../LINUXexample.out/lib -L../../../../linux/lib -L/usr/lib/openoffice/program \
	$(OUT_COMP_OBJ)/$(HELPER).o \
	-o$(OUT_COMP_BIN)/$(OUTBIN) $(OUT_COMP_OBJ)/$(OBJFILE) -lcppuhelpergcc3 -lcppu -lsalhelpergcc3 -lsal -lstlport_gcc


ProUNOCppBindingExample : $(COMPONENT_RDB) $(OUT_COMP_BIN)/$(OUTBIN) $(OUT_COMP_BIN)/office_connectrc
	@echo --------------------------------------------------------------------------------
	@echo Please use one of the following commands to execute the examples!
	@echo
	@echo make office_connect.run
	@echo --------------------------------------------------------------------------------

office_connect.run : $(OUT_COMP_BIN)/$(OUTBIN) $(OUT_COMP_BIN)/office_connectrc
	cd $(OUT_COMP_BIN) && $(OUTBIN)


See Also

Personal tools