Difference between revisions of "Component and Dialog"
SergeMoutou (Talk | contribs) m |
SergeMoutou (Talk | contribs) m |
||
Line 1: | Line 1: | ||
− | The goal of this chapter is to use a dialog to make [[Constructing_Components#Using_an_Helper_to_construct_the_scriptable_Counter|our counter]] working. Because it's very easy to construct a [[Documentation/BASIC_Guide/Dialogs|dialog]] with OOoBasic. | + | The goal of this chapter is to use a dialog to make [[Constructing_Components#Using_an_Helper_to_construct_the_scriptable_Counter|our counter]] working. Because it's very easy to construct a [[Documentation/BASIC_Guide/Dialogs|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 [[Constructing_Components#Using_an_Helper_to_construct_the_scriptable_Counter|our counter]] has four methods : | |
# increment | # increment | ||
# decrement | # decrement | ||
Line 8: | Line 8: | ||
# getCount | # 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 | |
[[Image:CounterDialog1.png|center|thumb|600px|Notre boîte de dialogue]] | [[Image:CounterDialog1.png|center|thumb|600px|Notre boîte de dialogue]] | ||
− | + | To make the complete example working, you can simply use the following OOoBasic code : | |
<source lang="oobas"> | <source lang="oobas"> |
Revision as of 15:28, 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.
Contents
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
en prenant soin d'associer aux boutons les sous-programmes OOoBasic correspondants. Dans ce programme on a laissé en commentaire les différentes façons de réaliser l'introspection sur notre compteur, mais ceci n'a pas grand intérêt pour ce qui nous préoccupe. Template:Documentation/Note
Appel direct des méthodes du compteur à l'aide du service d'Introspection
Comme indiqué dans le Developer's Guide, depuis la version 2.0.4 il est possible d'associer directement des méthodes d'un composant à des événements de boutons (ou autres contrôles). Une autre façon de dire les choses, c'est que l'enrobage que l'on a utilisé dans la section précédente du genre :
REM ***** BASIC ***** Sub increment oSimpleComponent.increment() End Sub
n'est plus nécessaire. Mais ceci a un coût : on ne pourra pas toujours garder le composant sans en modifier le code C++. Pour commencer par le plus simple, nous allons modifier notre programme OOoBasic, notre boîte de dialogue mais pas notre compteur.
Appel direct d'une méthode du compteur sans changer son code
On reprend en le modifiant le programme OOoBasic précédent pour qu'il utilise le service com.sun.star.awt.DialogProvider2 qui nous fournit la méthode "createDialogWithHandler" qui nous intéresse. Voici le programme correspondant :
REM ***** BASIC ***** Dim oSimpleComponent Dim oDialog Sub demonstrateSimpleComponent oSimpleComponent = CreateUnoService( "foo.Counter" ) oCreateDialog2=CreateUnoService("com.sun.star.awt.DialogProvider2") 'Merci ms777 pour la ligne suivante (voir 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
Comme vous pouvez le voir, j'ai mis en commentaire le sous-programme OOoBasic "increment", il ne peut donc plus fonctionner. En fait j'ai modifié aussi la boîte de dialogue pour que le bouton "increment" n'appelle plus un sous-programme de macro, mais directement une méthode du composant counter. Cette page en anglais du Developer's Guide explique comment on procède pour cela. La partie initialisation s'en trouve légèrement changée. 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