Make a form that allows user to drag and drop in an image

Hello

Thanks to the great instructions
http://sheepdogguides.com/fdb/fdb1imagbp.htm here , I learned how to link a
VARCHAR field to images in my filesystem, and display them in a form.

However, when I add new entries, I have to manually type in the relative
path of an image into that field. This is cumbersome when I need to add lots
of entries, e.g. making a catalouge of things with images for each.

Therefore, is it possible to create a form so that when adding a new entry,
there is a "box" in the form where the user can just drag and drop in the
desired image, and the appropriate relative path to the image will be added
to the field???

Thank you very much!

Sorry, I forgot to specify that I was referring to LibreOffice Base in my
original post!

No. How would the form know where the image had been located? When
pictures are linked in a Writer document, you have to browse to the
picture's location before inserting it in the document. It is the
browsing that tells Writer the proper link for it. Base does not have
this ability (to browse to different other locations).

--Dan

Hi Dan,

Wow - what timing - I was literally fiddling with an idea on this when
your email hit.

First- sure Base can browse to a file, just use the File Picker control.

Second - so far nothing I've tried will give you a drag/drop way to yank
the URL from an image driopped onto a form.

But as I say the file picker works just fine for this.

Avamk, have you used the file picker control and then if you need
relative paths, that would be easily done also.

//drew

I am actually quite new to Base, and didn't realise I could use the File
Picker control. When I try to change its reaction to the Mouse Pressed
event, it asked me for a macro.

Do I need to write a macro to get the relative path of my desired image??? I
actually have no macro coding skills to do that...

And how do I tie the selected file's relative path to the field I desire?

Sorry I am so new to this, but I would really appreciate any guidance you
can give me...

I am actually quite new to Base, and didn't realise I could use the File
Picker control. When I try to change its reaction to the Mouse Pressed
event, it asked me for a macro.

Do I need to write a macro to get the relative path of my desired image??? I
actually have no macro coding skills to do that...

Right you are, the control is not 'data aware', not bound to a data
column, so you will need to use a macro to transfer the file path
returned from browsing to the data column. In the default installation
of LibreOffice is a basic macro library, Tools, which contains a number
of file path manipulation macros, these should make conversion to a
relative path (note here - relative to current directory of the ODB
file, yes?)

Anyway, I don't have an example snippet of basic on the disk at the
moment, pretty sure there are some examples at different forums already
- but I would really like to see this as an answer at
ask.libreoffice.org - so if you don't mind I'll put an example there.
It's kind of late tonight, but maybe before I turn in. You won't need to
have a logon account there to retrieve it.

The actual move to a data column is all of 2-3 lines, I don't think the
conversion to relative path should be too much more.. :slight_smile: I don't
think..

What I'll do then is post back here, with a link to the entry there..or
if feel like check out the site and open the question and send that here
- then I'll post to that entry - either way is good.

Best wishes,

//drew

I am actually quite new to Base, and didn't realise I could use the File
Picker control. When I try to change its reaction to the Mouse Pressed
event, it asked me for a macro.

Do I need to write a macro to get the relative path of my desired image??? I
actually have no macro coding skills to do that...

And how do I tie the selected file's relative path to the field I desire?

Sorry I am so new to this, but I would really appreciate any guidance you
can give me...

Well, I feel stupid - you already asked on ask.libreoffice didn't you..
http://ask.libreoffice.org/question/1715/make-a-form-that-allows-drag-and-drop-of-images-in
... the other day.

OK- well, I haven't added the part to generate the relative path, but
for the basics of syncing the control with a data column I did put
together a little database.

It's only a few lines of basic, but if you only have a starting
knowledge of the basic scripting in the package it likely makes sense to
let you have this first.

To use the odb (embedded HSQL) database you will need to do two things
interactively first.

1 - you must enable macro execution. I really suggest going for the
highest setting, which requires you to designate what directories files
with macros will be allowed to execute from - I more highly suggest you
do not use your default download or document directory.. for myself I
create a sub-directory under my Documents directory for such.
Anyway - to do this open the Otpions dialog (File>Options), select
Security and then Macro Security. The information on the dialog should
be reasonably clear to follow.

2 - you must load the 'Tools' basic library before it can used. When
macros are embedded in and ODB, as they are in this example file this
must be done outside of the file. (don't ask me why, I think its a bug)

Now - there is a nice way to handle that. The example odb file has the
document level event 'Open Oocument' set to call the Main procedure in
the LibreOffice standard basic library. Every install of LibreOffice has
this User specific standard library - by default, empty save a stubbed
out Main procedure.

So - what you need to do is:
Start Libreoffice
Select Tools>Macros>Organize Dialogs
On the dialog select the 'Modules' tab.
You will see in the list of libraries the category:
My Macros
Under this - Standard
If you double click this, a single module - Module 1
Select that and click the Edit button.

Now you have the Basic IDE open.
There is a Main Procedure, with no actual basic commands.

Add this line between the opening
"Sub Main" and "End Sub" Lines
    BasicLibraries.LoadLibrary("Tools")

Save the library and close the IDE you are done with this.

Now - if you have authorized Macro execution at some level you can load
the example file 'linkme.odb', you will find a single table and form.

The form uses a hidden text control and two macros and two events to
transfer the URL selected from the file picker (File Selector) control
into two data columns in the table. One of the macros also uses the link
data column in the table to display the referenced image in an Image
Control on the form.

For kicks I've preloaded 3 records in the table - the first two will
display a graphic from the internet and the third will display no image,
as it has a file path (URL) to an image on my local disk.

So - you can find the example file here for the moment -
https://wiki.documentfoundation.org/File:Linkme.odb

After I finish up the PATH massaging will move that over to the ask LibO
site.

Also - I need to do a few things here this Saturday morning, but I'll
get back to it later this afernoon - if you check out the macros you
will likely say HUH - but I'll also explain the constructs when I get
back to this.

//drew

Yes, I already asked the question earlier this week. :slight_smile:

I am following your steps now, and eagerly await your relative paths
followup!

Thank you so much, this has been extremely helpful so far.

I just wish there is an official manual for LibreOffice Base, so far I have
only been able to find the Getting Started guide...

Hi :slight_smile:
I think it's being worked on.  There is a rough drafts of Chapter 2 here
https://wiki.documentfoundation.org/Documentation/Publications#LibreOffice_Base_Guide
Regards from
Tom :slight_smile:

Thanks for the link! Looks like the Base Guide is still in its very early
stages of development, are there comparable guides/books out that that I can
start studying now??

Also, I've downloaded the example database and have been able to add new
images to it, and am trying to play around with it now. If you could provide
any tips on making the form enter relative paths (relative to the database
file) that would be great!!

Thank you so much.

Hi :slight_smile:
A google search gave me these for OpenOffice.org's Base which is almost exactly the same
http://wiki.services.openoffice.org/wiki/Database
http://www.oooforum.org/forum/viewtopic.phtml?t=82025
http://sheepdogguides.com/fdb/fdb1main.htm

Wikipedia often has good links for this sort of ting
http://en.wikipedia.org/wiki/OpenOffice.org_Base

Regards from
Tom :slight_smile:

Thanks for the link! Looks like the Base Guide is still in its very early
stages of development, are there comparable guides/books out that that I can
start studying now??

Also, I've downloaded the example database and have been able to add new
images to it, and am trying to play around with it now.

Hi Avamk,

Wonderful, I was hoping you had done so.

Didn't make it back to this yesterday, having a quite coffee after the
Easter bustle here now and a good time to do so.

I noted in the first mail that I'll just stub in the code to handle
relative path from the perspective of the directory the ORB is stored
in, meaning that I'm flaking out on you :wink: and only handling sub-dirs as
relative.

Everything you need to handle more is of course there, it would just
take a bit more time - also, basic isn't the only scripting tool
available with LibreOffice, you could use python, javascript, rexx even.

Anyhow, I digress - off to add another half dozen lines of basic and
I'll update the example file.

Happy Easter and Shalom,

//drew

hi Tom,

Thanks for that - I hadn't looked over the stuff at sheedogguides in a
while, that's gotten better over time.

I noticed an example db's about birds, wouldn't be surprised if that
handles images, did you by chance check to see if it, or any of the
others there do what Avamk is looking for?

Also - I'd be nice to have a link to the sheepdog site somewhere on the
wiki perhaps, what do you think?

Best wishes,

//drew

<snip>

Howdy avamk,

Alright updated the file and added it as new file, (different URL on the
wiki) this is a zip file now.
https://wiki.documentfoundation.org/File:Linkme.zip

It will extract to a sub-dir named, linkme whith:
linkme.odb
lo-client-libo-wallpaper.jpg
images/basicorganizer.png

In the database the first two records should display a graphic from the
remote server, as before. The next two will display using the relative
path from the perspective of the odb file.

You can use the file picker (which I wish would just start in the last
dir..arrgh) to select files from anywhere on the disk, if they are in
the directory of the odb or a sub-dir then a relative path, where the
base file directory is <root>

Off to a supper, I'll come back and try to write a decent explanation of
the basic lines, tonight.

//drew

Hi :slight_smile:
I haven't had a look to see if it would help Avamk's issue about showing images in forms.  It's not something i have tried doing before and it looked like the question was going well.  Avamk, have you had a look through?

Sure, add the link if it's a good one.  I wasn't involved with OpenOffice.org and it's the first time i have seen those guides.  The wiki's are for anyone and everyone to update so anyone could add the link if they think it might help people. 
Regards from
Tom :slight_smile:

Wonderful!!

I will download the file and study it further. Thank you again for your
continuous updates. I already know so much more than a few days ago.

Yes, I just looked at sheepdogs explanation, it is so well written! Even
though it uses relative paths, they need to be typed in manually. So the
File Picker control still needs to be used.

Yes, I just looked at sheepdogs explanation, it is so well written! Even
though it uses relative paths, they need to be typed in manually. So the
File Picker control still needs to be used.

Well, if the example there is working then it is likely a cut above what
I have here - cause, I added some eye candy - had to, and then tried
this file under MS Windows, to a dis-satisfactory outcome.

But if you are using Linux, all seems to work well - though I could
likely clean it up still.

So - why is it failing under Windows, and how to fix that today (over
the next hour or so) is the call.

BTW, I'm guessing it runs as written on a Mac also, but do not have one
to try that on.

anyway - I updated the odb file on the wiki (not the zip file, if you
grabbed that great, just download the latest odb into your working
direcotry)

Added a couple things for kicks, as I say.

Made it pretty.

Added runtime controls to adjust some properties on the image control:
- Display Scaling (none, stretched keep ratio, stretched fill)
- Adjust background color (Black, White and Transparent [to come])

Added a couple of fields in the table, comments and one for storing a
custom property (just a string) for the selected display settings.
Hooking that up to single column vs data aware controls to two columns
just cause, already handling our own record change event so why not.

AGAIN - if you are running windows this file, the basic script, doesn't
work.. will update this again later today with a correction on that
hopefully.

but to check it out under Linux (Mac maybe) the updated file is at
https://wiki.documentfoundation.org/File:Linkme.odb

back in a bit,

//drew

Howdy Avamk, Tom

alright well, got a little ugly in that I should create a constant or
two, but the script works on both Windows and Linux now and will on Mac
I'm sure (he says with a grin)..

https://wiki.documentfoundation.org/File:Linkme.odb

was updated just a minute or two ago - Haven't finished with the save
for the display settings to the database field, should get that tonight.

Anyway - I'm glad I versioned this, as it might be a good example for
working with URL's vs OS specific paths also - which btw was my mistake,
I started out deciding to save URLs, meaning I used the converttoURL
function here and there, but then turned around an used the function
getPathSeparator also, well, the getPathSeparator returns the system (or
OS) character, which on Linux is the same as what is used in a URL, but
not so on windows..once I stopped trying to have it both ways in the
basic code :slight_smile: it all worked..surprise.

anyway - let me know if any of this makes sense, and if you it mostly
does, what is the best way to help reinforce it - I will try to write a
decent explanation, but always need help with that, I'm no word smith.

//drew

Hi Drew,

It looks amazing now, and the functionality is great. I am especially glad
that it will work in Linux, Mac, and Windows!

Were you going to write an explanation for the code, or just the form?
Either way, if you can start a draft, I can help word smith it (probably by
asking you questions about it).

I think this is so useful, perhaps it can be integrated into the standard
LibreOffice Base macro set???

Thanks!