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

What is the deal with this cursor?

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


Joined: 03 Apr 2003
Posts: 7
Location: Chicago, IL

PostPosted: Thu Apr 10, 2003 12:28 pm    Post subject: What is the deal with this cursor? Reply with quote

Hello all,
I'm working on one of my first macros and I can't seem to understand how to control the cursor. The macro I'm writing loops through a series of *.TIF images and inserts them into a Swriter document.
Problem: No matter what I try each TIF image is inserted "over" the previous one.
I'm working with OOo1.1Beta. and cobbling this macro together from different examples. I have the Developer's Guide but I can't seem to wade through and figure out what I'm doing wrong.?
please Help.. Crying or Very sad

Thanks,
chrisj0


Code:

Sub Main
   Dim oTIF as String
   
   oInDir = "/home/oper/stardocs/"
   oInDirURL = ConvertToURL(oInDir)
   oTif = Dir(oInDir+"*.TIF") ' lists directory for any *.TIF files.

   While oTif <> "" ' loop through all TIF files in the directory
      oTifURL = oInDirURL+oTif

   
   importTIF(oTifURL) ' call the import function for each TIF
      
      oTif = Dir
   wend
      

End Sub

Function importTIF(oTifURL as String)

   oDoc = ThisComponent
   oText = oDoc.getText()
   
   oGraphicObject = oDoc.createInstance("com.sun.star.text.GraphicObject")
   oGraphicObject.GraphicURL = oTifURL
REM *** these Cursor's have been modified alot they started as *.getStart()
   oViewCursor = oDoc.getCurrentController().getViewCursor()
   oCursor = oText.createTextCursorByRange(oViewCursor.getEnd())
   oText.insertTextContent(oCursor.getEnd(), oGraphicObject, false)   

End Function
Back to top
View user's profile Send private message
Skeeve
General User
General User


Joined: 09 Apr 2003
Posts: 31
Location: Germany

PostPosted: Thu Apr 10, 2003 10:15 pm    Post subject: Reply with quote

Hi!

I haven't looked into your code, but if the inserted text, graphics, whatever gets overwritten by the next insertion its mostly for the reason that the first insertion is selected and the new insertion replaces the selection. So to remedy this situation simply collapseToEnd() your cursor.
Back to top
View user's profile Send private message
chrisj0
General User
General User


Joined: 03 Apr 2003
Posts: 7
Location: Chicago, IL

PostPosted: Fri Apr 11, 2003 10:18 am    Post subject: I Still can't get it right... Reply with quote

thanks for the reply.
I added a collapseToEnd() after the Image is inserted. But it still is only creating a document with one image, which is the last one in the group.
I've again included some code. But this time just the function I'm calling from the main sub. this is where I'm having problems.
I still don't know if I'm approaching this correctly. The documentation I can find explains the model and view cursor but I don't get when to use them. The example macro I've been using as a reference shows how to create a view cursor so I stole that code for this macro.
thanks again,
chrisj0 Confused Exclamation

Code:

Function importTIF(oTifURL as String)

   oDoc = ThisComponent
   oText = oDoc.getText()
   
   oGraphicObject = oDoc.createInstance("com.sun.star.text.GraphicObject")
   oGraphicObject.GraphicURL = oTifURL
   oViewCursor = oDoc.getCurrentController().getViewCursor()
   oCursor = oText.createTextCursorByRange(oViewCursor().getEnd())
   oText.insertTextContent(oCursor(), oGraphicObject, false)
   oCursor.collapseToEnd()
REM   oText.insertControlCharacter(oCursor,com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK,FALSE)
REM   oText.insertTextContent(oText.getStart(), oGraphicObject, false)   


End Function
Back to top
View user's profile Send private message
Skeeve
General User
General User


Joined: 09 Apr 2003
Posts: 31
Location: Germany

PostPosted: Fri Apr 11, 2003 10:35 pm    Post subject: Reply with quote

Hmmm...


I will look into your code a bit more when I have time ;-) Right now my wife wants breakfast ;-)

Anyway. What I already saw is that you create your TextCursor based on the view Cursor EACH TIME! Why? Simply create the cursor once in your main routine and make it a parameter to your import function.

But as I said: I'll take a deeper look into it.
Back to top
View user's profile Send private message
Skeeve
General User
General User


Joined: 09 Apr 2003
Posts: 31
Location: Germany

PostPosted: Fri Apr 11, 2003 10:45 pm    Post subject: Reply with quote

I couldn't resist to test your code. My wife is starving ;-)

The good message: Your code works fine.

The bad message: You will have to take a look into how images are anchored to a page / character / Paragraph...

The proof: Simply click on the "one" image and drag it away...
Back to top
View user's profile Send private message
chrisj0
General User
General User


Joined: 03 Apr 2003
Posts: 7
Location: Chicago, IL

PostPosted: Mon Apr 14, 2003 9:58 am    Post subject: Reply with quote

Thanks Skeeve,
...for looking over my code.
I see what you mean in the first reply about calling the cursor every time (within the loop). But I'm not sure how to move into the main sub. I don't know how to pass the objects (the graphic object and the cursor ) to the second sub/function.
I'll look into how the Graphics object gets anchored to the page now.

thanks again,
chrisj0
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Mon Apr 14, 2003 3:38 pm    Post subject: Reply with quote

Hi Chris,

Just inserting graphics directly in the program by hand I note that after a graphic is inserted it is selected and you must click back into the document to get the focus there. At this point if you do not enter a paragraph break before inserting a new graphic it will pile on top of first one. With a paragraph break the next graphic will move down the page and be anchored to the new paragraph.

Hope this obsevation helps.

JohnV
Back to top
View user's profile Send private message
chrisj0
General User
General User


Joined: 03 Apr 2003
Posts: 7
Location: Chicago, IL

PostPosted: Tue Apr 15, 2003 1:25 pm    Post subject: some progress Reply with quote

Thanks JohnV,

I added a paragraph break. and now I have a blank page between each of the TIFs. These (TIFs) are scanned 8.5x11 documents and I need to assemble a document of multiple images one per page.
Why is this so hard??? Sad

i'm including the latest version of the macro below:
thanks again,
chrisj0

Code:

REM  *****  BASIC  *****

Sub Main

   Dim oTIF as String
   Dim oTifURL as String
   Dim oInDir as String
   Dim oInDirURL as String
   Dim oText as Object
   Dim oDoc as Object
   Dim oViewCursor as Object
   Dim oCursor as Object

   oInDir = "/home/oper/stardocs/"
   oInDirURL = ConvertToURL(oInDir)
   oDoc = ThisComponent
   oText = oDoc.getText()
   oTif = Dir(oInDir+"*.TIF") ' lists directory for any *.TIF files. next time dir is called it will be the next file
   oViewCursor = oDoc.getCurrentController().getViewCursor()
   oCursor = oText.createTextCursorByRange(oViewCursor.getText())
   

   While oTif <> "" ' loop through all TIF files in the directory
      oTifURL = oInDirURL+oTif

   
   importTIF(oTifURL, oCursor) ' call the import function for each TIF
      
      oTif = Dir
   wend
      

End Sub
 
 
Sub importTIF(oTifURL as String, oCursor as Object)

   Dim oDoc as Object
   Dim oText as Object
   Dim oGraphicsObject as Object
   Dim oNCursor as Object   

   oDoc = ThisComponent
   oText = oDoc.getText()
   
   
   oGraphicObject = oDoc.createInstance("com.sun.star.text.GraphicObject")
   oGraphicObject.GraphicURL = oTifURL
   TextContentAnchorType = AT_PARAGRAPH
   oText.insertTextContent(oCursor(), oGraphicObject, false)
   REM oCursor.collapseToEnd() 'I don't think this makes a difference...
   oText.insertControlCharacter(oCursor, PARAGRAPH_BREAK, false,)

End Sub
Back to top
View user's profile Send private message
Guest






PostPosted: Tue Apr 15, 2003 6:44 pm    Post subject: Reply with quote

The requirements are unfolding day by day!
Although JohnV rightly identified the paragraph point for anchoring, be aware that OOo is more flexible than that and allows the anchor to be set elsewhere (see Help on Anchor).
I suggest that you need to decide the format of the page that you require (the layout of graphics) which then determines the scaling to be applied to each graphic. The you need to anchor the graphic to the page and position it relative to the anchor depending on grid position.
Back to top
dfrench
Moderator
Moderator


Joined: 03 Mar 2003
Posts: 1605
Location: Wellington, New Zealand

PostPosted: Tue Apr 15, 2003 6:48 pm    Post subject: Reply with quote

Quote:
The requirements are unfolding day by day!


that guest was me!
Back to top
View user's profile Send private message
Skeeve
General User
General User


Joined: 09 Apr 2003
Posts: 31
Location: Germany

PostPosted: Tue Apr 15, 2003 9:44 pm    Post subject: Reply with quote

And when it comes to scaling of graphics, he will notice that it's not that easy too. I had that problem some time ago. I just found out that I have to wait an unspecified amount of time until OOo tells me about the size of the just-loaded graphics-file.

Maybe a listener method could help, but I went another workaround that worked for my problem.
Back to top
View user's profile Send private message
chrisj0
General User
General User


Joined: 03 Apr 2003
Posts: 7
Location: Chicago, IL

PostPosted: Wed Apr 16, 2003 2:36 pm    Post subject: Reply with quote

I'm not scaling anything. the tif images are already a full page. The insert works just fine but It will always put a blank page in between each inserted tif. the anchor doesn't seem to make a difference.
I'm so frustrated I could put a hole through the front of my monitor. Evil or Very Mad Evil or Very Mad
here is the current insert code snip...
Code:


   oDoc = ThisComponent
   oText = oDoc.getText()
   
   
   oGraphicObject = oDoc.createInstance("com.sun.star.text.GraphicObject")
   oGraphicObject.GraphicURL = oTifURL
   TextContentAnchorType = AT_PARAGRAPH
   oText.insertTextContent(oCursor(), oGraphicObject, true)
   oText.insertControlCharacter(oCursor, PARAGRAPH_BREAk, true,)   
   oCursor.collapseToEnd()


Am I even approaching this right?
chrisj
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Wed Apr 16, 2003 3:40 pm    Post subject: Reply with quote

This version of your code appears to get rid of the extra pages (except an extra one at the end). You will need to edit the oInDir back to your directory.

Code:
REM  *****  BASIC  *****

Sub Main

   Dim oTIF as String
   Dim oTifURL as String
   Dim oInDir as String
   Dim oInDirURL as String
   Dim oText as Object
   Dim oDoc as Object
   Dim oViewCursor as Object
   Dim oCursor as Object

   oInDir = "c:\docs\"
   oInDirURL = ConvertToURL(oInDir)
   oDoc = ThisComponent
   oText = oDoc.getText()
   oTif = Dir(oInDir+"*.TIF") ' lists directory for any *.TIF files. next time dir is called it will be the next file
   rem oViewCursor = oDoc.getCurrentController().getViewCursor()
   oCursor = oText.createTextCursor()
   

   While oTif <> "" ' loop through all TIF files in the directory
      oTifURL = oInDirURL+oTif
 
   
   importTIF(oTifURL, oCursor) ' call the import function for each TIF
       
      oTif = Dir
   wend
       

End Sub


Sub importTIF(oTifURL as String, oCursor as Object)

   Dim oDoc as Object
   Dim oText as Object
   Dim oGraphicsObject as Object
   REM Dim oCursor as Object   

   oDoc = ThisComponent
   oText = oDoc.getText()
   
   
   oGraphicObject = oDoc.createInstance("com.sun.star.text.GraphicObject")
   oGraphicObject.GraphicURL = oTifURL
   TextContentAnchorType = AT_PARAGRAPH
   oText.insertTextContent(oCursor(), oGraphicObject, false)
   REM oCursor.collapseToEnd() 'I don't think this makes a difference...
   oText.insertControlCharacter(oCursor, PARAGRAPH_BREAK, false,)
   

End Sub


Hope it works for you.
JohnV
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Wed Apr 16, 2003 6:38 pm    Post subject: Reply with quote

>appears to get rid of the extra pages

Well, I take that back. What it does seems to depend on the page setup. I was using a page with 1 inch margins all around and an extra page seems to sneak in at every fifth. When I changed to no margins all around the extra page appears to be every third.

JohnV
Back to top
View user's profile Send private message
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