Difference between revisions of "Documentation/DevGuide/Basic/Adding Event Handlers"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Initial author Sun Microsystems, Inc.)
 
m (1 revision(s))
(No difference)

Revision as of 12:35, 15 February 2008



Now we will write code to open the dialog and add functionality to the buttons. To show a dialog, create a dialog object using createUnoDialog() and call its execute() method. A dialog can be closed while it is shown by calling endExecute().

Template:Documentation/Tip

To add functionality to GUI elements, develop Subs to handle GUI events, then hook them to the GUI elements. To add functionality to the buttons of our dialog, click the Module1 tab in the lower part of the Basic IDE and enter the following Subs above the previous Sub Main to open, close and process the dialog. Note that a Private variable oDialog is defined outside of the Subs. After loading the dialog, this variable is visible from all Subs and Functions of Module1.

 Private oDialog as Variant ' private, module-wide variable
 
 Sub RunGraphicsWizard
     oDialog = createUnoDialog(DialogLibraries.Standard.Dialog1)
     oDialog.execute
 End Sub
 
 Sub CancelGraphicsDialog
     oDialog.endExecute()
 End Sub
 
 Sub FinishGraphicsDialog
     Dim sFile as String, sGraphicURL as String
 
     oDialog.endExecute()
 
     sFile = oDialog.Model.FileControl1.Text 
   
     ' the FileControl contains a system path, we have to transform it to a file URL
     ' We use the built-in Basic runtime function ConvertToURL for this purpose
     sGraphicURL = ConvertToURL(sFile)
   
     ' insert the graphics
     ' access the document model
     oDoc = ThisComponent
     ' get the Text service of the document
     oText = oDoc.getText()
     ' create an instance of a graphic object using the document service factory
     oGraphicObject = oDoc.createInstance("com.sun.star.text.GraphicObject")
     ' set the URL of the graphic
     oGraphicObject.GraphicURL = sGraphicURL
     ' get the current cursor position in the GUI and create a text cursor from it
     oViewCursor = oDoc.getCurrentController().getViewCursor()
     oCursor = oText.createTextCursorByRange(oViewCursor.getStart())
     ' insert the graphical object at the cursor position
     oText.insertTextContent(oCursor.getStart(), oGraphicObject, false)
 End Sub
 
 Sub Main
     ...
 End Sub

Select the Cancel button in our dialog in the dialog editor, and click the Events tab of the Properties Dialog, then click the ellipsis button on the right-hand side of the Event When Initiating. As shown in the next illustration the Assign Action dialog appears.

Assign Action dialog

In the Assign Action dialog press the Macro ... button to open the Macro Selector dialog shown in the illustration below. Navigate to FirstStepsBasic.Standard.Module1, select the Sub CancelGraphicsDialog and press the OK button to link this sub to the wizard dialog's Cancel button.

The Macro Selector dialog

The next illustration shows how the new assignment is shown in the Assign Action dialog.

The Assign Action dialog

Pressing the OK button in the Assign Action dialog finishes the assignment process.

Template:Documentation/Note

Using the same method, hook the Finish button to FinishGraphicsDialog.

MacroRun.png If the Run icon is selected now, the dialog is displayed, and the Finish and Cancel buttons are functional.


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools