Double click to open new window

I have a standard LibreOffice database form that displays a photo of an event/person whose path is indicated in the table. So far so good, however, since the form space is uniform and not very large, I would like to be able to double-click on the image to open the file in a separate window in its original size format. Is this possible? And if so, can you help me? Thank you.

  1. Download and install
    URLButton.py.odt (25.0 KB) No need to modify nor understand anything. Use this macro as is.
  2. Modify the source of your (sub-)form, so it includes the absolute full picture path. If the form was bound to some table “PICS” with a text field “FILE” holding the file names, switch the source to query
    SELECT "PICS".*, 'C:\full\path\' || "FILE" AS "PATH" which returns the entire table plus an extra column with the concatenated full path.
  3. Add a push button to the same (sub-)form.
    3.1. Assign the button’s approve event to MyMacros>pyDBA>FileButton_Approve
    3.2. Add the field name with the full paths to the button’s additional info, PATH in this scenario.

Now, the push button opens the picture file in the default file viewer application of your operating system.

Don’t know if it works on Windows or macOS :thinking:
Event = Image Controls > Mouse button pressed
CtrlClick on the image:

Option Explicit
Sub ViewImage(oEvent As Object)
	Dim oDoc As Object, oShell As Object
	Dim stUrl As String, stField As String
	Dim arUrl_Start()
	On Error GoTo Erro
	If oEvent.Modifiers = 2 Then
		oDoc = ThisComponent
		stField = oEvent.Source.Model.BoundField.getString()
		oShell = createUnoService("com.sun.star.system.SystemShellExecute")
		arUrl_Start = split(oDoc.Parent.Url, oDoc.Parent.Title)
		stUrl = convertToUrl(arUrl_Start(0) + stField)
		oShell.execute(stUrl,"", 0)
	End If	
	Exit Sub
	Erro: MsgBox "ERRO " & Err & Chr(10) & "na linha " & Erl 	
End Sub

ImagesRecord

1 Like

Muito obrigado Carlos. Funciona perfeitamente “Ctrl” + 1 Click sobre a foto. Era exactamente o que eu pretendia. Mas, infelizmente, eu olho para o código da macro e fico com a perfeita noção de que nunca a poderia ter escrito sozinho. Obrigado.
Ops…
Thank you very much, Carlos. It works perfectly: “Ctrl” + 1 Click on the photo. It was exactly what I wanted. But, unfortunately, I look at the macro code and I’m perfectly aware that I could never have written it myself. Thank you.


However, an error occurs when opening the file if the name has an accent mark. For example: “José Coronado.jpg”, “Virgílio Castelo.jpg”, “Zöe Saldaña.jpg”, “Totó.jpg”, “Milène Demongeot.jpg”, etc…
Which means I will have to change the name of all files with accent marks. Which won’t be a big deal!

Dear Villeroy, thank you very much for your reply and for the time you dedicated to it. I tried the macro sent by CRDF and it worked perfectly, so I didn’t try yours, which seemed more complicated, although I don’t take away any credit from you. Thank you very much.

You can try this code as a function (build a function with a string parameter returning a string) ON ANOTHER FORM.
Ref: Google AI

Option Explicit
Sub EscaparAcentos
    Dim oFunctionAccess As Object
    Dim original As String, convertido As String
    REM Set the original url as a string:
    original = "file:///home/Quaresma/EstudosDBs/BancoImagens/Zöe Saldaña.jpg"
    On Error GoTo Erro
    REM Create the FunctionAccess service
    oFunctionAccess = CreateUnoService("com.sun.star.sheet.FunctionAccess")
    REM Call the Calc function using its English name - parameter must be as an array!
    convertido = oFunctionAccess.callFunction("ENCODEURL", Array(original))
    print convertido
    Exit Sub
    Erro: MsgBox "ERRO " & Err & Chr(10) & "na linha " & Erl
End Sub

 

stUrl = convertToUrl(arUrl_Start(0) + stField)
stUrl = FuncaoEscaparAcentos(stUrl)
oShell.execute(stUrl,"", 0)

I’m unable to check it becase don’t have non ASCII or blanks in addresses.
EscaparURL

The problem is in these lines:

arUrl_Start = split(oDoc.Parent.Url, oDoc.Parent.Title)
stUrl = convertToUrl(arUrl_Start(0) + stField)

Replace description:

Dim arUrl_Start()

to

Dim arUrl

and use this version of the fragment:

arUrl = Split(ConvertFromUrl(oDoc.Parent.Url), GetPathSeparator())
arUrl(UBound(arUrl)) = stField
stUrl = ConvertToUrl(Join(arUrl, GetPathSeparator()))

Additional bonus: the document’s Title property, which can be changed by the user or a macro, is not used.

Changed the code to yours :+1:
Of course I’m unable to check the accent marks + blanks as don’t have them.
 
NewViewImage

Hello Carlos. I tried modifying the previous code, changing the line:
Dim arUrl_Start()
to
Dim arUrl

and the lines

arUrl_Start = split(oDoc.Parent.Url, oDoc.Parent.Title)
stUrl = convertToUrl(arUrl_Start(0) + stField)
to
arUrl = Split(ConvertFromUrl(oDoc.Parent.Url), GetPathSeparator())
arUrl(UBound(arUrl)) = stField
stUrl = ConvertToUrl(Join(arUrl, GetPathSeparator()))

And it resulted in an ERROR, whether the filename had accents or not.
So, I’m sticking with the previous code. I must be doing something wrong, for sure. I’ll just have to waste a lot of time removing the accents from the names of actors and films with accents. The spaces between the names were never a problem, only the accents. Thank you for trying.

:thinking: @sokol92’s code works fine here to me.
 
Please format code com 3 acentos graves:

```
the code
```

I´m sorry Carlos, where exactly should I put the three grave accents in the code? Before and after what? Thank you, and excuse my ignorance.! :cry:

The code, like

```
arUrl = Split(ConvertFromUrl(oDoc.Parent.Url), GetPathSeparator())
arUrl(UBound(arUrl)) = stField
stUrl = ConvertToUrl(Join(arUrl, GetPathSeparator()))
```

So the code gets formatted like code:

arUrl = Split(ConvertFromUrl(oDoc.Parent.Url), GetPathSeparator())
arUrl(UBound(arUrl)) = stField
stUrl = ConvertToUrl(Join(arUrl, GetPathSeparator()))

It’s funny, it keeps giving an error, now of a different nature. Could you tell me where I can read more about this to better understand this type of encoding preceded by 3 backticks? Thank you.

Misunderstanding :grimacing:
Backticks here in the messages to renderize :ok: the code text. Not in the macro itself.
 
I’m not with the same machine now, but will try to build an example using @sokol92’s code.
 
EDIT
It didn’t work to me here. Named a photo as

öe ña.jpg

and
NonASCII_name

Hello!
Sorry, I didn’t notice how exactly stURL was used.
In this case, the filename is expected in the operating system format.
So try this:

stUrl = Join(arUrl, GetPathSeparator())

I just tested this part of the code for Windows – no error is thrown.

1 Like

:clap: :clap: :clap:

1 Like

Yes, as I can see a slash behind BancoImagens, while backslash is used for the first part. I guess mixing separators in one path is no good idea.
.
But there seems also to be active url-encoding for the filename.
.
Can’t test today, but I think for ShellExecute we need a regular path for the OS and may even need to use the same encoding as the shell itself. Utf8 can not be assumed valid for Windows.
I’m astonished of the example with space in name, aa I had expected this to need quotes.
.
On the other hand: An url-encoded name should work for a hyperlink to a file://-resource

On Linux Mint 21.3 Cinnamon now
Made a copy of my photo “Adrar.jpg” as “öe ña.jpg” and “my” original code is working :ok: both in

  1. form with Image Control: Ctrlclick on the image;
  2. form (Table2) with TextBox for the Path string: Ctrlclick on the TextBox.
    :thinking:
    FotoRenomeada

    Table1

Could you upload a test file (*.odb)?

  1. There are 2 kinds of forms:
    Imagens with an Image Control — as per OP. This is not working for non ASCII.
    Source = table IMAGENS.
    Paths only a TextBox with the path to image. This runs :ok:
    Source = table PATHS.
  2. Of course place the images in subfolder.
  3. Please take notice that for the Image Control Base % encodes non ASCII paths. I imagine :thinking: this is the issue on Windows.
    On Linux all works :ok: here.
    Of course I’m doing this in a W-11 machine now.
     
    Fotos.odb (19.6 KB)
1 Like