Issue 76449 - CurrentSelection (writer) returns wrong values
Summary: CurrentSelection (writer) returns wrong values
Status: CLOSED IRREPRODUCIBLE
Alias: None
Product: App Dev
Classification: Unclassified
Component: api (show other issues)
Version: 3.3.0 or older (OOo)
Hardware: PC All
: P3 Trivial
Target Milestone: ---
Assignee: jsc
QA Contact: issues@api
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-04-17 15:25 UTC by mroe
Modified: 2013-02-24 21:08 UTC (History)
1 user (show)

See Also:
Issue Type: DEFECT
Latest Confirmation in: ---
Developer Difficulty: ---


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description mroe 2007-04-17 15:25:35 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.
Comment 1 andrew 2007-04-17 23:08:52 UTC
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.
Comment 2 mroe 2007-04-18 13:11:39 UTC
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".
Comment 3 andrew 2007-04-19 00:41:11 UTC
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...

Comment 4 mroe 2007-04-19 12:35:40 UTC
> 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 ...
Comment 5 jsc 2007-04-27 12:31:22 UTC
i agree that it is not intuitive but we won't fix anything here at the moment