Create fancy custom toolbars with OOo Basic

Creating Extension - Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This forum is not for asking questions about writing your own macros.

Create fancy custom toolbars with OOo Basic

Postby ms777 » Sat Apr 05, 2008 4:48 pm


this code shows two examples for custom designed toolbars. These are just very simple examples, the methods shown allow you to have complete control over your toolbar items and make them as fancy as you want. You only need to know OO Basic.

The first example generates a toolbar with a simple drop down box, very similar e.g. to the standard font selection drop down box. Whenever the user changes the selection, a listener is called (CBOX_itemStateChanged). In its current version, it only adds a line to the first sheet in the calc document. Overwrite this method to implement your own needs.

The second example uses the standard OO dialog capabilities to show a non modal dialog in the toolbar. I use it with a simple dialog created on the fly, but it should also work with dialogs defined by the OO Basic dialog editor capabilities. With this technique, you can transform all OO dialogs into non modal toolbar dialogs

In order to run thisDo not worry: In order to use the code you do not have to understand it ...
  • install the code in any module in the same document
  • install the subroutines into any other module in the same document
  • run the code
To switch between the two examples, change the sub LGCB_createItemWindow in the main code as outlined there

Have fun,


P.S. Thanks to Peter for his Toolbar routines

The main code:
Code: Select all   Expand viewCollapse view
Global logRow
Global logSheet

Sub Main

'define the strings we need
toolbarResourceUrl = "private:resource/toolbar/custom_ms777"
toolbarUIName = "Combobox via interface combine"
itemLabel = "MyLabel"
itemCommandUrl = ".uno:ms777Combobox"
itemService = "ms777.ToolbarCombobox"

'for the logging ...
logRow = 0
logSheet = ThisComponent.sheets.getByIndex(0)


'the to-be-combined interfaces XInitialization, XStatusListener, XUpdatable, XServiceInfo, XToolbarController
listXInitialization = createUnoListener("LGCB_","")
listXUpdatable = createUnoListener("LGCB_","")
listXToolbarController = createUnoListener("LGCB_","")
listXServiceInfo = createUnoListener("LGCB_","")
listXStatusListener = createUnoListener("LGCB_","")

oScript = ThisComponent.getScriptProvider("").getScript("")
oScript.invoke(Array(itemService, listXInitialization, listXUpdatable, listXToolbarController, _
   listXServiceInfo, listXStatusListener), Array(), Array())

call createToolbar(toolbarResourceUrl, toolbarUIName, itemLabel, itemCommandUrl, itemService)


End Sub

sub logMessage(sMessage as String)
logSheet.getCellByPosition(0,logRow).String = sMessage
logRow = logRow + 1
end sub

' here start the subs which are combined to form the toolbar window

sub LGCB_initialize(args() as Any)
end sub

sub LGCB_update()
end sub

function LGCB_getImplementationName() as String
LGCB_getImplementationName = "ms777.ToolbarCombobox"
end function

function LGCB_supportsService(s as String) as Boolean
LGCB_supportsService = (s = "ms777.ToolbarCombobox")
end function

function LGCB_getSupportedServiceNames() as Any
LGCB_getSupportedServiceNames = Array("ms777.ToolbarCombobox")
end function

sub LGCB_statusChanged(state as
end sub

sub LGCB_disposing(source as
end sub

sub LGCB_execute(KeyModifier as integer)
end sub

sub LGCB_click()
end sub

sub LGCB_doubleClick()
end sub

function LGCB_createPopupWindow() as
end function

function LGCB_createItemWindow(wParent as as
logMessage("createItemWindow start")
LGCB_createItemWindow = generateCBOXWindow(wParent) 'for first example
'LGCB_createItemWindow = generateDialogWindow(wParent) 'for second example
logMessage("createItemWindow end")
end function

'--------------  first example: generate a combobox in the toolbar
' see my earlier post

function generateCBOXWindow(wParent as as
'the itemlistener, which is added to the toolbar combobox
listItemListener = createUnoListener("CBOX_", "")
oWindow = makeAwtWindow(wParent, 0, 0, 230, 20, "combobox", or
oWindow.setText("Enter Command Here")
oWindow.addItems(Array( "test", "foo", "bar", "test2", "foo2", "bar2" ), 0)
generateCBOXWindow = oWindow
end function

' the listener to get the combobox events back to OO Basic

sub CBOX_itemStateChanged(event as
lSelected = event.selected
s = "selection change to item # " + lSelected
if lSelected >=0 then s = s & ": " & event.source.items(lSelected)
'xray event
end sub

'--------------  second example: hijack a normal OO Basic generated dialog

function generateDialogWindow(wParent as as
oDialog = generateRadioButtonDialog(Array("first option", "longer option text", "even much more longer option text"))
wDialog = makeAwtWindow(wParent, 0, 0, oDialog.Size.width, oDialog.Size.height, "window",
wDialog.Background = &HFF000000 'transparent

oControls = oDialog.Controls
for k=0 to UBound(oControls)
   oControls(k).createPeer(wDialog.Toolkit, wDialog)
next k

generateDialogWindow = wDialog
end function

'generateRadioButtonDialog creates a dialog on the fly ...
'it expects an array of strings showing the options the user can select
'see for a standalone version
'and some (?) explanations
function generateRadioButtonDialog(astrOptions) as Any
Dim lTab as Long

const lDeltaFromEdge = 5   'controls spacing from the window edge and between controls
const lButtonWidth  = 80
const lButtonHeight = 30
oActionListener = CreateUnoListener( "RBD_", "" )
oDialogModel = createUnoService( "" )
oDialogControl = createUnoService( "" )
oDialogControl.setModel( oDialogModel )

lTab = 0

lRadioButtonMinHeight = -1
lRadioButtonMinWidth  = -1
for k=0 to UBound(astrOptions) 'insert radio buttons and find out max height and width
  oRadioButtonModel = oDialogModel.createInstance( "" )
  setNameTabLabel(oRadioButtonModel, "RB" & k, lTab, astrOptions(k))   
  oDialogModel.insertByName( oRadioButtonModel.Name, oRadioButtonModel )
  PrefSize = oDialogControl.getControl( "RB" & k).PreferredSize
  if PrefSize.Width  > lRadioButtonMinWidth  then lRadioButtonMinWidth  =  PrefSize.Width
  if PrefSize.Height > lRadioButtonMinHeight then lRadioButtonMinHeight =  PrefSize.Height
  next k

for k=0 to UBound(astrOptions) 'set the size of the radiobutton controls, mark the first as set
  oRadioButtonControl = oDialogControl.getControl( "RB" & k)
  oRadioButtonControl.setPosSize(lDeltaFromEdge, lDeltaFromEdge*(k+1) + lRadioButtonMinHeight * k, lRadioButtonMinWidth, lRadioButtonMinHeight,
  if (k=0) then oRadioButtonControl.State = True
  next k

'insert the Accept Button   
oButtonModel = oDialogModel.createInstance( "" )
setNameTabLabel(oButtonModel, "Accept", lTab, "Accept")   
oDialogModel.insertByName( oButtonModel.Name, oButtonModel )
oButtonControl = oDialogControl.getControl( oButtonModel.Name )
oButtonControl.setPosSize(2*lDeltaFromEdge+lRadioButtonMinWidth, lDeltaFromEdge, lButtonWidth, lButtonHeight,
oButtonControl.addActionListener( oActionListener )

'now determine the size of the overall dialog   
lDialogWidth  =  3*lDeltaFromEdge + lRadioButtonMinWidth + lButtonWidth
lDialogHeight =  (UBound(astrOptions)+1)*(lRadioButtonMinHeight+lDeltaFromEdge) + lDeltaFromEdge
oDialogControl.setPosSize( 0, 0, lDialogWidth, lDialogHeight,

generateRadioButtonDialog = oDialogControl
end function

sub setNameTabLabel(o, n as String, Tab as long, Label as String)
o.Name = n
o.TabIndex = Tab
o.Label = Label
Tab = Tab+1
end sub

Sub RBD_actionPerformed( oEvent As )
oDialogControl = oEvent.Source.Context
s = "Button " & oEvent.Source.Model.Name & " pressed"
oC = oDialogControl.Controls
for k=0 to UBound(oC)
  if HasUnoInterfaces(oC(k), "") then
    s = s & ", RadBut " & k & " state: " & oC(k).Model.State
next k
end sub

The subroutines required:
Code: Select all   Expand viewCollapse view
sub createToolbar(toolbarResourceUrl as String, toolbarUIName as String, itemLabel as String, _
                  itemCommandUrl as String, itemService as String)
'I heavily borrowed from Peter when writing this

'register the itemService at the Toolbarfactory
oo = createUnoService("")
if oo.hasController( itemCommandUrl, "") then oo.deregisterController( itemCommandUrl, "")
oo.registerController( itemCommandUrl, "", itemService)

'delete toolbar if existing
oLM = ThisComponent.CurrentController.Frame.Layoutmanager
if not IsNull(oLM.getElement(toolbarResourceUrl)) then oLM.destroyElement(toolbarResourceUrl)

'create toolbar

'insert one component into toolbar and set the UIName
oEL = oLM.getElement(toolbarResourceUrl)
oToolbarSettings = oEL.getSettings(true)
Dim aItem(1) as new
aItem(0).Name = "CommandURL"
aItem(0).Value = itemCommandUrl
aItem(1).Name = "Label"
aItem(1).Value = itemLabel

oToolbarSettings.insertByIndex( 0, aItem )
oToolbarSettings.UIName = toolbarUIName

'make sure that the toolbar is docked in a separate new line in the docking area
cDockArea =
Dim aPos as New
aPos.X = 0
aPos.Y = 0

end sub

function makeAwtWindow(wParent as, x as integer, y as integer, width as integer, height as integer, wServiceName as String, wAttribute as long) as
oToolkit = createUnoService("")

' create WindowDescriptor
Dim rBounds as new
rBounds.X = x
rBounds.Y = y
rBounds.width = width
rBounds.height = height

Dim wd as new
wd.Type =
'wd.Type =
wd.Parent = wParent
wd.Bounds = rBounds
wd.ParentIndex = -1
wd.WindowAttributes = wAttribute
wd.WindowServiceName = wServiceName

makeAwtWindow = oToolkit.createWindow(wd)
end function
Last edited by ms777 on Sat Jun 20, 2015 2:09 pm, edited 1 time in total.
Posts: 141
Joined: Mon Oct 08, 2007 1:33 am

Re: Create fancy custom toolbars with OOBasic

Postby TerryE » Sun Apr 06, 2008 3:28 am

Placeholder for Change Control and to remove topic from "View unanswered posts" list.
Ubuntu 11.04-x64 + LibreOffice 3 and MS free except the boss's Notebook which runs XP + OOo 3.3.
Posts: 1402
Joined: Sat Oct 06, 2007 10:13 pm
Location: UK

Re: Create fancy custom toolbars with OOo Basic

Postby taipeihugo » Mon Jun 15, 2015 7:53 pm

when the program run to
oo = createUnoService("")
I got oo values is null,please tell me why,thanks.
OpenOffice 4.1.1 on Linux mint 17
Posts: 2
Joined: Sat May 23, 2015 4:48 am

Re: Create fancy custom toolbars with OOo Basic

Postby ms777 » Sat Jun 20, 2015 2:08 pm


8 years later ... wow !

If you change "" to "" it should work again


Posts: 141
Joined: Mon Oct 08, 2007 1:33 am

Re: Create fancy custom toolbars with OOo Basic

Postby taipeihugo » Wed Aug 12, 2015 11:23 am

thank you ms777。^^
OpenOffice 4.1.1 on Linux mint 17
Posts: 2
Joined: Sat May 23, 2015 4:48 am

Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest