Development/Cpp/Helper/ReflectionHelper
From Apache OpenOffice Wiki
< Development | Cpp | Helper
The aim of this Helper is to provide information on UNO Interface using the Core reflection service and its Interfaces.
| This class is not a component at the moment, and then can only be used with a binary executable, as mentioned in Reflection Helper section. |
Here is the complete hpp File :
//LGPL Licence
#ifndef REFLECTIONHELPER_H
#define REFLECTIONHELPER_H
#include <rtl/ustring.hxx>
#include <com/sun/star/uno/Sequence.hxx>
#include <com/sun/star/uno/Any.hxx>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/beans/XIntrospection.hpp>
#include <com/sun/star/beans/XIntrospectionAccess.hpp>
#include <com/sun/star/lang/XTypeProvider.hpp>
#include <com/sun/star/beans/MethodConcept.hpp>
#include <com/sun/star/reflection/XIdlMethod.hpp>
#include <com/sun/star/uno/Type.hxx>
#include <com/sun/star/beans/Property.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/reflection/ParamMode.hpp>
using rtl::OUString;
using namespace com::sun::star::reflection;
using namespace com::sun::star::beans;
using namespace com::sun::star::lang;
using namespace com::sun::star::uno;
class ReflectionHelper {
public:
ReflectionHelper(Any any,Reference< XMultiServiceFactory > oSVM);
Sequence < OUString > getMethods(),
getTypes(),
getServices(),
getPropertiesWithoutValues(),
getPropertiesWithValues();
// Print out
void printOut(); //perhaps const ?
private:
Any toInspect;
Reference< XMultiServiceFactory > xServiceManager;
Reference< XIntrospection >xIntrospection;
Reference< XIntrospectionAccess > xIntrospec;
Reference< XTypeProvider > xTypeProvider;
OUString getValueName(Any object);
OUString getParamMode(ParamMode paramMode);
// methods
Sequence< Reference< XIdlMethod > > mMethods;
// Interfaces
Sequence< Type > types;
// Services
Reference< XServiceInfo > xServiceInfo;
// Sequence< OUString > services;
// Properties
Sequence< Property > Properties;
// protected methods
protected:
Sequence< OUString > convert2SeqOUStrInDEBUGMODE(Sequence < sal_Int8 > seqBytes);
// same as "convert2SeqOUStrInDEBUGMODE" but in one OUString
OUString convert2OUStrInDEBUGMODE(Sequence < sal_Int8 > seqBytes);
};
#endif //REFLECTIONHELPER_H
And now the complete cpp implementation :
// C++
// Serge Moutou
// with help of <OpenOffice1.1_SDK>/examples/java/Inspector
// and Bernard Marcelly XRay tool
// version 0.1 (22 Dec 2004)
// version 0.2 (4 Jun 2005) added printout in a dialog
// version 0.2.1 (4 Apr 2009) added name of parameters in methods
// version 0.3 (23 Apr 2009) added DEBUG MODE for []bytes properties
// To do : Exception Handling, to go further with properties values
#include "/home/smoutou/OpenOffice.org1.1_SDK/examples/DevelopersGuide/ProfUNO/CppBinding/ReflectionHelper.hpp"
#include <com/sun/star/reflection/XIdlClass.hpp>
#include <com/sun/star/beans/PropertyConcept.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
//#include <com/sun/star/reflection/ParamMode.hpp> done in ReflectionHelper.hpp
#include <com/sun/star/container/XNameContainer.hpp>
#include <cppuhelper/implbase1.hxx>
#include <com/sun/star/awt/XTextComponent.hpp>
#include <com/sun/star/awt/XActionListener.hpp>
#include <com/sun/star/awt/XToolkit.hpp>
#include <com/sun/star/awt/XControlContainer.hpp>
#include <com/sun/star/awt/PushButtonType.hpp>
#include <com/sun/star/awt/XButton.hpp>
#include <com/sun/star/awt/XWindow.hpp>
#include <com/sun/star/awt/XDialog.hpp>
//#include <com/sun/star/lang/XMultiServiceFactory.hpp>
//#define DEBUG
using namespace com::sun::star::awt;
//using namespace com::sun::star::frame;
using namespace com::sun::star::lang;
using namespace com::sun::star::container;
// Don't forget the #include <cppuhelper/implbase1.hxx>
typedef ::cppu::WeakImplHelper1< ::com::sun::star::awt::XActionListener > ActionListenerHelper;
/** action listener
*/
class ActionListenerImpl : public ActionListenerHelper
{
private :
sal_Int32 _nCounts;
Reference< XControlContainer > _xControlCont;
//XControlContainer _xControlCont;
ReflectionHelper *refHelp;
Sequence <OUString> methods,properties,services,types;
public :
ActionListenerImpl(const Reference< XControlContainer >& xControlCont,ReflectionHelper *rHelper) {
_xControlCont = xControlCont;
_nCounts = 0;
refHelp = rHelper;
methods = refHelp->getMethods();
properties = refHelp->getPropertiesWithValues();
services = refHelp->getServices();
types = refHelp->getTypes();
}
// XEventListener
virtual void SAL_CALL disposing (const com::sun::star::lang::EventObject& aEventObj ) throw(::com::sun::star::uno::RuntimeException) {
// _xControlCont = NULL;
#ifdef DEBUG
printf("object listened to will be disposed\n");
#endif
}
// XActionListener
virtual void SAL_CALL actionPerformed (const ::com::sun::star::awt::ActionEvent& rEvent ) throw ( RuntimeException) {
OUString OUStr;
// set label text
Reference< XControl > 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< XTextComponent > xLabel(label,UNO_QUERY);
switch (_nCounts%4){
case 0 :
for (int i=0;i<methods.getLength();i++){
if(i==0) xLabel->setText(methods[i] + OUString::createFromAscii("\n"));else
xLabel->insertText (xLabel->getSelection(),
methods[i] + OUString::createFromAscii("\n"));
}
xLabel->insertText (xLabel->getSelection(),
OUString::createFromAscii("******** Methods : ") +
OUStr.valueOf((sal_Int32)methods.getLength())+ OUString::createFromAscii("\n"));
break;
case 1 :for (int i=0;i<properties.getLength();i++){
if(i==0) xLabel->setText(properties[i] + OUString::createFromAscii("\n"));else
xLabel->insertText (xLabel->getSelection(),
properties[i] + OUString::createFromAscii("\n"));
}
xLabel->insertText (xLabel->getSelection(),
OUString::createFromAscii("******** Properties : ") +
OUStr.valueOf((sal_Int32)properties.getLength())+ OUString::createFromAscii("\n"));
break;
case 2 :for (int i=0;i<services.getLength();i++){
if(i==0) xLabel->setText(services[i] + OUString::createFromAscii("\n"));else
xLabel->insertText (xLabel->getSelection(),
services[i] + OUString::createFromAscii("\n"));
}
xLabel->insertText (xLabel->getSelection(),
OUString::createFromAscii("******** Services : ") +
OUStr.valueOf((sal_Int32)services.getLength())+ OUString::createFromAscii("\n"));
break;
case 3 :for (int i=0;i<types.getLength();i++){
if(i==0) xLabel->setText(types[i] + OUString::createFromAscii("\n"));else
xLabel->insertText (xLabel->getSelection(),
types[i] + OUString::createFromAscii("\n"));
}
xLabel->insertText (xLabel->getSelection(),
OUString::createFromAscii("******** Types - Interfaces : ") +
OUStr.valueOf((sal_Int32)types.getLength())+ OUString::createFromAscii("\n"));
break;
}
// increase click counter
_nCounts++;
}
};
// constructor
ReflectionHelper::ReflectionHelper(Any any,Reference< XMultiServiceFactory > oSVM)
: toInspect(any), xServiceManager(oSVM){
xIntrospection = Reference< XIntrospection >( xServiceManager->createInstance(
OUString( RTL_CONSTASCII_USTRINGPARAM(
"com.sun.star.beans.Introspection" ))), UNO_QUERY );
xIntrospec = xIntrospection->inspect(toInspect);
mMethods = xIntrospec -> getMethods(MethodConcept::ALL);
xTypeProvider = Reference< XTypeProvider> (toInspect,UNO_QUERY);
types = xTypeProvider->getTypes();
xServiceInfo = Reference< XServiceInfo>(toInspect,UNO_QUERY);
Properties = xIntrospec -> getProperties(PropertyConcept::ALL);
}
//OK
Sequence < OUString > ReflectionHelper::getServices() {
return xServiceInfo->getSupportedServiceNames();
}
//OK
Sequence < OUString > ReflectionHelper::getMethods(){
Sequence< OUString > methods(mMethods.getLength());
for (int i=0;i<mMethods.getLength();i++){
OUString params;
params=OUString::createFromAscii("(");
Sequence< ParamInfo > ParamInfos = mMethods[i]->getParameterInfos();
if (ParamInfos.getLength() > 0) {
for (int j=0;j<ParamInfos.getLength();j++){
Reference< XIdlClass > xIdlClass = ParamInfos[j].aType;
if (j == 0)
// first parameter has no leading comma
params += OUString::createFromAscii("[") + getParamMode(ParamInfos[j].aMode)+
OUString::createFromAscii("]") +
xIdlClass->getName() + OUString::createFromAscii(" ") + ParamInfos[j].aName;
else
params += OUString::createFromAscii(",[") + getParamMode(ParamInfos[j].aMode)+
OUString::createFromAscii("]")+
xIdlClass->getName() + OUString::createFromAscii(" ") + ParamInfos[j].aName;
}
}
params += OUString::createFromAscii(")");
methods[i] = mMethods[i]->getReturnType()->getName()+OUString::createFromAscii(" ")+
mMethods[i]->getName()+params;
}
return methods;
}
// OK
Sequence < OUString > ReflectionHelper::getTypes(){
Sequence< OUString > interfaces(types.getLength());
for (int i=0;i<types.getLength();i++){
interfaces[i] = types[i].getTypeName();
}
return interfaces;
}
// to improve : change all the tests with getCppuType : probably quicker than a string test
OUString ReflectionHelper::getValueName(Any object){
OUString OUStr;
OUStr = OUString::createFromAscii("!! Unable to compute values !!");
if (object.hasValue()) {
if (object.isExtractableTo(getCppuBooleanType())){
sal_Bool MyBool;
object >>= MyBool;
return OUStr.valueOf((sal_Bool) MyBool);
} else
if (object.getValueTypeName() == OUString::createFromAscii("string")) {
OUString *MyOUStr;
MyOUStr = (OUString *) object.getValue();
OUStr = OUString::createFromAscii("\"");
return OUStr + *MyOUStr + OUString::createFromAscii("\"");
} else
if (object.getValueTypeName() == OUString::createFromAscii("long")) {
sal_Int32 *MyLong;
MyLong = (sal_Int32*) object.getValue();
return OUStr.valueOf((sal_Int32) *MyLong);
} else
if (object.getValueTypeName() == OUString::createFromAscii("short")) {
sal_Int16 *MyShort;
MyShort = (sal_Int16*) object.getValue();
return OUStr.valueOf((sal_Int32) *MyShort);
} else
if (object.getValueTypeName() == OUString::createFromAscii("[]byte")) {
Sequence< sal_Int8 > SeqByte;
object >>= SeqByte;
OUString OUStr=convert2OUStrInDEBUGMODE(SeqByte); //0.3 version
return OUStr;
} else
if (object.getValueTypeName() == OUString::createFromAscii("[]string")) {
Sequence< OUString > SeqOUStr;
object >>= SeqOUStr;
OUStr = OUString::createFromAscii("Length:");
OUStr=OUStr.concat(OUStr.valueOf((sal_Int32) SeqOUStr.getLength())+
OUString::createFromAscii(" : "));
for (sal_Int32 i=0; i<SeqOUStr.getLength(); i++){
OUStr=OUStr.concat(OUString::createFromAscii("\"")
+SeqOUStr[i]
+ OUString::createFromAscii("\""));
}
return OUStr;
} else return OUStr;
} else return OUStr;
}
// OK
// Get properties with values : only those computed in getValueName
// améliorer avec :
// voir SDK2.3.1 portable IUT <SDK>/examples/cpp/extensions/source/objectbrowser/uno_lang.cxx
Sequence < OUString > ReflectionHelper::getPropertiesWithValues(){
Sequence< OUString > propWithVal(Properties.getLength());
for (int i=0;i<Properties.getLength();i++){
Type typ = getCppuType( (const Reference< XPropertySet > *)0);
Reference< XPropertySet > rPropertySet(xIntrospec->queryAdapter(typ),UNO_QUERY);
Reference< XPropertySetInfo > rPropertySetInfo=rPropertySet->getPropertySetInfo();
Any object;
if (rPropertySetInfo->hasPropertyByName(Properties[i].Name)){
object <<= rPropertySet->getPropertyValue(Properties[i].Name);
//if (object.hasValue()) printf("Valeur trouvee : \n");
propWithVal[i] = Properties[i].Name + OUString::createFromAscii(" = (")+
Properties[i].Type.getTypeName() + OUString::createFromAscii(") ")
+ getValueName(object);
//+ object.getValueTypeName();
}
}
return propWithVal;
}
// OK
// Get properties without values but types
Sequence < OUString > ReflectionHelper::getPropertiesWithoutValues(){
Sequence< OUString > propWithVal(Properties.getLength());
for (int i=0;i<Properties.getLength();i++){
Type typ = getCppuType( (const Reference< XPropertySet > *)0);
Reference< XPropertySet > xPropertySet(xIntrospec->queryAdapter(typ),UNO_QUERY);
Reference< XPropertySetInfo > xPropertySetInfo=xPropertySet->getPropertySetInfo();
if (xPropertySetInfo->hasPropertyByName(Properties[i].Name)){
propWithVal[i] = Properties[i].Name + OUString::createFromAscii(" = (")+
Properties[i].Type.getTypeName() + OUString::createFromAscii(")");
}
}
return propWithVal;
}
// Don't forget to add : #include <com/sun/star/reflection/ParamMode.hpp>
// Don't forget to add "com.sun.star.reflection.ParamMode \" in the makefile
OUString ReflectionHelper::getParamMode(ParamMode paramMode) {
OUString toReturn;
toReturn = OUString::createFromAscii("");
if (paramMode == ParamMode_IN) toReturn = OUString::createFromAscii("IN"); else
if (paramMode == ParamMode_OUT) toReturn = OUString::createFromAscii("OUT"); else
if (paramMode == ParamMode_INOUT) toReturn = OUString::createFromAscii("INOUT");
return toReturn;
}
// added for 0.3 version
Sequence< OUString > ReflectionHelper::convert2SeqOUStrInDEBUGMODE(Sequence < sal_Int8 > seqBytes){
sal_Int8 Hexa[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
sal_Int32 lin,col;
sal_Char line[80];
sal_Int32 len,sequenceSize,index;
len=seqBytes.getLength();
sequenceSize=(len>>4)+2;
if ((len&0XFFF0)==len) sequenceSize--; //if len%16==0
Sequence< OUString > SeqOUStr(sequenceSize);
// First line with length
SeqOUStr[0] = OUString::createFromAscii("Length:")+
OUString::valueOf((sal_Int32) seqBytes.getLength());
len = len&0XFFF0; // remove the modulo 16
for(lin=0;lin<len;lin=lin+16){
for(col=0;col<16;col++){
line[3*col]=Hexa[((unsigned char)seqBytes[lin+col])>>4];
line[3*col+1]=Hexa[seqBytes[lin+col]&0x0F];
line[3*col+2]=' ';
if ((seqBytes[lin+col]<128)&&(seqBytes[lin+col]>20)) line[50+col]=seqBytes[lin+col];
else line[50+col]='.';
} /* end of for */
line[66]=0; /* end of cstring...*/
line[48]=' ';line[49]=' ';
// ready to add the OUString in Sequence
if ((lin%16)==0) index = lin/16+1; else index=lin/16+2;
SeqOUStr[index]=OUString::createFromAscii(line);
} /* end of for */
// the last line is more complicated because not complete
// we only keep modulo
len=seqBytes.getLength()&0x000F;
if (len>0) { // only a line here if non-empty
for (lin=0;lin<len;lin++){
col=lin;
line[3*col]=Hexa[((unsigned char)seqBytes[lin])>>4];
line[3*col+1]=Hexa[seqBytes[lin]&0x0F];
line[3*col+2]=' ';
if ((seqBytes[lin]<128)&&(seqBytes[lin]>20)) line[50+col]=seqBytes[lin];
else line[50+col]='.';
}
// we complete the line
for (++col;col<16;col++){
line[3*col]=' ';line[3*col+1]=' ';line[3*col+2]=' ';line[50+col]=' ';
}
line[66]=0; /* end of string...*/
line[48]=' ';line[49]=' ';
// ready to add the OUString in Sequence
SeqOUStr[lin/16+2]=OUString::createFromAscii(line);
} //end if
return SeqOUStr;
}
// added for 0.3 version
OUString ReflectionHelper::convert2OUStrInDEBUGMODE(Sequence < sal_Int8 > seqBytes){
Sequence <OUString> SeqOUStr = convert2SeqOUStrInDEBUGMODE(seqBytes);
OUString OUStr;
for(sal_Int32 i=0;i<SeqOUStr.getLength();i++)
OUStr = OUStr+SeqOUStr[i]+OUString::createFromAscii("\n");
return OUStr;
}
void ReflectionHelper::printOut(){
Reference< XInterface > xdialogModel =
xServiceManager->createInstance(
OUString::createFromAscii("com.sun.star.awt.UnoControlDialogModel"));
#ifdef DEBUG
if (xdialogModel.is()) printf("OK XDialogModel\n"); else printf("Error ... XDialodModel\n");
#endif
Any val;
Reference< XPropertySet > xPSetDialog(xdialogModel,UNO_QUERY);
#ifdef DEBUG
if (xPSetDialog.is()) printf("OK XPropertySet\n"); else printf("Error ... XPropertySet\n");
#endif
sal_Int32 value=100;
val<<=value;
xPSetDialog->setPropertyValue(OUString::createFromAscii("PositionX"),val);
xPSetDialog->setPropertyValue(OUString::createFromAscii("PositionY"),val);
value=300;val<<=value;
xPSetDialog->setPropertyValue(OUString::createFromAscii("Width"),val);
value=200;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"));
#ifdef DEBUG
if (xbuttonModel.is()) printf("OK UnoControlButtonModel\n"); else printf("Error ... UnoControlButtonModel\n");
#endif
Reference< XPropertySet > xPSetButton(xbuttonModel,UNO_QUERY);
#ifdef DEBUG
if (xPSetButton.is()) printf("OK XPropertySet\n"); else printf("Error ... XPropertySet\n");
#endif
value=20; val <<= value;
xPSetButton->setPropertyValue(OUString::createFromAscii("PositionX"),val);
value=170; 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(">>");
xPSetButton->setPropertyValue(OUString::createFromAscii("Label"),val);
// second button : OK button
// we need a second button modedl
Reference< XInterface > xbuttonModel2 = xMultiServiceFactory->createInstance(
OUString::createFromAscii("com.sun.star.awt.UnoControlButtonModel"));
#ifdef DEBUG
if (xbuttonModel.is()) printf("OK UnoControlButtonModel\n"); else printf("Error ... UnoControlButtonModel\n");
#endif
Reference< XPropertySet > xPSetButton2(xbuttonModel2,UNO_QUERY);
#ifdef DEBUG
if (xPSetButton2.is()) printf("OK XPropertySet\n"); else printf("Error ... XPropertySet\n");
#endif
// *** The following property is not position-independant !!!!!
// Don't forget to add : #include <com/sun/star/awt/PushButtonType.hpp>
// Don't forget to add "com.sun.star.awt.PushButtonType \" in the makefile
// short is found with Inspector
val <<= (short)PushButtonType_OK;
xPSetButton2->setPropertyValue(OUString::createFromAscii("PushButtonType"),val);
value=220; val <<= value;
xPSetButton2->setPropertyValue(OUString::createFromAscii("PositionX"),val);
value=170; val <<= value;
xPSetButton2->setPropertyValue(OUString::createFromAscii("PositionY"),val);
value=50; val <<= value;
xPSetButton2->setPropertyValue(OUString::createFromAscii("Width"),val);
value=14; val <<= value;
xPSetButton2->setPropertyValue(OUString::createFromAscii("Height"),val);
val <<=OUString::createFromAscii("Button2");
xPSetButton2->setPropertyValue(OUString::createFromAscii("Name"),val);
xPSetButton2->setPropertyValue(OUString::createFromAscii("TabIndex"),makeAny((short)1));
val <<=OUString::createFromAscii("OK");
xPSetButton2->setPropertyValue(OUString::createFromAscii("Label"),val);
Reference< XInterface > xlabelModel = xMultiServiceFactory->createInstance(
OUString::createFromAscii("com.sun.star.awt.UnoControlEditModel"));
#ifdef DEBUG
if (xlabelModel.is()) printf("OK EditModel\n"); else printf("Error ... EditModel\n");
#endif
Reference< XPropertySet > xPSetLabel(xlabelModel,UNO_QUERY);
#ifdef DEBUG
if (xPSetLabel.is()) printf("OK XPropertySet2\n"); else printf("Error ... XPropertySet2\n");
#endif
value=10; val <<= value;
xPSetLabel->setPropertyValue(OUString::createFromAscii("PositionX"),val);
value=10; val <<= value;
xPSetLabel->setPropertyValue(OUString::createFromAscii("PositionY"),val);
value=280; val <<= value;
xPSetLabel->setPropertyValue(OUString::createFromAscii("Width"),val);
value=150; val <<= value;
xPSetLabel->setPropertyValue(OUString::createFromAscii("Height"),val);
xPSetLabel->setPropertyValue(OUString::createFromAscii("HScroll"),makeAny((sal_Bool)true));
xPSetLabel->setPropertyValue(OUString::createFromAscii("VScroll"),makeAny((sal_Bool)true));
xPSetLabel->setPropertyValue(OUString::createFromAscii("MultiLine"),makeAny((sal_Bool)true));
xPSetLabel->setPropertyValue(OUString::createFromAscii("HardLineBreaks"),makeAny((sal_Bool)true));
val <<=OUString::createFromAscii("Label1");
xPSetLabel->setPropertyValue(OUString::createFromAscii("Name"),val);
xPSetLabel->setPropertyValue(OUString::createFromAscii("TabIndex"),makeAny((short)2));
//val <<=OUString::createFromAscii("Text1");
//xPSetLabel->setPropertyValue(OUString::createFromAscii("Label"),val);
// insert all the control in container
Reference< XNameContainer > xNameCont(xdialogModel,UNO_QUERY);
#ifdef DEBUG
if (xNameCont.is()) printf("OK XNameContainer\n"); else printf("Error ... XNameContainer\n");
#endif
val <<= xbuttonModel;
// We insert first the button
xNameCont->insertByName(OUString::createFromAscii("Button1") ,val);
#ifdef DEBUG
printf("First\n");
#endif
val <<= xbuttonModel2;
xNameCont->insertByName(OUString::createFromAscii("Button2") ,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"));
#ifdef DEBUG
if (dialog.is()) printf("OK dialog\n"); else printf("Error ... dialog\n");
#endif
Reference< XControl > xControl(dialog,UNO_QUERY);
#ifdef DEBUG
if (xControl.is()) printf("OK XControl\n"); else printf("Error ... XControl\n");
#endif
Reference< XControlModel > xControlModel(xdialogModel,UNO_QUERY);
#ifdef DEBUG
if (xControlModel.is()) printf("OK xControlModel\n"); else printf("Error ... xControlModel\n");
#endif
xControl->setModel(xControlModel);
// add an action listener to the button control
Reference< XControlContainer > xControlCont(dialog,UNO_QUERY);
#ifdef DEBUG
if (xControlCont.is()) printf("OK xControlContainer\n"); else printf("Error ... xControlContainer\n");
#endif
Reference< XInterface > objectButton=xControlCont->getControl(OUString::createFromAscii("Button1"));
#ifdef DEBUG
if (objectButton.is()) printf("OK objectButton\n"); else printf("Error ... objectButton\n");
#endif
Reference< XButton > xButton(objectButton,UNO_QUERY);
ActionListenerImpl *xListener = new ActionListenerImpl( xControlCont,this );
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 );
#ifdef DEBUG
if (xToolkit.is()) printf ("XToolkit OK...\n"); else printf("XToolkit Error\n");
#endif
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();
}
If you want know how to use this snippet have look at Reflection Helper.
Here is also a screen shot :