StarOffice 8 Programming Guide for BASIC
  Procure somente este livro
Fazer download desta apostila em PDF (1223 KB)

Chapter 11 Dialogs

You can add custom dialog windows and forms to StarOffice documents. These in turn can be linked to StarOffice Basic macros to considerably extend the usage range of StarOffice Basic. Dialogs can, for example, display database information or guide users through a step-by-step process of creating a new document in the form of an AutoPilot.

Working With Dialogs

StarOffice Basic dialogs consist of a dialog window that can contain text fields, list boxes, radio buttons, and other control elements.

Creating Dialogs

You can create and structure dialogs using the StarOffice dialog editor that you can use in the same way as a StarOffice Draw:

StarOffice dialog design window.

Essentially, you drag the control elements that you want from the design pallet (right) into the dialog area where you can define their position and size.

The example shows a dialog that contains a label and a list box.

A dialog with a label and a list box in design mode.

You can open a dialog with the following code:

Dim Dlg As Object

DialogLibraries.LoadLibrary("Standard")
Dlg = CreateUnoDialog(DialogLibraries.Standard.DlgDef)

Dlg.Execute()
Dlg.dispose()

CreateUnoDialog creates an object called Dlg that references the associated dialog. Before you can create the dialog, you must ensure that the library it uses (in this example, the Standard library) is loaded. If not, the LoadLibrary method performs this task.

Once the Dlg dialog object has been initialized, you can use the Execute method to display the dialog. Dialogs such as this one are described as modal because they do not permit any other program action until they are closed. While this dialog is open, the program remains in the Execute call.

The dispose method at the end of the code approves the resources used by the dialog once the program ends.

Closing Dialogs

Closing With OK or Cancel

If a dialog contains an OK or a Cancel button, the dialog is automatically closed when you press one of these buttons. More information about working with these buttons are discussed in this Dialog Control Elements in Detail section of this chapter.

If you close a dialog by clicking the OK button, the Execute-method returns a return value of 1, otherwise a value of 0 is returned.

Dim Dlg As Object

DialogLibraries.LoadLibrary("Standard")
Dlg = CreateUnoDialog(DialogLibraries.Standard.MyDialog)

Select Case Dlg.Execute() 
Case 1
   MsgBox "Ok pressed"
Case 0 
   MsgBox "Cancel pressed"
End Select

Closing With the Close Button in the Title Bar

If you want, you can close a dialog by clicking the close button on the title bar of the dialog window. In this instance, the Execute method of the dialog returns the value 0, the same as when you press the Cancel button.

Closing With an Explicit Program Call

You can also close an open dialog window with the endExecute method:

Dlg.endExecute()

Access to Individual Control Elements

A dialog can contain any number of control elements. You can access these elements through the getControl method that returns the name of the control element.

Dim Ctl As Object

Ctl = Dlg.getControl("MyButton")
Ctl.Label = "New Label"

This code determines the object for the MyButton control element and then initializes the Ctl object variable with a reference to the element. Finally the code sets the Label property of the control element to the New Label value.


Note –

StarOffice Basic distinguishes between uppercase and lowercase characters for the names of control elements.


Working With the Model of Dialogs and Control Elements

The division between visible program elements (View) and the data or documents behind them (Model) occurs at many places in StarOffice API. In addition to the methods and properties of control elements, both dialog and control element objects have a subordinate Model object. This object allows you to directly access the content of a dialog or control element.

In dialogs, the distinction between data and depiction is not always as clear as in other API areas of StarOffice. Elements of the API are available through both the View and the Model.

The Model property provides program-controlled access to the model of dialog and control element objects.

Dim cmdNext As Object

cmdNext = Dlg.getControl("cmdNext")
cmdNext.Model.Enabled = False

This example deactivates the cmdNtext button in the Dlg dialog with the aid of the model object from cmdNtext.

Properties

Name and Title

Every control element has its own name that can be queried using the following model property:

  • Model.Name (String) – control element name

You can specify the title that appears in the title bar of a dialog with the following model property:

  • Model.Title (String) – dialog title (only applies to dialogs).

Position and Size

You can query the size and position of a control element using the following properties of the model object:

  • Model.Height (long) – height of control element (in ma units)

  • Model.Width (long) – width of control element (in ma units)

  • Model.PositionX (long) – X-position of control element, measured from the left inner edge of the dialog (in ma units)

  • Model.PositionY (long) – Y-position of control element, measured from top inner edge of the dialog (in ma units)

To ensure platform independence for the appearance of dialogs, StarOffice uses the Map AppFont (ma) internal unit to specify the position and size within dialogs. An ma unit is defined as being one eighth of the average height of a character from the system font defined in the operating system and one quarter of its width. By using ma units, StarOffice ensures that a dialog looks the same on different systems under different system settings.

If you want to change the size or position of control elements for runtime, determine the total size of the dialog and adjust the values for the control elements to the corresponding part ratios.


Note –

The Map AppFont (ma) replaces the Twips unit to achieve better platform independence.


Focus and Tabulator Sequence

You can navigate through the control elements in any dialog by pressing the Tab key. The following properties are available in this context in the control elements model:

  • Model.Enabled (Boolean) – activates the control element

  • Model.Tabstop (Boolean) – allows the control element to be reached through the Tab key

  • Model.TabIndex (Long) – position of control element in the order of activation

Finally, the control element provides a getFocus method that ensures that the underlying control element receives the focus:

  • getFocus – control element receives the focus (only for dialogs)

Multi-Page Dialogs

A dialog in StarOffice can have more than one tab page. The Step property of a dialog defines the current tab page of the dialog whereas the Step property for a control element specifies the tab page where the control element is to be displayed.

The Step-value of 0 is a special case. If you set this value to zero in a dialog, all of the control elements are visible regardless of their Step value. Similarly, if you set this value to zero for a control element, the element is displayed on all of the tab pages in a dialog.

A multi page dialog in design mode.

In the preceding example, you can also assign the Step value of 0 to the dividing line as well as the Cancel, Prev, Next, and Done buttons to display these elements on all pages. You can also assign the elements to an individual tab page (for example page 1).

The following program code shows how the Step value in event handlers of the Next and Prev buttons can be increased or reduced and changes the status of the buttons.

Sub cmdNext_Initiated
   Dim cmdNext As Object
   Dim cmdPrev As Object
   
   cmdPrev = Dlg.getControl("cmdPrev")
   cmdNext = Dlg.getControl("cmdNext")
   
   cmdPrev.Model.Enabled = Not cmdPrev.Model.Enabled
   cmdNext.Model.Enabled = False
   
   Dlg.Model.Step = Dlg.Model.Step + 1
End Sub

Sub cmdPrev_Initiated
   Dim cmdNext As Object
   Dim cmdPrev As Object
   
   cmdPrev = Dlg.getControl("cmdPrev")
   cmdNext = Dlg.getControl("cmdNext")
   
   cmdPrev.Model.Enabled = False
   cmdNext.Model.Enabled = True
   
   Dlg.Model.Step = Dlg.Model.Step - 1
End Sub

A global Dlg variable that references an open dialog must be included to make this example possible. The dialog then changes its appearance as follows:

Page 1:

Page 1 of a multi page dialog.

Page 2:

Page 2 of a multi page dialog.

Events

StarOffice dialogs and forms are based on an event-oriented programming model where you can assign event handlers to the control elements. An event handler runs a predefined procedure when a particular action occurs, even when the action is another event. You can also edit documents or open databases with event handling as well as access other control elements.

StarOffice control elements recognize different types of events that can be triggered in different situations. These event types can be divided into four groups:

  • Mouse control: Events that correspond to mouse actions (for example, simple mouse movements or a click on a particular screen location)

  • Keyboard control: Events that are triggered by keyboard strokes

  • Focus modification: Events that StarOffice perform when control elements are activated or deactivated

  • Control element-specific events: Events that only occur in relation to certain control elements

When you work with events, ensure that you create the associated dialog in the StarOffice development environment and that it contains the required control elements or documents (if you the events apply to a form).

A dialog with two list boxes in design mode.

The preceding figure shows the StarOffice Basic development environment with a dialog window that contains two list boxes. You can move the data from one list to the other using the buttons between the two list boxes.

If you want to display the layout on screen, then you should create the associated StarOffice Basic procedures so that they can be called up by the event handlers. Even though you can use these procedures in any module, it is best to limit their use to two modules. To make your code easier to read, you should assign meaningful names to these procedures. Jumping directly to a general program procedure from a macro can result in unclear code. Instead, to simplify code maintenance and troubleshooting, you should create another procedure to serve as an entry point for event handling - even if it only executes a single call to the target procedure.

The code in the following example moves an entry from the left to the right list box of a dialog.

Sub cmdSelect_Initiated
   Dim objList As Object
   lstEntries = Dlg.getControl("lstEntries")
   lstSelection = Dlg.getControl("lstSelection")

   If lstEntries.SelectedItem > 0 Then
      lstSelection.AddItem(lstEntries.SelectedItem, 0)
      lstEntries.removeItems(lstEntries.SelectItemPos, 1)
   Else
      Beep
   End If
End Sub

If this procedure was created in StarOffice Basic, you can assign it to an event required using the property window of the dialog editor.

Dialog to assign events to macros.

The assignment dialog lists all of the StarOffice Basic procedures. To assign a procedure to an event, select the procedure, and then click Assign.

Parameters

The occurrence of a particular event is not always enough for an appropriate response. Additional information may be required. For example, to process a mouse click, you may need the screen position where the mouse button was pressed.

In StarOffice Basic, you can use object parameters to provide more information about an event to a procedure, for example:

Sub ProcessEvent(Event As Object)

End Sub

The accuracy with which the Event object is structured and its properties depend on the type of event that the procedure call triggers. The following sections describe event types in detail.

Regardless of the type of event, all objects provide access to the relevant control element and its model. The control element can be reached using

Event.Source 

and its model using

Event.Source.Model

You can use these properties to trigger an event within an event handler.

Mouse Events

StarOffice Basic recognizes the following mouse events:

  • Mouse moved – user moves mouse

  • Mouse moved while key pressed – user drags mouse while holding down a key

  • Mouse button pressed – user presses a mouse button

  • Mouse button released – user releases a mouse button

  • Mouse outside – user moves mouse outside of the current window

The structure of the associated event objects is defined in the com.sun.star.awt.MouseEvent structure which provides the following information:

  • Buttons (short) – button pressed (one or more constants in accordance with com.sun.star.awt.MouseButton).

  • X (long) – X-coordinate of mouse, measured in pixels from the top left corner of the control element

  • Y (long) – Y-coordinate of mouse, measured in pixels from the top left corner of the control element

  • ClickCount (long) – number of clicks associated with the mouse event (if StarOffice can respond fast enough, ClickCount is also 1 for a double-click because only an individual event is initiated).

The constants defined in com.sun.star.awt.MouseButton for the mouse buttons are:

  • LEFT - left mouse button

  • RIGHT- right mouse button

  • MIDDLE- middle mouse button

The following example outputs the mouse position as well as the mouse button that was pressed:

Sub MouseUp(Event As Object)
   Dim Msg As String
   Msg = "Keys: "
   If Event.Buttons AND com.sun.star.awt.MouseButton.LEFT Then
      Msg = Msg & "LEFT "
   End If
   If Event.Buttons AND com.sun.star.awt.MouseButton.RIGHT Then
      Msg = Msg & "RIGHT "
   End If
   If Event.Buttons AND com.sun.star.awt.MouseButton.MIDDLE Then
      Msg = Msg & "MIDDLE "
   End If
   Msg = Msg & Chr(13) & "Position: "
   Msg = Msg & Event.X & "/" & Event.Y
   MsgBox Msg
End Sub

Note –

The VBA Click and Doubleclick events are not available in StarOffice Basic. Instead use the StarOffice Basic MouseUp event for the click event and imitate the Doubleclick event by changing the application logic.


Keyboard Events

The following keyboard events are available in StarOffice Basic:

  • Key pressed – user presses a key

  • Key released – user releases a key

Both events relate to logical key actions and not to physical actions. If the user presses several keys to output a single character (for example, to add an accent to a character), then StarOffice Basic only creates one event.

A single key action on a modification key, such as the Shift key or the Alt key does not create an independent event.

Information about a pressed key is provided by the event object that StarOffice Basic supplies to the procedure for event handling. It contains the following properties:

  • KeyCode (short) – code of the pressed key (default values in accordance with com.sun.star.awt.Key)

  • KeyChar (String) – character that is entered (taking the modification keys into consideration)

The following example uses the KeyCode property to establish if the Enter key, the Tab key, or one of the other control keys has been pressed. If one of these keys has been pressed, the name of the key is returns, otherwise the character that was typed is returned:

Sub KeyPressed(Event As Object)
   Dim Msg As String
   Select Case Event.KeyCode
   Case com.sun.star.awt.Key.RETURN
      Msg = "Return pressed"
   Case com.sun.star.awt.Key.TAB
      Msg = "Tab pressed"
   Case com.sun.star.awt.Key.DELETE
      Msg = "Delete pressed"
   Case com.sun.star.awt.Key.ESCAPE
      Msg = "Escape pressed"
   Case com.sun.star.awt.Key.DOWN
      Msg = "Down pressed"
   Case com.sun.star.awt.Key.UP
      Msg = "Up pressed"
   Case com.sun.star.awt.Key.LEFT
      Msg = "Left pressed"
   Case com.sun.star.awt.Key.RIGHT
      Msg = "Right pressed"
   Case Else
      Msg = "Character " & Event.KeyChar & " entered"
   End Select
   MsgBox Msg
End Sub

Information about other keyboard constants can be found in the API Reference under the com.sun.star.awt.Key group of constants.

Focus Events

Focus events indicate if a control element receives or loses focus. You can use these events to, for example, determine if a user has finished processing a control element so that you can update other elements of a dialog. The following focus events are available:

  • When receiving focus – element receives focus

  • When losing focus – element loses focus

The Event objects for the focus events are structured as follows:

  • FocusFlags (short) – cause of focus change (default value in accordance with com.sun.star.awt.FocusChangeReason ).

  • NextFocus (Object) – object that receives focus (only for the When losing focus event)

  • Temporary (Boolean) – the focus is temporarily lost

Control Element-Specific Events

In addition to the preceding events, which are supported by all control elements, there are also some control element-specific events that are only defined for certain control elements. The most important of these events are:

  • When Item Changed – the value of a control element changes

  • Item Status Changed – the status of a control element changes

  • Text modified – the text of a control element changes

  • When initiating – an action that can be performed when the control element is triggered (for example, a button is pressed)

When you work with events, note that some events, such as the When initiating event, can be initiated each time you click the mouse on some control elements (for example, on radio buttons). No action is performed to check if the status of the control element has actually changed. To avoid such “blind events”, save the old control element value in a global variable, and then check to see if the value has changed when an event is executing.

The properties for the Item Status Changed event are:

  • Selected (long) – currently selected entry

  • Highlighted (long) – currently highlighted entry

  • ItemId (long) – ID of entry

Dialog Control Elements in Detail

StarOffice Basic recognizes a range of control elements which can be divided into the following groups:

Entry fields:

  • Text fields

  • Date fields

  • Time fields

  • Numerical fields

  • Currency fields

  • Fields adopting any format

Buttons:

  • Standard buttons

  • Checkboxes

  • Radio Buttons

Selection lists:

  • List boxes

  • Combo-boxes

Other control elements:

  • Scrollbars ( horizontal and vertical)

  • Fields of groups

  • Progress bars

  • Dividing lines (horizontal and vertical)

  • Graphics

  • File selection fields

The most important of these control elements are presented below.

Buttons

A button performs an action when you click it.

The simplest scenario is for the button to trigger a When Initiating event when it is clicked by a user. You can also link another action to the button to open a dialog using the PushButtonType property. When you click a button that has this property set to the value of 0, the dialog remains unaffected. If you click a button that has this property set to the value of 1, the dialog is closed, and the Execute method of the dialog returns the value 1 (dialog sequence has been ended correctly). If the PushButtonType has the value of 2, the dialog is closed and the Execute method of the dialog returns the value 0 (dialog closed).

The following are all of the properties that are available through the button model:

  • Model.BackgroundColor (long) – color of background

  • Model.DefaultButton (Boolean) – The button is used as the default value and responds to the Enter key if it has no focus.

  • Model.FontDescriptor (struct) – structure that specifies the details of the font to be used (in accordance with com.sun.star.awt.FontDescriptor structure)

  • Model.Label (String) – label that is displayed on the button

  • Model.Printable (Boolean) – the control element can be printed

  • Model.TextColor (Long) – text color of the control element

  • Model.HelpText (String) – help text that is displayed when you move the mouse cursor over the control element

  • Model.HelpURL (String) – URL of the online help for the corresponding control element

  • PushButtonType (short) – action that is linked to the button (0: no action, 1: OK, 2: Cancel)

Option Buttons

These buttons are generally used in groups and allow you to select from one of several options. When you select an option, all of the other options in the group are deactivated. This ensures that at any one time, only one option button is set.

An option button control element provides two properties:

  • State (Boolean) – activates the button

  • Label (String) – label that is displayed on the button

You can also use the following properties from the model of the option buttons:

  • Model.FontDescriptor (struct) –structure with details of the font to be used (in accordance with com.sun.star.awt.FontDescriptor)

  • Model.Label (String) – label that is displayed on the control element

  • Model.Printable (Boolean) – control element can be printed

  • Model.State (Short) – if this property is equal to 1, the option is activated, otherwise it is deactivated

  • Model.TextColor (Long) – text color of control element

  • Model.HelpText (String) – help text that is displayed when the mouse cursor rests over the control element

  • Model.HelpURL (String) – URL of online help for the corresponding control element

To combine several option buttons in a group, you must position them one after another in the activation sequence without any gaps (Model.TabIndex property, described as Order in the dialog editor). If the activation sequence is interrupted by another control element, then StarOffice automatically starts with a new control element group that can be activated regardless of the first group of control elements.


Note –

Unlike VBA, you cannot insert option buttons in a group of control elements in StarOffice Basic. The grouping of control elements in StarOffice Basic is only used to ensure a visual division by drawing a frame around the control elements.


Checkboxes

Checkboxes are used to record a Yes or No value and depending on the mode, they can adopt two or three states. In addition to the Yes and No states, a check box can have an in-between state if the corresponding Yes or No status has more than one meaning or is unclear.

Checkboxes provide the following properties:

  • State (Short) – state of the checkbox (0: no, 1: yes, 2: in-between state)

  • Label (String) – label for the control element

  • enableTriState (Boolean) – in addition to the activated and deactivated states, you can also use the in-between state

The model object of a checkbox provides the following properties:

  • Model.FontDescriptor (struct) – structure with details of the font used (in accordance with com.sun.star.awt.FontDescriptor structure)

  • Model.Label (String) – label for the control element

  • Model.Printable (Boolean) – the control element can be printed

  • Model.State (Short) – state of the checkbox (0: no, 1: yes, 2: in-between state)

  • Model.Tabstop (Boolean) – the control element can be reached with the Tab key

  • Model.TextColor (Long) – text color of control element

  • Model.HelpText (String) – help text that is displayed when you rest the mouse cursor over the control element

  • Model.HelpURL (String) – URL of online help for the corresponding control element

Text Fields

Text fields allow users to type numbers and text. The com.sun.star.awt.UnoControlEdit. service forms the basis for text fields.

A text field can contain one or more lines and can be edited or blocked for user entries. Text fields can also be used as special currency and numerical fields as well as screen fields for special tasks. As these control elements are based on the UnoControlEdit Uno service, their program-controlled handling is similar.

Text fields provide the following properties:

  • Text (String) – current text

  • SelectedText (String) – currently highlighted text

  • Selection (Struct) – read-only highlighting of details (structure in accordance with com.sun.star.awt.Selection, with the Min and Max properties to specify the start and end of the current highlighting)

  • MaxTextLen (short) – maximum number of characters that you can type in the field

  • Editable (Boolean)True activates the option for entering text, False blocks the entry option (the property cannot be called up directly but only through IsEditable)

  • IsEditable (Boolean) – the content of the control element can be changed, read-only.

Furthermore, the following properties are provided through the associated model object:

  • Model.Align (short) – orientation of text (0: left-aligned, 1: centered, 2: right-aligned)

  • Model.BackgroundColor (long) – color of the background of the control element

  • Model.Border (short) –type of border (0: no border, 1: 3D border, 2: simple border)

  • Model.EchoChar (String) – echo character for password fields

  • Model.FontDescriptor (struct) – structure with details of font used (in accordance with com.sun.star.awt.FontDescriptor structure)

  • Model.HardLineBreaks (Boolean) – automatic line breaks are permanently inserted in the control element text

  • Model.HScroll (Boolean) – the text has a horizontal scrollbar

  • Model.MaxTextLen (Short) – maximum length of text, where 0 corresponds to no length limit

  • Model.MultiLine (Boolean) – permits entry to spans several lines

  • Model.Printable (Boolean) – the control element can be printed

  • Model.ReadOnly (Boolean) – the content of the control element is read-only

  • Model.Tabstop (Boolean) – the control element can be reached with the Tab key

  • Model.Text (String) – text associate with the control element

  • Model.TextColor (Long) – text color of control element

  • Model.VScroll (Boolean) – the text has a vertical scrollbar

  • Model.HelpText (String) – help text that is displayed when the mouse cursor rests over the control element

  • Model.HelpURL (String) – URL of online help for the corresponding control element

List Boxes

List boxes (com.sun.star.awt.UnoControlListBox service) support the following properties:

  • ItemCount (Short) – number of elements, read-only

  • SelectedItem (String) – text of highlighted entry, read-only

  • SelectedItems (Array Of Strings) – data field with highlighted entries, read-only

  • SelectItemPos (Short) – number of the entry highlighted at present, read-only

  • SelectItemsPos (Array of Short) – data field with the number of highlighted entries (for lists which support multiple selection), read-only

  • MultipleMode (Boolean)True activates the option for multiple selection of entries, False blocks multiple selections (the property cannot be called up directly but only through IsMultipleMode)

  • IsMultipleMode (Boolean) – permits multiple selection within lists, read-only

List boxes provide the following methods:

  • addItem (Item, Pos) – enters the string specified in the Item into the list at the Pos position

  • addItems (ItemArray, Pos) – enters the entries listed in the string's ItemArray data field into the list at the Pos position

  • removeItems (Pos, Count) – removes Count entries as of the Pos position

  • selectItem (Item, SelectMode) – activates or deactivates highlighting for the element specified in the string Item depending on the SelectMode Boolean variable

  • makeVisible (Pos) – scrolls through the list field so that the entry specified with Pos is visible

The model object of the list boxes provides the following properties:

  • Model.BackgroundColor (long) – background color of control element

  • Model.Border (short) – type of border (0: no border, 1: 3D border, 2: simple border)

  • Model.FontDescriptor (struct) – structure with details of font used (in accordance with com.sun.star.awt.FontDescriptor structure)

  • Model.LineCount (Short) – number of lines in control element

  • Model.MultiSelection (Boolean) – permits multiple selection of entries

  • Model.SelectedItems (Array of Strings) – list of highlighted entries

  • Model.StringItemList (Array of Strings) – list of all entries

  • Model.Printable (Boolean) – the control element can be printed

  • Model.ReadOnly (Boolean) – the content of the control element is read-only

  • Model.Tabstop (Boolean) – the control element can be reached with the Tab key.

  • Model.TextColor (Long) – text color of control element

  • Model.HelpText (String) – automatically displayed help text which is displayed if the mouse cursor is above the control element

  • Model.HelpURL (String) – URL of online help for the corresponding control element


Note –

The VBA option for issuing list entries with a numerical additional value (ItemData) does not exist in StarOffice Basic. If you want to administer a numerical value (for example a database ID) in addition to the natural language text, you must create an auxiliary data field that administers in parallel to the list box.