Difference between revisions of "Working with Shapes"
SergeMoutou (Talk | contribs) |
SergeMoutou (Talk | contribs) (→See also) |
||
Line 1: | Line 1: | ||
+ | |||
+ | We describe first our new starting code. Any procedures are added to simplify the listings given [http://www.oooforum.org/forum/viewtopic.phtml?t=11128 here] (from Christian Junker). | ||
+ | <code>[cpp] | ||
+ | //Listing 1 New sub to simplify Shape Management | ||
+ | // C++ | ||
+ | // 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 | ||
+ | |||
+ | // Christian Junker code | ||
+ | void MakePosition(sal_Int32 x, sal_Int32 y, ::com::sun::star::awt::Point *Pos) | ||
+ | { | ||
+ | |||
+ | Pos->X = x; | ||
+ | Pos->Y = y; | ||
+ | } | ||
+ | |||
+ | void MakeSize(sal_Int32 width, sal_Int32 height, ::com::sun::star::awt::Size *Size) | ||
+ | { | ||
+ | Size->Width = width; | ||
+ | Size->Height = height; | ||
+ | } | ||
+ | |||
+ | void DrawMe(Reference< XShape > &Shape, Reference< XDrawPage > &page, const char *shapename) | ||
+ | { | ||
+ | Reference< XShapes > Shapes(page, UNO_QUERY); | ||
+ | Shapes->add(Shape); | ||
+ | Reference< XPropertySet > shapeprops(Shape, UNO_QUERY); | ||
+ | shapeprops->setPropertyValue(OUString::createFromAscii("Name"), | ||
+ | makeAny(OUString::createFromAscii(shapename))); | ||
+ | } | ||
+ | </code> | ||
+ | With these procedures we start now from a rectangle shape : our new starting main code is then : | ||
+ | <code>[cpp] | ||
+ | //Listing 2 Our new starting 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 OOowriter document | ||
+ | Reference< XComponent > xcomponent = rComponentLoader->loadComponentFromURL( | ||
+ | OUString::createFromAscii("private:factory/sdraw"), | ||
+ | OUString::createFromAscii("_blank"), | ||
+ | 0, | ||
+ | Sequence < ::com::sun::star::beans::PropertyValue >()); | ||
+ | |||
+ | // added code here | ||
+ | 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); | ||
+ | |||
+ | // Query the XDrawPage Interface | ||
+ | Reference< XDrawPage > rDrawPage(DrawPage, UNO_QUERY); | ||
+ | // query for the XNamed Interface | ||
+ | Reference< XNamed > rNamed(DrawPage, UNO_QUERY); | ||
+ | rNamed->setName(OUString::createFromAscii("My first page")); | ||
+ | |||
+ | Reference< XMultiServiceFactory > DocFactory(xcomponent, UNO_QUERY); | ||
+ | |||
+ | Point *Pos = new (Point); | ||
+ | Size *TheSize = new ( Size ); | ||
+ | |||
+ | Reference< XInterface > RectangleShape = DocFactory->createInstance( | ||
+ | OUString::createFromAscii("com.sun.star.drawing.RectangleShape") ); | ||
+ | Reference< XShape > rRectShape(RectangleShape, UNO_QUERY); | ||
+ | MakePosition(2000, 6000, Pos); | ||
+ | MakeSize(7000, 2000, TheSize); | ||
+ | rRectShape->setPosition(*Pos); | ||
+ | rRectShape->setSize(*TheSize); | ||
+ | DrawMe(rRectShape, rDrawPage, "My TextShape"); | ||
+ | |||
+ | // add code here | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </code> | ||
+ | This code draws a blue colored rectangle. | ||
+ | |||
+ | =Shape's Properties= | ||
+ | ==Background Colors and Shape Colors== | ||
+ | The shape is usually created with a default background color and and a uniform (solid) color. It is possible to change this. We search how, and then begin with a look to <OpenOffice.org1.1_SDK>/idl/com/sun/star/drawing/fillstyle.idl file : | ||
+ | <pre> | ||
+ | //Listing 3 Fillstyle IDL File | ||
+ | // IDL | ||
+ | module com { module sun { module star { module drawing { | ||
+ | enum FillStyle | ||
+ | { | ||
+ | NONE, | ||
+ | SOLID, | ||
+ | GRADIENT, | ||
+ | HATCH, | ||
+ | BITMAP | ||
+ | }; | ||
+ | }; }; }; }; | ||
+ | </pre> | ||
+ | This problem has been already tackled in [[chapter 3.9]] "To go further : the Enumeration values Problem” : we can deduce from this IDL file that our constants in C++ are : FillStyle_NONE, FillStyle_SOLID, ... and FillStyle_BITMAP and we have to construct the corresponding hpp and hxx files. | ||
+ | The second problem is how to translate this OOoBasic piece of code : | ||
+ | <pre> | ||
+ | 'Listing 4 How to translate this OOoBasic Code ? | ||
+ | REM ***** BASIC ***** | ||
+ | MaForme.FillColor = RGB(255,200,255) | ||
+ | MaForme.FillStyle = com.sun.star.drawing.FillStyle.NONE | ||
+ | </pre> | ||
+ | To put it differently, is the FillStyle a property and what about FillColor ? To search an answer we return to IDL file with FillProperties.idl : | ||
+ | <pre> | ||
+ | //Listing 5 Fillproperties IDL File | ||
+ | // IDL | ||
+ | module com { module sun { module star { module drawing { | ||
+ | service FillProperties | ||
+ | { | ||
+ | [property] com::sun::star::drawing::FillStyle FillStyle; | ||
+ | [property] long FillColor; | ||
+ | [property] short FillTransparence; | ||
+ | [property] string FillTransparenceGradientName; | ||
+ | [optional, property] com::sun::star::awt::Gradient FillTransparenceGradient; | ||
+ | [property] string FillGradientName; | ||
+ | [optional, property] com::sun::star::awt::Gradient FillGradient; | ||
+ | [property] string FillHatchName; | ||
+ | [optional, property] com::sun::star::drawing::Hatch FillHatch; | ||
+ | [property] string FillBitmapName; | ||
+ | [optional, property] com::sun::star::awt::XBitmap FillBitmap; | ||
+ | [optional, property] string FillBitmapURL; | ||
+ | [property] short FillBitmapOffsetX; | ||
+ | [property] short FillBitmapOffsetY; | ||
+ | [property] short FillBitmapPositionOffsetX; | ||
+ | [property] short FillBitmapPositionOffsetY; | ||
+ | [property] com::sun::star::drawing::RectanglePoint FillBitmapRectanglePoint; | ||
+ | [property] boolean FillBitmapLogicalSize; | ||
+ | [property] long FillBitmapSizeX; | ||
+ | [property] long FillBitmapSizeY; | ||
+ | [property] com::sun::star::drawing::BitmapMode FillBitmapMode; | ||
+ | [property] boolean FillBackground; | ||
+ | }; | ||
+ | }; }; }; }; | ||
+ | </pre> | ||
+ | The both first properties seem interesting for us. We begin with FillColor. If we add this code we obtain a rectangle with an other color. | ||
+ | <code>[cpp] | ||
+ | //Listing 6 Coloring the Shape Background | ||
+ | // 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 | ||
+ | |||
+ | // Get the property set of the rRectShape | ||
+ | Reference< XPropertySet > rShapeProps(rRectShape,UNO_QUERY); | ||
+ | Any color; | ||
+ | color<<=(long)0xFF00; //green | ||
+ | rShapeProps->setPropertyValue(OUString::createFromAscii("FillColor"),color); | ||
+ | </code> | ||
+ | The color is defined in RGB : one octet for each : 0xff0000 is red, 0x00ff00 is green and 0x0000ff is blue. | ||
+ | It's time to remove the background color with the FillStyle_NONE constant. Simply add this code : | ||
+ | <code>[cpp] | ||
+ | //Listing 7 Removing the Shape Background | ||
+ | // C++ | ||
+ | // Don't forget to add : #include <com/sun/star/drawing/FillStyle.hpp> | ||
+ | // Don't forget to add "com.sun.star.drawing.FillStyle \" in the makefile | ||
+ | |||
+ | Any FillStyle; | ||
+ | FillStyle <<= FillStyle_NONE; | ||
+ | rShapeProps->setPropertyValue(OUString::createFromAscii("FillStyle"),FillStyle); | ||
+ | </code> | ||
+ | We stop here but there is a lot to do with other FillStyle constants. | ||
+ | |||
= See also= | = See also= |
Revision as of 13:05, 20 May 2006
We describe first our new starting code. Any procedures are added to simplify the listings given here (from Christian Junker).
[cpp]
//Listing 1 New sub to simplify Shape Management
// C++
// 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
// Christian Junker code void MakePosition(sal_Int32 x, sal_Int32 y, ::com::sun::star::awt::Point *Pos) {
Pos->X = x; Pos->Y = y; }
void MakeSize(sal_Int32 width, sal_Int32 height, ::com::sun::star::awt::Size *Size) { Size->Width = width; Size->Height = height; }
void DrawMe(Reference< XShape > &Shape, Reference< XDrawPage > &page, const char *shapename)
{
Reference< XShapes > Shapes(page, UNO_QUERY);
Shapes->add(Shape);
Reference< XPropertySet > shapeprops(Shape, UNO_QUERY);
shapeprops->setPropertyValue(OUString::createFromAscii("Name"),
makeAny(OUString::createFromAscii(shapename)));
}
With these procedures we start now from a rectangle shape : our new starting main code is then :
[cpp]
//Listing 2 Our new starting 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 OOowriter document
Reference< XComponent > xcomponent = rComponentLoader->loadComponentFromURL(
OUString::createFromAscii("private:factory/sdraw"),
OUString::createFromAscii("_blank"), 0, Sequence < ::com::sun::star::beans::PropertyValue >());
// added code here 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);
// Query the XDrawPage Interface Reference< XDrawPage > rDrawPage(DrawPage, UNO_QUERY); // query for the XNamed Interface Reference< XNamed > rNamed(DrawPage, UNO_QUERY); rNamed->setName(OUString::createFromAscii("My first page"));
Reference< XMultiServiceFactory > DocFactory(xcomponent, UNO_QUERY);
Point *Pos = new (Point); Size *TheSize = new ( Size );
Reference< XInterface > RectangleShape = DocFactory->createInstance( OUString::createFromAscii("com.sun.star.drawing.RectangleShape") ); Reference< XShape > rRectShape(RectangleShape, UNO_QUERY); MakePosition(2000, 6000, Pos); MakeSize(7000, 2000, TheSize); rRectShape->setPosition(*Pos); rRectShape->setSize(*TheSize); DrawMe(rRectShape, rDrawPage, "My TextShape");
// add code here
return 0;
} This code draws a blue colored rectangle.
Shape's Properties
Background Colors and Shape Colors
The shape is usually created with a default background color and and a uniform (solid) color. It is possible to change this. We search how, and then begin with a look to <OpenOffice.org1.1_SDK>/idl/com/sun/star/drawing/fillstyle.idl file :
//Listing 3 Fillstyle IDL File // IDL module com { module sun { module star { module drawing { enum FillStyle { NONE, SOLID, GRADIENT, HATCH, BITMAP }; }; }; }; };
This problem has been already tackled in chapter 3.9 "To go further : the Enumeration values Problem” : we can deduce from this IDL file that our constants in C++ are : FillStyle_NONE, FillStyle_SOLID, ... and FillStyle_BITMAP and we have to construct the corresponding hpp and hxx files. The second problem is how to translate this OOoBasic piece of code :
'Listing 4 How to translate this OOoBasic Code ? REM ***** BASIC ***** MaForme.FillColor = RGB(255,200,255) MaForme.FillStyle = com.sun.star.drawing.FillStyle.NONE
To put it differently, is the FillStyle a property and what about FillColor ? To search an answer we return to IDL file with FillProperties.idl :
//Listing 5 Fillproperties IDL File // IDL module com { module sun { module star { module drawing { service FillProperties { [property] com::sun::star::drawing::FillStyle FillStyle; [property] long FillColor; [property] short FillTransparence; [property] string FillTransparenceGradientName; [optional, property] com::sun::star::awt::Gradient FillTransparenceGradient; [property] string FillGradientName; [optional, property] com::sun::star::awt::Gradient FillGradient; [property] string FillHatchName; [optional, property] com::sun::star::drawing::Hatch FillHatch; [property] string FillBitmapName; [optional, property] com::sun::star::awt::XBitmap FillBitmap; [optional, property] string FillBitmapURL; [property] short FillBitmapOffsetX; [property] short FillBitmapOffsetY; [property] short FillBitmapPositionOffsetX; [property] short FillBitmapPositionOffsetY; [property] com::sun::star::drawing::RectanglePoint FillBitmapRectanglePoint; [property] boolean FillBitmapLogicalSize; [property] long FillBitmapSizeX; [property] long FillBitmapSizeY; [property] com::sun::star::drawing::BitmapMode FillBitmapMode; [property] boolean FillBackground; }; }; }; }; };
The both first properties seem interesting for us. We begin with FillColor. If we add this code we obtain a rectangle with an other color.
[cpp]
//Listing 6 Coloring the Shape Background
// 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
// Get the property set of the rRectShape
Reference< XPropertySet > rShapeProps(rRectShape,UNO_QUERY);
Any color;
color<<=(long)0xFF00; //green
rShapeProps->setPropertyValue(OUString::createFromAscii("FillColor"),color);
The color is defined in RGB : one octet for each : 0xff0000 is red, 0x00ff00 is green and 0x0000ff is blue.
It's time to remove the background color with the FillStyle_NONE constant. Simply add this code :
[cpp]
//Listing 7 Removing the Shape Background
// C++
// Don't forget to add : #include <com/sun/star/drawing/FillStyle.hpp>
// Don't forget to add "com.sun.star.drawing.FillStyle \" in the makefile
Any FillStyle; FillStyle <<= FillStyle_NONE; rShapeProps->setPropertyValue(OUString::createFromAscii("FillStyle"),FillStyle); We stop here but there is a lot to do with other FillStyle constants.