Difference between revisions of "Component and Dialog"
SergeMoutou (talk | contribs) |
SergeMoutou (talk | contribs) |
||
| Line 56: | Line 56: | ||
{{Documentation/Note|The point is to see what I have called encapsulation in this code a Sub in OOoBasic which call a method of the component.}} | {{Documentation/Note|The point is to see what I have called encapsulation in this code a Sub in OOoBasic which call a method of the component.}} | ||
| − | = | + | =Methods Direct Call with Introspection Service= |
| − | + | As mentioned in [[Documentation/DevGuide/WritingUNO/Accessing_Dialogs|Developer's Guide]], since 2.0.4 version it is possible to bound methods of a component with events of a control in a Dialog. To put it differently, encapsulation as in the OOoBasic code below : | |
<source lang="oobas"> | <source lang="oobas"> | ||
REM ***** BASIC ***** | REM ***** BASIC ***** | ||
| Line 65: | Line 65: | ||
End Sub | End Sub | ||
</source> | </source> | ||
| − | + | could be avoided now. But, for that, you have a price to pay : we have eventually to change our C++ code of our component. To begin with the more easy, we will modify our OOoBasic program, our Dialog but not our counter. | |
| − | == | + | ==Direct Call of Counter Methods without changing its C++ code== |
| − | + | We have to change the previous OOobasic program to use the new <idl>com.sun.star.awt.DialogProvider2</idl> service which provide the very interesting "createDialogWithHandler" method. Here is the corresponding code : | |
<source lang="oobas"> | <source lang="oobas"> | ||
REM ***** BASIC ***** | REM ***** BASIC ***** | ||
| Line 77: | Line 77: | ||
oSimpleComponent = CreateUnoService( "foo.Counter" ) | oSimpleComponent = CreateUnoService( "foo.Counter" ) | ||
oCreateDialog2=CreateUnoService("com.sun.star.awt.DialogProvider2") | oCreateDialog2=CreateUnoService("com.sun.star.awt.DialogProvider2") | ||
| − | ' | + | 'Thank you ms777 for the line below (see http://www.oooforum.org/forum/viewtopic.phtml?t=84168) |
oCreateDialog2.initialize(Array(ThisComponent)) | oCreateDialog2.initialize(Array(ThisComponent)) | ||
oDialog=oCreateDialog2.createDialogWithHandler("vnd.sun.star.script:Standard.Dialog1?location=document", _ | oDialog=oCreateDialog2.createDialogWithHandler("vnd.sun.star.script:Standard.Dialog1?location=document", _ | ||
| Line 106: | Line 106: | ||
End Sub | End Sub | ||
</source> | </source> | ||
| − | + | As you can see, the OOoBasic Sub "increment" is now in comment and then cannot works. We have also to modify the Dialog for the "increment" button which have to call a method instead of a macro. This operation is explained in [[Documentation/DevGuide/WritingUNO/Assigning_Component_Methods_to_Control_Events|du Developer's Guide]]. | |
{{Documentation/Note|Pour cet exemple le code du composant compteur n'a toujours pas été changé. Ce que l'on vient de faire fonctionne car notre compteur fournit l'interface <idl>com.sun.star.lang.XTypeProvider</idl>.}} | {{Documentation/Note|Pour cet exemple le code du composant compteur n'a toujours pas été changé. Ce que l'on vient de faire fonctionne car notre compteur fournit l'interface <idl>com.sun.star.lang.XTypeProvider</idl>.}} | ||
Il nous est possible de poursuivre comme cela avec la méthode decrement du compteur, mais impossible de continuer avec les deux autres méthodes "setCount" et "getCount". Pour comprendre pourquoi, il faut se poser la question de savoir comment tout cela fonctionne. En fait dans ce cas précis c'est le service d'introspection <idl>com.sun.star.beans.Introspection</idl> qui va être utilisé automatiquement pour retrouver la méthode. Mais cette introspection automatique n'est capable de retrouver que les méthode ayant pour signature : | Il nous est possible de poursuivre comme cela avec la méthode decrement du compteur, mais impossible de continuer avec les deux autres méthodes "setCount" et "getCount". Pour comprendre pourquoi, il faut se poser la question de savoir comment tout cela fonctionne. En fait dans ce cas précis c'est le service d'introspection <idl>com.sun.star.beans.Introspection</idl> qui va être utilisé automatiquement pour retrouver la méthode. Mais cette introspection automatique n'est capable de retrouver que les méthode ayant pour signature : | ||
Revision as of 15:56, 18 May 2009
The goal of this chapter is to use a dialog to make our counter working. Because it's very easy to construct a dialog with OOoBasic we will use the OOoBasic Daialog editor and not the Dialog at runtime. As we will see along this chapter, two methods are now designed to facilitate the call of a component method with a dialog event. But befor going further we will first examine a method which consists to encapsulate C++ with OOoBasic.
Adding a Dialog to the Counter
For your information, we recall that our counter has four methods :
- increment
- decrement
- setCount
- getCount
We then design a Dialog with one button by method (then four buttons) and two text fields : O
- a text field to set a value in the counter (then associated with "setCount" button)
- a text field to get the value of the counter (then associated with "getCount" button)
Here is the corresponding dialog
To make the complete example working, you can simply use the following OOoBasic code :
'Listing 1
REM ***** BASIC *****
Dim oSimpleComponent
Dim oDialog
Sub demonstrateSimpleComponent
oSimpleComponent = CreateUnoService( "foo.Counter" )
'oInspector = createUnoService("org.openoffice.InstanceInspector")
'oInspector.inspect(oSimpleComponent, "MyCounter")
'XRay oSimpleComponent
oDialog=CreateUnoDialog(DialogLibraries.Standard.Dialog1)
oDialog.Execute()
oDialog.dispose()
End Sub
Sub increment
oSimpleComponent.increment()
End Sub
Sub decrement
oSimpleComponent.decrement()
End Sub
Sub getCount
Dim oTextField
oTextField = oDialog.getControl("TextField2")
oTextField.setText( oSimpleComponent.getCount())
End Sub
Sub setCount
Dim oTextField
oTextField = oDialog.getControl("TextField1")
'implicit conversion String to Integer
oSimpleComponent.setCount(oTextField.getText())
End Sub
Because these procedures are created in OpenOffice.org Basic, you can assign them to an event required using the property window of the dialog editor. In this program we have left comment on different introspection uses in our counter but this is not important. Template:Documentation/Note
Methods Direct Call with Introspection Service
As mentioned in Developer's Guide, since 2.0.4 version it is possible to bound methods of a component with events of a control in a Dialog. To put it differently, encapsulation as in the OOoBasic code below :
REM ***** BASIC *****
Sub increment
oSimpleComponent.increment()
End Sub
could be avoided now. But, for that, you have a price to pay : we have eventually to change our C++ code of our component. To begin with the more easy, we will modify our OOoBasic program, our Dialog but not our counter.
Direct Call of Counter Methods without changing its C++ code
We have to change the previous OOobasic program to use the new com.sun.star.awt.DialogProvider2 service which provide the very interesting "createDialogWithHandler" method. Here is the corresponding code :
REM ***** BASIC *****
Dim oSimpleComponent
Dim oDialog
Sub demonstrateSimpleComponent
oSimpleComponent = CreateUnoService( "foo.Counter" )
oCreateDialog2=CreateUnoService("com.sun.star.awt.DialogProvider2")
'Thank you ms777 for the line below (see http://www.oooforum.org/forum/viewtopic.phtml?t=84168)
oCreateDialog2.initialize(Array(ThisComponent))
oDialog=oCreateDialog2.createDialogWithHandler("vnd.sun.star.script:Standard.Dialog1?location=document", _
oSimpleComponent, StarDesktop.getActiveFrame() )
oDialog.Execute()
oDialog.dispose()
End Sub
'Sub increment
' oSimpleComponent.increment()
'End Sub
Sub decrement
oSimpleComponent.decrement()
End Sub
Sub getCount
Dim oTextField
oTextField = oDialog.getControl("TextField2")
oTextField.setText( oSimpleComponent.getCount())
End Sub
Sub setCount
Dim oTextField
oTextField = oDialog.getControl("TextField1")
'implicit conversion String to Integer
oSimpleComponent.setCount(oTextField.getText())
End Sub
As you can see, the OOoBasic Sub "increment" is now in comment and then cannot works. We have also to modify the Dialog for the "increment" button which have to call a method instead of a macro. This operation is explained in du Developer's Guide. Template:Documentation/Note Il nous est possible de poursuivre comme cela avec la méthode decrement du compteur, mais impossible de continuer avec les deux autres méthodes "setCount" et "getCount". Pour comprendre pourquoi, il faut se poser la question de savoir comment tout cela fonctionne. En fait dans ce cas précis c'est le service d'introspection com.sun.star.beans.Introspection qui va être utilisé automatiquement pour retrouver la méthode. Mais cette introspection automatique n'est capable de retrouver que les méthode ayant pour signature :
void nomMethod(void);
ou encore
void [MethodName]
(
[in] com::sun::star::awt::XDialog xDialog,
[in] any aEvent
);
et cela tombe bien parce que nos deux premières méthodes "increment" et "decrement" ont cette signature là, mais ce n'est pas le cas des deux suivantes.
Appel direct de toutes les méthodes du compteur
Dans cette section nous allons chercher à modifier le compteur pour qu'il fonctionne compètement par appel direc de teoutes ses méthodes.
Retour à la page d'accueil
Page d'accueil du développement C++ à l'aide du SDK
Voir aussi
- Construire des composants
- Le paragraphe consacré au sujet dans le Developer's Guide
- La description du langage OOoBasic
- La gestion des boîtes de dialogue en OOoBasic.
- Objects contextuels en OOoBasic
