[Solved] Intercept onClose event of database doc? (in Basic)

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
DocCyber
Posts: 9
Joined: Sat Feb 15, 2014 4:02 pm
Location: Erkelenz, Germany

[Solved] Intercept onClose event of database doc? (in Basic)

Post by DocCyber »

I am already searching quite long for some solution and I am hoping to find help here.

I would like to intercept the event when a database document is being closed (no matter whether I haven chosen "File - Close" from the menu, or I clicked the red icon in the top right corner). In addition I want the same for the appendant form documents.
Apparently simple tasks. However, the "built-in" option to associate some code to the "onClose" event is useless because obviously the code is simply not triggered at all.

Any help / support / idea is most appreciated

Thanks a lot in advance.
Last edited by Hagar Delest on Sun Feb 16, 2014 7:01 pm, edited 3 times in total.
Reason: tagged solved
OpenOffice 4.0.1
Windows 7 / 64Bit
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to intercept onClose event of database document (Bas

Post by Villeroy »

DocCyber wrote:Apparently simple tasks.
Not at all. Nothing is simple with this hell of an API. But all components support interface http://www.openoffice.org/api/docs/comm ... aster.html
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
DocCyber
Posts: 9
Joined: Sat Feb 15, 2014 4:02 pm
Location: Erkelenz, Germany

Re: How to intercept onClose event of database document (Bas

Post by DocCyber »

Villeroy wrote:Not at all. Nothing is simple with this hell of an API.
Well, that's why I wrote "apparently". ;) Seems you didn't recognise my irony ... :)

Thank you for the link. I'm gonna check it out...
Last edited by DocCyber on Sat Feb 15, 2014 6:05 pm, edited 1 time in total.
OpenOffice 4.0.1
Windows 7 / 64Bit
DocCyber
Posts: 9
Joined: Sat Feb 15, 2014 4:02 pm
Location: Erkelenz, Germany

Re: How to intercept onClose event of database doc? (in Basi

Post by DocCyber »

Unfortunately this is Java? or whatever... Sooner or later I need learn this.
But now I can't cope with this language yet.
Perhaps you know another good example in Basic?
OpenOffice 4.0.1
Windows 7 / 64Bit
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: How to intercept onClose event of database doc? (in Basi

Post by rudolfo »

The link describes the interface XCloseBroadcaster. This is a generic interface it is not limited to any language: You can use it from Java, Python, Javascript or StarBasic. If you have ever programmed in Java or Javascript you have surely come across functions or methods like AddEventListener or RemvoeEventListener. In essence the concept behind that is called the observer programming pattern:

One unit or object tells another object that it wants to be informed if a certain event happens in that other object. Typically this needs 2 parameters: one to specify the type of the event and a second one to specify how the information notification should happen.

In case of the addCloseListener it is not necessary to specify the type of event with a parameter, because the name of the AddListener method already tells us that this method will only apply to Close events.
Specifying the information notification is traditionally done with a so called callback function pointer. Newer languages like C# use the phrase delegate for this concept. Now the problem with Basic is that doesn't really support function pointers or delegates. Hence you will have a tough time to use Event listeners with Basic. There might be some work around to mimic function pointers in Basic, but I am not aware of them.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to intercept onClose event of database doc? (in Basi

Post by Villeroy »

This is not Java or any other programming language. It is the API documentation. It describes all properties and interfaces (bundles of methods) of all availlable objects that can be accessed from any of the supported languages. Formally it looks like Java or Cpp documentation but it applies to all supported programming languages.
You can attach certain listeners to certain objects at run time. This is beyond the so called "script events" that are declared in the document or in the office config.
Sorry, but why do you need this esotheric stuff anyway? I doubt that anybody will ever produce a professional database application with Base and Basic. I have never seen any such application in 10 years.
You have some professional database engine with a driver (MySQL, PostgreSQL, whatever) or just a collection of tabular data files. Now you want to access that database to generate printable reports, serial letters, label prints, feed some spreadsheet models with database data. This is where Base comes into play. It provides the connection and some meaningful queries to dump database data into office documents and templates.
If you connect Base to a professional database engine you may even write data from the office documents back into the database by means of input forms which are always attached to office documents. The input forms are very, very limited but they are able to reflect the most common field types and the relations of a relational database. If you are very, very proficient with this simple tool set ("simple" being the opposite of "easy"), then you may even spice up your input forms with a little bit of scripting to make the work flow more convenient.
Scripting a fool-proof GUI for inventory control systems is way beyond Base. It is the most underdeveloped component of this office suite.
The API is only a very thin layer between your programming language and the office suite. It is really complex with no syntactical sugar and not too many nice wrappers. The shipped StarBasic lingo is a tiny little helper for simple API calls.
The "embedded HSQLDB" is the biggest mistake ever. You must not think that this is a self-contained "Base database". It is just a cheap trick to store a http://hsqldb.org within a zip archive. Sooner or later the existance of your embedded HSQL database will rely on your backup strategy.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to intercept onClose event of database doc? (in Basi

Post by Villeroy »

rudolfo wrote:Specifying the information notification is traditionally done with a so called callback function pointer. Newer languages like C# use the phrase delegate for this concept. Now the problem with Basic is that doesn't really support function pointers or delegates. Hence you will have a tough time to use Event listeners with Basic. There might be some work around to mimic function pointers in Basic, but I am not aware of them.
Basic has a helper function createUnoListener as work-around. It takes some prefix string and the name of the interface.

Code: Select all

Global myCloseListener

Sub addMyListener(obj)
If not isObject(myCloseListener) then 
  myCloseListener = createUnoListener("myown_", "com.sun.star.util.XCloseListener")
endif
obj.addCloseListener(myCloseListener)
End Sub

Sub removeMyListener(obj)
REM myCloseListener has to be the very same instance that had been attached before
REM therefore it requires a global var
obj.removeCloseListener(myCloseListener)
End Sub
With myCloseListener attached to a given object, the object will call Basic the routines named with the prefix "myown_".
We've got to implement all methods of http://www.openoffice.org/api/docs/comm ... tener.html including the inherited http://www.openoffice.org/api/docs/comm ... tener.html

Code: Select all

Sub myown_Closing(ev, bGetsOwnership)
MsgBox "Hello! this is com/sun/star/util/XCloseListener.myown_Closing"
objCaller = ev.Source
End Sub

Sub myown_notifyClosing(ev)
MsgBox "Hello! this is com/sun/star/util/XCloseListener.myown_notifyClosing"
objCaller = ev.Source
End Sub

Sub myown_disposing(ev)
MsgBox "Hello! this is com/sun/star/lang/XEventListener.myown_disposing"
objCaller = ev.Source
End Sub
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
DocCyber
Posts: 9
Joined: Sat Feb 15, 2014 4:02 pm
Location: Erkelenz, Germany

Re: How to intercept onClose event of database doc? (in Basi

Post by DocCyber »

rudolfo wrote:If you have ever programmed in Java or Javascript
Exactly this is the problem. I never used Java or JavaScript. (Only Fortran, Pascal, Basic and some PHP)
But I realise it's time to begin with that ...

Thank you, Rudolfo, for your explanations!
OpenOffice 4.0.1
Windows 7 / 64Bit
DocCyber
Posts: 9
Joined: Sat Feb 15, 2014 4:02 pm
Location: Erkelenz, Germany

Re: How to intercept onClose event of database doc? (in Basi

Post by DocCyber »

Villeroy wrote:Formally it looks like Java or Cpp documentation
Yes, I assumed this.
You can attach certain listeners to certain objects at run time.
I read about that already but I wasn't sure how to implement this for my problem. That's why I am here.
This is beyond the so called "script events" that are declared in the document or in the office config.
This is for me an important statement!
It's confusing if one can enter something during development which is totally ignored at run-time.
Sorry, but why do you need this esoteric stuff anyway? I doubt that anybody will ever produce a professional database application with Base and Basic.
Of course nobody would run a professional DB under Basic!
I used to create several smaller apps under Excel and wanted to see how it works under OOo. Generally.
I am curious. That's all. Why "database"? Just by chance.

But it doesn't matter because my "problems" are -at this stage- fundamental.
Therefore I am glad that I am slowly beginning to understand - thanks to you guys!!
From my point of view the philophies of the different models (COM vs UNO) are quite different.

Again - thanks.

I'll read and understand your examples.
OpenOffice 4.0.1
Windows 7 / 64Bit
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: How to intercept onClose event of database doc? (in Basi

Post by rudolfo »

When Villeroy mentioned CreateUnoListener I realized that I posted against my own standards. Very often when browsing through the forum and looking at the titles of some threads I start to wonder about the attitude of those that ask the question. Obiously in our days it is easier and more convenient to post to a forum or quickly googling for something then to open the builtin help system of an application and start to search there for the problem or how to do or achieve something. This builtin help system comes from the same source that has also written the software and has gone through a similar kind of release cycle as the application software itself. In other words a high level of quality is guaranteed in the builtin help.

But that's rather a side note. My main point is that there is quite a lot of description about Basic's CreateUnoListener function in the builtin Help system. I don't say, that I fully understood what they try to say. So compared to the documentation for other Basic statements or function you immediately understand that this is not for beginners. But I think with the background from the builtin help and our explanations in this thread and of course also the sample code given by Villeroy you should be ready to go.

One last remark: Java and Javascript only sound like they are similar, but they are quite different. You might as well choose Python, which is somewhere in between these two languages, although it doesn't use the C-Style syntax with semicolons as statement terminators and braces { and } as block delimiters.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
DocCyber
Posts: 9
Joined: Sat Feb 15, 2014 4:02 pm
Location: Erkelenz, Germany

Re: How to intercept onClose event of database doc? (in Basi

Post by DocCyber »

Hi Villeroy,

your code works fine! It is exactly what I was searching for!! :bravo:
Thank you very much indeed.

However, I can't yet understand the purpose of the code segment below.
In my case this sub is NOT being called.
Villeroy wrote:

Code: Select all

Sub myown_Closing(ev, bGetsOwnership)
MsgBox "Hello! this is com/sun/star/util/XCloseListener.myown_Closing"
objCaller = ev.Source
End Sub
OpenOffice 4.0.1
Windows 7 / 64Bit
DocCyber
Posts: 9
Joined: Sat Feb 15, 2014 4:02 pm
Location: Erkelenz, Germany

Re: How to intercept onClose event of database doc? (in Basi

Post by DocCyber »

rudolfo wrote:I start to wonder about the attitude of those that ask the question. Obiously in our days it is easier and more convenient to post to a forum or quickly googling for something then to open the builtin help system of an application and start to search there for the problem or how to do or achieve something.
Basically you are right and I fully agree. I also used to try and search long before I post something and ask for support. Simply because the learning effect is much bigger. However, in this case I had a fundamental understanding problem. It's always hard to find something if one doesn't know what to search for.
rudolfo wrote:...with the background from the builtin help and our explanations in this thread and of course also the sample code given by Villeroy you should be ready to go.
Absolutely true. The code is already implemented and works great.
This was the kind of help / support I was looking for.
Again - thank you very much. This applies for Villeroy, too.

(Wondering why we're discussing this in English and not in German. ;) )
OpenOffice 4.0.1
Windows 7 / 64Bit
DocCyber
Posts: 9
Joined: Sat Feb 15, 2014 4:02 pm
Location: Erkelenz, Germany

Re: How to intercept onClose event of database doc? (in Basi

Post by DocCyber »

However, I can't yet understand the purpose of the code segment below.
In my case this sub is NOT being called.
Got it. :)
No reply required.

I consider this topic solved!
OpenOffice 4.0.1
Windows 7 / 64Bit
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: How to intercept onClose event of database doc? (in Basi

Post by rudolfo »

My comments about the habits of some folks to rather throw questions into a forum then to search for a solution on their own was not targeting this thread. I guess we all know this, but sometimes it is good to make sure that you are not missunderstood.

And I see your point about not knowing where to start or what to search for. You are right, after the solution is found and looking back on the process you will sometimes ask yourself: "Yes, the answer was really nearby. I wonder why I didn't find it!" But that's the classic you always know better after the fact.
As an experienced forum user I guess I have a kind of internal synonym table that rings the right bells and whistles: If I read "doing something at closing time" I immediately think "AddListener" which is the phrase that gives you the right answers when searching for it. Or "Serien-Brief" in German lets me think of "mail merge" now, but not in my first 4 or 5 months with OpenOffice.

"Serienbrief" brings me to your question why the conversation of 3 native speaking German people was not held in German language. The answer is very straight forward: This is a public forum in English language. To ensure that all forum users can read and understand the topics all threads are in English. There are also national language forums under the same domain: https://forum.openoffice.org/
There is also a German forum which is maintained independently from this one. I think in the past some attempts where made have the German forum added to the other language forums on the above mentioned landing page. It didn't work out. No severe conflicts or anything like that. It just didn't happen.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
DocCyber
Posts: 9
Joined: Sat Feb 15, 2014 4:02 pm
Location: Erkelenz, Germany

Re: How to intercept onClose event of database doc? (in Basi

Post by DocCyber »

rudolfo wrote: ... why the conversation of 3 native speaking German people was not held in German language. The answer is very straight forward: This is a public forum in English language. To ensure that all forum users can read and understand the topics all threads are in English
. Of course I am fully aware of this (you've seen my smiley?) It's a good way to support to community idea. Otherwise I would have switched to German right away.

btw: I had posted the same question about 2 weeks ago in the German forum but I didn't even get one single answer.
Am glad that I found it here.
OpenOffice 4.0.1
Windows 7 / 64Bit
Post Reply