Dodgy add complex control to toolbar in Basic

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: 2778
Joined: Wed Mar 09, 2016 2:40 pm

Dodgy add complex control to toolbar in Basic

Post by JeJe »

This recent submission

https://forum.openoffice.org/en/forum/v ... p?t=108410

reminded me of this old code. My understanding is OOBasic doesn't allow access to XToolbarController.

What you can do in Basic is add a normal button as a position holder and then add an awt control on top.

For the below example for OO the findbar(Find toolbar) (Edit: in Writer in the active frame) needs to be visible and docked.
It adds a combobox at the end which could be used for entering replace text.
A message box is shown if you press enter from the new combobox.


******CAUTION - MAY CAUSE CRASH. eg if you undock toolbar and click close button********


Code: Select all

Sub AddComboToFindBar()
	le = thiscomponent.currentcontroller.frame.layoutmanager
	for i = 0 to ubound(le.elements)
		
		'find the findbar
		if instr(1,le.elements(i).resourceurl,"FindBar") then

		'add a normal button as a place holder
			le.elements(i).getrealinterface.enabledocking TRUE
			aRect = CreateUnoStruct("com.sun.star.awt.Rectangle")
			sets =le.elements(i).getsettings(true)
			ee= sets.getbyindex(3)
			ub =ubound(ee)+1
			redim preserve ee(ub)

			ee(ub)=  New com.sun.star.beans.PropertyValue
			ee(ub).name= "IsEnabled"
			ee(ub).value = false
			ee(0).value = "ReplacePerhaps"
			ee(2).value = "                                  "
			sets.insertbyindex(4,ee)
			le.elements(i).setsettings (sets)
			le.elements(i).updatesettings
			bounds=  le.elements(i).GETREALINTERFACE.accessiblecontext.getaccessiblechild(4).bounds
		
		
		'create an awt combobox and position over placeholder button
			ARECT.WIDTH =bounds.width
			ARECT.HEIGHT = bounds.height
			WA = com.sun.star.awt.WindowAttribute
			WC= com.sun.star.awt.WindowClass
			aRect = CreateUnoStruct("com.sun.star.awt.Rectangle")
			WindowServiceName ="combobox"
			oParent = ThisComponent.getCurrentController().getFrame().ComponentWindow()
			oToolkit =  le.elements(i).realinterface.getToolkit()
			windowAttributes =16 or 1 or com.sun.star.awt.VclWindowPeerAttribute.DROPDOWN
			win= createWindowJe(oToolkit, le.elements(i).realinterface,WindowServiceName,bounds,windowAttributes ,windowtype)

		'add a listener to show message box when enter key pressed
			oListener = CreateUnoListener( "FBKeyListen2_", "com.sun.star.awt.XKeyListener" )
			win.addKeyListener(oListener)
			exit sub
		end if
	next
End Sub

function createWindowJe(oToolkit,parent,WindowServiceName,arect,windowAttributes ,windowtype) 'helper to create awt winod
	Dim  WA , oDesc
	WA = com.sun.star.awt.WindowAttribute
	oDesc = CreateUnoStruct("com.sun.star.awt.WindowDescriptor")
	With oDesc
		.Type = windowtype
		.WindowServiceName =WindowServiceName
		.Parent =  parent
		.ParentIndex =0 
		.Bounds = aRect
		.windowAttributes = windowAttributes
	End With
	button = oToolkit.createWindow(oDesc)
	createWindowJe = button
end function

Sub FBKeyListen2_keyPressed(ev) 'listener for combo key events
	select case ev.keycode
	case 1280
		msgbox "return pressed"
	end select
end sub
Sub FBKeyListen2_keyReleased(ev)
end sub
Sub FBKeyListen2_disposing(oEvent)
End Sub



Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2778
Joined: Wed Mar 09, 2016 2:40 pm

Re: Dodgy add complex control to toolbar in Basic

Post by JeJe »

The crashing on closing the toolbar appears to have been caused by setting the parent of the control to the component window. This can be changed by replacing in the AddComboToFindBar sub

Code: Select all

oParent = ThisComponent.getCurrentController().getFrame().ComponentWindow()
with

Code: Select all

oparent = le.elements(i).realinterface.accessiblecontext.getaccessibleparent
Edit: or, for the toolbar itself

Code: Select all

oparent = le.elements(i).realinterface
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2778
Joined: Wed Mar 09, 2016 2:40 pm

Re: Dodgy add complex control to toolbar in Basic

Post by JeJe »

No... still dodgy. Intermittent crashing when closing the toolbar - and having to remember not to do that isn't ideal.

Using a dialog with any controls you like as a toolbar by setting the panel (normal toolbar parent) as the parent and having small or empty toolbar(s) positioned so as to create the needed panel space seems to be the way to do this in Basic.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply