Difference between revisions of "First Dialog"

From Apache OpenOffice Wiki
Jump to: navigation, search
Line 202: Line 202:
What is obtained is presented in the below figure :
What is obtained is presented in the below figure :
[[Image:Dialog1.png|none|thumb|310px|The First Dialog Example]]
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 16:22, 5 March 2008

Let's give now a complete translation of SampleDialog.java example. You can find it in <OpenOffice.org1.1_SDK>/examples/DevelopersGuide/BasicAndDialogs/CreatingDialogs This is a dialog box with a “Click Me” button. When you click this button it increments a counter and prints out the result in the dialog box. I have done this example in few hours starting from the previous one. Without GAP (from India) 's help it would be a few months postponed. This example is constructed as usually, without helper (using the style defined in chapter 4.3 “Prepare a new code as a starting point”) Here are the listings : we begin with the event listener :

//Listing 18 The Action performed Event Listener
// C++
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) {
// increase click counter
//	printf("OK :  %d\n",_nCounts); OLD listener
	OUString OUStr;
// set label text
	Reference< XControl > label = _xControlCont->getControl(OUString::createFromAscii("Label1"));
// Don't forget to add : #include <com/sun/star/awt/XFixedText.hpp>
// Don't forget to add "com.sun.star.awt.XFixedText \" in the makefile
	Reference< XFixedText > xLabel(label,UNO_QUERY);
	xLabel->setText (OUString::createFromAscii("Text1 ") + OUStr.valueOf((sal_Int32)_nCounts));

What differs from the previous one is only the action performed method where we replace a printf (or cout<<) with a setText. We don't print out in the shell windows but in the dialog box windows. Let's have a look in the corresponding IDL file :

//Listing 19 XFixedText Interface : IDL File 
// IDL
module com {  module sun {  module star {  module awt {
interface XFixedText: com::sun::star::uno::XInterface
	[oneway] void setText( [in] string Text );
	string getText();
	/** sets the alignment of the text in the control.
		0: left
		1: center
		2: right
	[oneway] void setAlignment( [in] short nAlign ); 
 	short getAlignment(); 
}; }; }; };  

We are ready to construct and use the dialog box now. Here is the listing (we don't give the ooConnect() procedure already given many times in this document.

//Listing 20 The complete Code
// 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);
    	if( rComponentLoader.is() ){
        	printf( "XComponentloader successfully instanciated\n" );
	Reference< XInterface > xdialogModel =
	if (xdialogModel.is()) printf("OK XDialogModel\n"); else printf("Error ... XDialodModel\n");
	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 <<=OUString::createFromAscii("Runtime Dialog Demo");
	Reference< XMultiServiceFactory > xMultiServiceFactory( xdialogModel,UNO_QUERY);
//******** in the above line xMultiServiceFactory instead xServiceManager !!!!!!
//Reference< XInterface > xbuttonModel = xServiceManager>createInstance( ....
	Reference< XInterface > xbuttonModel = xMultiServiceFactory->createInstance(
	if (xbuttonModel.is()) printf("OK UnoControlButtonModel\n"); else printf("Error ... UnoControlButtonModel\n");
	Reference< XPropertySet > xPSetButton(xbuttonModel,UNO_QUERY);
	if (xPSetButton.is()) printf("OK XPropertySet\n"); else printf("Error ... XPropertySet\n");
	value=20; val <<= value;
	value=70; val <<= value;
	value=50; val <<= value;
	value=14; val <<= value;
	val <<=OUString::createFromAscii("Button1");
	val <<=OUString::createFromAscii("Click Me");
// create the label model and set the properties
	Reference< XInterface > xlabelModel = xMultiServiceFactory->createInstance(
	if (xlabelModel.is()) printf("OK xlabelModel\n"); else printf("Error ... xlabelModel\n");
	Reference< XPropertySet > xPSetLabel(xlabelModel,UNO_QUERY);
	if (xPSetLabel.is()) printf("OK XPropertySet2\n"); else printf("Error ... XPropertySet2\n");
	value=40; val <<= value;
	value=30; val <<= value;
	value=100; val <<= value;
	value=14; val <<= value;
	val <<=OUString::createFromAscii("Label1");
	val <<=OUString::createFromAscii("Text1");
// 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");
// 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(
	if (dialog.is()) printf("OK dialog\n"); else printf("Error ... dialog\n");
	Reference< XControl > xControl(dialog,UNO_QUERY);
	if (xControl.is()) printf("OK XControl\n"); else printf("Error ... XControl\n");
	Reference< XControlModel > xControlModel(xdialogModel,UNO_QUERY);
	if (xControlModel.is()) printf("OK xControlModel\n"); else printf("Error ... xControlModel\n");
// add an action listener to the button control
	Reference< XControlContainer > xControlCont(dialog,UNO_QUERY);
	if (xControlCont.is()) printf("OK xControlContainer\n"); else printf("Error ... xControlContainer\n");
	Reference< XInterface > objectButton=xControlCont->getControl(OUString::createFromAscii("Button1"));
	if (objectButton.is()) printf("OK objectButton\n"); else printf("Error ... objectButton\n");
	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 );
	if (xToolkit.is()) printf ("XToolkit OK...\n"); else printf("XToolkit Error\n");
	Reference< XWindow > xWindow(xControl,UNO_QUERY);
	Reference< XDialog > xDialog(dialog,UNO_QUERY);
	Reference< XComponent > xComponent(dialog,UNO_QUERY);
	return 0;

What is obtained is presented in the below figure :

The First Dialog Example

Back to Playing with Window Toolkit AWT or to main page

Personal tools