Creating a UNO Dialog


Use the function CreateUnoDialog to create an existing dialog. Before you can create a dialog using this function, you must create it by hand and store it in a dialog library. This section only introduces the function CreateUnoDialog. I created a dialog called MyFirstDialog in the OOMECH09 library. The only argument to the CreateUnoDialog function is the definition of a dialog. To access a dialog definition, you must include the full path to the dialog.

 CreateUnoDialog(GlobalScope.BasicLibraries.LibName.DialogName) 

The variable GlobalScope provides access to the application-level libraries-one of which is named DlgOverwriteAll and is included with OOo in the Tools library. You must load the library itself before you can use the dialog. You can manually load the library, or do it directly from the macro.

 GlobalScope.BasicLibraries.LoadLibrary("Tools") CreateUnoDialog(GlobalScope.BasicLibraries.Tools.DlgOverwriteAll) 

If the dialog is defined inside a document, directly use the BasicLibraries variable.

 CreateUnoDialog(BasicLibraries.LibName.DialogName) 

Typically, events inside the dialog are told to call subroutines that you write. For example, a button in a dialog does nothing unless events are mapped to subroutines or functions that you write. A common event to map is a "button pressed" event that tells the dialog to stop executing and unload (close the dialog). For a routine that you write to access the dialog, the dialog must be stored in a variable that it can access. I generally choose to create a private variable that holds this type of data. See Listing 21 .

Listing 21: DisplayObjectlnformation is found in the UNO module in this chapter's source code files as SC09.sxw.
start example
 Private MySampleDialog As Object Sub DisplayObjectInformation(Optional vOptionalObj)   Dim vControl  'I access the text control in the dialog   Dim s$        'Temporary string variable   Dim vObj      'Object about which to display information   REM If ths object is not provided, use the current document   If IsMissing(vOptionalObj) Then     vObj = ThisComponent   Else     vObj = vOptionalObj   End If   REM Create the dialog and set the title   MySampleDialog = CreateUnoDialog(DialogLibraries.OOMECH09.MyFirstDialog)   MySampleDialog.setTitle("Variable Type " & TypeName(vObj))   REM Get the text field from the dialog   REM I added this text manually   vControl = MySampleDialog.getControl("TextField1")   If InStr(TypeName(vObj), "Object") < 1 Then     REM If this is NOT an object, simply display simple information     vControl.setText(Dlg  _  GetObjTypeInfo(vObj))   ElseIf NOT HasUnoInterfaces(vObj, "com.sun.star.uno.XInterface") Then     REM It is an object but it is not a UNO object     REM I cannot call HasUnoInterfaces if it is not an object     vControl.setText(Dlg  _  GetObjTypeInfo(vObj))   Else     REM This is a UNO object so attempt to access the "dbg  _  " properties     MySampleDialog.setTitle("Variable Type " & vObj .getImplementationName())     s = "**************** Methods **************" & CHR$(10) &_         Dlg_DisplayDbgInfoStr(vObj.dbg_methods, ";") & CHR$(10) &_         "*************** properties **************" & CHR$(10) &_         Dlg_DisplayDbgInfoStr(vObj.dbg_properties, ";") & CHR$(10) &_         "*************** Services **************" & CHR$(10) &_         Dlg_DisplayDbgInfoStr(vObj.dbg_supportedInterfaces, CHR$(10))     vControl.setText(s)   End If   REM tell the dialog to start itself   MySampleDialog.execute() End Sub 
end example
 
Tip  

Store the dialog in a private variable declared as type Object. Use a private variable so that it doesn't needlessly affect other modules. On at least one occasion, a dialog that I created worked when it was held in a variable declared as an Object, but failed when held in a variable declared as a Variant.

The macro in Listing 21 displays a dialog that displays information about the object that was passed in as an argument. I use the HasUnoInterfaces function to see if this is a UNO service. I first check to see if the object's TypeName contains the text "Object"; this tells me if the object is really an object.

 If InStr(TypeName(vObj), "Object") < 1 Then 

If the object is not an object that supports a UNO interface, the object is inspected and information about it displayed. Listing 22 shows the text string to do this. Figure 6 shows the results.

click to expand
Figure 6: The variable is an array of strings
Listing 22: Dlg_GetObjTypelnfo is found in the UNO module in this chapter's source code files as SC09.sxw.
start example
 Function Dlg_GetObjTypeInfo(vObj) As String   Dim s As String   s = "TypeName = " & TypeName(vObj) & CHR$(10) &_     "VarType = " & VarType(vObj) & CHR$(10)   If IsNull(vObj) Then     s = s & "IsNull = True"   ElseIf IsEmpty(vObj) Then     s = s & "IsEmpty = True"   Else     If IsObject(vObj) Then s = s & "IsObject = True" & CHR$(10)     If IsUnoStruct(vObj) Then s = s & "IsUnoStruct = True" & CHR$(10)     If IsDate(vObj) Then s = s & "IsDate = True" & CHR$(10)     If IsNumeric(vObj) Then s = s & "IsNumeric = True" & CHR$(10)     If IsArray(vObj) Then       On Local Error Goto DebugBoundsError:       Dim i%, sTemp$       s = s & "IsArray = True" & CHR$(10) & "range = ("       Do While (i% >= 0)         i% = i% + 1         sTemp$ = LBound(vObj, i%) & " To " & UBound(vObj, i%)          If i% > 1 Then s = s & ", "         s = s & sTemp$       Loop       DebugBoundsError:       On Local Error Goto 0       s = s & ")" & CHR$(10)     End If   End If   Dlg_GetObjTypeInfo = s End Function 
end example
 

If the first argument supports a UNO interface, the "dbg_" properties are used to display all of the methods, properties, and services supported by this object (see Listing 23 and Figure 7 ). This is very similar to the code in Listing 9, except that it returns a string rather than displaying a series of simple dialogs. This is an excellent way to quickly see what methods, properties, and interfaces an object supports. Notice also that the variable type is displayed in the upper-left corner of the dialog.

click to expand
Figure 7: The variable is a Writer document.
Listing 23: Dlg_DisplayDbgInfoStr is found in the UNO module in this chapter's source code files as SC09.sxw.
start example
 Function Dlg_DisplayDbgInfoStr(sInfo$, sSep$) As String   Dim aInfo()                     'Array to hold each string   Dim i As Integer                'Index variable   Dim j As Integer                'Junk integer variable for temporary values   Dim s As String                 'holds the portion that is not completed   s = sInfo$   j = InStr(s, ":")               'Initial colon   If j > 0 Then Mid(s, 1, j, "")  'Remove portion up to the initial colon   aInfo() = Split(s, sSep$)                   'Split string based on delimiter   For i = LBound(aInfo()) To Ubound(aInfo())  'Look at each piece to remove     aInfo(i) = Trim(aInfo(i))                 'leading and trailing spaces     j = InStr(aInfo(i), CHR$(10))             'Some have an extra     If j > 0 Then Mid(aInfo(i), j, 1, "")     'new line that should be removed   Next   Dlg_DisplayDbgInfoStr = Join(aInfo(), CHR$(10)) End Function 
end example
 



OpenOffice.org Macros Explained
OpenOffice.org Macros Explained
ISBN: 1930919514
EAN: 2147483647
Year: 2004
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net