Programming OOoDraw and OOoImpress

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

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

OOoDraw is my favorite application of OpenOffice.org suite. I think then I will spend a lot of time with this chapter. I start writing this chapter with the Help of Christian post in OOoForum (Code snippets) In this chapter, we use again the starting code described [UNO_automation_with_a_binary_%28executable%29| here]. It consists of a subprogram “ooConnect()” and a main. Working with this code consists of adding new code in the main (see the red comment). We give again the main program slightly modified to fit OOoDraw requirements : [cpp] //Listing 1 Again the starting Code // C++ // adapted for OOoDraw 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 OOoDraw document

   Reference< XComponent > xcomponent = rComponentLoader->loadComponentFromURL(

OUString::createFromAscii("private:factory/sdraw"),

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

// add code here

   return 0;

}

Finding the drawing Slide

Bear in mind that if OOoSheet opens with three sheets by default, OOoDraw opens with only one page/slide (index 0). An existing Slide If you only know the index of the slide :

[cpp] //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 : [cpp] //Listing 3 How many Slides ? // C++ rPageIndexAccess->getCount(); If you only know the page name : [cpp] //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 a new slide with a known Index : [cpp] //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 [chapter 6.1.1] previous code and this one : the former uses an Any type variable (DrawPage) although the latter code obtains directly a XDrawPage interface. One more step is needed to obtain a XDrawPage when working with Any : a UNO query. Add the above code to the former and you obtain like the latter code a XDrawPage interface. [cpp] //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); How to rename your slide ? You have to change the Name property of the slide : [cpp] //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 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. And now you want to copy a page : you have to query the 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 . [cpp] //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 If you want to remove a page known by its index [cpp] //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 XDrawPage interface corresponding to a hidden page and you want to put it on the top : [cpp] //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);

Property of a Slide

Properties belong to the slide :

Personal tools