Dialog box with an edit control

From Apache OpenOffice Wiki
Jump to: navigation, search

We want now to construct the dialog box below.

An example of dialog with a text control

We have to have a look at the corresponding IDL files. We begin with com.sun.star.awt.XTextComponent Interface.

//Listing 21 XTextComponent Interface : IDL File 
// IDL
module com {  module sun {  module star {  module awt {
interface XTextComponent: com::sun::star::uno::XInterface
{
	[oneway] void addTextListener( [in] com::sun::star::awt::XTextListener l );
	[oneway] void removeTextListener( [in] com::sun::star::awt::XTextListener l );
	[oneway] void setText( [in] string aText );
	[oneway] void insertText( [in] com::sun::star::awt::Selection Sel,
			 [in] string Text );
	string getText();
	string getSelectedText();
	[oneway] void setSelection( [in] com::sun::star::awt::Selection aSelection );
	com::sun::star::awt::Selection getSelection();
	boolean isEditable();
	[oneway] void setEditable( [in] boolean bEditable );
	[oneway] void setMaxTextLen( [in] short nLen );
	short getMaxTextLen();
};
}; }; }; };

As shown in Figure above this example has a defect : the text is always added on the top instead on the bottom ! At the moment, I don't know how to resolve this problem : If I use the setText method I always obtain only one line, if I use insertText method I have to provide a selection. I take this selection with the getSelection but there is nothing selected and then this method return the beginning of the text.

We show now the com.sun.star.awt.UnoControlEditModel IDL file :

//Listing 22 UnoControlEditModel Service : IDL File 
// IDL
module com {  module sun {  module star {  module awt {
service UnoControlEditModel
{
	service com::sun::star::awt::UnoControlModel;
	[property] short Align;
	[property] long BackgroundColor;
	[property] short Border; 
	[optional, property] short EchoChar; 
	[property] boolean Enabled; 
	[property] com::sun::star::awt::FontDescriptor FontDescriptor; 
	[property] short FontEmphasisMark;
	[property] short FontRelief;
	[property] boolean HScroll;
	[property] boolean HardLineBreaks;
	[property] string HelpText;
	[property] string HelpURL;
	[property] short MaxTextLen; 
	[property] boolean MultiLine; 
	[property] boolean Printable; 
	[property] boolean ReadOnly; 
	[property] boolean Tabstop; 
	[property] string Text; 
	[property] long TextColor; 
	[property] long TextLineColor;
	[property] boolean VScroll;    
}; 
}; }; }; };

We use any of these properties in the example above. To construct it, I modify first the event listener of the previous example like the listing above :

//Listing 23 We first remplace XFixedText with XTextComponent interface
// C++
....
// 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)+
		OUString::createFromAscii("\n"));
....

and then I replace the “com.sun.star.awt.UnoControlFixedTextModel” control with "com.sun.star.awt.UnoControlEditModel". This is done with code :

//Listing 24 The UnoControlEditModel : an Example
// C++
	Reference< XInterface > xlabelModel = xMultiServiceFactory->createInstance(
		OUString::createFromAscii("com.sun.star.awt.UnoControlEditModel"));
	Reference< XPropertySet > xPSetLabel(xlabelModel,UNO_QUERY);
	value=20; val <<= value;
	xPSetLabel->setPropertyValue(OUString::createFromAscii("PositionX"),val);
	value=10; val <<= value;
	xPSetLabel->setPropertyValue(OUString::createFromAscii("PositionY"),val);
	value=110; val <<= value;
	xPSetLabel->setPropertyValue(OUString::createFromAscii("Width"),val);
	value=50; 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)1));

We will construct helper to simplify the task of writing such a code later on. But at first we have to speak on an other kind of button : OK button. The property to change is "PushButtonType". Values taken by this property are given by a IDL file as usual :

Listing 25 PushButtonType enumeration : IDL File 
// IDL
module com {  module sun {  module star {  module awt {
enum PushButtonType
{
	STANDARD,
	OK, 
	CANCEL, 
	HELP  
}; 
}; }; }; };

The values are given by an enum type. We have already encounter such a problem and then know how to resolve it. I give here the code to add if you want an OK button :

//Listing 26 adding a second “OK” button
// C++
// second button : OK button
// we need a second button modedl
	Reference< XInterface > xbuttonModel2 = xMultiServiceFactory->createInstance(
		OUString::createFromAscii("com.sun.star.awt.UnoControlButtonModel"));
	if (xbuttonModel.is()) printf("OK UnoControlButtonModel\n"); else printf("Error ... UnoControlButtonModel\n");
 
	Reference< XPropertySet > xPSetButton2(xbuttonModel2,UNO_QUERY);
	if (xPSetButton2.is()) printf("OK XPropertySet\n"); else printf("Error ... XPropertySet\n");
	// *** 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=80; val <<= value;
	xPSetButton2->setPropertyValue(OUString::createFromAscii("PositionX"),val);
	value=70; 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);

And then we have to add it and then to modify the insert listing's part like that :

//Listing 27 Inserting all control in container
// C++
// 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");
	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);

Back to Playing with Window Toolkit AWT or to main page

Personal tools