This code to insertDrawingByIndex() in Gallery theme doesn't work

Found some info around Gallery themes here (& elsewhere):

https://opengrok.libreoffice.org/xref/core/offapi/com/sun/star/gallery/XGalleryTheme.idl?r=5687eba4#98

The following doesn’t add an entry to the gallery theme.
Any idea what I miss?
(There might be an alternative, exporting all shapes by code and use insertURLByIndex - didn’t test that)

Sub Tst_InsertGrapicObject
	Dim oGalleryProvider, oTheme, oShape, oGraph
	Dim oSelection
	Dim nRslt&

	oGalleryProvider  = createUnoService("com.sun.star.gallery.GalleryThemeProvider")
	oTheme = oGalleryProvider.getByName("_TestTheme") 
		' "_TestTheme" does exist and has some elements added via the UI
	oGraph = ThisComponent.createInstance("com.sun.star.drawing.GraphicObjectShape")

 	oSelection = ThisComponent.currentController.Selection
		' selected is a simple group shape, line + circle
	If IsEmpty(oSelection) then
		msgBox "No selection"
	Else
		oGraph = ThisComponent.currentController.Selection.getByIndex(0)
		nRslt = oTheme.insertDrawingByIndex (oGraph, 2)
		oTheme.update() ' makes no difference
		msgbox nRslt   '  returns -1 

		' NB
		' oTheme.removeByIndex(1)
		' does do the expected..
	End If

End Sub

Looking at the C++ didn’t make me wiser.
https://opengrok.libreoffice.org/xref/core/svx/source/unogallery/unogaltheme.cxx?r=bc91cc47#198

probably a chance to ping more devs on IRC or mailing list :
Development - The Document Foundation Wiki
image

insertDrawingByIndex expects XComponent as its argument, and actually expects it to provide XDrawPagesSupplier interface. I suppose that this code needs to create a new drawing from the objects, and then provide that drawing as the argument. Or maybe a transferable.

But:

  1. There is no way to create a transferable in Draw other than copy to system clipboard (not nice, and prone to race conditions, because system clipboard is shared resource);
  2. There is a bug, that crashes Draw when you simply do
    nRslt = oTheme.insertDrawingByIndex (ThisComponent, 2)

tdf#162555

After the bug above is fixed, the code using ThisComponent will work (well, the gallery doesn’t get updated until I close and reopen Draw, but still).

Ah, so that is what I missed.
I tried before oTheme.insertGraphicByIndex that indeed in the source has some ‘conversion trick’
https://opengrok.libreoffice.org/xref/core/svx/source/unogallery/unogaltheme.cxx?r=bc91cc47#244

but that gave this error:
'cannot coerce argument type during coreflection call: arg no.: 0 expected: "com.sun.start.graphic.XGraphic" actual: "com.sun.start.graphic.XDrawing" at ../libo-core/stoc/source/coreflection/criface.cxx:570.'

leading me to try this:

	oGraph = ThisComponent.currentController.Selection.getByIndex(0).Bitmap
	nRslt = oTheme.insertGraphicByIndex (oGraph, 2)

which (seems to) work(s).

Thanks Mike :slight_smile:

But is a bitmap image, not a nicely scaleable svg…

Could be ThisComponent.drawPages, but neither works, or crashes :wink: