2021-04-18

58: Register Menu Items/Tool Buttons for LibreOffice/OpenOffice

<The previous article in this series | The table of contents of this series | The next article in this series>

For invoking macros (in Python, Java, JavaScript, BeanShell, or Basic). Via extensions. Per document type.

Topics


About: LibreOffice
About: Apache OpenOffice
About: The Java programming language
About: The Python programming language
About: JavaScript
About: BeanShell
About: LibreOffice Basic
About: Apache OpenOffice Basic

The table of contents of this article


Starting Context



Target Context


  • The reader will know how to register menu items and tool buttons for LibreOffice or Apache OpenOffice via extensions.

Orientation


There is an article on creating any LibreOffice or Apache OpenOffice extension that contains any Python macros.

There is an article on invoking any LibreOffice or Apache OpenOffice macro (including ones that are contained in extensions) from your program.

There is an article on registering key bindings for LibreOffice or Apache OpenOffice via extensions.


Main Body

Stage Direction
Here are Hypothesizer 7, Objector 58A, and Objector 58B in front of a computer.


1: The Motivation for Registering a Menu Item and a Tool Button


Objector 58A
They are a bunch of fools!

Hypothesizer 7
Excuse me, sir?

Objector 58A
I said that they were fools!.

Hypothesizer 7
Who are, if I may ask?

Objector 58A
The users, of course!

Hypothesizer 7
. . . I am sorry, I do not understand.

Objector 58A
They want a menu item while I have already provided a key binding.

Hypothesizer 7
For LibreOffice or Apache OpenOffice?

Objector 58A
I have provided the key binding for LibreOffice, according to your previous article.

Hypothesizer 7
That is good.

Objector 58A
But they require a menu item.

Hypothesizer 7
I do not think that that makes them "fools".

Objector 58A
That does! Who wants to do a roundabout thing like grabbing the mouse, move the cursor way up to the top, and click several times, while one can just press 'Alt' + 'R' instead?

Hypothesizer 7
They do not remember the key sequence, I guess.

Objector 58A
That makes them fools! They can't remember a single key sequence!

Hypothesizer 7
They can (probably) remember if that is a very important thing, but the key sequence is not their central concern in their lives, so, it could be dropped out of their memories, if it is not used frequently.

Objector 58A
The key sequence is unforgettable: 'R' for 'Relax'!

Hypothesizer 7
What kind of function is that? I would like to try it. . . . Anyway, if they want the menu item, why not?

Objector 58B
I would like a tool button too.

Objector 58A
Here is a fool too.

Objector 58B
Excuse me?!

Objector 58A
Remember 'Alt' + 'R', lady!

Objector 58B
I'm not talking about your stupid function! I don't need to "Relax"!

Objector 58A
You sound to need to.

Hypothesizer 7
All right people. We will register also a tool button, madam.


2: Any Menu Item or Tool Button Can Be Registered Easily with an Extension


Hypothesizer 7
Any menu item or tool button can be registered easily with an extension.

Objector 58B
What do you mean by "with an extensions"?

Hypothesizer 7
You will create some XML files and archive them in a ZIP file, which is an extension file, madam.

Objector 58B
Will I create "some XML files"? Do you call it "easily"?

Hypothesizer 7
Yes, it is easy; please read a previous article.

And the remaining issue here is just the contents of the GUI items configuration file.

A good thing about extension is that you can package some mutually-related files; so, putting also the macros invoked from the GUI items into the package will be a natural course. That way, all of the related files will be able to be registered and unregistered as a whole.

Objector 58B
But I have to know how to put macros into the extension.

Hypothesizer 7
A previous article has already dealt with how to put any Python macro into the extension.

Objector 58B
"Python"? What do you mean "Python"?

Hypothesizer 7
Python is a programming language, in which you can write macros.

Objector 58B
Huh? Macros are written in Basic, right?

Hypothesizer 7
Not right, madam: macros can be written in Python.

Objector 58B
Anyway, how can I put Basic macros into the extension?

Hypothesizer 7
I will explain that in a future article.


3: What to Write in the Configuration File


Hypothesizer 7
Anyway, the contents of the configuration file is like this.

GuiItems.xml

@XML Source Code
<?xml version='1.0' encoding='UTF-8'?>
<oor:component-data xmlns:oor="http://openoffice.org/2001/registry" xmlns:xs="http://www.w3.org/2001/XMLSchema" oor:name="Addons" oor:package="org.openoffice.Office">
	<node oor:name="AddonUI">
		<node oor:name="OfficeMenuBar">
			<node oor:name="theBiasPlanet.testUnoExtension:menu01" oor:op="replace">
				<prop oor:name="Title" oor:type="xs:string">
					<value/>
					<value xml:lang="en">Test</value>
				</prop>
				<prop oor:name="Context" oor:type="xs:string">
					<value>com.sun.star.text.TextDocument</value>
				</prop>
				<node oor:name="Submenu">
					<node oor:name="subMenu01" oor:op="replace">
						<prop oor:name="URL" oor:type="xs:string">
							<value xml:lang="en-US">vnd.sun.star.script:theBiasPlanet.test.unoExtension.oxt/Scripts/python/theBiasPlanet/testUnoExtension/macros/Test1.py$test1?language=Python&amp;location=user:uno_packages</value>
						</prop>
						<prop oor:name="Target" oor:type="xs:string">
							<value>_self</value>
						</prop>
						<prop oor:name="Title" oor:type="xs:string">
							<value/>
							<value xml:lang="en">Test</value>
						</prop>
					</node>
				</node>
			</node>
		</node>
		<node oor:name="OfficeToolBar">
			<node oor:name="theBiasPlanet.testUnoExtension:toolBar01" oor:op="replace">
				<node oor:name="button01" oor:op="replace">
					<prop oor:name="URL">
						<value xml:lang="en-US">vnd.sun.star.script:theBiasPlanet.test.unoExtension.oxt/Scripts/python/theBiasPlanet/testUnoExtension/macros/Test1.py$test1WithAnArgument?language=Python&amp;location=user:uno_packages</value>
					</prop>
					<prop oor:name="Title">
						<value xml:lang="en-US">Test</value>
					</prop>
					<prop oor:name="Target" oor:type="xs:string">
						<value>_self</value>
					</prop>
					<prop oor:name="Context" oor:type="xs:string">
						<value>com.sun.star.text.TextDocument</value>
					</prop>
					<prop oor:name="ImageIdentifier" oor:type="xs:string">
						<value>theBiasPlanet.testUnoExtension:image01</value>
					</prop>
				</node>
			</node>
		</node>
		<node oor:name="Images">
			<node oor:name="theBiasPlanet.testUnoExtension:image01" oor:op="replace">
				<prop oor:name="URL" oor:type="xs:string">
					<value>theBiasPlanet.testUnoExtension:image01</value>
				</prop>
				<node oor:name="UserDefinedImages">
					<prop oor:name="ImageSmallURL" oor:type="xs:string">
						<value>%origin%/images/ExclamationMark.png</value>
					</prop>
					<prop oor:name="ImageBigURL" oor:type="xs:string">
						<value>%origin%/images/ExclamationMark.png</value>
					</prop>
					<prop oor:name="ImageSmallHCURL" oor:type="xs:string">
						<value>%origin%/images/ExclamationMark.png</value>
					</prop>
					<prop oor:name="ImageBigHCURL" oor:type="xs:string">
						<value>%origin%/images/ExclamationMark.png</value>
					</prop>
				</node>
			</node>
		</node>
	</node>
</oor:component-data>

Objector 58A
. . . Hmm, those long things are macro specifications, right?

Hypothesizer 7
Yes, user-owned in-extension Python macro specifications. The expression for any macro of each type has been explained in a previous article.

A point to note is that any tool button invokes the macro with an argument, so, the macro has to be one that takes the argument.

Objector 58A
What argument?

Hypothesizer 7
As far as I know, an integer, '0', seems to be passed in.

Objector 58A
Huh? In what way, is that helpful?

Hypothesizer 7
In no way, I suppose. But the macro has to have the argument, anyway.

Objector 58A
. . .

Hypothesizer 7
Each of those "Context" nodes specifies the document type for which the GUI item appears.

You can specify 'com.sun.star.text.TextDocument' for Writer, 'com.sun.star.sheet.SpreadsheetDocument' for Calc, 'com.sun.star.presentation.PresentationDocument' for Impress, and 'com.sun.star.drawing.DrawingDocument' for Draw.

Objector 58B
That image specification is an icon, right?

Hypothesizer 7
Yes. If you do not want any icon on the tool button, the "ImageIdentifier" node and the "Images" node can be omitted.

Objector 58B
I certainly want.

Hypothesizer 7
If you want to use a preregistered image, you can use a URL like '.uno:DataSort' in the "ImageIdentifier" node.

Objector 58B
Huh? Where did the URL come from?

Hypothesizer 7
In the LibreOffice or Apache OpenOffice GUI menu, you can click "Tools" -> "Customize...", click the "Toolbars" tab, select an item in "Assigned Commands", click "Modify" -> "Change Icon...", and hover the mouse cursor over an icon, then the tool tip will show the URL.


References


<The previous article in this series | The table of contents of this series | The next article in this series>