Creating a simple extension

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Creating a simple extension

Post by JeJe »

One of the disappointing things about OO/LO is the lack of extensions - when there is enormous scope for them. One of the great things about the office suite is the nuts and bolts underneath are exposed so you can write extensions in OOBasic that can implement new features without having to wait for a developer to update the software.

I'm showing how easy this is here - to encourage people to take this up - using Writer's go to page dialog. LO has one of these but OO doesn't. This feature is a tiny amount of code, less than an hour's work in Basic to create from scratch. With the code written for you, by following the steps below its a few minutes.

Create a simple extension

Goto Tools Menu/macros/Organise Dialogs/Libraries Tab
Click on new to create a library called GoToPage and click edit to edit it.

Create a module called GoToPageM and add the code below

Code: Select all

	REM  *****  BASIC  *****

	dim pgdlg 'the dialog

sub ShowGoToPage 'shows the dialog to go to a page
	DialogLibraries.LoadLibrary("GoToPage") 'load the dialog library
	pgdlg=  CreateUnoDialog(DialogLibraries.GetByName("GoToPage").GetByName("GoToPageD")) 'load the dialog
	pgdlg.getcontrol("Label2").model.label ="of " & thiscomponent.currentcontroller.pagecount 'set a label on the dialog to show the pagecount
	pgdlg.execute 'execute the dialog
end sub

sub gotopageKeyPress(ev) 'event fired by the Textbox keyPresses.
	if ev.keycode = 1280 then gotopageBtnExecute 'if return then Calls the gotopageBtnExecute sub
end sub

sub gotopageBtnExecute() ' fired by the okay button. Goes to the page
	newpgno = val(pgdlg.getcontrol("TextField1").model.text) 'get the page number from the textbox
	if newpgno <= thiscomponent.currentcontroller.pagecount and newpgno>=0 then 'make sure its in range
		vc = thiscomponent.currentcontroller.viewcursor 'get the viewcursor
		vc.jumpToPage(newpgno,false) 'this line does the work
	end if
 pgdlg.endexecute 'close the dialog
'	if pgdlg.getcontrol("StickyButton").model.state = 0 then pgdlg.endexecute 'version with "StickyButton" to not close if pressed down
end sub

Create a dialog called GoToPageD and add the textField first, a label with "Page" before it and one with "of" after. Add two command buttons for Okay and Cancel.

Set the Cancel button to be a cancel button, the TextField's keypress event to call gotopageKeyPress, the Okay button's execute event to call gotopageBtnExecute.

goback to Tools Menu/macros/Organise Dialogs/Libraries Tab
Click on your GoToPage library / Click export and export as an extension.

That's it!

Same as LibreOffice's goto page dialog in Basic! You just need to add a menu item in the edit menu which calls the ShowGoToPage sub to run it.

Note the brevity of the code - less than 20 lines. You can follow it by looking at the comments.

If you look at the supplied extension I made one tiny improvement - I added a button with a pin label and the toggle set to yes. I made it so the dialog won't automatically close if that button is pressed down by adding half a line of code. This extra feature is two minutes work tops. You can now jump about through the pages without having to reopen the dialog.

Edit: note the dialog won't work if loaded from the IDE.. the ShowGoToPage has to be called from Writer by adding that menu item, or using run...
Edit2: amended code to comment out and replace "StickyButton" line with original to just unload the dialog.
Attachments
GoToPage.oxt
(2.44 KiB) Downloaded 304 times
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply