| View previous topic :: View next topic |
| Author |
Message |
ramcarsoft Guest
|
Posted: Tue Mar 30, 2004 1:40 am Post subject: how to obtain the start position of a textcursor in a number |
|
|
Hi,
I need to obtain de position of a textcursor in a writer document. In my document I must to serach a few words and I need to know in which position they are.
I'm trying with getStart but with this method I obtain another cursor and i need the positon in a number no the object.
Thaks a lot!!! |
|
| Back to top |
|
 |
pitonyak Administrator


Joined: 09 Mar 2004 Posts: 3618 Location: Columbus, Ohio, USA
|
Posted: Tue Mar 30, 2004 1:59 pm Post subject: |
|
|
What do you mean you require a position that is a number and not a text cursor?
OOo returns a text cursor when it finds text. There is no particular number to associate with the object.
Are you saying that you want to know how many characters are between the text cursor and the beginning of the document? To find this, you must count them.
Do you want the location on the page? This, you can find!
do you want to know the page number? _________________ --
Andrew Pitonyak
http://www.pitonyak.org/oo.php |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Tue Mar 30, 2004 2:06 pm Post subject: |
|
|
I have wanted something similar in the past.
Let me try to state differently, what I think the original thread might be asking for.....
Is there a way to compare two text cursors to see if one comes before, or is on the same position, or comes after another text cursor?
Even without some kind of absolute numbering, just being able to simply compare two text cursors for beforeness and afterness would be very useful.
Maybe I am misinterpreting what the original poster is asking? _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
Iannz OOo Advocate

Joined: 14 Feb 2004 Posts: 494 Location: Christchurch, New Zealand
|
Posted: Wed Mar 31, 2004 5:00 pm Post subject: Comparing regions in Writer |
|
|
Andrew Pitonyak has a section about this in his document.
But, there is a problem.
See http://www.oooforum.org/forum/viewtopic.php?t=7331
The macro code below works for finding something and checking whether it is within the selected range except if the selection starts outside a table and the found object is within a table. The code doesn't yet deal with textFrames at all.
It is also worth noting that "find" doesn't search inside fields and it searches textframes after it is has searched the rest of the document.
Here's the code that I have been experimenting with (dim statements removed), for what its worth:
| Code: |
Sub Main
oRange = StarDesktop.currentComponent.currentSelection
nCount = 0
sFind = "\<." '"([:alnum:]|'|-|\(|\.)*"
bInTable = fnInTable(oRange)
if hasUnoInterfaces(oRange, "com.sun.star.text.XTextTableCursor") then
'There can be only one selection if the current selection is a TextTableCursor
nCount = fnFind(oRange, sFind, bInTable)
else
'The selection is either a selection in the main document
'or within a cell but not crossing a cell boundary
for i = 0 to oRange.Count - 1
nCount = nCount + fnFind(oRange(i), sFind, bInTable)
next
end if
msgbox "Number of words = " & nCount
end sub
function fnFind(oRange, sFind, bInTable) as long
'This function uses concepts that were used by Andrew Brown, and he credits Andrew Pitonyak
dim oDoc as object, oSearchDescriptor as object, oText as object
dim oFound as object, oEndCursor as object, oTestCursor as object
dim nCount as long
oDoc = StarDesktop.CurrentComponent
oSearchDescriptor = oDoc.createSearchDescriptor
oSearchDescriptor.searchRegularExpression=TRUE
oSearchDescriptor.setSearchString(sFind)
oText = oRange.text 'oDoc.text
if not oText.compareRegionStarts(oRange.start, oRange.end) then
oFound = oText.createTextCursorByRange(oRange.start)
oEndCursor = oText.createTextCursorByRange(oRange.end)
else
oFound = oText.createTextCursorByRange(oRange.end)
oEndCursor = oText.createTextCursorByRange(oRange.start)
end if
oFound = oDoc.findNext(oFound.end, oSearchDescriptor)
'on error goto finished
do while not isnull(oFound)
if not isEmpty(oFound.cell) then
if not bInTable then
'Current find is in a table but the original selection didn't start in a table
'so is this table within the original selection
'msgbox oText.compareRegionEnds(oFound.textTable.anchor.start, oEndCursor)
else
if oText.compareRegionEnds(oFound, oEndCursor) = -1 then
'Original selection in a table and current find in a table but past end of original selection
exit do
end if
end if
else
if bInTable then
'Original selection in a table but now not in a table so have finished
exit do
end if
if oText.compareRegionEnds(oFound, oEndCursor) = -1 then exit do
end if
nCount = nCount + 1
oFound = oDoc.findNext(oFound.end, oSearchDescriptor)
loop
finished:
fnFind = nCount
end function
function fnInTable(oRange)
if hasUnoInterfaces(oRange, "com.sun.star.text.XTextTableCursor") then
fnInTable = true
else
fnInTable = not isEmpty(oRange(0).cell)
end if
end function |
_________________ Cheers, Ian
http://wiki.services.openoffice.org/wiki/Extensions_development_basic a wiki about writing OpenOffice.org extensions. |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
ramcarsoft Guest
|
Posted: Thu Apr 01, 2004 11:21 pm Post subject: ok |
|
|
Ok,
a lot of thaks to all. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|