First Dialog
Let's give now a complete translation of SampleDialog.java example. You can find it in <OpenOffice.org1.1_SDK>/examples/DevelopersGuide/BasicAndDialogs/CreatingDialogs This is a dialog box with a “Click Me” button. When you click this button it increments a counter and prints out the result in the dialog box. I have done this example in few hours starting from the previous one. Without GAP (from India) 's help it would be a few months postponed. This example is constructed as usually, without helper (using the style defined in chapter 4.3 “Prepare a new code as a starting point”) Here are the listings : we begin with the event listener :
//Listing 18 The Action performed Event Listener // C++ typedef ::cppu::WeakImplHelper1< ::com::sun::star::awt::XActionListener > ActionListenerHelper; /** action listener */ class ActionListenerImpl : public ActionListenerHelper { private : sal_Int32 _nCounts; Reference< XControlContainer > _xControlCont; //XControlContainer _xControlCont; public : ActionListenerImpl(const Reference< XControlContainer >& xControlCont) { _xControlCont = xControlCont; _nCounts = 0; } // XEventListener virtual void SAL_CALL disposing (const com::sun::star::lang::EventObject& aEventObj ) throw(::com::sun::star::uno::RuntimeException) { // _xControlCont = NULL; printf("object listened to will be disposed\n"); } // XActionListener virtual void SAL_CALL actionPerformed (const ::com::sun::star::awt::ActionEvent& rEvent ) throw ( RuntimeException) { // increase click counter _nCounts++; // printf("OK : %d\n",_nCounts); OLD listener OUString OUStr; // set label text Reference< XControl > label = _xControlCont->getControl(OUString::createFromAscii("Label1")); // Don't forget to add : #include <com/sun/star/awt/XFixedText.hpp> // Don't forget to add "com.sun.star.awt.XFixedText \" in the makefile Reference< XFixedText > xLabel(label,UNO_QUERY); xLabel->setText (OUString::createFromAscii("Text1 ") + OUStr.valueOf((sal_Int32)_nCounts)); } };
What differs from the previous one is only the action performed method where we replace a printf (or cout<<) with a setText. We don't print out in the shell windows but in the dialog box windows. Let's have a look in the corresponding IDL file :
//Listing 19 XFixedText Interface : IDL File // IDL module com { module sun { module star { module awt { interface XFixedText: com::sun::star::uno::XInterface { [oneway] void setText( [in] string Text ); string getText(); /** sets the alignment of the text in the control. <pre> 0: left 1: center 2: right */ [oneway] void setAlignment( [in] short nAlign ); short getAlignment(); }; }; }; }; };
We are ready to construct and use the dialog box now. Here is the listing (we don't give the ooConnect() procedure already given many times in this document.
//Listing 20 The complete Code // C++ main( ) { //retrieve an instance of the remote service manager Reference< XMultiServiceFactory > xServiceManager; xServiceManager = ooConnect(); if( xServiceManager.is() ){ printf( "Connected sucessfully to the office\n" ); } //get the desktop service using createInstance returns an XInterface type Reference< XInterface > Desktop = xServiceManager->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" ); } Reference< XInterface > xdialogModel = xServiceManager->createInstance( OUString::createFromAscii("com.sun.star.awt.UnoControlDialogModel")); if (xdialogModel.is()) printf("OK XDialogModel\n"); else printf("Error ... XDialodModel\n"); Any val; Reference< XPropertySet > xPSetDialog(xdialogModel,UNO_QUERY); if (xPSetDialog.is()) printf("OK XPropertySet\n"); else printf("Error ... XPropertySet\n"); sal_Int32 value=100; val<<=value; xPSetDialog->setPropertyValue(OUString::createFromAscii("PositionX"),val); xPSetDialog->setPropertyValue(OUString::createFromAscii("PositionY"),val); value=150;val<<=value; xPSetDialog->setPropertyValue(OUString::createFromAscii("Width"),val); value=100;val<<=value; xPSetDialog->setPropertyValue(OUString::createFromAscii("Height"),val); val <<=OUString::createFromAscii("Runtime Dialog Demo"); xPSetDialog->setPropertyValue(OUString::createFromAscii("Title"),val); Reference< XMultiServiceFactory > xMultiServiceFactory( xdialogModel,UNO_QUERY); ///***************** //******** in the above line xMultiServiceFactory instead xServiceManager !!!!!! //Reference< XInterface > xbuttonModel = xServiceManager>createInstance( .... Reference< XInterface > xbuttonModel = xMultiServiceFactory->createInstance( OUString::createFromAscii("com.sun.star.awt.UnoControlButtonModel")); if (xbuttonModel.is()) printf("OK UnoControlButtonModel\n"); else printf("Error ... UnoControlButtonModel\n"); Reference< XPropertySet > xPSetButton(xbuttonModel,UNO_QUERY); if (xPSetButton.is()) printf("OK XPropertySet\n"); else printf("Error ... XPropertySet\n"); value=20; val <<= value; xPSetButton->setPropertyValue(OUString::createFromAscii("PositionX"),val); value=70; val <<= value; xPSetButton->setPropertyValue(OUString::createFromAscii("PositionY"),val); value=50; val <<= value; xPSetButton->setPropertyValue(OUString::createFromAscii("Width"),val); value=14; val <<= value; xPSetButton->setPropertyValue(OUString::createFromAscii("Height"),val); val <<=OUString::createFromAscii("Button1"); xPSetButton->setPropertyValue(OUString::createFromAscii("Name"),val); xPSetButton->setPropertyValue(OUString::createFromAscii("TabIndex"),makeAny((short)0)); val <<=OUString::createFromAscii("Click Me"); xPSetButton->setPropertyValue(OUString::createFromAscii("Label"),val); // create the label model and set the properties Reference< XInterface > xlabelModel = xMultiServiceFactory->createInstance( OUString::createFromAscii("com.sun.star.awt.UnoControlFixedTextModel")); if (xlabelModel.is()) printf("OK xlabelModel\n"); else printf("Error ... xlabelModel\n"); Reference< XPropertySet > xPSetLabel(xlabelModel,UNO_QUERY); if (xPSetLabel.is()) printf("OK XPropertySet2\n"); else printf("Error ... XPropertySet2\n"); value=40; val <<= value; xPSetLabel->setPropertyValue(OUString::createFromAscii("PositionX"),val); value=30; val <<= value; xPSetLabel->setPropertyValue(OUString::createFromAscii("PositionY"),val); value=100; val <<= value; xPSetLabel->setPropertyValue(OUString::createFromAscii("Width"),val); value=14; val <<= value; xPSetLabel->setPropertyValue(OUString::createFromAscii("Height"),val); val <<=OUString::createFromAscii("Label1"); xPSetLabel->setPropertyValue(OUString::createFromAscii("Name"),val); xPSetLabel->setPropertyValue(OUString::createFromAscii("TabIndex"),makeAny((short)1)); val <<=OUString::createFromAscii("Text1"); xPSetLabel->setPropertyValue(OUString::createFromAscii("Label"),val); // insert all the control in container Reference< XNameContainer > xNameCont(xdialogModel,UNO_QUERY); if (xNameCont.is()) printf("OK XNameContainer\n"); else printf("Error ... XNameContainer\n"); val <<= xbuttonModel; // We insert first the button xNameCont->insertByName(OUString::createFromAscii("Button1") ,val); printf("First\n"); // We insert now the text control val <<= xlabelModel; xNameCont->insertByName(OUString::createFromAscii("Label1") , val); // create the dialog control and set the model Reference< XInterface >dialog = xServiceManager->createInstance( OUString::createFromAscii("com.sun.star.awt.UnoControlDialog")); if (dialog.is()) printf("OK dialog\n"); else printf("Error ... dialog\n"); Reference< XControl > xControl(dialog,UNO_QUERY); if (xControl.is()) printf("OK XControl\n"); else printf("Error ... XControl\n"); Reference< XControlModel > xControlModel(xdialogModel,UNO_QUERY); if (xControlModel.is()) printf("OK xControlModel\n"); else printf("Error ... xControlModel\n"); xControl->setModel(xControlModel); // add an action listener to the button control Reference< XControlContainer > xControlCont(dialog,UNO_QUERY); if (xControlCont.is()) printf("OK xControlContainer\n"); else printf("Error ... xControlContainer\n"); Reference< XInterface > objectButton=xControlCont->getControl(OUString::createFromAscii("Button1")); if (objectButton.is()) printf("OK objectButton\n"); else printf("Error ... objectButton\n"); Reference< XButton > xButton(objectButton,UNO_QUERY); ActionListenerImpl *xListener = new ActionListenerImpl( xControlCont ); Reference< XActionListener > xActionListener = static_cast< XActionListener* > ( xListener ); xButton->addActionListener( xActionListener ); // create a peer Reference< XToolkit >xToolkit = Reference< XToolkit >( xServiceManager->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.Toolkit" ))), UNO_QUERY ); if (xToolkit.is()) printf ("XToolkit OK...\n"); else printf("XToolkit Error\n"); Reference< XWindow > xWindow(xControl,UNO_QUERY); xWindow->setVisible(true); xControl->createPeer(xToolkit,NULL); Reference< XDialog > xDialog(dialog,UNO_QUERY); xDialog->execute(); Reference< XComponent > xComponent(dialog,UNO_QUERY); xComponent->dispose(); return 0; }
What is obtained is presented in the below figure :
Back to Playing with Window Toolkit AWT or to main page