Difference between revisions of "Documentation/DevGuide/Basic/Writing and Debugging a Basic UNO program"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (FINAL VERSION FOR L10N)
m
 
(8 intermediate revisions by 3 users not shown)
Line 7: Line 7:
 
{{Documentation/DevGuideLanguages|Documentation/DevGuide/Basic/{{SUBPAGENAME}}}}  
 
{{Documentation/DevGuideLanguages|Documentation/DevGuide/Basic/{{SUBPAGENAME}}}}  
 
  {{DISPLAYTITLE:Writing and Debugging a Basic UNO program}}
 
  {{DISPLAYTITLE:Writing and Debugging a Basic UNO program}}
Enter the following source code in the Basic editor window. The example asks the user for the location of a graphics file and inserts it at the current cursor position of our document. Later, the example will be extended by a small insert graphics autopilot.  
+
Enter the following source code in the Basic editor window. The example asks the user for the location of a graphics file and inserts it at the current cursor position of our document. Later, the example will be extended by a small insert graphics autopilot.
 
<!--[SOURCE:BasicAndDialogs/FirstStepsBasic.odt]-->
 
<!--[SOURCE:BasicAndDialogs/FirstStepsBasic.odt]-->
 
+
<syntaxhighlight lang="oobas">
 
   Sub Main
 
   Sub Main
 
       ' ask the user for a graphics file
 
       ' ask the user for a graphics file
Line 38: Line 38:
 
       oText.insertTextContent(oCursor.getStart(), oGraphicObject, false)
 
       oText.insertTextContent(oCursor.getStart(), oGraphicObject, false)
 
   End Sub
 
   End Sub
 +
</syntaxhighlight>
 +
If help is required on Basic keywords, press {{key|F1}} while the text cursor is on a keyword. The {{AOo}} online help contains descriptions of the Basic language as supported by {{AOo}}.
  
If help is required on Basic keywords, press F1 while the text cursor is on a keyword. The {{PRODUCTNAME}} online help contains descriptions of the Basic language as supported by {{PRODUCTNAME}}.
+
Starting with the line <tt>oDoc = ThisComponent</tt>, where the document model is accessed, we use the UNO integration of {{AOo}} Basic. <tt>ThisComponent</tt> is a shortcut to access a document model from the Basic code contained in it. Earlier, you created <tt>Module1</tt> in ''FirstStepsBasic.odt'', that is, your Basic code is embedded in the document ''FirstStepsBasic.odt'', not in a global library below the '''My Macros''' container. The property <tt>ThisComponent</tt> therefore contains the document model of ''FirstStepsBasic.odt''.
 
+
Starting with the line <tt>oDoc = ThisComponent</tt>, where the document model is accessed, we use the UNO integration of {{PRODUCTNAME}} Basic. <tt>ThisComponent</tt> is a shortcut to access a document model from the Basic code contained in it. Earlier, you created <tt>Module1</tt> in ''FirstStepsBasic.odt'', that is, your Basic code is embedded in the document ''FirstStepsBasic.odt'', not in a global library below the '''My Macros''' container. The property <tt>ThisComponent</tt> therefore contains the document model of ''FirstStepsBasic.odt''.
+
  
{{Documentation/Tip|Outside document libraries use <tt>ThisComponent</tt> or <tt>StarDesktop.CurrentComponent</tt> to retrieve the current document. If access to an open document is required, even if it is not the current document, you have to iterate over the components in <tt>StarDesktop.Components</tt>, checking their URL property with code similar to the following:
+
{{Tip|Outside document libraries use <tt>ThisComponent</tt> or <tt>StarDesktop.CurrentComponent</tt> to retrieve the current document. If access to an open document is required, even if it is not the current document, you have to iterate over the components in <tt>StarDesktop.Components</tt>, checking their URL property with code similar to the following:
  
  <nowiki>oComps = StarDesktop.Components
+
<syntaxhighlight lang="oobas">oComps = StarDesktop.Components
 
   oCompsEnum = oComps.createEnumeration()
 
   oCompsEnum = oComps.createEnumeration()
 
    
 
    
Line 54: Line 54:
 
         print oComp.getURL()
 
         print oComp.getURL()
 
       endif
 
       endif
   wend</nowiki>
+
   wend</syntaxhighlight>
 
}}
 
}}
  
Line 69: Line 69:
 
|-
 
|-
 
|[[Image:Macros.png]]
 
|[[Image:Macros.png]]
|Click the '''Macros''' icon if you need to run a Sub other than the first Sub in the module.. In the '''{{PRODUCTNAME}} Basic Macros''' dialog, navigate to the appropriate module, select the Sub to run and press the '''Run''' button.  
+
|Click the '''Macros''' icon if you need to run a Sub other than the first Sub in the module. In the '''{{AOo}} Basic Macros''' dialog, navigate to the appropriate module, select the Sub to run and press the {{button|'''Run'''}} button.  
 
|}
 
|}
  
To observe the values of Basic variables during debugging, enter a variable name in the '''Watch''' field of the Basic editor and press the '''Enter''' key to add the watch, or point at a variable name with the mouse cursor without clicking it. In the example below, we can observe the variables <tt>sGraphicUrl</tt> and <tt>oGraphicObject</tt>:
+
To observe the values of Basic variables during debugging, enter a variable name in the '''Watch''' field of the Basic editor and press the {{key|Enter}} key to add the watch, or point at a variable name with the mouse cursor without clicking it. In the example below, we can observe the variables <tt>sGraphicUrl</tt> and <tt>oGraphicObject</tt>:
  
[[Image:Watch.png|none|thumb|500px|A macro in the source editor window]]
+
[[Image:Watch.png|none|thumb|700px|A macro in the source editor window]]
  
Since {{PRODUCTNAME}} {{OO2.0.0}} it is also possible to inspect the values of UNO objects in the Basic debugger during runtime.
+
Since OpenOffice.org 2.0.0 it is also possible to inspect the values of UNO objects in the Basic debugger during runtime.
  
 
{{PDL1}}
 
{{PDL1}}
  
 
[[Category:Documentation/Developer's Guide/Basic and Dialogs]]
 
[[Category:Documentation/Developer's Guide/Basic and Dialogs]]

Latest revision as of 10:40, 18 January 2024



Enter the following source code in the Basic editor window. The example asks the user for the location of a graphics file and inserts it at the current cursor position of our document. Later, the example will be extended by a small insert graphics autopilot.

  Sub Main
      ' ask the user for a graphics file
      sGraphicUrl = InputBox("Please enter the URL of a graphic file", _
          "Import Graphics", _
          "file:///")
      if sGraphicURL = "" then ' User clicked Cancel
          exit sub
      endif
 
      ' 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

If help is required on Basic keywords, press  F1  while the text cursor is on a keyword. The Apache OpenOffice online help contains descriptions of the Basic language as supported by Apache OpenOffice.

Starting with the line oDoc = ThisComponent, where the document model is accessed, we use the UNO integration of Apache OpenOffice Basic. ThisComponent is a shortcut to access a document model from the Basic code contained in it. Earlier, you created Module1 in FirstStepsBasic.odt, that is, your Basic code is embedded in the document FirstStepsBasic.odt, not in a global library below the My Macros container. The property ThisComponent therefore contains the document model of FirstStepsBasic.odt.

Tip.png Outside document libraries use ThisComponent or StarDesktop.CurrentComponent to retrieve the current document. If access to an open document is required, even if it is not the current document, you have to iterate over the components in StarDesktop.Components, checking their URL property with code similar to the following:
oComps = StarDesktop.Components
  oCompsEnum = oComps.createEnumeration()
 
  while oCompsEnum.hasMoreElements()
      oComp = oCompsEnum.nextElement()
      ' not all desktop components are necessarily models with a URL
      if HasUnoInterfaces(oComp, "com.sun.star.frame.XModel") then
        print oComp.getURL()
      endif
  wend


Breakpoint.png To debug the program, put the cursor into the line oDoc = ThisComponent and click the Breakpoint icon in the macro bar.
MacroRun.png The Run icon launches the first Sub in the current module. Execution stops with the first breakpoint.
SingleStep.png Now step through the program by clicking the Single Step icon.
Macros.png Click the Macros icon if you need to run a Sub other than the first Sub in the module. In the Apache OpenOffice Basic Macros dialog, navigate to the appropriate module, select the Sub to run and press the  Run  button.

To observe the values of Basic variables during debugging, enter a variable name in the Watch field of the Basic editor and press the  ↵ Enter  key to add the watch, or point at a variable name with the mouse cursor without clicking it. In the example below, we can observe the variables sGraphicUrl and oGraphicObject:

A macro in the source editor window

Since OpenOffice.org 2.0.0 it is also possible to inspect the values of UNO objects in the Basic debugger during runtime.

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