Difference between revisions of "Programming OOoDraw and OOoImpress"
SergeMoutou (Talk | contribs) m (→Inserting a Shape) |
m (→Inserting a bitmap) |
||
(29 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{NeedsRework|EN}} | ||
==Introduction== | ==Introduction== | ||
− | OOoDraw is my favorite application of OpenOffice | + | OOoDraw is my favorite application of OpenOffice suite. Therefore I think I will spend a lot of time with this chapter. I start writing this chapter with the Help of Christian's post in OOoForum (Code snippets) |
[http://www.oooforum.org/forum/viewtopic.phtml?t=11128 Working with a Presentation Document in C++] | [http://www.oooforum.org/forum/viewtopic.phtml?t=11128 Working with a Presentation Document in C++] | ||
− | In this chapter, we use again the starting code described [[UNO_automation_with_a_binary_%28executable%29 | here ]]. It consists of a subprogram “ooConnect()” | + | In this chapter, we use again the starting code described [[UNO_automation_with_a_binary_%28executable%29 | here ]]. It consists of a subprogram “ooConnect()” |
− | + | {{:OOConnect}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | The main function is examined now. Working with this code consists of adding new code in the main (see the comment). We give again the main program slightly modified. | |
− | + | {{Warn|To fit OOoDraw requirements you have to replace the string "private:factory/scalc" with "private:factory/sdraw".}} | |
− | + | ||
− | + | {{:MainOOConnect}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | } | + | |
− | + | ||
− | + | ||
− | + | ||
=Finding the drawing Slide= | =Finding the drawing Slide= | ||
− | Bear in mind that if OOoSheet opens with three sheets by default, | + | Bear in mind that if OOoSheet opens with three sheets by default, {{AOo}} Draw opens with only one page/slide (index 0). |
== An existing Slide == | == An existing Slide == | ||
If only the index of the slide is known, have look at <idl>com.sun.star.drawing.XDrawPagesSupplier</idl> and <idl>com.sun.star.container.XIndexAccess</idl> interfaces : | If only the index of the slide is known, have look at <idl>com.sun.star.drawing.XDrawPagesSupplier</idl> and <idl>com.sun.star.container.XIndexAccess</idl> interfaces : | ||
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 2 Index access to a draw Slide | //Listing 2 Index access to a draw Slide | ||
// C++ | // C++ | ||
Line 62: | Line 35: | ||
Any DrawPage = rPageIndexAccess->getByIndex(0); | Any DrawPage = rPageIndexAccess->getByIndex(0); | ||
− | </ | + | </syntaxhighlight> |
If you want to know how many pages belong to your document, you can reach it from rPageIndexAccess object with : | If you want to know how many pages belong to your document, you can reach it from rPageIndexAccess object with : | ||
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 3 How many Slides ? | //Listing 3 How many Slides ? | ||
// C++ | // C++ | ||
rPageIndexAccess->getCount(); | rPageIndexAccess->getCount(); | ||
− | </ | + | </syntaxhighlight> |
If you only know the page name (see <idl>com.sun.star.container.XNameAccess</idl> interface): | If you only know the page name (see <idl>com.sun.star.container.XNameAccess</idl> interface): | ||
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 4 Slide Name Access | //Listing 4 Slide Name Access | ||
//C++ | //C++ | ||
Line 84: | Line 57: | ||
getByName(OUString::createFromAscii("Slide 1")); | getByName(OUString::createFromAscii("Slide 1")); | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
==Create, rename, copy and remove a Slide == | ==Create, rename, copy and remove a Slide == | ||
=== Create=== | === Create=== | ||
Create a new slide with a known Index (see <idl>com.sun.star.drawing.XDrawPagesSupplier</idl>, <idl>com.sun.star.drawing.XDrawPage</idl>, <idl>com.sun.star.drawing.XDrawPages</idl> and <idl>com.sun.star.container.XIndexAccess</idl> interfaces) : | Create a new slide with a known Index (see <idl>com.sun.star.drawing.XDrawPagesSupplier</idl>, <idl>com.sun.star.drawing.XDrawPage</idl>, <idl>com.sun.star.drawing.XDrawPages</idl> and <idl>com.sun.star.container.XIndexAccess</idl> interfaces) : | ||
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 5 Creating a new Slide | //Listing 5 Creating a new Slide | ||
// C++ | // C++ | ||
Line 106: | Line 79: | ||
// query for the XDrawPage Inteface | // query for the XDrawPage Inteface | ||
Reference< XDrawPage > rDrawPage = rDrawPages->insertNewByIndex(2); | Reference< XDrawPage > rDrawPage = rDrawPages->insertNewByIndex(2); | ||
− | </ | + | </syntaxhighlight> |
In this example, the new slide is indexed with value 2 and then its default name is “Slide 3”. You can see a difference between [[Programming_OOoDraw_and_OOoImpress#An_existing_Slide|previous code]] and this one : the former uses an Any type variable (DrawPage) although the latter code obtains directly a <idl>com.sun.star.drawing.XDrawPage</idl> interface. One more step is needed to obtain a <idl>com.sun.star.drawing.XDrawPage</idl> when working with Any : a UNO query. Add the above code to the former and you obtain like the latter code a <idl>com.sun.star.drawing.XDrawPage</idl> interface. | In this example, the new slide is indexed with value 2 and then its default name is “Slide 3”. You can see a difference between [[Programming_OOoDraw_and_OOoImpress#An_existing_Slide|previous code]] and this one : the former uses an Any type variable (DrawPage) although the latter code obtains directly a <idl>com.sun.star.drawing.XDrawPage</idl> interface. One more step is needed to obtain a <idl>com.sun.star.drawing.XDrawPage</idl> when working with Any : a UNO query. Add the above code to the former and you obtain like the latter code a <idl>com.sun.star.drawing.XDrawPage</idl> interface. | ||
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 6 Accessing the XDrawPage Service | //Listing 6 Accessing the XDrawPage Service | ||
// C++ | // C++ | ||
Line 115: | Line 88: | ||
// Query the XDrawPage Interface | // Query the XDrawPage Interface | ||
Reference< XDrawPage > rDrawPage(DrawPage, UNO_QUERY); | Reference< XDrawPage > rDrawPage(DrawPage, UNO_QUERY); | ||
− | </ | + | </syntaxhighlight> |
+ | |||
===Rename=== | ===Rename=== | ||
How to rename your slide ? You have to change the Name property of the slide (see <idl>com.sun.star.container.XNamed</idl> interface): | How to rename your slide ? You have to change the Name property of the slide (see <idl>com.sun.star.container.XNamed</idl> interface): | ||
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 7 Renaming a Slide | //Listing 7 Renaming a Slide | ||
// C++ | // C++ | ||
Line 127: | Line 101: | ||
Reference< XNamed > rNamed(rDrawPage, UNO_QUERY); | Reference< XNamed > rNamed(rDrawPage, UNO_QUERY); | ||
rNamed->setName(OUString::createFromAscii("My second page")); | rNamed->setName(OUString::createFromAscii("My second page")); | ||
− | </ | + | </syntaxhighlight> |
The <idl>com.sun.star.container.XNamed</idl> interface is your friend as seen in this code. The corresponding IDL code is given now : | The <idl>com.sun.star.container.XNamed</idl> interface is your friend as seen in this code. The corresponding IDL code is given now : | ||
− | < | + | <syntaxhighlight lang="idl"> |
// IDL | // IDL | ||
module com { module sun { module star { module container { | module com { module sun { module star { module container { | ||
Line 139: | Line 113: | ||
}; | }; | ||
}; }; }; }; | }; }; }; }; | ||
− | </ | + | </syntaxhighlight> |
As seen only two methods are available setName already used in the example and getName. | As seen only two methods are available setName already used in the example and getName. | ||
We have already focused our attention on the difference between PageDraw and rPageDraw variables : please note we can use both in the previous code. | We have already focused our attention on the difference between PageDraw and rPageDraw variables : please note we can use both in the previous code. | ||
Line 145: | Line 119: | ||
And now you want to copy a page : you have to query the <idl>com.sun.star.drawing.XDrawPageDuplicator</idl> interface. | And now you want to copy a page : you have to query the <idl>com.sun.star.drawing.XDrawPageDuplicator</idl> interface. | ||
We have to pass a draw page reference to the method duplicate(). A new draw page is appended at the end of the page list, using the default naming scheme for pages, Slide n . | We have to pass a draw page reference to the method duplicate(). A new draw page is appended at the end of the page list, using the default naming scheme for pages, Slide n . | ||
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 8 This Listing doesn't work at the Moment | //Listing 8 This Listing doesn't work at the Moment | ||
// C++ to check (doesn't work !!!) | // C++ to check (doesn't work !!!) | ||
Line 156: | Line 130: | ||
Reference< XDrawPage> rDrawPage2 = rDrawPageDuplicator->duplicate(rDrawPage); | Reference< XDrawPage> rDrawPage2 = rDrawPageDuplicator->duplicate(rDrawPage); | ||
// This last line doesn't work | // This last line doesn't work | ||
− | </ | + | </syntaxhighlight> |
===Remove=== | ===Remove=== | ||
If you want to remove a page known by its index (see <idl>com.sun.star.drawing.XDrawPagesSupplier</idl> and <idl>com.sun.star.container.XIndexAccess</idl> interfaces) | If you want to remove a page known by its index (see <idl>com.sun.star.drawing.XDrawPagesSupplier</idl> and <idl>com.sun.star.container.XIndexAccess</idl> interfaces) | ||
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 9 Index access | //Listing 9 Index access | ||
// C++ | // C++ | ||
Line 178: | Line 152: | ||
rDrawPages->remove(rDrawPage); | rDrawPages->remove(rDrawPage); | ||
− | </ | + | </syntaxhighlight> |
Removing a page known by its name is let as an exercise. | Removing a page known by its name is let as an exercise. | ||
== Setting the Focus on a Slide== | == Setting the Focus on a Slide== | ||
If you obtain a <idl>com.sun.star.drawing.XDrawPage</idl> interface corresponding to a hidden page and you want to put it on the top, have a look on <idl>com.sun.star.frame.XModel</idl> and <idl>com.sun.star.drawing.XDrawView</idl> interfaces : | If you obtain a <idl>com.sun.star.drawing.XDrawPage</idl> interface corresponding to a hidden page and you want to put it on the top, have a look on <idl>com.sun.star.frame.XModel</idl> and <idl>com.sun.star.drawing.XDrawView</idl> interfaces : | ||
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 10 Setting a Slide as currently focused | //Listing 10 Setting a Slide as currently focused | ||
// C++ | // C++ | ||
Line 202: | Line 176: | ||
Reference< XDrawView > rDrawView(ctl, UNO_QUERY); | Reference< XDrawView > rDrawView(ctl, UNO_QUERY); | ||
rDrawView->setCurrentPage(rDrawPage); | rDrawView->setCurrentPage(rDrawPage); | ||
− | </ | + | </syntaxhighlight> |
See also <idl>com.sun.star.frame.XModel</idl> and <idl>com.sun.star.drawing.XDrawView</idl>. | See also <idl>com.sun.star.frame.XModel</idl> and <idl>com.sun.star.drawing.XDrawView</idl>. | ||
Line 235: | Line 209: | ||
If you want for instance retrieve the page width (a property), use this code with the <idl>com.sun.star.beans.XPropertySet</idl> interface : | If you want for instance retrieve the page width (a property), use this code with the <idl>com.sun.star.beans.XPropertySet</idl> interface : | ||
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 11 Slide Properties | //Listing 11 Slide Properties | ||
// C++ | // C++ | ||
Line 247: | Line 221: | ||
sal_Int32 pageWidth; | sal_Int32 pageWidth; | ||
ApageWidth >>= pageWidth; | ApageWidth >>= pageWidth; | ||
− | </ | + | </syntaxhighlight> |
If you want to set the four page margins properties you can use this code : | If you want to set the four page margins properties you can use this code : | ||
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 12 Margin Properties | //Listing 12 Margin Properties | ||
// C++ | // C++ | ||
Line 263: | Line 237: | ||
rPageProps->setPropertyValue(OUString::createFromAscii("BorderTop"),props[2]); | rPageProps->setPropertyValue(OUString::createFromAscii("BorderTop"),props[2]); | ||
rPageProps->setPropertyValue(OUString::createFromAscii("BorderBottom"),props[3]); | rPageProps->setPropertyValue(OUString::createFromAscii("BorderBottom"),props[3]); | ||
− | </ | + | </syntaxhighlight> |
An other simpler way is to use the build in makeAny function : | An other simpler way is to use the build in makeAny function : | ||
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 13 Margin Properties and makeAny Function | //Listing 13 Margin Properties and makeAny Function | ||
// C++ | // C++ | ||
Line 277: | Line 251: | ||
rPageProps-> | rPageProps-> | ||
setPropertyValue(OUString::createFromAscii("BorderBottom"),makeAny((short)1200)); | setPropertyValue(OUString::createFromAscii("BorderBottom"),makeAny((short)1200)); | ||
− | </ | + | </syntaxhighlight> |
It's time now to draw shapes on pages. | It's time now to draw shapes on pages. | ||
Line 295: | Line 269: | ||
The corresponding C++ code atomatically generated is presented below. It use <idl>com.sun.star.frame.XModel</idl>, <idl>com.sun.star.frame.XController</idl>, <idl>com.sun.star.drawing.XDrawView</idl>, <idl>com.sun.star.drawing.XDrawPage</idl> and <idl>com.sun.star.container.XNamed</idl> interfaces, but a closer look shows you have to add include directives : | The corresponding C++ code atomatically generated is presented below. It use <idl>com.sun.star.frame.XModel</idl>, <idl>com.sun.star.frame.XController</idl>, <idl>com.sun.star.drawing.XDrawView</idl>, <idl>com.sun.star.drawing.XDrawPage</idl> and <idl>com.sun.star.container.XNamed</idl> interfaces, but a closer look shows you have to add include directives : | ||
− | < | + | <syntaxhighlight lang="cpp"> |
#include "com/sun/star/frame/XModel.hpp" | #include "com/sun/star/frame/XModel.hpp" | ||
#include "com/sun/star/uno/Reference.hxx" | #include "com/sun/star/uno/Reference.hxx" | ||
Line 316: | Line 290: | ||
} | } | ||
//... | //... | ||
− | </ | + | </syntaxhighlight> |
− | {{ | + | {{Warn|I have not checked this code at the moment. I probably have to add any include directives and because we don't encounter "any" type during the IDL-tree traversing, we probably haven't to modify C++ source of codesnippet sub.}} |
=Drawing with OOoDraw= | =Drawing with OOoDraw= | ||
==Inserting a Shape== | ==Inserting a Shape== | ||
With OOoDraw text is considered as a shape like rectangle, ellipses... Let's then start with a <idl>com.sun.star.drawing.TextShape</idl> service and <idl>com.sun.star.drawing.XShape</idl>, <idl>com.sun.star.drawing.XShapes</idl> and <idl>com.sun.star.text.XText</idl> interfaces: | With OOoDraw text is considered as a shape like rectangle, ellipses... Let's then start with a <idl>com.sun.star.drawing.TextShape</idl> service and <idl>com.sun.star.drawing.XShape</idl>, <idl>com.sun.star.drawing.XShapes</idl> and <idl>com.sun.star.text.XText</idl> interfaces: | ||
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 14 Inserting a text Shape | //Listing 14 Inserting a text Shape | ||
// C++ | // C++ | ||
Line 357: | Line 331: | ||
Reference< XText > rText(rTxtShape, UNO_QUERY); | Reference< XText > rText(rTxtShape, UNO_QUERY); | ||
rText->setString(OUString::createFromAscii("Hello people around the world")); | rText->setString(OUString::createFromAscii("Hello people around the world")); | ||
− | </ | + | </syntaxhighlight> |
This listing when executed writes out “Hello people around the world” on a page. | This listing when executed writes out “Hello people around the world” on a page. | ||
Changing a shape is simple : if you want to draw a rectangle, the only line to change is : | Changing a shape is simple : if you want to draw a rectangle, the only line to change is : | ||
− | < | + | <syntaxhighlight lang="cpp"> |
// Listing 15 Change this Line | // Listing 15 Change this Line | ||
// C++ | // C++ | ||
Reference< XInterface > textshape = DocFactory->createInstance( | Reference< XInterface > textshape = DocFactory->createInstance( | ||
OUString::createFromAscii("com.sun.star.drawing.TextShape") ); | OUString::createFromAscii("com.sun.star.drawing.TextShape") ); | ||
− | </ | + | </syntaxhighlight> |
− | with : | + | with corresponding service <idl>com.sun.star.drawing.RectangleShape</idl>: |
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 16 Inserting a Rectangle Shape | //Listing 16 Inserting a Rectangle Shape | ||
// C++ | // C++ | ||
Reference< XInterface > textshape = DocFactory->createInstance( | Reference< XInterface > textshape = DocFactory->createInstance( | ||
OUString::createFromAscii("com.sun.star.drawing.RectangleShape") ); | OUString::createFromAscii("com.sun.star.drawing.RectangleShape") ); | ||
− | </ | + | </syntaxhighlight> |
− | We shall examine how to draw a shape again later. Note that you can write text too with this new shape as explained now | + | We shall examine how to draw a shape again later. Note that you can write text too with this new shape, as explained now. |
− | + | ||
− | + | ||
==Writing Text in a Shape== | ==Writing Text in a Shape== | ||
− | Writing text in a shape is obtained with the same way as writing text in a TextShape. For a RectangleShape (rRecShape) we have then : | + | Writing text in a shape is obtained with the same way as writing text in a TextShape. For a RectangleShape (rRecShape) we have then to query a <idl>com.sun.star.text.XText</idl> interface : |
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 17 setString method to insert a Text | //Listing 17 setString method to insert a Text | ||
// C++ | // C++ | ||
Line 385: | Line 357: | ||
Reference< XText > rText(rRecShape, UNO_QUERY); | Reference< XText > rText(rRecShape, UNO_QUERY); | ||
rText->setString(OUString::createFromAscii("Hello")); | rText->setString(OUString::createFromAscii("Hello")); | ||
− | </ | + | </syntaxhighlight> |
which draws a rectangle and writes out “Hello” in this rectangle. | which draws a rectangle and writes out “Hello” in this rectangle. | ||
==Giving a Name to the Shape== | ==Giving a Name to the Shape== | ||
− | Every shape can receive a name. This feature is very interesting if you want transform OOoDraw as a graphical front-end. Here is a code example which give a name to a shape : | + | Every shape can receive a name. This feature is very interesting if you want transform OOoDraw as a graphical front-end. Here is a code example which give a name to a shape with <idl>com.sun.star.beans.XPropertySet</idl> interface : |
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 18 Naming a Shape | //Listing 18 Naming a Shape | ||
// C++ | // C++ | ||
Line 402: | Line 374: | ||
shapeprops->setPropertyValue(OUString::createFromAscii("Name"), | shapeprops->setPropertyValue(OUString::createFromAscii("Name"), | ||
makeAny(OUString::createFromAscii("demo"))); | makeAny(OUString::createFromAscii("demo"))); | ||
− | </ | + | </syntaxhighlight> |
The XShapes interface allows adding, removing as shown in the IDL file : | The XShapes interface allows adding, removing as shown in the IDL file : | ||
− | < | + | <syntaxhighlight lang="idl"> |
//Listing 19 Index access to Shapes (IDL file) | //Listing 19 Index access to Shapes (IDL file) | ||
// IDL | // IDL | ||
Line 412: | Line 384: | ||
void remove( [in] com::sun::star::drawing::XShape xShape ); | void remove( [in] com::sun::star::drawing::XShape xShape ); | ||
}; | }; | ||
− | </ | + | </syntaxhighlight> |
The XIndexAccess inheritance allows also getcount() and getByIndex. This allows the construction of a loop on all shapes. | The XIndexAccess inheritance allows also getcount() and getByIndex. This allows the construction of a loop on all shapes. | ||
Line 421: | Line 393: | ||
==Inserting a bitmap== | ==Inserting a bitmap== | ||
− | Here is an example which insert a bitmap. We start as in previous chapter : with a drawing page. The only difference is again with the shape which is now a GraphicObjectShape. And we have to give a file | + | Here is an example which insert a bitmap. We start as in previous chapter : with a drawing page. The only difference is again with the shape which is now a GraphicObjectShape. And we have to give a file URL and use <idl>com.sun.star.drawing.XShape</idl>, <idl>com.sun.star.drawing.XShapes</idl> and <idl>com.sun.star.beans.XPropertySet</idl> interfaces : |
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 20 Inserting an Image in a Slide | //Listing 20 Inserting an Image in a Slide | ||
// C++ | // C++ | ||
Line 462: | Line 434: | ||
Reference< XShapes > Shapes(rDrawPage, UNO_QUERY); | Reference< XShapes > Shapes(rDrawPage, UNO_QUERY); | ||
Shapes->add(rGrafShape); | Shapes->add(rGrafShape); | ||
− | </ | + | </syntaxhighlight> |
This code doesn't insert the picture in the document but the link. Inserting effectively is not possible with the API but with menus : | This code doesn't insert the picture in the document but the link. Inserting effectively is not possible with the API but with menus : | ||
− | 1. | + | 1. Edit -> link |
select all the pictures link | select all the pictures link | ||
Line 475: | Line 447: | ||
Or you copy the bitmap of the linked picture after it was added to the document and create a new shape with it. This new shape contains the actual image and not only the link. | Or you copy the bitmap of the linked picture after it was added to the document and create a new shape with it. This new shape contains the actual image and not only the link. | ||
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 20.2 Inserting an Image and embedding it | //Listing 20.2 Inserting an Image and embedding it | ||
Line 510: | Line 482: | ||
//Dont forget to remove the linked shape | //Dont forget to remove the linked shape | ||
rShapes->remove(rGrafShape); | rShapes->remove(rGrafShape); | ||
− | </ | + | </syntaxhighlight> |
See also <idl>com.sun.star.drawing.XShape</idl>, <idl>com.sun.star.drawing.XShapes</idl> <idl>com.sun.star.beans.XPropertySet</idl> and <idl>com.sun.star.drawing.GraphicObjectShape</idl>. | See also <idl>com.sun.star.drawing.XShape</idl>, <idl>com.sun.star.drawing.XShapes</idl> <idl>com.sun.star.beans.XPropertySet</idl> and <idl>com.sun.star.drawing.GraphicObjectShape</idl>. | ||
==Changing the Size of a Picture== | ==Changing the Size of a Picture== | ||
− | To keep proportion when changing the size, you have first to calculate it. For that we first get the picture's size in pixels but this is possible only after the “Shapes->add(rGrafShape);” Here is an example which resize the picture with a chosen width and a height calculated. | + | To keep proportion when changing the size, you have first to calculate it. For that we first get the picture's size in pixels, but this is possible only after the “Shapes->add(rGrafShape);” Here is an example which resize the picture with a chosen width and a height calculated. |
− | < | + | <syntaxhighlight lang="cpp"> |
//Listing 21 Modifying a Bitmap Size | //Listing 21 Modifying a Bitmap Size | ||
//C++ | //C++ | ||
Line 531: | Line 503: | ||
TheSize->Height = 14000*proportion; | TheSize->Height = 14000*proportion; | ||
rGrafShape->setSize(*TheSize); | rGrafShape->setSize(*TheSize); | ||
− | </ | + | </syntaxhighlight> |
See also <idl>com.sun.star.awt.XBitmap</idl>. | See also <idl>com.sun.star.awt.XBitmap</idl>. | ||
==Inserting many Pictures== | ==Inserting many Pictures== | ||
− | Every time you insert a picture, you have to query many times the GraphicObjectShape, even if you insert many times the same picture. | + | Every time you insert a picture, you have to query many times the <idl>com.sun.star.drawing.GraphicObjectShape</idl> service, even if you insert many times the same picture. |
==Finding a Picture with its Name== | ==Finding a Picture with its Name== | ||
Line 547: | Line 519: | ||
= See also= | = See also= | ||
+ | * [[FR/Documentation/OpenOffice_Draw|French version of this chapter]]. | ||
* Drawing Documents and Presentation Documents [[Documentation/DevGuide/Drawings/Drawing_Documents_and_Presentation_Documents|in Developer's Guide]] | * Drawing Documents and Presentation Documents [[Documentation/DevGuide/Drawings/Drawing_Documents_and_Presentation_Documents|in Developer's Guide]] | ||
* [[Documentation/BASIC_Guide/Drawings_Presentations|Drawings ans Presentations]] | * [[Documentation/BASIC_Guide/Drawings_Presentations|Drawings ans Presentations]] | ||
Line 553: | Line 526: | ||
[[Category:OOoDraw]] | [[Category:OOoDraw]] | ||
− | |||
[[Category:Uno]] | [[Category:Uno]] | ||
[[Category:Cpp]] | [[Category:Cpp]] |
Latest revision as of 12:15, 16 May 2022
Contents
Introduction
OOoDraw is my favorite application of OpenOffice suite. Therefore I think I will spend a lot of time with this chapter. I start writing this chapter with the Help of Christian's post in OOoForum (Code snippets) Working with a Presentation Document in C++ In this chapter, we use again the starting code described here . It consists of a subprogram “ooConnect()”
// Listing 0 // C++ #include <stdio.h> #include <cppuhelper/bootstrap.hxx> #include <com/sun/star/bridge/XUnoUrlResolver.hpp> #include <com/sun/star/lang/XMultiServiceFactory.hpp> // added #include <com/sun/star/frame/XComponentLoader.hpp> using namespace com::sun::star::uno; using namespace com::sun::star::lang; using namespace com::sun::star::bridge; // added using namespace com::sun::star::frame; using namespace rtl; using namespace cppu; // a procedure for what the so called boostrap Reference< XMultiServiceFactory > ooConnect(){ // create the initial component context Reference< XComponentContext > rComponentContext = defaultBootstrap_InitialComponentContext(); // retrieve the servicemanager from the context Reference< XMultiComponentFactory > rServiceManager = rComponentContext->getServiceManager(); // instantiate a sample service with the servicemanager. Reference< XInterface > rInstance = rServiceManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.bridge.UnoUrlResolver" ),rComponentContext ); // Query for the XUnoUrlResolver interface Reference< XUnoUrlResolver > rResolver( rInstance, UNO_QUERY ); if( ! rResolver.is() ){ printf( "Error: Couldn't instantiate com.sun.star.bridge.UnoUrlResolver service\n" ); return NULL; } try { // resolve the uno-url rInstance = rResolver->resolve( OUString::createFromAscii( "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" ) ); if( ! rInstance.is() ){ printf( "StarOffice.ServiceManager is not exported from remote counterpart\n" ); return NULL; } // query for the simpler XMultiServiceFactory interface, sufficient for scripting Reference< XMultiServiceFactory > rOfficeServiceManager (rInstance, UNO_QUERY); if( ! rOfficeServiceManager.is() ){ printf( "XMultiServiceFactory interface is not exported for StarOffice.ServiceManager\n" ); return NULL; } return rOfficeServiceManager; } catch( Exception &e ){ OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ); printf( "Error: %s\n", o.pData->buffer ); return NULL; } return NULL; }
Note : You have eventually to change "port=8100" into "port=2083" for recent SDK (after 2.X).
You can have a look at :
- com.sun.star.uno.XComponentContext, com.sun.star.lang.XMultiComponentFactory, com.sun.star.uno.XInterface, com.sun.star.bridge.XUnoUrlResolver and com.sun.star.lang.XMultiServiceFactory interfaces,
- com.sun.star.bridge.UnoUrlResolver service
The main function is examined now. Working with this code consists of adding new code in the main (see the comment). We give again the main program slightly modified.
To fit OOoDraw requirements you have to replace the string "private:factory/scalc" with "private:factory/sdraw". |
//Listing 0b Again our starting main Code int main( ) { //retrieve an instance of the remote service manager Reference< XMultiServiceFactory > rOfficeServiceManager; rOfficeServiceManager = ooConnect(); if( rOfficeServiceManager.is() ){ printf( "Connected sucessfully to the office\n" ); } //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); if( rComponentLoader.is() ){ printf( "XComponentloader successfully instanciated\n" ); } //get an instance of the spreadsheet Reference< XComponent > xcomponent = rComponentLoader->loadComponentFromURL( OUString::createFromAscii("private:factory/scalc"), OUString::createFromAscii("_blank"), 0, Sequence < ::com::sun::star::beans::PropertyValue >()); // add code here return 0; }
This code is called a bootstrap and uses :
- com.sun.star.lang.XMultiServiceFactory, com.sun.star.uno.XInterface, com.sun.star.frame.XComponentLoader and com.sun.star.lang.XComponent interfaces
- com.sun.star.frame.Desktop service
- com.sun.star.beans.PropertyValue structure.
Remember each time you query for an interface you have to add code lines (if they don't exist) in the source code and a line in the makefile. I will generally add comments to prevent omissions. You can find more explanations here (if you are not a beginner).
Important Note : The more important point in the compilation chain of the examples above is that cppumaker will construct every hpp and hdl files you need in your application. The SDK doesn't provide every hpp files, but you have to construt them starting from IDL files provided by SDK.
|
It is possible to construct all the hpp files when installing the SDK as mentioned in a Windows installation. It's also possible with other OS. In doing so, you don't need modifying your MakeFile. |
Finding the drawing Slide
Bear in mind that if OOoSheet opens with three sheets by default, Apache OpenOffice Draw opens with only one page/slide (index 0).
An existing Slide
If only the index of the slide is known, have look at com.sun.star.drawing.XDrawPagesSupplier and com.sun.star.container.XIndexAccess interfaces :
//Listing 2 Index access to a draw Slide // C++ // Don't forget to add : using namespace com::sun::star::drawing; // Don't forget to add : #include <com/sun/star/drawing/XDrawPagesSupplier.hpp> // Don't forget to add "com.sun.star.drawing.XDrawPagesSupplier \" in the makefile // Don't forget to add : using namespace com::sun::star::container; // Don't forget to add : #include <com/sun/star/container/XIndexAccess.hpp> // Don't forget to add "com.sun.star.container.XIndexAccess \" in the makefile Reference< XDrawPagesSupplier > rDrawDoc(xcomponent, UNO_QUERY); // query the XDrawPages Interface Reference< XDrawPages > rDrawPages = rDrawDoc->getDrawPages(); // query the XIndexAccess Interface Reference< XIndexAccess > rPageIndexAccess(rDrawPages, UNO_QUERY); Any DrawPage = rPageIndexAccess->getByIndex(0);
If you want to know how many pages belong to your document, you can reach it from rPageIndexAccess object with :
//Listing 3 How many Slides ? // C++ rPageIndexAccess->getCount();
If you only know the page name (see com.sun.star.container.XNameAccess interface):
//Listing 4 Slide Name Access //C++ // Don't forget to add : #include <com/sun/star/container/XNameAccess.hpp> // Don't forget to add "com.sun.star.container.XNameAccess \" in the makefile //query the XNameAccess Interface Reference< XNameAccess > rNameAccess(rDrawPages, UNO_QUERY); if (rNameAccess->hasByName(OUString::createFromAscii("Slide 1"))) { Any DrawPage = rNameAccess-> getByName(OUString::createFromAscii("Slide 1")); }
Create, rename, copy and remove a Slide
Create
Create a new slide with a known Index (see com.sun.star.drawing.XDrawPagesSupplier, com.sun.star.drawing.XDrawPage, com.sun.star.drawing.XDrawPages and com.sun.star.container.XIndexAccess interfaces) :
//Listing 5 Creating a new Slide // C++ // Don't forget to add : using namespace com::sun::star::drawing; // Don't forget to add : #include <com/sun/star/drawing/XDrawPagesSupplier.hpp> // Don't forget to add "com.sun.star.drawing.XDrawPagesSupplier \" in the makefile // Don't forget to add : using namespace com::sun::star::container; // Don't forget to add : #include <com/sun/star/container/XIndexAccess.hpp> // Don't forget to add "com.sun.star.container.XIndexAccess \" in the makefile Reference< XDrawPagesSupplier > rDrawDoc(xcomponent, UNO_QUERY); // query the XDrawPages Interface Reference< XDrawPages > rDrawPages = rDrawDoc->getDrawPages(); // query for the XDrawPage Inteface Reference< XDrawPage > rDrawPage = rDrawPages->insertNewByIndex(2);
In this example, the new slide is indexed with value 2 and then its default name is “Slide 3”. You can see a difference between previous code and this one : the former uses an Any type variable (DrawPage) although the latter code obtains directly a com.sun.star.drawing.XDrawPage interface. One more step is needed to obtain a com.sun.star.drawing.XDrawPage when working with Any : a UNO query. Add the above code to the former and you obtain like the latter code a com.sun.star.drawing.XDrawPage interface.
//Listing 6 Accessing the XDrawPage Service // C++ // Don't forget to add : #include <com/sun/star/drawing/XDrawPage.hpp> // Don't forget to add "com.sun.star.drawing.XDrawPage \" in the makefile // Query the XDrawPage Interface Reference< XDrawPage > rDrawPage(DrawPage, UNO_QUERY);
Rename
How to rename your slide ? You have to change the Name property of the slide (see com.sun.star.container.XNamed interface):
//Listing 7 Renaming a Slide // C++ // Don't forget to add : #include <com/sun/star/container/XNamed.hpp> // Don't forget to add "com.sun.star.container.XNamed \" in the makefile // query for the XNamed Interface Reference< XNamed > rNamed(rDrawPage, UNO_QUERY); rNamed->setName(OUString::createFromAscii("My second page"));
The com.sun.star.container.XNamed interface is your friend as seen in this code. The corresponding IDL code is given now :
// IDL module com { module sun { module star { module container { interface XNamed: com::sun::star::uno::XInterface { string getName(); [oneway] void setName( [in] string aName ); }; }; }; }; };
As seen only two methods are available setName already used in the example and getName. We have already focused our attention on the difference between PageDraw and rPageDraw variables : please note we can use both in the previous code.
Copy
And now you want to copy a page : you have to query the com.sun.star.drawing.XDrawPageDuplicator interface. We have to pass a draw page reference to the method duplicate(). A new draw page is appended at the end of the page list, using the default naming scheme for pages, Slide n .
//Listing 8 This Listing doesn't work at the Moment // C++ to check (doesn't work !!!) // Don't forget to add : #include <com/sun/star/drawing/XDrawPageDuplicator.hpp> // Don't forget to add "com.sun.star.drawing.XDrawPageDuplicator \" in the makefile // query for the XDrawPageDuplicator Interface Reference< XDrawPageDuplicator > rDrawPageDuplicator(rDrawPage(s)???, UNO_QUERY); Reference< XDrawPage> rDrawPage2 = rDrawPageDuplicator->duplicate(rDrawPage); // This last line doesn't work
Remove
If you want to remove a page known by its index (see com.sun.star.drawing.XDrawPagesSupplier and com.sun.star.container.XIndexAccess interfaces)
//Listing 9 Index access // C++ // Don't forget to add : using namespace com::sun::star::drawing; // Don't forget to add : #include <com/sun/star/drawing/XDrawPagesSupplier.hpp> // Don't forget to add "com.sun.star.drawing.XDrawPagesSupplier \" in the makefile // Don't forget to add : using namespace com::sun::star::container; // Don't forget to add : #include <com/sun/star/container/XIndexAccess.hpp> // Don't forget to add "com.sun.star.container.XIndexAccess \" in the makefile Reference< XDrawPagesSupplier > rDrawDoc(xcomponent, UNO_QUERY); // query the XDrawPages Interface Reference< XDrawPages > rDrawPages = rDrawDoc->getDrawPages(); // query for the XDrawPage Inteface Reference< XDrawPage > rDrawPage = rDrawPages->insertNewByIndex(0); rDrawPages->remove(rDrawPage);
Removing a page known by its name is let as an exercise.
Setting the Focus on a Slide
If you obtain a com.sun.star.drawing.XDrawPage interface corresponding to a hidden page and you want to put it on the top, have a look on com.sun.star.frame.XModel and com.sun.star.drawing.XDrawView interfaces :
//Listing 10 Setting a Slide as currently focused // C++ // First Obtain a XDrawPage interface for this page, and after : // Don't forget to add : using namespace com::sun::star::frame; // Don't forget to add : #include <com/sun/star/frame/XModel.hpp> // Don't forget to add "com.sun.star.frame.XModel \" in the makefile // Don't forget to add : #include <com/sun/star/drawing/XDrawView.hpp> // Don't forget to add "com.sun.star.drawing.XDrawView \" in the makefile // Query the XModel Interface Reference< XModel > rmodel(rDrawDoc, UNO_QUERY); Reference< XController > ctl = rmodel->getCurrentController(); // Query the XDrawView interface Reference< XDrawView > rDrawView(ctl, UNO_QUERY); rDrawView->setCurrentPage(rDrawPage);
See also com.sun.star.frame.XModel and com.sun.star.drawing.XDrawView.
Property of a Slide
Properties belong to the slide are presented :
- slide's properties
Properties | Signification |
Name | Name of the slide |
BorderLeft | Left margin in 1/100 mm |
BorderRight | Right margin in 1/100 mm |
BorderTop | Top margin in 1/100 mm |
MBorderBottom | Bottom margin in 1/100 mm |
Height | Height of the page in 1/100 mm |
Width | Width of the page in 1/100 mm |
Number | Number of the page begins from 1 |
Orientation | Portrait : com.sun.star.view.PaperOrientation.PORTRAIT
or Landscape : com.sun.star.view.PaperOrientation.LANDSCAPE |
If you want for instance retrieve the page width (a property), use this code with the com.sun.star.beans.XPropertySet interface :
//Listing 11 Slide Properties // C++ // Don't forget to add : using namespace com::sun::star::beans; // Don't forget to add : #include <com/sun/star/beans/XPropertySet.hpp> // Don't forget to add "com.sun.star.beans.XPropertySet \" in the makefile // find out page width Reference< XPropertySet > rPageProps(rDrawPage, UNO_QUERY); Any ApageWidth = rPageProps->getPropertyValue(OUString::createFromAscii("Width")); sal_Int32 pageWidth; ApageWidth >>= pageWidth;
If you want to set the four page margins properties you can use this code :
//Listing 12 Margin Properties // C++ // Obtain rPageProps like previous code Any props[4]; props[0] <<= (short)1200; props[1] <<= (short)1200; props[2] <<= (short)1200; props[3] <<= (short)1200; rPageProps->setPropertyValue(OUString::createFromAscii("BorderLeft"),props[0]); rPageProps->setPropertyValue(OUString::createFromAscii("BorderRight"),props[1]); rPageProps->setPropertyValue(OUString::createFromAscii("BorderTop"),props[2]); rPageProps->setPropertyValue(OUString::createFromAscii("BorderBottom"),props[3]);
An other simpler way is to use the build in makeAny function :
//Listing 13 Margin Properties and makeAny Function // C++ // Obtain rPageProps like previous code rPageProps-> setPropertyValue(OUString::createFromAscii("BorderLeft"),makeAny((short)1200)); rPageProps-> setPropertyValue(OUString::createFromAscii("BorderRight"),makeAny((short)1200)); rPageProps-> setPropertyValue(OUString::createFromAscii("BorderTop"),makeAny((short)1200)); rPageProps-> setPropertyValue(OUString::createFromAscii("BorderBottom"),makeAny((short)1200));
It's time now to draw shapes on pages.
Going further with Inspector
The new object inspector is able to generate Java or C++ or OOoBasic code. In order to try inspector ability to generate C++ code, we want to use it for the following problem : obtaining the default selected slide from a draw document. See also similar problem with writer document and particularly the definition of an IDL-tree.
Using inspector gives us the following IDL-tree :
MyDocument ->methods ->com.sun.star.frame.XController getCurrentController() ->xController->methods -> xDrawPage getCurrentPage ->String getName return Slide 2
The corresponding C++ code atomatically generated is presented below. It use com.sun.star.frame.XModel, com.sun.star.frame.XController, com.sun.star.drawing.XDrawView, com.sun.star.drawing.XDrawPage and com.sun.star.container.XNamed interfaces, but a closer look shows you have to add include directives :
#include "com/sun/star/frame/XModel.hpp" #include "com/sun/star/uno/Reference.hxx" #include "com/sun/star/uno/XInterface.hpp" #include "rtl/ustring.hxx" #include "sal/config.h" #include "sal/types.h" namespace css = com::sun::star; using namespace rtl; //... void codesnippet(const css::uno::Reference<css::uno::XInterface>& _oUnoEntryObject ){{ css::uno::Reference<css::frame::XModel> xModel( _oUnoEntryObject, css::uno::UNO_QUERY_THROW); css::uno::Reference<css::frame::XController> xController = xModel->getCurrentController(); css::uno::Reference<css::drawing::XDrawView> xDrawView( xController, css::uno::UNO_QUERY_THROW); css::uno::Reference<css::drawing::XDrawPage> xDrawPage = xDrawView->getCurrentPage(); css::uno::Reference<css::container::XNamed> xNamed( xDrawPage, css::uno::UNO_QUERY_THROW); OUString sName = xNamed->getName(); } //...
Drawing with OOoDraw
Inserting a Shape
With OOoDraw text is considered as a shape like rectangle, ellipses... Let's then start with a com.sun.star.drawing.TextShape service and com.sun.star.drawing.XShape, com.sun.star.drawing.XShapes and com.sun.star.text.XText interfaces:
//Listing 14 Inserting a text Shape // C++ // Don't forget to add :using namespace com::sun::star::awt; for Point and Size // Don't forget to add : #include <com/sun/star/drawing/XShape.hpp> // Don't forget to add "com.sun.star.drawing.XShape \" in the makefile // Don't forget to add : #include <com/sun/star/drawing/XShapes.hpp> // Don't forget to add "com.sun.star.drawing.XShapes \" in the makefile // Don't forget to add : using namespace com::sun::star::text; // Don't forget to add : #include <com/sun/star/text/XText.hpp> // Don't forget to add "com.sun.star.text.XText \" in the makefile Reference< XMultiServiceFactory > DocFactory(xcomponent, UNO_QUERY); Reference< XInterface > textshape = DocFactory->createInstance( OUString::createFromAscii("com.sun.star.drawing.TextShape") ); Reference< XShape > rTxtShape(textshape, UNO_QUERY); Point *Pos = new ( Point ); Size *TheSize = new ( Size ); Pos->X = 2000; Pos->Y = 6000; TheSize->Width = 7000; TheSize->Height = 2000; rTxtShape->setPosition(Pos); rTxtShape->setSize(*TheSize); // Query XShapes Interface Reference< XShapes > Shapes(rDrawPage, UNO_QUERY); Shapes->add(rTxtShape); // Query the XText Interface Reference< XText > rText(rTxtShape, UNO_QUERY); rText->setString(OUString::createFromAscii("Hello people around the world"));
This listing when executed writes out “Hello people around the world” on a page. Changing a shape is simple : if you want to draw a rectangle, the only line to change is :
// Listing 15 Change this Line // C++ Reference< XInterface > textshape = DocFactory->createInstance( OUString::createFromAscii("com.sun.star.drawing.TextShape") );
with corresponding service com.sun.star.drawing.RectangleShape:
//Listing 16 Inserting a Rectangle Shape // C++ Reference< XInterface > textshape = DocFactory->createInstance( OUString::createFromAscii("com.sun.star.drawing.RectangleShape") );
We shall examine how to draw a shape again later. Note that you can write text too with this new shape, as explained now.
Writing Text in a Shape
Writing text in a shape is obtained with the same way as writing text in a TextShape. For a RectangleShape (rRecShape) we have then to query a com.sun.star.text.XText interface :
//Listing 17 setString method to insert a Text // C++ // Query the XText Interface Reference< XText > rText(rRecShape, UNO_QUERY); rText->setString(OUString::createFromAscii("Hello"));
which draws a rectangle and writes out “Hello” in this rectangle.
Giving a Name to the Shape
Every shape can receive a name. This feature is very interesting if you want transform OOoDraw as a graphical front-end. Here is a code example which give a name to a shape with com.sun.star.beans.XPropertySet interface :
//Listing 18 Naming a Shape // C++ // Don't forget to add : using namespace com::sun::star::beans; // Don't forget to add : #include <com/sun/star/beans/XPropertySet.hpp> // Don't forget to add "com.sun.star.beans.XPropertySet \" in the makefile // Query the XPropertySet Interface Reference< XPropertySet > shapeprops(rtxtShape, UNO_QUERY); shapeprops->setPropertyValue(OUString::createFromAscii("Name"), makeAny(OUString::createFromAscii("demo")));
The XShapes interface allows adding, removing as shown in the IDL file :
//Listing 19 Index access to Shapes (IDL file) // IDL interface XShapes: com::sun::star::container::XIndexAccess { void add( [in] com::sun::star::drawing::XShape xShape ); void remove( [in] com::sun::star::drawing::XShape xShape ); };
The XIndexAccess inheritance allows also getcount() and getByIndex. This allows the construction of a loop on all shapes.
See also com.sun.star.beans.XPropertySet and com.sun.star.drawing.XShape.
Graphics and OOoDraw
In OOoDraw a graphic is a bitmap or vector picture. We are interesting in this chapter with bitmap graphics.
Inserting a bitmap
Here is an example which insert a bitmap. We start as in previous chapter : with a drawing page. The only difference is again with the shape which is now a GraphicObjectShape. And we have to give a file URL and use com.sun.star.drawing.XShape, com.sun.star.drawing.XShapes and com.sun.star.beans.XPropertySet interfaces :
//Listing 20 Inserting an Image in a Slide // C++ // Don't forget #include <osl/file.hxx> // Don't forget to add :using namespace com::sun::star::awt; for Point and size // Don't forget to add : #include <com/sun/star/drawing/XShape.hpp> // Don't forget to add "com.sun.star.drawing.XShape \" in the makefile // Don't forget to add : #include <com/sun/star/drawing/XShapes.hpp> // Don't forget to add "com.sun.star.drawing.XShapes \" in the makefile // Don't forget to add : using namespace com::sun::star::beans; // Don't forget to add : #include <com/sun/star/beans/XPropertySet.hpp> // Don't forget to add "com.sun.star.beans.XPropertySet \" in the makefile Reference< XMultiServiceFactory > DocFactory(xcomponent, UNO_QUERY); Reference< XInterface > GraphicShape = DocFactory->createInstance( OUString::createFromAscii("com.sun.star.drawing.GraphicObjectShape")); Reference< XShape > rGrafShape(GraphicShape, UNO_QUERY); Point *Pos = new (Point); Size *TheSize = new ( Size ); Pos->X = 2000; Pos->Y = 6000; TheSize->Width = 7000; TheSize->Height = 2000; rGrafShape->setPosition(*Pos); rGrafShape->setSize(*TheSize); OUString sImageUrl; osl::FileBase::getFileURLFromSystemPath( OUString::createFromAscii("/home/smoutou/photoperso.jpg"),sImageUrl); Reference< XPropertySet > rShapeProps(rGrafShape, UNO_QUERY); rShapeProps->setPropertyValue(OUString::createFromAscii("GraphicURL"), makeAny(sImageUrl)); // Query XShapes Interface Reference< XShapes > Shapes(rDrawPage, UNO_QUERY); Shapes->add(rGrafShape);
This code doesn't insert the picture in the document but the link. Inserting effectively is not possible with the API but with menus :
1. Edit -> link
select all the pictures link
click on deconnect button; confirm
2. Save the document.
Or you copy the bitmap of the linked picture after it was added to the document and create a new shape with it. This new shape contains the actual image and not only the link.
//Listing 20.2 Inserting an Image and embedding it // C++ // Don't forget #include <com/sun/star/awt/XBitmap.hpp> //Only after adding the rGrafShape to the Shapes collection, //the GraphicObjectFillBitmap is set. //Get a copy of the image bitmap Any bmp = rShapeProps->getPropertyValue( OUString::createFromAscii("GraphicObjectFillBitmap")); Reference< XBitmap > rBitmap(bmp, UNO_QUERY); Reference< XInterface > embedShape = DocFactory->createInstance( OUString::createFromAscii("com.sun.star.drawing.GraphicObjectShape")); Reference< XShape > rEmbedShape(embedShape , UNO_QUERY); //Set size and pos again (this time for the embedded shape) rEmbedShape->setPosition(Position); rEmbedShape->setSize(TheSize); Reference< XPropertySet > rEmbedPropSet(rEmbedShape, UNO_QUERY); //Set the bitmap for the new embedded shape rEmbedPropSet->setPropertyValue( OUString::createFromAscii("GraphicObjectFillBitmap"), makeAny(rBitmap)); rEmbedPropSet->setPropertyValue( OUString::createFromAscii("Name"), makeAny(sImageUrl)); //Finally add the embedded shape to the shapes collection rShapes->add(rEmbedShape); //Dont forget to remove the linked shape rShapes->remove(rGrafShape);
See also com.sun.star.drawing.XShape, com.sun.star.drawing.XShapes com.sun.star.beans.XPropertySet and com.sun.star.drawing.GraphicObjectShape.
Changing the Size of a Picture
To keep proportion when changing the size, you have first to calculate it. For that we first get the picture's size in pixels, but this is possible only after the “Shapes->add(rGrafShape);” Here is an example which resize the picture with a chosen width and a height calculated.
//Listing 21 Modifying a Bitmap Size //C++ // Don't forget to add : #include <com/sun/star/awt/XBitmap.hpp> // Don't forget to add "com.sun.star.awt.XBitmap \" in the makefile Any Bitmap = rShapeProps-> getPropertyValue(OUString::createFromAscii("GraphicObjectFillBitmap")); Reference< XBitmap > rBitmap(Bitmap, UNO_QUERY); // query the size in pixel *TheSize = rBitmap->getSize(); sal_Int32 proportion = TheSize->Height/TheSize->Width; TheSize->Width = 14000; TheSize->Height = 14000*proportion; rGrafShape->setSize(*TheSize);
See also com.sun.star.awt.XBitmap.
Inserting many Pictures
Every time you insert a picture, you have to query many times the com.sun.star.drawing.GraphicObjectShape service, even if you insert many times the same picture.
Finding a Picture with its Name
The picture like other shapes can be accessed with an index, not very easy to use. But we can give them a name see ( Giving a Name to the Shape) We shall tackle this problem later.
If you want to go further with shapes go to next chapter.
See also
- French version of this chapter.
- Drawing Documents and Presentation Documents in Developer's Guide
- Drawings ans Presentations
- C++ and UNO tutorial
- Writing a Program to Control OpenOffice.org, by Franco Pingiori — Part 1 and Part 2, Linux Journal