Dialog box with an Radio Button

From Apache OpenOffice Wiki
Revision as of 13:12, 16 February 2007 by Fpe (Talk | contribs)

Jump to: navigation, search

It is possible to add a radio button. The corresponding IDL files are :

//Listing 33 UnoControlRadioButton Service : IDL File 
// IDL
module com {  module sun {  module star {  module awt {
service UnoControlRadioButton
{
	service com::sun::star::awt::UnoControl;

	interface com::sun::star::awt::XRadioButton;

	interface com::sun::star::awt::XLayoutConstrains;

};
}; }; }; };

and

//Listing 34 UnoControlRadioButtonModel Service : IDL File 
// IDL
module com {  module sun {  module star {  module awt {
service UnoControlRadioButtonModel
{
	service com::sun::star::awt::UnoControlModel;
	[property] boolean Enabled;
	[property] com::sun::star::awt::FontDescriptor FontDescriptor;
	[property] short FontEmphasisMark;
	[property] short FontRelief;
	[property] string HelpText;
	[property] string HelpURL;
	[property] string Label;
	[property] boolean Printable;

	/** specifies the state of the control.
		0: not checked
		1: checked	 */
	[property] short State;
	[property] boolean Tabstop;
	[property] long TextColor;
	[property] long TextLineColor;
};
}; }; }; };

Our skills are now complete enough to make our first radio button

1)First create the corresponding model : [cpp] //Listing 35 The Radio Button Control : an Example // C++ // RadioButton Reference< XInterface > xRadioModel = xMultiServiceFactory->createInstance( OUString::createFromAscii("com.sun.star.awt.UnoControlRadioButtonModel")); Reference< XPropertySet > xPSetRadio(xRadioModel,UNO_QUERY); //val <<= (sal_Bool)true; //xPSetRadio->setPropertyValue(OUString::createFromAscii("Enabled"),val); value=20; val <<= value; xPSetRadio->setPropertyValue(OUString::createFromAscii("PositionX"),val); xPSetRadio->setPropertyValue(OUString::createFromAscii("Width"),val); value=10; val <<= value; xPSetRadio->setPropertyValue(OUString::createFromAscii("PositionY"),val); xPSetRadio->setPropertyValue(OUString::createFromAscii("Height"),val); val <<=OUString::createFromAscii("Lab1"); xPSetRadio->setPropertyValue(OUString::createFromAscii("Label"),val); val <<=OUString::createFromAscii("Radio1"); xPSetRadio->setPropertyValue(OUString::createFromAscii("Name"),val); value=20; val <<= value; xPSetRadio->setPropertyValue(OUString::createFromAscii("PositionX"),val); //xPSetRadio->setPropertyValue(OUString::createFromAscii("TabIndex"),makeAny((short)3)); 2) add this radio button to the dialog [cpp] //Listing 36 adding the Radio Control to the Dialog // C++ // We insert now the radio button val <<= xRadioModel; xNameCont->insertByName(OUString::createFromAscii("Radio1") , val); 3) check the state of the radio button : this is done in the event listener associated with the button : [cpp] //Listing 37 The actionPerformed Event Listener // C++ // XActionListener virtual void SAL_CALL actionPerformed (const ::com::sun::star::awt::ActionEvent& rEvent ) throw ( RuntimeException) { // increase click counter _nCounts++; printf("OK :  %d\n",_nCounts); OUString OUStr,OUStr2; // look for radioButton Reference< XControl > label = _xControlCont->getControl(OUString::createFromAscii("Radio1")); // Don't forget to add : #include <com/sun/star/awt/XRadioButton.hpp> // Don't forget to add "com.sun.star.awt.XRadioButton \" in the makefile Reference< XRadioButton > xRadio(label,UNO_QUERY); sal_Bool state = xRadio->getState(); if (state) OUStr2 = OUString::createFromAscii(" OK "); else OUStr2 = OUString::createFromAscii(" NOK "); // set label text // Reference< XControl > label = _xControlCont->getControl(OUString::createFromAscii("Label1")); label = _xControlCont->getControl(OUString::createFromAscii("Label1")); // Don't forget to add : #include <com/sun/star/awt/XTextComponent.hpp> // Don't forget to add "com.sun.star.awt.XTextComponent \" in the makefile // Reference< XFixedText > xLabel(label,UNO_QUERY); //OLD Reference< XTextComponent > xLabel(label,UNO_QUERY); xLabel->insertText (xLabel->getSelection(), OUString::createFromAscii("Text1 ") + OUStr.valueOf((sal_Int32)_nCounts)+OUStr2 + OUString::createFromAscii("\n"));

}

This example write some text in a text control :

Dialog3.png

But with this code, we are able to check the radio button but not uncheck it. We have then to write an event listener. As usual we first inquire what kind of event listener we have to write. This is given by XRadioButton interface :

//Listing 38 XRadioButton Interface : IDL File 
// IDL
module com {  module sun {  module star {  module awt {
interface XRadioButton: com::sun::star::uno::XInterface
{
	[oneway] void addItemListener( [in] com::sun::star::awt::XItemListener l );
	[oneway] void removeItemListener( [in] com::sun::star::awt::XItemListener l );
	boolean getState();
	[oneway] void setState( [in] boolean b ); 
	[oneway] void setLabel( [in] string Label );  
};
}; }; }; }; 

which shows us to have a look at XItemEventListener :

//Listing 39 XItemListerner Interface : IDL File 
// IDL
module com {  module sun {  module star {  module awt {
interface XItemListener: com::sun::star::lang::XEventListener
{
	[oneway] void itemStateChanged( [in] com::sun::star::awt::ItemEvent rEvent );
};
}; }; }; };

Finally that file gives our method's name : itemStateChanged.


Back to Playing with Window Toolkit AWT or to main page

Personal tools