Ask Your Question

Revision history [back]

For all modules (Writer, Calc, Impress, Draw), the graphic objects are iterated using draw pages.

As you are talking about Writer: there is only one global draw page per Writer document, which is accessed using com.sun.star.drawing.XDrawPageSupplier interface. oDoc.DrawPage returns the draw page.

To iterate through objects, you get their count (using oDrawPage.Count), and then get individual objects using oDrawPage(i).

To find out if a graphical object is in selection, you need to know what selection you have. It might be no selection, or a range, or a single object, or multiple objects. You may use this code:

Function IsSelectionEmpty(ByRef oSel)
  IsSelectionEmpty = False
  If (IsNull(oSel)) Then
    IsSelectionEmpty = True
  ElseIf (HasUnoInterfaces(oSel, "com.sun.star.drawing.XShape")) Then
    ' Selected single object - selection isn't empty; do nothing
  ElseIf (HasUnoInterfaces(oSel, "com.sun.star.sheet.XSheetCellRange")) Then
    ' Something is selected anyway
  ElseIf (oSel.Count = 0) Then
    IsSelectionEmpty = True
  ElseIf ((oSel.Count = 1) And oSel.supportsService("com.sun.star.text.TextRanges")) Then
    With oSel(0)
      If (.text.compareRegionStarts(.getStart, .getEnd)=0) Then
        IsSelectionEmpty = True
      End If
    End With
  End If
End Function

Function IsTextRangeInsideRange(ByRef oRange1, ByRef oRange2)
  IsTextRangeInsideRange = (oRange2.text.compareRegionStarts(oRange1, oRange2)<=0)_
                       And (oRange2.text.compareRegionEnds(oRange1, oRange2)>=0)
End Function

Function IsInSelection(ByRef oObj, ByRef oSel)
  Dim i As Long
  If (IsSelectionEmpty(oSel)) Then
    IsInSelection = True
  ElseIf (HasUnoInterfaces(oSel, "com.sun.star.drawing.XShape")) Then
    IsInSelection = EqualUnoObjects(oObj, oSel)
  ElseIf (HasUnoInterfaces(oSel, "com.sun.star.drawing.XShapes")) Then
    IsInSelection = False
    For i = 0 To oSel.Count - 1
      If (EqualUnoObjects(oObj, oSel(i))) Then
        IsInSelection = True
        Exit For
      End If
    Next i
  Else
    IsInSelection = False
    For i = 0 To oSel.Count - 1
      If (IsTextRangeInsideRange(oObj.Anchor, oSel(i))) Then
        IsInSelection = True
        Exit For
      End If
    Next i
  End If
End Function

After you identified that object is inside selection, then you need to check that it has proper type (e.g., it is an image), and perform required actions.

HTH.

For all modules (Writer, Calc, Impress, Draw), the graphic objects are iterated using draw pages.

As you are talking about Writer: there is only one global draw page per Writer document, which is accessed using com.sun.star.drawing.XDrawPageSupplier interface. oDoc.DrawPage returns the draw page.

To iterate through objects, you get their count (using oDrawPage.Count), and then get individual objects using oDrawPage(i).

To find out if a graphical object is in selection, you need to know what selection you have. It might be no selection, or a range, or a single object, or multiple objects. You may use this code:

Function IsSelectionEmpty(ByRef oSel)
  IsSelectionEmpty = False
  If (IsNull(oSel)) Then
    IsSelectionEmpty = True
  ElseIf (HasUnoInterfaces(oSel, "com.sun.star.drawing.XShape")) Then
    ' Selected single object - selection isn't is not empty; do nothing
nothing '
  ElseIf (HasUnoInterfaces(oSel, "com.sun.star.sheet.XSheetCellRange")) Then
    ' Something is selected anyway
anyway '
  ElseIf (oSel.Count = 0) Then
    IsSelectionEmpty = True
  ElseIf ((oSel.Count = 1) And oSel.supportsService("com.sun.star.text.TextRanges")) Then
    With oSel(0)
      If (.text.compareRegionStarts(.getStart, .getEnd)=0) Then
        IsSelectionEmpty = True
      End If
    End With
  End If
End Function

Function IsTextRangeInsideRange(ByRef oRange1, ByRef oRange2)
  IsTextRangeInsideRange = (oRange2.text.compareRegionStarts(oRange1, oRange2)<=0)_
                       And (oRange2.text.compareRegionEnds(oRange1, oRange2)>=0)
End Function

Function IsInSelection(ByRef oObj, ByRef oSel)
  Dim i As Long
  If (IsSelectionEmpty(oSel)) Then
    IsInSelection = True
  ElseIf (HasUnoInterfaces(oSel, "com.sun.star.drawing.XShape")) Then
    IsInSelection = EqualUnoObjects(oObj, oSel)
  ElseIf (HasUnoInterfaces(oSel, "com.sun.star.drawing.XShapes")) Then
    IsInSelection = False
    For i = 0 To oSel.Count - 1
      If (EqualUnoObjects(oObj, oSel(i))) Then
        IsInSelection = True
        Exit For
      End If
    Next i
  Else
    IsInSelection = False
    For i = 0 To oSel.Count - 1
      If (IsTextRangeInsideRange(oObj.Anchor, oSel(i))) Then
        IsInSelection = True
        Exit For
      End If
    Next i
  End If
End Function

After you identified that object is inside selection, then you need to check that it has proper type (e.g., it is an image), and perform required actions.

HTH.