Working with Shapes

From Apache OpenOffice Wiki
Revision as of 13:34, 20 May 2006 by SergeMoutou (Talk | contribs)

Jump to: navigation, search

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 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 : [oobas] '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.

Shape and Shadow

Shadow is described by ShadowProperties.idl file :

// IDL
module com {  module sun {  module star {  module drawing {
service ShadowProperties
{
	[property] boolean Shadow;
	[property] long ShadowColor;
	[property] short ShadowTransparence;
	[property] long ShadowXDistance;
	[property] long ShadowYDistance;
};
}; }; }; };  

Then the corresponding C++ code to manipulate these properties could be : [cpp] //Listing 8 Shadowing a Shape // C++ // Shadow Any ShadowProperties[4]; ShadowProperties[0] <<= (sal_Bool)true; ShadowProperties[1] <<= (long) 0xFF0000; // red ShadowProperties[2] <<= (long) 300; ShadowProperties[3] <<= (long) 300; rShapeProps->setPropertyValue(OUString::createFromAscii("Shadow"),ShadowProperties[0]); rShapeProps->setPropertyValue(OUString::createFromAscii("ShadowColor"), ShadowProperties[1]); rShapeProps->setPropertyValue(OUString::createFromAscii("ShadowXDistance"), ShadowProperties[2]); rShapeProps->setPropertyValue(OUString::createFromAscii("ShadowYDistance"), ShadowProperties[3]); This code adds a red shadow at the rectangle shape.

Shape's Rotation and sharing

The corresponding IDL file is

// IDL
module com {  module sun {  module star {  module drawing {
service RotationDescriptor
{
/** This is the angle for rotation of this Shape.
	The shape is rotated counter-clockwise around the center of the bounding box. */

	[property] long RotateAngle;
	[optional, property] long ShearAngle;
};
}; }; }; };

The angle is given in 1/100° starting from an horizontal line. [cpp] //Listing 9 Rotating a Shape // C++ Any Angle; Angle <<= (long)3000; // 30 degres rShapeProps->setPropertyValue(OUString::createFromAscii("RotateAngle"),Angle); which gives a 30° rotation.

Line Style

The corresponding IDL files are :

//Listing 10 Lineproperties IDL file 
// IDL
module com {  module sun {  module star {  module drawing {
service LineProperties
{
	[property] com::sun::star::drawing::LineStyle LineStyle;
	[property] com::sun::star::drawing::LineDash LineDash;
	[property] long LineColor;
	[property] short LineTransparence;
	[property] long LineWidth; 
	[property] com::sun::star::drawing::LineJoint LineJoint; 
	[optional, property] string LineStartName;
	[optional, property] com::sun::star::drawing::PolyPolygonBezierCoords LineStart; 
	[optional, property] com::sun::star::drawing::PolyPolygonBezierCoords LineEnd; 
	[optional, property] boolean LineStartCenter; 
	[optional, property] long LineStartWidth; 
	[optional, property] boolean LineEndCenter; 
	[optional, property] long LineEndWidth; 
}; 
}; }; }; }; 

completed with :

//Listing 11 LineStyle Enumeration
// IDL
module com {  module sun {  module star {  module drawing {
enum LineStyle
{
	NONE,
	SOLID, 
	DASH
};
}; }; }; };

We give the significance of these constants :

ligne styles
Constant Signification
NONE Line is not visible
SOLID continuous line (default value)
DASH Dash line; LineDash property is controling the dash's shape

We give now the LineDash properties with an IDL file

//Listing 12 IDL File : Linedash
// IDL
module com {  module sun {  module star {  module drawing {
struct LineDash
{
	com::sun::star::drawing::DashStyle Style;
	short Dots; 
	long DotLen; 
	short Dashes; 
	long DashLen; 
	long Distance; 
};
}; }; }; };  

and their meaning :

Proprerty Signification Style A constant ( com.sun.star.drawing.DashStyle.xxx) gives the dash style Dots How many dots DotLen Dot length in 1/100 mm Dashes How many dashes DashLen Dash length in 1/100 mm Distance distance between dots in 1/100 mm


See also

  • Using C++ with OOo SDK : Main Page
  • Shapes (Chapter 7 from UNO/C++ document)
  • Writing a Program to Control OpenOffice.org, by Franco Pingiori — Part 1 and Part 2, Linux Journal</code>
Personal tools