[Solved] How to deselect a picture?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Hagar Delest
Moderator
Posts: 32804
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

[Solved] How to deselect a picture?

Post 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.
Last edited by Hagar Delest on Wed Nov 09, 2022 6:44 pm, edited 1 time in total.
LibreOffice 24.2 on Xubuntu 24.04 and 7.6.4.1 portable on Windows 10
FJCC
Moderator
Posts: 9428
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to deselect a picture?

Post 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.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Hagar Delest
Moderator
Posts: 32804
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: How to deselect a picture?

Post 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.
LibreOffice 24.2 on Xubuntu 24.04 and 7.6.4.1 portable on Windows 10
JeJe
Volunteer
Posts: 2906
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] How to deselect a picture?

Post 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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2906
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] How to deselect a picture?

Post 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.)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Hagar Delest
Moderator
Posts: 32804
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: [Solved] How to deselect a picture?

Post 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.
LibreOffice 24.2 on Xubuntu 24.04 and 7.6.4.1 portable on Windows 10
Post Reply