Difference between revisions of "Dialog box with an Radio Button"
SergeMoutou (Talk | contribs) |
SergeMoutou (Talk | contribs) |
||
Line 1: | Line 1: | ||
+ | It is possible to add a radio button. The corresponding IDL files are : | ||
+ | <pre> | ||
+ | //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; | ||
+ | |||
+ | }; | ||
+ | }; }; }; }; | ||
+ | </pre> | ||
+ | and | ||
+ | <pre> | ||
+ | //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; | ||
+ | }; | ||
+ | }; }; }; }; | ||
+ | </pre> | ||
+ | Our skills are now complete enough to make our first radio button | ||
+ | 1)First create the corresponding model : | ||
+ | <code>[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)); | ||
+ | </code> | ||
+ | 2) add this radio button to the dialog | ||
+ | <code>[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); | ||
+ | </code> | ||
+ | 3) check the state of the radio button : this is done in the event listener associated with the button : | ||
+ | <code>[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")); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | This example write some text in a text control : | ||
+ | |||
[[Image:Dialog3.png]] | [[Image:Dialog3.png]] | ||
Back to [[Playing_with_Window_Toolkit_AWT|Playing with Window Toolkit AWT]] or to [[Using_Cpp_with_the_OOo_SDK|main page]] | Back to [[Playing_with_Window_Toolkit_AWT|Playing with Window Toolkit AWT]] or to [[Using_Cpp_with_the_OOo_SDK|main page]] |
Revision as of 15:56, 25 May 2006
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 :
Back to Playing with Window Toolkit AWT or to main page