OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Set a document embedded graphic as pushbutton image

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
etuardu
General User
General User


Joined: 29 Apr 2008
Posts: 13

PostPosted: Wed May 21, 2008 2:36 am    Post subject: Set a document embedded graphic as pushbutton image Reply with quote

Hi, I need to set the image of a pushbutton (com.sun.star.comp.forms.OButtonControl) and have it saved (embedded) in the document.
I managed to set the button's image through the GraphicProvider service, but using an external file. Here is the code:

Code:

Sub Main
   Dim oProps(0) as new com.sun.star.beans.PropertyValue

   oButtonModel = thisComponent.Sheets(0).DrawPage.Forms(0).getByIndex(0)
   ' I'm in a Calc document, my button is the first form control in the first sheet

   cFile = "D:\Documents and Settings\username\Desktop\Image.bmp"
   ' the file I'd like to use as image for the button

   oProvider = createUnoService("com.sun.star.graphic.GraphicProvider")
   oProps(0).Name = "URL"
   oProps(0).Value = ConvertToUrl(cFile)
   oButtonModel.Graphic = oProvider.queryGraphic(oProps())

End Sub


This works, but if I delete the file Image.bmp and reload the document I have no image in the button.

Searching around I read that images can be embedded using BitmapTable and indeed I am able to embed images in the document and get their internal URL, like "vnd.sun.star.GraphicObject:10000200000000B70000008E8EB2B056".
Unfortunately, using this kind of URL in the code above seems not to work.

I see (here) that you can identify a graphic internal resouce by a URL in the form: "private:resource/projectshortname/bitmap/12345", but I have no idea about how to convert the URL BitmapTable gave me into this kind of ones.

Any help would be much appreciated!
Back to top
View user's profile Send private message
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Wed May 21, 2008 6:25 am    Post subject: Reply with quote

Hi,

I think there are several trials reported in this forum to use the private:resource url for dialogs, and all failed ...

A workaround is here: http://www.oooforum.org/forum/viewtopic.phtml?t=71492

Good luck,

ms777
Back to top
View user's profile Send private message
etuardu
General User
General User


Joined: 29 Apr 2008
Posts: 13

PostPosted: Fri May 23, 2008 3:55 am    Post subject: Reply with quote

Thank you ms777, it was a very good hint.
With your code I can set the button's image when the document gets opened.
It seemed just perfect to me, but... Calc resets the image if I change sheet and go back to the button's one.
I thought to work around this issue adding a listener on the controller's "activesheet" property in order to set the button's image every time the user switches to the sheet in which the button is.
This actually works, but not very properly: sometimes the image is not set. I tried to add a delay before the instruction which sets the image (wait 100), and this way it seems to work.

I know this is definitely not a good way to face the problem (too many nested workarounds!), but it works.

Well, it works until the controller object which is binded to the listener gets changed. For example this happens if the user asks Calc for a print preview: when back in the spreadsheet, since the object has changed, no action is performed on sheet changing because no listener is set on the new object.

And, honestly, I won't go to work around on this adding a listener on the StarDesktop object or somewhere else to catch the controller-changing event...

Anyway, thinking it might be useful for someone, I post my code to sets the button's image.
Code:

' A pushbutton named "myButton" is placed on "Sheet1"
' An image is placed on "Sheet2"
' The docWhenLoading sub is binded to the "on open" document event

Sub docWhenLoading
   Dim oDoc as Object, oListener as Object

   oDoc = ThisComponent
   oListener = createUnoListener("docActiveSheet_","com.sun.star.beans.XPropertyChangeListener")
   oDoc.currentController.addPropertyChangeListener("ActiveSheet", oListener)
   
   setButtonImage
End Sub

Sub setButtonImage

   Dim oDocSheets as Object, oSheetButton as Object, oSheetImage as Object

   oDocSheets = thisComponent.Sheets

   oSheetButton = oDocSheets.getByName("Sheet1")
   oSheetImage = oDocSheets.getByName("Sheet2")

   oButton = oSheetButton.Drawpage.Forms(0).getByName("myButton")
   oImage = oSheetImage.Drawpage.getByIndex(0)

   oButton.Graphic = oImage.Graphic

End Sub

Sub docActiveSheet_propertyChange(oEvent)

   If oEvent.Source.ActiveSheet.Name = "Sheet1" Then ' the sheet in which the button is
      wait 100 ' this delay seems needed to work properly
      setButtonImage
   End If
   
End Sub


Now I'm looking for a drastically different solution, e.g. using images instead of pushbuttons or something. However, I hope one day someone will find a clean way to put images in buttons.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group