... some common things one can do with impress documents
#include <stdio.h>
#include <string.h>
#include <cppuhelper/bootstrap.hxx>
#include <osl/file.hxx>
#include <osl/process.h>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#ifdef _COM_SUN_STAR_BRIDGE_XUNOURLRESOLVER_HPP_
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
#include <com/sun/star/document/XDocumentInfoSupplier.hpp>
#include <com/sun/star/drawing/XDrawPages.hpp>
#include <com/sun/star/drawing/XDrawPage.hpp>
#include <com/sun/star/container/XNamed.hpp>
#include <com/sun/star/frame/XModel.hpp>
#include <com/sun/star/frame/XController.hpp>
#include <com/sun/star/drawing/XDrawView.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/drawing/XShape.hpp>
#include <com/sun/star/text/XText.hpp>
#include <com/sun/star/presentation/XPresentationSupplier.hpp>
#include <com/sun/star/presentation/XPresentation.hpp>
// handy cause OUStrings are very often used in the API
#define createStr(x) (OUString::createFromAscii(x))
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
using namespace rtl;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::frame;
using namespace com::sun::star::drawing;
using com::sun::star::registry::XSimpleRegistry;
using com::sun::star::beans::XPropertySet;
using com::sun::star::bridge::XUnoUrlResolver;
using com::sun::star::lang::XMultiServiceFactory;
using com::sun::star::text::XText;
// Function declarations
void ChangeTitle(const Reference <XDrawPagesSupplier> Document);
void Presentation(const Reference <XComponent> &Component);
void MakePosition(sal_Int32 x, sal_Int32 y, ::com::sun::star::awt::Point *Position );
void MakeSize(sal_Int32 x, sal_Int32 y, ::com::sun::star::awt::Size *Size );
void DrawMe(Reference <XShape> &Shape, Reference <XDrawPage> &page, const char *shapename);
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
int SAL_CALL main( int argc, char *argv[] )
{
/***************************
*** INITIALIZATION PART ***
***************************/
OUString sConnectionString(RTL_CONSTASCII_USTRINGPARAM("uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager"));
// Creates a simple registry service instance.
Reference <XSimpleRegistry> registry(
::cppu::createSimpleRegistry() );
// Connects the registry to a persistent data source represented by an URL.
registry->open( OUString( RTL_CONSTASCII_USTRINGPARAM(
"DocumentLoader.rdb") ), sal_True, sal_False );
Reference <XComponentContext> context(
::cppu::bootstrap_InitialComponentContext(registry) );
// creates the local ServiceManager
Reference <XMultiComponentFactory> xMultiComponentFactoryClient(
context->getServiceManager() );
/* Creates an instance of a component which supports the services specified
by the factory.
*/
Reference <XInterface> xInterface =
xMultiComponentFactoryClient->createInstanceWithContext(
createStr("com.sun.star.bridge.UnoUrlResolver"), context );
Reference <XUnoUrlResolver> resolver(xInterface, UNO_QUERY);
// Resolves the component context from the office, on the uno URL given by argv[1].
try
{
xInterface = Reference <XInterface>(
resolver->resolve( sConnectionString ), UNO_QUERY );
}
catch ( Exception &e )
{
printf("Error: cannot establish a connection using '%s':\n %s\n",
OUStringToOString(sConnectionString, RTL_TEXTENCODING_ASCII_US).getStr(),
OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr());
exit(1);
}
// gets the server component context as property of the office component factory
Reference <XPropertySet> mgrPropSet( xInterface, UNO_QUERY );
mgrPropSet->getPropertyValue( createStr("DefaultContext") ) >>= context;
// gets the service manager from the office
Reference <XMultiComponentFactory> oomgr( context->getServiceManager() );
/**********************
**** CONNECTED !! ****
**********************/
Reference <XComponentLoader> comploader(
oomgr->createInstanceWithContext(
OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop" ) ),
context ), UNO_QUERY );
Reference <XComponent> component = comploader->loadComponentFromURL(
createStr("private:factory/simpress"), OUString( RTL_CONSTASCII_USTRINGPARAM("_blank") ),
0, Sequence <::com::sun::star::beans::PropertyValue>() );
Reference <XDrawPagesSupplier> oDoc(component, UNO_QUERY);
// set the Title with XDocumentInfoSupplier
ChangeTitle(oDoc);
// get DrawPages Container
Reference <XDrawPages> drawpages = oDoc->getDrawPages();
// for index access and counting usage
Reference <::com::sun::star::container::XIndexAccess> pagecount(drawpages, UNO_QUERY);
// insert a new drawpage -- identifier: newDrawPage
Reference <XDrawPage> newDrawPage = drawpages->insertNewByIndex(0);
Reference <::com::sun::star::container::XNamed> namecontainer(newDrawPage, UNO_QUERY);
// count pages and print them out to stdout
sal_Int16 countresult = pagecount->getCount();
printf("\nThere are %d pages in the Impress document\n\n", countresult);
//Loop through all pages and give Pagenumber+Pagename to each of them
for (int icounter = 0; icounter < pagecount->getCount(); icounter++)
{
Any mPage = pagecount->getByIndex(icounter);
Reference <XDrawPage> drawpage(mPage, UNO_QUERY);
Reference <::com::sun::star::container::XNamed> names(drawpage, UNO_QUERY);
OUString pagename = names->getName();
OString sName = OUStringToOString( pagename , RTL_TEXTENCODING_ASCII_US );
printf("\n Page Number %d has the name %s", icounter, sName.getStr());
}
// Playing around with the inserted page...
namecontainer->setName(createStr("My second page"));
OUString Name;
Name = namecontainer->getName();
OString sName = rtl::OUStringToOString( Name , RTL_TEXTENCODING_ASCII_US );
// get the Number of the inserted page
Reference <XPropertySet> PageProps(newDrawPage, UNO_QUERY);
Any mValue;
mValue = PageProps->getPropertyValue( createStr("Number") );
// conversion...
sal_Int32 pagenumber;
mValue >>= pagenumber;
//print the PropertyValue out:
printf("\n\nThe newly inserted page \'%s\' is in position %d", sName.getStr(), pagenumber);
// set the focus on the newly inserted page
Reference <XModel> xmodel(oDoc, UNO_QUERY);
Reference <XController> ctl = xmodel->getCurrentController();
Reference <XDrawView> oview(ctl, UNO_QUERY);
oview->setCurrentPage(newDrawPage);
/**********************
**** DRAWING PART ****
**********************/
// get Document factory
Reference <XMultiServiceFactory> DocFactory(component, UNO_QUERY);
// allocate memory!!
::com::sun::star::awt::Point *Pos = new ( ::com::sun::star::awt::Point );
::com::sun::star::awt::Size *Size = new ( ::com::sun::star::awt::Size );
/* ~~ ON FIRST PAGE ~~ */
MakePosition(7000, 7000, Pos);
MakeSize(16000, 3000, Size);
// instantiate TextShape and assign properties
Reference <XInterface> textshape = DocFactory->createInstance(
createStr("com.sun.star.drawing.TextShape") );
Reference <XShape> txtShape(textshape, UNO_QUERY);
txtShape->setPosition(*Pos);
txtShape->setSize(*Size);
Any mfirstpage = pagecount->getByIndex(0);
Reference <XDrawPage> page1(mfirstpage, UNO_QUERY);
DrawMe(txtShape, page1, "My TextShape" );
Reference <XText> Text(txtShape, UNO_QUERY);
// use the much simpler method XTextRange setString() rather than insertTextContent()
Text->setString(createStr("Hello to all people around the world"));
/* ~~ ON SECOND PAGE ~~ */
MakePosition(2000, 6000, Pos);
MakeSize(7000, 2000, Size);
Reference <XInterface> rectangleshape = DocFactory->createInstance(
createStr("com.sun.star.drawing.RectangleShape") );
Reference <XShape> rectShape(rectangleshape, UNO_QUERY);
rectShape->setPosition(*Pos);
rectShape->setSize(*Size);
DrawMe(rectShape, newDrawPage, "My Rectangle");
// release allocated memory
delete Pos;
delete Size;
// play a Presentation
Presentation(component);
// dispose the local service manager
Reference <XComponent>::query( xMultiComponentFactoryClient )->dispose();
return 0;
}
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
void ChangeTitle(const Reference <XDrawPagesSupplier> oDoc)
{
Reference <::com::sun::star::document::XDocumentInfoSupplier> infosupplier( oDoc, UNO_QUERY );
Reference <::com::sun::star::document::XDocumentInfo> docinfo = infosupplier->getDocumentInfo();
Reference <XPropertySet> DocInfoProps(docinfo, UNO_QUERY );
Any mTitle;
//another method to create a PropertyValue with makeAny()
mTitle = makeAny(createStr("First attempt"));
Any *test_pointer = &mTitle; // testing
}
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
void Presentation(const Reference <XComponent> &component)
{
Reference <::com::sun::star::presentation::XPresentationSupplier> showsupplier(component, UNO_QUERY);
Reference <::com::sun::star::presentation::XPresentation> show = showsupplier->getPresentation();
Reference <XPropertySet> mPres_Props(show, UNO_QUERY);
sal_Int16 startpage = 0;
mPres_Props->setPropertyValue(createStr("FirstPage"), makeAny(startpage) );
show->start();
}
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
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( createStr("Name"),
makeAny( createStr(shapename)) );
}
#else
#error "No connection to UNO possible, necessary XUnoUrlResolver not included properly"
#endif