Assign Macro to Button without using the GUI

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
zzyzcx
Posts: 13
Joined: Wed Dec 09, 2009 9:12 pm

Assign Macro to Button without using the GUI

Post by zzyzcx »

Is there a way to assign an event to a button in a dialog without using the GUI? Example: I have a Button called "Add". When I press this button I want the program to call a macro and pass some parameters to it. For reasons of flexibility and curiosity, I want to program the event directly into the macro instead of using the GUI to assign a macro to the event.

I've looked in the Basic Guide, and the Developer's guide, and neither offers instructions on how to do this - they all say "use the GUI". What's the big secret?
OpenOffice 3.2 on Mac OS X 10.5
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Assign Macro to Button without using the GUI

Post by hanya »

I wonder why you do not use conditional branch for flexiblity reason. There is a way to add listener to controls of the dialog.

Code: Select all

Sub DialogButtonAction
  oDialog = CreateUnoDialog(DialogLibraries.Standard.Dialog4)
  oButton = oDialog.getControl("CommandButton1")
  oButton.ActionCommand = "Something"
  ' create new listener with "Button_" prefix
  oListener = CreateUnoListener("Button_", "com.sun.star.awt.XActionListener")
  oButton.addActionListener(oListener)
  oDialog.execute()
  oButton.removeActionListener(oListener)
  oDialog.dispose()
End Sub

Sub Button_actionPerformed( oEv As com.sun.star.awt.ActionEvent )
  msgbox oEv.ActionCommand
End Sub
Sub Button_disposing( oEv As com.sun.star.lang.EventObject)
End Sub
There is no difference between to do it by the UI and by the code above, because the specific interface is created and assgined internally for the event.

They are described in the document:
http://wiki.services.openoffice.org/wik ... at_Runtime
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
zzyzcx
Posts: 13
Joined: Wed Dec 09, 2009 9:12 pm

Re: Assign Macro to Button without using the GUI

Post by zzyzcx »

Thank you for posting this code example. I am somewhat new to programming macros and I don't fully understand the role that EventListeners play in assigning events to controls, but after analyzing your code I think I see how it works. I'll play around a bit and see.

You ask why I want to do it this way - I am an attorney and I have created a database that stores the citations for the cases that I use in my legal research. I have a generic dialog box that has the same fields but can be used for different purposes depending on the macro that opens the dialog. For example, if I am adding a case that hasn't yet been entered into the database, I open the dialog and pass it the parameter "Add"; if I am searching the database I open the dialog and pass in the parameter "Search". When I pass in the parameter "Add", the dialog opens and the button at the bottom is labeled "Add Case", and when I press the button it calls the AddCase macro. When I pass it the "Search" parameter, the same button is labeled "Search", and I want it to call a different macro, called SearchCase. So I want the same button to call different macros depending on the parameter that is passed into the dialog. Using the GUI, I didn't see how to assign different macros to the same button to be activated conditionally depending on a variable that was passed to the dialog. Maybe there's an easier way to do what I am trying to do, but it seemed like the easiest way would be to program it directly into the macro - only the documentation on how to do that is a little skimpy.

So, that's a long answer, but I appreciate your help and your code snippet.
OpenOffice 3.2 on Mac OS X 10.5
User avatar
MisterBill
Posts: 19
Joined: Sat Dec 05, 2009 5:25 pm
Location: Lincoln, Nebraska, USA

Re: Assign Macro to Button without using the GUI

Post by MisterBill »

hanya wrote: There is no difference between to do it by the UI and by the code above, because the specific interface is created and assgined internally for the event.
Hi. In a related vein, what's the easiest way to duplicate in a macro the act of assigning a function to MouseButtonPressed, or one of the other events listed on a control's properties in the GUI form editor? I'd like to avoid as much complexity as possible; all I want to do is create a control (a button, in my case) and add a pointer to the routine I want to handle mouse clicks. Do I still have to create a new listener and so on if I just want to use an existing event? Guidance would be very much appreciated. :cry:
Ubuntu 8.04, OpenOffice.org 2.4.1
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Assign Macro to Button without using the GUI

Post by hanya »

The event assgined using the GUI is kept at the control model and you can copy it to another control after the CreateUnoDialog (in the Basic) call. But it does not work because the event is created during the function call.
Here is an try to do it.

Code: Select all

' this code does not work as expected
Sub dialogevent10
  dlg = CreateUnoDialog(DialogLibraries.Standard.Dialog1)
  
  btn_model = dlg.getControl("btn1").Model
  events = btn_model.getEvents()
  event_names = events.getElementNames()
  'If events.hasElements() Then ' broken
  if IsArray(event_names) Then
    event_name = event_names(0)
    
    btn2_model = dlg.getControl("btn2").Model
    btn2_events = btn2_model.getEvents()
    btn2_events.insertByName(event_name, events.getByName(event_name))
  End If
  
  ' event copied to btn2 does not work
  dlg.execute()
End Sub
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
User avatar
MisterBill
Posts: 19
Joined: Sat Dec 05, 2009 5:25 pm
Location: Lincoln, Nebraska, USA

Re: Assign Macro to Button without using the GUI

Post by MisterBill »

Thanks. Hm. That's unfortunate. So what's the next best way to do it? My problem is that my understanding of the bits between the GUI setting on the Events tab for (for example) "Mouse Button Pressed" being assigned the action "vnd.sun.star.script:Standard.Module1.DrawBedClicked?language=Basic&location=application" and my macro subroutine code "DrawBedClicked" is pretty hazy. Well, actually, completely opaque to be honest. :? Would you mind spelling out what I would need to do in a macro to set up a button (call it "Button1") so when the user clicks it, it executes the "Sub DrawBedClicked" code? I need to be able to do this dynamically as my code creates buttons on the form. I have a feeling it's gong to involve "listeners" at some point, but that's about the extent of my understanding. Any additional help you can offer would be most welcome.
Ubuntu 8.04, OpenOffice.org 2.4.1
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Assign Macro to Button without using the GUI

Post by hanya »

You are talking about event on forms but there is difference about event between on dialogs and on forms. Please see another thread that you post.
Code to assign event to button by copying from GUI example?
http://user.services.openoffice.org/en/ ... 20&t=25554
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
User avatar
MisterBill
Posts: 19
Joined: Sat Dec 05, 2009 5:25 pm
Location: Lincoln, Nebraska, USA

Re: Assign Macro to Button without using the GUI

Post by MisterBill »

Oh. I didn't realize that it was done differently for dialogs vs. forms. :oops: I've started looking at the code you posted on the other thread, and it'll take me a while to digest it, but I really appreciate your help. :-)
Ubuntu 8.04, OpenOffice.org 2.4.1
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Assign Macro to Button without using the GUI

Post by Villeroy »

Locating the button from a document's view point:
A Writer document has one DrawPage, Calc has one DrawPage per sheet, Impress and Draw consist of DrawPages.
A DrawPage is a layer where you attach shapes (rectangular boxes with various content).

The functional aspect of a button as form control:
Form controls can be seen as shapes bundled in abstract containers called "Forms".
ThisComponent.DrawPage.Forms gets the collection of forms on a Writer doc's DrawPage.
Get toolbar "Form Design" and from there you can call the "forms navigator" (button #5) which displays the full hierarchy of forms and controls on the current DrawPage.

Normally, a form is connected to a database row set, bundling form controls which are visible shapes possibly bound to one of the fields of the form's row set.

A button is a form control which is not be to a field. It supposrts several actions you may set in the button's properties.
The action labeled "Open Document or URL" can also be used to call a macro. Instead of "file:///path/doc.html" or "ftp://server.com/file.ods" you can set a macro-URL like this one for Basic code embedded in the form's document:
vnd.sun.star.script:Library.Module.Routine?language=Basic&location=document&arg1=23&arg2=foo&arg3=bar
The URL looks pretty much like the one currently displayed in your browser:
protocol:location.domain?FirstArgument=value&nextArgument=otherValue

On the macro side, the button receives the full URL which can be parsed to receive the additional arguments arg1, arg2 and arg3
http://user.services.openoffice.org/en/ ... 6847#p6847 shows an example with 2 pseudo-buttons (spreadsheet function HYPERLINK in grey shaded cells).
The same type of static URL as used in the left pseudo-button can be used statically as some push button's URL, only passing the required information about the location and macro language.
The right button is somewhat spreadsheet specific, since it reads calculated arguments from named spreadsheet cells, passing them over to the macro triggered by that hyperlink, but the idea of named arguments works with regular push-buttons and ordinary text-hyperlinks as well.
Simply put a stop mark in routine "RefreshDBQByURL" push the left pseudo-button and watch how it works in Basic's step mode.

From the passed variable sURL with content ...

Code: Select all

vnd.sun.star.script:Standard.DBQ.RefreshDBQByURL?language=Basic&location=document&dbRange=DBQ&Source=Bibliography&SQLCell=DBQsql
... helper function getArgumentFromURL filters out the dbRange name ("dbRange"), the datasource name ("Bibliography") and the name of a named cell ("DBQsql").
The working routine "refreshDbRangeBySQL" on the other module manipulates the import descriptor of the given database range to pull the required data from the given table belonging to the given data source, using the SELECT statement of the given named cell.

Anyhow, prepare a button to call a document by URL and use this URL ...

Code: Select all

vnd.sun.star.script:Standard.Module1.TestButton?language=Basic&location=document&foo=1&bar=2
In the form's document you create routine TestButton on Module1 in lib Standard and copy my "getArgumentFromURL" somewhere in the same lib.

Code: Select all

Sub TestButton(sURL$)
print sURL
sFoo = getArgumentFromURL(sURL,"foo")
sBar = getArgumentFromURL(sURL,"bar")
print sFoo, sBar
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
User avatar
MisterBill
Posts: 19
Joined: Sat Dec 05, 2009 5:25 pm
Location: Lincoln, Nebraska, USA

[Solved] Re: Assign Macro to Button without using the GUI

Post by MisterBill »

I tested using:

Code: Select all

Sub PlotClick(sURL$)

print sURL$

'What to do when the user clicks on one of the plots in the bed layout

End Sub
which I had previously added to "My Macros & Dialogs.Standard.Module1"

I had to replace "document" with "application" in the URL:

vnd.sun.star.script:Standard.Module1.PlotClick?language=Basic&location=application&plot=Plot1

based (somewhat as a random "shot in the dark") on Hanya's post:

Re: Executing a macro through OOo API

Postby hanya » Tue Jul 15, 2008 7:18 pm
Replace "user" with "application". The ScriptProvider of the Basic does not allow user and share locations.
but in any event, the whole URL shows up in sURL$, where I can parse out what I need. Thanks so much Villeroy and Hanya! :super: :D

:bravo: :bravo: :bravo: :bravo: :bravo: :bravo: :bravo: :bravo: :bravo: :bravo: :bravo: :bravo: :bravo: :bravo: :bravo: :bravo: :bravo: :bravo:
Ubuntu 8.04, OpenOffice.org 2.4.1
User avatar
MisterBill
Posts: 19
Joined: Sat Dec 05, 2009 5:25 pm
Location: Lincoln, Nebraska, USA

Re: Assign Macro to Button without using the GUI

Post by MisterBill »

There's trouble in paradise. I could have sworn this worked at least once right after I read your post, Villeroy, but now when I add parameters to the URL of the document to open, when I click the button that points to the URL, nothing happens:

Code: Select all

/home/bill/Documents/Gardening Database Project/SubPlots Form.odt?language=Basic&location=application&foo=1&bar=2
However, if I specify the URL as just the path and document name, it works:

Code: Select all

/home/bill/Documents/Gardening Database Project/SubPlots Form.odt
Any ideas? :crazy:
Ubuntu 8.04, OpenOffice.org 2.4.1
carmela
Posts: 2
Joined: Thu Jan 21, 2010 6:17 am

Re: Assign Macro to Button without using the GUI

Post by carmela »

Hi all,

Would someone be able to tell me how I can associate a button on my worksheet called Button1 with a macro I have written for when this button is clicked. I called it Button1_Click(). But unfortunately the association between the two is not automatic as I had thought it would be. The code given by Hanya in this thread is probably what I need but I am unable to modify it to fit my button as I cannot understand it. Sorry. Would someone please enlighten me.

Thank you for any help.
Carmela
OpenOffice Version 3.1.1 installed on Windows XP
FJCC
Moderator
Posts: 9281
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Assign Macro to Button without using the GUI

Post by FJCC »

If you want to assign the macro to the button manually, the process is fairly simple.
1. Use the Form Control toolbar to get the button in design mode. The icon to toggle Design mode is the one with the ruler, pencil and triangle and it is the second icon on my tool bar.
2. Left click on the button and it will be selected
3. Right click on the button, select Control and then choose the Events tab.
4. Click on the small square with dots to the right of the event Mouse Button Pressed or Mouse Button Released
5. A new dialog will appear. Choose Macro and you will see a Macro Selector dialog. Navigate to your macro, select it and choose OK until you are back at the Events tab.
6. Close that dialog and you are done.
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
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Assign Macro to Button without using the GUI

Post by Villeroy »

MisterBill wrote:There's trouble in paradise. I could have sworn this worked at least once right after I read your post, Villeroy, but now when I add parameters to the URL of the document to open, when I click the button that points to the URL, nothing happens:

Code: Select all

/home/bill/Documents/Gardening Database Project/SubPlots Form.odt?language=Basic&location=application&foo=1&bar=2
However, if I specify the URL as just the path and document name, it works:

Code: Select all

/home/bill/Documents/Gardening Database Project/SubPlots Form.odt
Any ideas? :crazy:
If you want to call a macro by URL (including your own additional parameters or not) then you should know what an URL actually is. Wikipedia helps, this is the full reference: http://tools.ietf.org/html/rfc3986

... and you have to store public code in public libraries.
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
carmela
Posts: 2
Joined: Thu Jan 21, 2010 6:17 am

Re: Assign Macro to Button without using the GUI

Post by carmela »

Hi Villeroy,

Actually I need to do it through code as the files are automatically generated. How can I write a simple snippet to associate Button1 with Button1_Click()?

Thank you for replying.
Carmela
OpenOffice Version 3.1.1 installed on Windows XP
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Assign Macro to Button without using the GUI

Post by Villeroy »

Set the button's URL property to a valid URL would be an alternative way. Just try in the general properties of the button together with action "Open Document or URL". Effectively such a button is a hyperlink.
The "official" way goes through the script provider of your scripting language, fully documented at http://api.openoffice.org/docs/common/r ... le-ix.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
Post Reply