Dialog box with an List Box

From Apache OpenOffice Wiki
Jump to: navigation, search

Here is what we hope to do : if you select one of both item and push the “Click Me” button you obtain the result of the selection as result :

Selected Item Pos :-1
Selected Item Pos :1
Selected Item Pos :0

The first line is obtained when nothing is selected in the list box. To learn what we can do with this list box, we first present the IDL file of the com.sun.star.awt.XListBox interface :

//Listing 28 XListBox Interface : IDL File 
// IDL
module com {  module sun {  module star {  module awt {
interface XListBox: 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 ); 
	[oneway] void addActionListener( [in] com::sun::star::awt::XActionListener l ); 
	[oneway] void removeActionListener( [in] com::sun::star::awt::XActionListener l ); 
	[oneway] void addItem( [in] string aItem, 
			 [in] short nPos ); 
	[oneway] void addItems( [in] sequence<string> aItems, 
			 [in] short nPos ); 
	[oneway] void removeItems( [in] short nPos, 
			 [in] short nCount ); 
	short getItemCount(); 
	string getItem( [in] short nPos ); 
	sequence<string> getItems(); 
	short getSelectedItemPos(); 
	sequence<short> getSelectedItemsPos(); 
	string getSelectedItem(); 
	sequence<string> getSelectedItems(); 
	[oneway] void selectItemPos( [in] short nPos, 
			 [in] boolean bSelect ); 
	[oneway] void selectItemsPos( [in] sequence<short> aPositions, 
			 [in] boolean bSelect ); 
	[oneway] void selectItem( [in] string aItem, 
			 [in] boolean bSelect ); 
	boolean isMutipleMode(); 
	[oneway] void setMultipleMode( [in] boolean bMulti ); 
	short getDropDownLineCount(); 
	[oneway] void setDropDownLineCount( [in] short nLines ); 
	[oneway] void makeVisible( [in] short nEntry );
};
}; }; }; };

We focus our attention now on the C++ code.

First create an event listener :

//Listing 29 Our new Action performed Listener
// C++
// 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;
 
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) {
	OUString OUStr;
// set label text
	Reference< XControl > xListControl = _xControlCont->getControl(OUString::createFromAscii("Label1"));
// from adipan
	Reference< XListBox> xPSetListBox(xListControl,UNO_QUERY);
	sal_Int16 selItem = xPSetListBox->getSelectedItemPos ();
	printf ("Selected Item Pos :%d\n", selItem);
 }
};

This event listener will be associated with the “Click Me” button. The printf instruction recalls us we have to have a look in the shell windows when clicking. The complete dialog is constructed in the main(). It would be probably better with subprogram (maybe later).

//Listing 30 Dialog Code : an other Example
// 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);
 
	Reference< XInterface > xdialogModel =
	 xServiceManager->createInstance(
	 	OUString::createFromAscii("com.sun.star.awt.UnoControlDialogModel"));
 
	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"));
 
	Reference< XPropertySet > xPSetButton(xbuttonModel,UNO_QUERY);
 
	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);
 
// 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);
 
	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);
 
	Reference< XInterface > xlistBoxModel = xMultiServiceFactory->createInstance(
		OUString::createFromAscii("com.sun.star.awt.UnoControlListBoxModel"));
	Reference< XPropertySet > xPSetLabel(xlistBoxModel,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);
	val <<=OUString::createFromAscii("Label1");
	xPSetLabel->setPropertyValue(OUString::createFromAscii("Name"),val);
	xPSetLabel->setPropertyValue(OUString::createFromAscii("TabIndex"),makeAny((short)2));
 
// insert all the control in container
	Reference< XNameContainer > xNameCont(xdialogModel,UNO_QUERY);
	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 ListBox control
	val <<= xlistBoxModel;
	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"));
	Reference< XControl > xControl(dialog,UNO_QUERY);
	Reference< XControlModel > xControlModel(xdialogModel,UNO_QUERY);
	xControl->setModel(xControlModel);
 
// adipan
	Sequence<OUString>seqStrlistItems(2);
	seqStrlistItems[0]=OUString::createFromAscii("Item1");
	seqStrlistItems[1]=OUString::createFromAscii("Item2");
	Reference< XControlContainer > xControlCont(dialog,UNO_QUERY);
 
	Reference <XInterface>  objectListBox=xControlCont->getControl(OUString::createFromAscii("Label1"));
	Reference< XControl > xListControl(objectListBox,UNO_QUERY);
	Reference< XListBox> xPSetListBox(xListControl,UNO_QUERY);
	xPSetListBox->removeItems(0, xPSetListBox->getItemCount());
	xPSetListBox->addItems ( seqStrlistItems, 0);
	xPSetListBox->selectItemPos (sal_Int16(0), sal_Bool(true) );
	xPSetListBox->makeVisible (sal_Int16(0));
 
// add an action listener to the button control
	//Reference< XControlContainer > xControlCont(dialog,UNO_QUERY);
	Reference< XInterface > objectButton=xControlCont->getControl(OUString::createFromAscii("Button1"));
	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 );
 
	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;
}

See also com.sun.star.lang.XMultiServiceFactory, com.sun.star.uno.XInterface, com.sun.star.frame.XComponentLoader, com.sun.star.beans.XPropertySet, com.sun.star.container.XNameContainer, com.sun.star.awt.XControl, com.sun.star.awt.XControlModel, com.sun.star.awt.XControlContainer, com.sun.star.awt.XListBox, com.sun.star.awt.XButton, com.sun.star.awt.XActionListener, com.sun.star.awt.XToolkit, com.sun.star.awt.XWindow and com.sun.star.ui.dialogs.XDialog interfaces and com.sun.star.awt.UnoControlListBoxModel service.

Back to Playing with Window Toolkit AWT or to main page

Personal tools