[ Previous document | Content Table | Next document ]

12  OpenOffice.org Basic and Dialogs

 

OpenOffice.org provides functionality to create and manage Basic macros and dialogs. The following sections examine the usage of the OpenOffice.org Basic programming environment.

12.1  First Steps with OpenOffice.org Basic

Step By Step Tutorial

This section provides a tutorial to enable developers to use the Basic IDE. It describes the necessary steps to write and debug a program in the Basic IDE, and to design a Basic dialog. A comprehensive reference of all tools and options can be found at 12.2 OpenOffice.org Basic and Dialogs - OpenOffice.org Basic IDE.

Creating a Module in a Standard Library
  1. Create a new Writer document and save the document, for example, FirstStepsBasic.odt.

  2. Click Tools – Macros – Organize Macros – OpenOffice.org Basic.

The OpenOffice.org Basic Macros dialog appears. The Macro from list shows macro containers where Basic source code (macros) can come from. There is always a My Macros and a OpenOffice.org Macros  container for Basic libraries. Additionally each loaded document can contain Basic libraries.

Screenshot showing the Macro dialogIllustration 12.1: Macro dialog

The illustration above shows that the document FirstStepsBasic.odt is the only document loaded. Therefore, the My Macros, OpenOffice.org Macros and FirstStepsBasic.odt containers are displayed in the illustration above. Both containers, My Macros and FirstStepsBasic.odt, contain a library named Standard. The OpenOffice.org Macros container contains the libraries that come with a default OpenOffice.org installation – most of them are AutoPilots. The Standard libraries of the application and for all open documents are always loaded. They appear enabled in the dialog. Other libraries have to be loaded before they can be used.

The libraries contain modules with the actual Basic source code. Our next step will create a new module for source code in the Standard library of our FirstStepsBasic.odt document.

  1. Scroll to the document node FirstStepsBasic.odt in the Macro from list.

  2. Select the Standard entry below the document node and click New.

OpenOffice.org shows a small dialog that suggests to create a new module named Module1. 

  1. Click OK to confirm.  

The Basic source editor window (Illustration 12.2) appears containing a Sub (subroutine) Main.

Screenshot showing the Basic source editor windowIllustration 12.2: Basic source editor window

The status bar of the Basic editor window shows that the Sub Main is part of FirstStepsBasic.Standard.Module1. If you click Tools – Macros – OpenOffice.org Basic in the Basic editor, you will see that OpenOffice.org created a module Module1 below the Standard library in FirstStepsBasic.odt.

Screenshot showing the Macro dialog containing a docment macroIllustration 12.3

When a module is selected, the Macro name list box on the left shows the Subs and Functions in that module. In this case, Sub Main. If you click Edit while a Sub or Function is selected, the Basic editor opens and scrolls to the selected Sub or Function.

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. (BasicAndDialogs/FirstStepsBasic.odt

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 OpenOffice.org online help contains descriptions of the Basic language as supported by OpenOffice.org. 

Starting with the line oDoc = ThisComponent, where the document model is accessed, we use the UNO integration of OpenOffice.org 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 graphics marks a hint section in the text

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 icon

To debug the program, put the cursor into the line oDoc = ThisComponent and click the Breakpoint icon in the macro bar.

Run icon

The Run icon launches the first Sub in the current module. Execution stops with the first breakpoint.

Single Step icon

Now step through the program by clicking the Single Step icon.

Macros icon

Click the Macros icon if you need to run a Sub other than the first Sub in the module.. In the OpenOffice.org 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:

Screenshot showing a macro in the source editor windowIllustration 12.4

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

Calling a Sub from the User Interface

A Sub can be called from customized icons, menu entries, upon keyboard shortcuts and on certain application or document events. The entry point for all these settings is the Customize dialog accessible through the Assign button in the Macro dialog or the Tools – Customize command.

To assign the Sub Main to a toolbar icon, select Tools – Customize and click the Toolbars tab The Toolbars tab looks like this:

Screenshot of the Customize Toolbars dialogIllustration 12.5

Click the Add button in the Toolbars tab. In the Add Commands dialog that pops up, scroll down the Category list until you see the OpenOffice.org Macros node.Expand it and the FirstStepsBasic.odt node. Navigate to the Module FirstStepsBasic.Standard.Module1 and select it. When Module1 is selected, the Commands list shows an entry "Main" for the Sub Main in Module1.Clicking Add will add it to a toolbar.

You can now click the new toolbar item to launch the example macros. 

The section 12.2.3 OpenOffice.org Basic and Dialogs - OpenOffice.org Basic IDE - Assigning Macros To GUI Events describes other options to make your Sub accessible from the user interface.

A Simple Dialog

Creating Dialogs

To create a dialog in the Basic IDE, right-click the Module1 tab at the bottom of the Basic source editor and select Insert – Basic Dialog. The IDE creates a new page named Dialog1:

Screenshot of the Basic GUI editorIllustration 12.6
Controls icon

To add controls to the dialog, we require the dialog design tools. Click the Controls icon to pop up the design tools window. The title bar of the tools window can be used to drag the window away from the toolbar to keep it open permanently.

Our dialog shall offer a more convenient way to select a file than the simple input box of our first example. Furthermore, the user shall be able to control how the picture is anchored in the text after inserting it. For this, we will create a wizard dialog with two steps. 

File Selection

In the design tools window, select File Selection and define the size of the Browse control by dragging a rectangle in the dialog using the left-mouse button.

Properties icon

The Properties icon displays the Properties Dialog that is used to edit controls and hook up event handling code to events occurring at dialog controls.

Button icon

Next, add << Back and Next >> Buttons to move between the dialog steps, and a Finish and Cancel button. Select the Button icon and define the button size using the left-mouse button. Buttons are labeled with a default text, such as CommandButton1. If the Properties Dialog is not open, double click the newly inserted button controls to display it. Enter new labels in the Label field as suggested, and name the dialog step buttons Back and Next. Set the property Enabled for the << Back button to false.

Label icon

Use the Label tool to create a label "Select Graphics File:" in the same manner.

Now the dialog looks similar to the illustration below: 

Screenshot of a custom dialogIllustration 12.7
Activate Test Mode icon

Test the dialog using the Activate Test Mode icon from the design tool window. After you have finished the test, click the Close button of the test dialog window.

To edit the dialog, such as setting the title and changing the size, select it by clicking the outer border of the dialog. Green handles appear around the dialog. The green handles can be used to alter the dialog size.The Properties Dialog is used to define a dialog title and other dialog properties.

Adding Event Handlers

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().

Tip graphics marks a hint section in the text

It is possible to configure the Finish button and the Cancel button to close the dialog by setting the button property PushButtonType to OK and Cancel respectively. The method execute() returns 0 for Cancel and 1 for OK.

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. (BasicAndDialogs/FirstStepsBasic.odt)

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.

Screenshot showing the Assign Macro dialogIllustration 12.8Assign 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.

Screenshot of the Macro dialog with a second standard mouleIllustration 12.9

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

 

Screenshot of the Macro dialog with a second standard mouleIllustration 12.10

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

Note graphics marks a special text section

In the Assign Action dialog there's also a Component ... button. This button is only needed in the context of dialogs used by UNO components, see 4.11 Writing UNO Components - Accessing Dialogs. In the Basic context this button is not relevant.

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

Run icon

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

AutoPilot Dialogs

The final step is to create a small AutoPilot with two pages. The OpenOffice.org Dialogs have a simple concept for AutoPilot pages. Each dialog and each control in a dialog has a property Page (Step) to control the pages of a dialog. Normally, dialogs are on page 0, but they can be set to a different page, for example, to page 1. All controls having 1 in their Page property are visible as long as the dialog is on page 1. All controls having 2 in their page property are only displayed on page 2 and so forth. If the dialog is on Page 0, all controls are visible at once. If a control has its Page property set to 0, it is visible on all dialog pages.

This feature is used to create a second page in our dialog. Hold down the Control key, and click the label and file control in the dialog to select them. In the Properties Dialog, fill in 1 for the Page property and press Enter to apply the change. Next, select the dialog by clicking the outer rim of the dialog in the dialog editor, enter 2 for the Page property and press the Enter key. The label and file control disappear, because we are on page 2 now. Only the buttons are visible since they are on page 0.

On page 2, add a label "Anchor" and two option buttons "at Paragraph" and "as Character". Name the option buttons AtParagraph and AsCharacter, and toggle the State property of the AtParagraph button, so that it is selected by default. The new controls automatically receive 2 in their Page property. When page 2 is finished, set the dialog to page 1 again, because we want it to be on page 1 on startup.

Screenshot of the Insert Graphic WizardIllustration 12.11

The Subs below handle the << Back and Next >> buttons, and the Sub  FinishGraphicsDialog has been extended to anchor the new graphics selected by the user. Note that the property that is called Page (Step) in the GUI, is called Step in the API. (BasicAndDialogs/FirstStepsBasic.odt)

Sub BackGraphicsDialog 

    oDialog.Model.Step = 1

    oDialog.Model.Back.Enabled = false

    oDialog.Model.Next.Enabled = true

End Sub 

 

Sub NextGraphicsDialog 

    oDialog.Model.Step = 2

    oDialog.Model.Back.Enabled = true

    oDialog.Model.Next.Enabled = false

End Sub 

 

Sub FinishGraphicsDialog 

    Dim sGraphicURL as String, iAnchor as Long

    oDialog.endExecute()

    sFile = oDialog.Model.FileControl1.Text

    ' State = Selected corresponds to 1 in the API

    if oDialog.Model.AsCharacter.State = 1 then

        iAnchor = com.sun.star.text.TextContentAnchorType.AS_CHARACTER

    elseif oDialog.Model.AtParagraph.State = 1 then

        iAnchor = com.sun.star.text.TextContentAnchorType.AT_PARAGRAPH

    endif

    ' the File Selection control returns a system path, we have to transform it to a File URL

    ' We use a small helper function MakeFileURL for this purpose (see below)

    sGraphicURL = MakeFileURL(sFile)

    ' 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

    oGraphicObject.AnchorType = iAnchor

    ' 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 beginning of the text

    oText.insertTextContent(oCursor.getStart(), oGraphicObject, false)               

End Sub 

12.2  OpenOffice.org Basic IDE

This section discusses all features of the Integrated Development Environment (IDE) for OpenOffice.org Basic. It shows how to manage Basic and dialog libraries, discusses the tools of the Basic IDE used to create Basic macros and dialogs, and it treats the various possibilities to assign Basic macros to events.

12.2.1  Managing Basic and Dialog Libraries

The main entry point to the library management UI is the Tools – Macros – Organize Macros – OpenOffice.org Basic menu item. This item activates the OpenOffice.org Basic Macros dialog where the user can manage all operations related to Basic and dialog libraries.

OpenOffice.org Basic Macros Dialog

The following picture shows an example macro dialog. From here you can run, create, edit and delete macros, assign macros to UI events, and administer Basic libraries and modules. 

Screenshot of the Macro dialog with a second standard mouleIllustration 12.12
Displayed Information

The tree titled with Macro from shows the complete library hierarchy that is available the moment the dialog is opened. See 12.4 OpenOffice.org Basic and Dialogs - Advanced Library Organization for details about the library organization in OpenOffice.org..

Unlike the library organization API, this dialog does not distinguish between Basic and dialog libraries. Usually the libraries displayed in the tree are both Basic and dialog libraries.  

Note graphics marks a special text section

Although it is possible to create Basic-only or dialog-only libraries using the API this is not the normal case, because the graphical user interface (see 12.2.1 OpenOffice.org Basic and Dialogs - OpenOffice.org Basic IDE - Managing Basic and Dialog Libraries - Macro Organizer Dialog below) only allows the creation of Basic and dialog libraries simultaneously. Nevertheless, the dialog can also deal with Basic-only or dialog-only libraries, but they are not marked in any way.

The tree titled Macro from represents a structure consisting of three levels:

Library container -> library -> library element 

If a library is password-protected and a user double-clicks it to load it, a dialog is displayed requesting a password. The library is only loaded and expanded if the user enters the correct password. If a password-protected library is loaded using the API, for example, through a call to BasicLibraries.loadLibrary("Library1"), it is displayed as loaded, not grayed out, but it remains condensed until the correct password is entered (see 12.4 OpenOffice.org Basic and Dialogs - Advanced Library Organization).

 

The middle column contains information about the macros, that is, the Subs and Functions, in the libraries. In the list box at the bottom, all Subs and Functions belonging to the module selected in the tree are listed. In the edit field titled Macro name, the Sub or Function currently selected in the list box is displayed. If there is no module selected in the tree, the edit field and list are empty. You can type in a desired name in the edit field.

Buttons

On the right-hand side of the OpenOffice.org Basic Macros dialog, there are several buttons. The following list describes the buttons:

OpenOffice.org Basic Macro Organizer Dialog

This dialog is opened by clicking the Button Organizer in the OpenOffice.org Basic Macros dialog. The dialog contains the tab pages Modules, Dialogs and Libraries. While the OpenOffice.org         Basic Macros dialog refers to Subs and Functions inside Basic modules, such as run Subs, delete Subs, and insert new Subs, this dialog accesses the library system on module (tab page Modules) , dialog (tab page Dialogs)  and library (tab page Libraries) level.

Modules

Illustration 12.13 shows the OpenOffice.org Basic Macro Organizer dialog with the Modules tab page activated. The list titled Module is similar to the Macro from list in the Macro dialog, but it contains the complete library hierarchy for the OpenOffice.org application libraries and the document libraries. The libraries are loaded, and condensed or expaned by double-clicking the library.  The illustration shows the application library Standard containing two modules, Module1 and Module2.

Screenshot showing the Macro Organizer dialogIllustration 12.13

 

The illustration above shows that two documents are loaded. The illustration shows a library Standard in document Description.odt containing a module named Module1, and another library Standard in document Calculation.ods containing no Basic module.

The following list describes the buttons on the right side of the dialog: 

Dialogs

Illustration 12.14 shows the OpenOffice.org Basic Macro Organizer dialog with the Dialogs tab page activated. The illustration shows the application library Standard containing two dialogs, Dialog1 and Dialog2.

Screenshot showing the Macro Organizer dialog with document macrosIllustration 12.14

The illustration shows a library Standard in document Calculation.ods containing a dialog named Dialog1, and another library Standard in document Description.odt containing no dialog.

The following list describes the buttons on the right side of the dialog: 

Libraries

The following illustrations show the OpenOffice.org Basic Macro Organizer dialog with the Libraries tab page activated. In this dialog, the application and document libraries are listed separately. The Library list only contains the libraries of the library container currently selected in the Location list box. The second illustration is dropped down showing the My Macros & Dialogs and  OpenOffice.org Macros & Dialogs  entries and the two open documents.

Screenshot of the Libraries tab page of the Macro Organizer dialogIllustration 12.15
Screenshot of the Libraries tab page showing the Application/Document selectionIllustration 12.16

The libraries are displayed in the following manner: 

Clicking a library twice (notdouble-click) allows the user to rename it. 

The following list describes the buttons on the right side of the dialog: 

Screenshot showing the Change Password dialogIllustration 12.17
Screenshot showing the Append Libraries dialogIllustration 12.18
Screenshot showing the Append Libraries dialog with selected librariesIllustration 12.19
Screenshot showing the Append Libraries dialog with selected librariesIllustration 12.20
Screenshot showing the Append Libraries dialog with selected librariesIllustration 12.21
Screenshot showing the Append Libraries dialog with selected librariesIllustration 12.22

12.2.2  Basic IDE Window

The OpenOffice.org IDE is mainly represented by the Basic IDE window. The IDE window has two different modes: 

Basic source code and dialogs are never displayed at the same time. The IDE window is in Basic editor or debugger, or in dialog editor mode. The following illustration shows the Basic IDE window in the Basic editor mode displaying Module2 of the application Standard library. 

Screenshot showing the Basic IDE windowIllustration 12.23

The IDE window control elements common to the Basic editor and dialog editor mode are described below. The mode specific control elements are described in the corresponding subchapters 12.2.2 OpenOffice.org Basic and Dialogs - OpenOffice.org Basic IDE - Basic IDE Window - Basic Source Editor and Debugger and 12.2.2 OpenOffice.org Basic and Dialogs - OpenOffice.org Basic IDE - Basic IDE Window - Dialog Editor:

Basic Source Editor and Debugger

The Basic editor and debugger of the IDE window is shown when the user edits a Sub or Function from the Tools-Macros-Organize Macros-OpenOffice.org Basic dialog (see Illustration 12.24: Basic Editor and Debugger). In this mode, the window contains the actual editor main window, debugger Watch window to display variable values and the debugger Calls window to display the Basic call stack. The Watch and Calls windows are only used when a Basic program is running and halted by the debugger.

The editor supports common editor features. Since the editor is only used for the OpenOffice.org Basic programming language, it supports a Basic syntax specific highlighting and F1 help for Basic keywords. 

Screenshot shwoing the IDE window in debug modeIllustration 12.24: Basic Editor and Debugger

The following list explains the functionality of the macro toolbar buttons. 

Compile icon

Compile: Compiles the active module and displays an error message, if necessary. This button is disabled if a Basic program is running. Always compile libraries before distributing them.

Run icon

Run: Executes the active module, starting with the first Sub in the module, before all modified modules of the active library are compiled. Clicking this button can also result in compiler errors before the program is started. This button resumes the execution if the program is halted by the debugger.

Stop icon

Stop: Stops the Basic program execution. This button is disabled if a program is not running.

Procedure Step icon

Procedure Step: Executes one Basic statement without stepping into Subs or Functions called in the statement. The execution is halted after the statement has been executed. If the Basic program not is running the execution is started and halted at the first statement of the first Sub in the current module.

Single Step icon

Single Step: Executes one Basic statement. If the statement contains another Sub, execution is halted at the first statement of the called Sub. If no Subs or Functions are called in the statement, this button has the same functionality as the Step over button (key command F8).

Step back icon

Step back: Steps out of the current executed Sub or Function and halts at the next statement of the caller Sub or Function. If the currently executed Sub or Function was not called by another Sub or Function or if the Basic program is not running, this button has the same effect as the Run button.

Breakpoint icon

Breakpoint: Toggles a breakpoint at the current cursor line in the Basic editor. If a breakpoint can not be set at this line a beep warns the user and the action is ignored (key command F9). A breakpoint is displayed as a red dot in the left column of the editor window.

Add watch icon

Add watch: Adds the identifier currently touched by the cursor in the Basic editor to the watch window (key command F7).

Object catalog icon

Object Catalog: Opens the Objects dialog. This dialog displays the complete library hierarchy including dialogs, modules and the Subs inside the modules.

Macros icon

Macros: Opens the OpenOffice.org Basic Macros Dialog.

Modules icon

Modules: Opens the OpenOffice.org Basic Macro Organizer dialog

Find Parentheses icon

Find Parentheses: If the cursor in the Basic editor is placed before a parenthesis, the matching parenthesis is searched. If a matching parenthesis is found, the code between the two parentheses is selected, otherwise the user is warned by a beep.

Controls icon

Controls: Opens the dialog editing tools in the dialog editor. In Basic editor mode this button is disabled.

Insert Source File icon

Insert Source File: Displays a file open dialog and inserts the selected text file (*.bas is the standard extension) at the current cursor position into the active module.

Save Source As icon

Save Source As: Displays a file Save As dialog to save the active module as a text file (*.bas is the standard extension).

Illustration 12.24: Basic Editor and Debugger shows how the IDE window looks while a Basic program is executed in debugging mode.

Dialog Editor

This section provides an overview of the Dialog editor functionality. The controls that are used to design a dialog are not explained. See 12.5 OpenOffice.org Basic and Dialogs - Programming Dialogs and Dialog Controls for details on programming these controls. The dialog editor is activated by creating a new dialog, clicking a dialog tab at the bottom of the IDE window, or selecting a dialog in the OpenOffice.org Basic Macro Organizer dialog and clicking the Edit button.

Initially, a new dialog consists of an empty dialog frame. The next illustration shows Dialog2 of the application Standard library in this state. 

Screenshot showing the Dialog editorIllustration 12.25

In the dialog editor mode, the Controls button is enabled and the illustration shows the result by clicking this button. A small toolbar with dialog specific tools is displayed. The buttons in this toolbar represent the types of controls that can be inserted into the dialog. The user clicks the desired button, then draws a frame with the mouse at the position to insert the corresponding control type.

The following three buttons in the dialog tools window do not represent controls: 

Select icon

The Select button at the lower right of the dialog tools window switches the mouse cursor to selection mode. In this mode, controls are selected by clicking the control with the cursor. If the Shift key is held down simultaneously, the selection is extended by each control the user clicks. Controls can also be selected by drawing a rubberband frame with the mouse. All controls that are completely inside the frame will be selected. To select the dialog frame the user clicks its border or includes it in a selection frame completely.

Activate Test Mode icon

The Activate Test Mode button switches on the test mode for dialogs. In this mode, the dialog is displayed as if it was a Basic script (see 12.5 OpenOffice.org Basic and Dialogs - Programming Dialogs and Dialog Controls). However, the macros assigned to the controls do not work in this mode. They are thereto help the user design the look and feel of the dialog.

Properties icon

The Properties button at the lower left of the dialog tools window opens and closes the Properties dialog. This dialog is used to edit all properties of the selected control(s). The next illustration shows the Properties dialog for a selected button control.

Properties icon

The Manage Language button (available since OpenOffice.org 2.2) opens the Manage User Interface Languages dialog allowing to manage the localization of dialogs. All details concerning Dialog localization are described in 12.2.4 OpenOffice.org Basic and Dialogs - OpenOffice.org Basic IDE - Dialog Localization.

 

Screenshot showing the Properties dialog of a Command buttonIllustration 12.26

The illustration above shows that the dialog tool window can be pulled from the main toolbar by dragging the window at its caption bar after opening it. 

The Properties dialog has two tabs. The General tab, visible in Illustration 12.26, contains a list of properties. Their values are represented by a control. For most properties this is a list box, such as color and enum types, or an edit field, such as numeric or text properties. For more complex properties, such as fonts or colors, an additional ellipsis button opens another type of dialog, for example, to select a font. When the user changes a property value in an edit field this value is not applied to the control until the edit field has lost the focus. This is forced with the tab key. Alternatively, the user can commit a change by pressing the Enter key.

The Events tab page displays the macros assigned to the events supported by the selected control:

Screenshot of the Events tab page of the the Properties dialogIllustration 12.27

In the example above, a macro is assigned to the Key pressed event: When this event occurs, the displayed Sub doNothing in Module2 of the application Basic library Standard is called. The events that are available depend on the type of control selected.

To change the event assignment the user has to click one of the ellipsis buttons to open the Assign Action dialog displayed in Illustration 12.28: Assign Action Dialog.

Screenshot of the Assign Macros dialogIllustration 12.28: Assign Action Dialog

The list box titled Event displays the same information as the Events tab of the Properties dialog. The Assign Action dialog is always the same, that is only the selected event in its Event list changes according to the ellipsis button the user selected on the Events tab of the Properties dialog.

To assign a macro to an event, the user needs to click on the Macro ... button. This opens the Macro Selector dialog which allows the user to select a macro from the library hierarchy. Clicking OK in the Macro Selector assigns the selected macro to the event. If another macro is already assigned to an event, this macro is replaced. If no Sub is selected, the OK button is disabled.

If the dialog is stored in a document, the library hierarchy displayed in the Macro Selector dialog contains the application library containers and the library container of the document. If the dialog belongs to an application dialog library, document macros are not displayed since they cannot be assigned to the controls of application dialogs. This is because it cannot be guaranteed that the document will be loaded when the application dialog event is fired. 

The Remove button is enabled if an event with an assigned macro is selected. Clicking this button removes the macro from the event, therefore the event will have no macro binding.

The list box below the Remove button is used to select different macro languages. Currently, only OpenOffice.org Basic is available.

The OK button closes the Assign Action dialog, and applies all event assignments and removals to the control. The changes are reflected on the Events tab of the Properties dialog.

The Cancel button also closes the Assign Action, but all assignment and removal operations are discarded.

As previously explained, it is also possible to select several controls simultaneously. The next picture shows the situation if the user selects both CommandButton1 and CheckBox1.For the Properties dialog such a multi selection has some important effects.

Screenshot showing the Properties dialog with multiselectionIllustration 12.29: Properties dialog for multi selection

Here the caption of the Properties contains the string Multiselection to point out the special situation. The two important differences compared to the single selection situation are:

12.2.3  Assigning Macros to GUI Events

The functionality to assign macros to control events in the dialog editor was discussed earlier. There is also a general functionality to assign macros or other actions to events. This functionality can be accessed through the Customize dialog that is opened using Tools – Customize or by clicking the Assign button in the Macro dialog. In this section, only the assignment of macros is discussed. For more information about this dialog, refer to the OpenOffice.org documentation.

The next illustration shows the Menu tab of the Customize dialog

Screenshot of the Configuration dialogIllustration 12.30: Configuration dialog for Menu

The illustration above shows how a macro is assigned to a new menu item. The Menu and Menu Content list boxes can be used to navigate the OpenOffice.org menu hierarchy. Clicking the Add... button opens the Add Commands dialog. The Category list box  in the Add Commands dialog contains entries for built-in OpenOffice.org functions, and a OpenOffice.org Macros entry that represents the  hierarchy of  OpenOffice.org macros. When an entry is selected in the Categories list box, any commands or macros it contains are displayed in the Commands list box on the right.

Clicking the Add button in the Add Commands dialog adds the selected command or macro to a menu.

The other buttons in the Menus tab of the Customize dialog are as follows:

The next illustration shows the Events tab of the Customize dialog:

 

Screenshot showing the Configuration dialogs Event tab pageIllustration 12.31: Configuration dialog for Events

On this tab, macros can be assigned to general events in OpenOffice.org. The events are listed in the list box titled Event.  The Assign button opens the Macro Selector from which the user can select macros to assign to events. The Remove button removes the assigned macro from the selected event.

In the Keyboard tab macros are accessed in Category and Function list boxes, then assigned to a shortcut key that can be specified in the Shortcut keys list box. There are also Load, Save and Reset buttons for loading, storing and resetting keyboard configurations.

The Keyboard tab contains a OpenOffice.org and a Document radio button which controls the scope for which keyboard assignments are made.

12.2.4  Dialog Localization

Beginning with OpenOffice.org 2.2 it's possible to localize dialogs created in the Dialog Editor. The localization always refers to complete Dialog Libraries, not to single dialogs. A Dialog Library's default state is “Not localized”. In this state dialogs behave and are stored in the same way as before the localization feature was available. 

The entry point for localizing a Dialog Library is the Manage User Interface Languages dialog that can be openend by clicking the Manage Language button in the dialog tool window (placed at the right in the second line in  Illustration 12.32). The next illustrations shows the dialog editor with an opened Manage User Interface Languages dialog.

Screenshot showing the Properties dialog of a Command buttonIllustration 12.32: Dialog Editor with Manage User Interface Languages dialog

Initially no language is defined, so the Present Languages list has no entry. The dialog captions shows that the localization refers to the complete library Standard. To enable localization for this library the Add... button has to be used. It opens another dialog allowing to chose the first language to be supported (see next illustration). The currently active UI language is preselected.

<