OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

how to obtain the start position of a textcursor in a number

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
ramcarsoft
Guest





PostPosted: Tue Mar 30, 2004 1:40 am    Post subject: how to obtain the start position of a textcursor in a number Reply with quote

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
Administrator


Joined: 09 Mar 2004
Posts: 3655
Location: Columbus, Ohio, USA

PostPosted: Tue Mar 30, 2004 1:59 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website AIM Address
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Mar 30, 2004 2:06 pm    Post subject: Reply with quote

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
View user's profile Send private message
Iannz
OOo Advocate
OOo Advocate


Joined: 14 Feb 2004
Posts: 494
Location: Christchurch, New Zealand

PostPosted: Wed Mar 31, 2004 5:00 pm    Post subject: Comparing regions in Writer Reply with quote

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
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Apr 01, 2004 8:09 am    Post subject: Reply with quote

compareRegionEnds() and compareRegionStarts() in the Text service answered my question. Thanks.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
ramcarsoft
Guest





PostPosted: Thu Apr 01, 2004 11:21 pm    Post subject: ok Reply with quote

Ok,
a lot of thaks to all.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
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