Page 1 of 1

[Solved] How to deselect a picture?

Posted: Wed Nov 09, 2022 4:59 pm
by Hagar Delest
Dear All,

I'm working on that macro: Set and retrieve last cursor position (with a bookmark).

The issue is when a picture is selected.
I've slightly changed the code and switched to a ViewCursor (oVC) and get the position with collapseToEnd. As a dirty workaround, if the selection is a picture, then I just leave it as it is (meaning: I don't insert the bookmark where the pic is):

Code: Select all

if oDoc.CurrentController.Selection.supportsService("com.sun.star.text.TextGraphicObject") then exit sub
insPos     =oVC.collapseToEnd
If I remove the if condition, then I get an error (Message: no text selection.).
My question is: if the current selection is a picture, how can I deselect it so that I fall back on the text view cursor?

I've done some extensive search and not found relevant code. I admit I'm a poor coder and merely adapt what I find, thus, there may be some trivial trick or command in the API that I don't know.

Re: How to deselect a picture?

Posted: Wed Nov 09, 2022 6:02 pm
by FJCC
Here is an untested guess:

Code: Select all

if oDoc.CurrentController.Selection.supportsService("com.sun.star.text.TextGraphicObject") then 
  oAnchor = oDoc.CurrentController.Selection.Anchor
  oDoc.CurrentController.select(oAnchor)
End If
The idea is to make the selection a TextRange. I don't know how that will work for different settings of Anchor for the picture, especially Anchor To Page.

Re: How to deselect a picture?

Posted: Wed Nov 09, 2022 6:44 pm
by Hagar Delest
Excellent, that did it! Thanks a lot!
I've updated the code (the other topic).

Doesn't work if it's anchored to page but no big deal (for me), I've kept the error handler for that.

Re: [Solved] How to deselect a picture?

Posted: Wed Nov 09, 2022 7:11 pm
by JeJe
Hagar Delest - your link results in a security warning for me

I think you need to post

https://forum.openoffice.org/en/forum/v ... p?p=529522

Edit: and same problem with the link in the other thread.

Re: [Solved] How to deselect a picture?

Posted: Wed Nov 09, 2022 7:21 pm
by JeJe
if anchored to the page you could try jumping the cursor to that page.

Code: Select all

odoc = thiscomponent
	if oDoc.CurrentController.Selection.supportsService("com.sun.star.text.TextGraphicObject") then
		if oDoc.CurrentController.Selection.AnchorType =com.sun.star.text.TextContentAnchorType.AT_PAGE  then

			oDoc.CurrentController.viewcursor.jumptopage ( oDoc.CurrentController.Selection.AnchorPageNo ,false)

		else

			oAnchor = oDoc.CurrentController.Selection.Anchor
			oDoc.CurrentController.select(oAnchor)
		end if
	End If
Edit: The picture isn't necessarily visible on the page its anchored to - it could be anchored on page 1 and visible on page 100 - but I'd guess few people would be doing that...

Edit2: correction: it looks like with a picture it does have to be visible on the same page anchored to. (With a form control listbox it doesn't - which I've found useful.)

Re: [Solved] How to deselect a picture?

Posted: Wed Nov 09, 2022 10:07 pm
by Hagar Delest
JeJe wrote: Wed Nov 09, 2022 7:11 pm Hagar Delest - your link results in a security warning for me

I think you need to post

https://forum.openoffice.org/en/forum/v ... p?p=529522

Edit: and same problem with the link in the other thread.
Yes, I know, I reported that on the dev mailing list twice already: https://www.mail-archive.com/dev@openof ... 43589.html
With Brave and Firefox, it can be overridden. But I may switch to the full url indeed.

Thanks a lot for your code, it works great indeed! I've updated the macro.