Apache OpenOffice (AOO) Bugzilla – Issue 76449
CurrentSelection (writer) returns wrong values
Last modified: 2013-02-24 21:08:31 UTC
Try the following code at a text document. Sub Testselect Dim oDoc As Object Dim oSelection As Object Dim i As Integer Dim s As String oDoc = ThisComponent oSelection = ThisComponent.CurrentSelection s = "" If oSelection.hasElements() Then For i = 0 To oSelection.getCount() - 1 If i > 0 Then s = s & chr( 13 ) s = s & Format( i, "00" ) & (": ") & oSelection.getByIndex( i ).getString() Next i MsgBox( s ) Else MsgBox( "no selection" ) End If End Sub Problems: 1. oSelection.hasElements() is even True if nothing is selected. 2. oSelection.getCount() returns 1 if nothing is selected or there exits one selection. 3. oSelection.getCount() returns n + 1 if n ranges are selected (n > 1). 4. Exists only one selection then oSelection.getByIndex( 0 ) returns the selection. 5. Exists n > 1 selections then oSelection.getByIndex( 0 ) is empty and oSelection.getByIndex( 1 ) to oSelection.getByIndex( n ) returns the selections.
It has been this way since version 1.0. What you do not see is that the "empty" selection is really the empty cursor selection. Although you may argue that this is a bug, it is possible that existing code relies on the empty selection as the cursor position. Consider the following, which provides a little text around the "empty" selection. Sub Testselect Dim oDoc As Object Dim oSelection As Object Dim i As Integer Dim s As String Dim oText Dim oSel Dim oCurs oDoc = ThisComponent oSelection = ThisComponent.CurrentSelection s = "" If oSelection.hasElements() Then For i = 0 To oSelection.getCount() - 1 If i > 0 Then s = s & chr( 13 ) oSel = oSelection.getByIndex( i ) s = s & Format( i, "00" ) & (": ") & oSel.getString() oText = oSel.getText() If oText.compareRegionStarts(oSel.getStart(), oSel.getEnd()) = 0 Then oCurs = oText.createTextCursorByRange(oSel) oCurs.goLeft(2, False) oCurs.goRight(4, True) s = s & "<EMPTY> : " & oCurs.getString() End If Next i MsgBox( s ) Else REM You will probably NEVER get here MsgBox( "no selection" ) End If End Sub In Summary, yes, the behavior is exactly as you state (you should have read my book or my free macro guide, which discusses this), but, is it a bug? I would say no, but I am not in the position to say so.
Is there a need for the empty selection if more than one ranges selected? I think no. So the logical results should be oSelection.getCount() - oSelection.hasElements() . 0 - False . 1 - True . 2 - True . ... Or even because there are always at least one (empty) selection: . 1 - False . 1 - True . 2 - True . ... If there is any need for the empty selection if any is selected (but IMO there isn't), then it should also _always_ exist: . 1 - True . 2 - True . 3 - True . ... But not 1 - 1 - 3 - 4 - ... as now! Sub Testselect Dim oDoc As Object Dim oSelection As Object Dim i As Integer Dim s As String oDoc = ThisComponent oSelection = ThisComponent.getCurrentSelection() If oSelection.hasElements() Then For i = 0 To oSelection.getCount() - 1 oSelection.getByIndex( i ).setString( "<--- " & Format( i, "00" ) & " --->" ) Next i MsgBox( s ) Else MsgBox( "no selection" ) End If End Sub With this sample you can see that it counts 0 0 1 - 2+0 1 - 2 - 3+0 1 - 2 - 3 - 4+0 But i can't see the logic and need for the "+0".
The reason that you see the "empty" selection is because you did start to make a selection. Consider the following: Nothing selected: Empty selection representing the current cursor position Select a single area: One selection Now, how do you select multiple areas? it causes you to enter a multi-selection mode. The method usually is done as follows: 1. Select initial area any way you desire. 2. Hold down the control key and place the cursor at a specific point 3. Hold down the left mouse button (you just set the cursor) 4. Now drag the mouse and release (you just set the cursor again) and you have three selections. On a whim, Hold down the control key and use the cursor keys to move the cursor. You still have three selections but they all contain text. If you stop at step three and then use the keyboard to move the mouse, then you do not start the third selection. My guess is that this behavior represents how the controller works internally. mroe, I had to read your message a few times to understand what you were saying... I never have selections.getCount() return zero. I do not always have an empty selection. I think that what you really want to say is that you should never return an empty selection. Be careful, because that empty selection may have something of interest attached to it such as an image. I did test and try to select some text, a graphic, and some text, but the act of selecting the graphic "unselected" the text. The returned object was then the graphic and NOT a selections object. That said, I rarely care about the empty selection...
> I had to read your message a few times to understand what you were saying... Is it my english or my description or both? But now i understand that there is a possibility that oSelection.getCount() returns 2. So i think also that is not a bug, but a unhappy implementation ...
i agree that it is not intuitive but we won't fix anything here at the moment