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

[SOLVED] Can't Access Custom Toolbar with Basic

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


Joined: 17 Feb 2012
Posts: 12
Location: Canada

PostPosted: Fri Mar 30, 2012 12:29 pm    Post subject: [SOLVED] Can't Access Custom Toolbar with Basic Reply with quote

Hi,

I found the following code through a search and started to play with it. I have a custom toolbar that I've named Chess, but the code just can't seem to access it. Here is the code:

Code:
Sub TestingToolbars

Dim sToolbar$ : sToolbar = "private:resource/toolbar/custom_Chess"
Dim sDocType$ : sDocType = "com.sun.star.text.TextDocument"
Dim oSupplier
Dim oModuleCfgMgr
Dim oImageMgr
Dim oToolbarSettings
Dim nCount As Integer
Dim oToolbarButton()
Dim nToolbarButtonCount As Integer
Dim i%, j%

   oSupplier = CreateUnoService("com.sun.star.ui.ModuleUIConfigurationManagerSupplier")

   oModuleCfgMgr = oSupplier.getUIConfigurationManager(sDocType)
   oImageMgr = oModuleCfgMgr.getImageManager()
   oToolbarSettings = oModuleCfgMgr.getSettings(sToolbar, True)

End Sub


The last line always gives me an NoSuchElement Exception. Now, I've tried various names for the sToolbar including Chess, chess, custom_Chess, custom_chess, custom_chessbar etc. None of them work. I even tried to rename the toolbar 'chess' just to eliminate any case problem. No difference. On the other hand, standardbar works just fine (in place of custom_Chess)

Does anyone have any idea why this is not working for me? How do you access a custom toolbar? Could the problem be that the custom toolbar resides in the .ott file and not in Writer itself?

OK, I've edited this post to add a bit more info. I ran the following code from Andrew Pitonyak's free macro book:

Code:
Sub SeeComponentsElements()
Dim oDoc, oFrame
Dim oCfgManager
Dim x
Dim s$

oDoc = ThisComponent
oFrame = oDoc.getCurrentController().getFrame()
For Each x in oFrame.LayoutManager.getElements()
 s = s & x.ResourceURL & CHR$(10)
Next
 MsgBox s, 0, "Toolbars in Component"
End Sub


Now, the above code reports that my 'Chess' toolbar is actually called:

private:resource/toolbar/custom_toolbar_719

I can confirm this because I copied the .ott file to a temporary directory, renamed the extension to .zip, and unzipped the contents of the file. Sure enough there was a folder called 'Configurations2' that contained a folder called 'toolbar' that had an XML file called 'custom_toolbar_719'

But when I change sToolbar, in the first Sub, to "private:resource/toolbar/custom_toolbar_719" I still get the NoSuchElement Exception. Strange...


Last edited by canuck on Sun Apr 01, 2012 7:54 am; edited 1 time in total
Back to top
View user's profile Send private message
hanya
Super User
Super User


Joined: 04 May 2005
Posts: 543
Location: Japan

PostPosted: Fri Mar 30, 2012 10:07 pm    Post subject: Reply with quote

You have already the answer in the latter half of your post. Work with layout manager of the frame. Because of your toolbar is stored in the document.
css.ui.ModuleUIConfigurationManagerSupplier service manages only the ui configuration stored in the user's profile. Call getSettings method of the element to get its settings.
Back to top
View user's profile Send private message
canuck
General User
General User


Joined: 17 Feb 2012
Posts: 12
Location: Canada

PostPosted: Sun Apr 01, 2012 7:53 am    Post subject: Reply with quote

Yes, you were absolutely right, hanya, thank you for your kind assistance.

As it turns out, I didn't even need to access the custom toolbar at all. What I wanted to do was simply replace one of the Icon images on my custom toolbar each time that it was clicked, just to reflect the new 'toggled' state. I found a nice Sub in Andrew Pitonyak's free ebook collection of macros and I simply modified it very slightly to get what I wanted. (The original code was written by Carsten Driesner.)

The FlipBoard Sub below gets called whenever its button is pressed on the custom toolbar. It simply toggles the Flipped variable and then replaces the current Icon image associated with the FlipBoard button with another to reflect the new toggled state.

Code:

Sub FlipBoard

Dim sCmdID$ : sCmdID = "vnd.sun.star.script:Standard.ChessDiag1.FlipBoard?language=Basic&location=document"
Dim oImageMgr
Dim ImageURL As String

   If Flipped = 0 Then
      Flipped = 1
      oImageURL = "file:///C:/OoChess/imgs/FlipBIcon.png"
   Else
      Flipped = 0
      oImageURL = "file:///C:/OoChess/imgs/FlipWIcon.png"
   Endif
   
   oImageMgr = ThisComponent.getUIConfigurationManager().getImageManager()
   Dim oImageCmds(0)
   Dim oImages(0)
   Dim oImage
      REM Try to load the image from the file URL
      oImage = GetImageFromURL(oImageURL)
      If Not isNull(oImage) Then
         oImageCmds(0) = sCmdID
         oImages(0) = oImage
         oImageMgr.replaceImages(0, oImageCmds(), oImages())
      End If

End Sub


Function GetImageFromURL( URL as String ) as Variant

Dim oMediaProperties(0) As New com.sun.star.beans.PropertyValue
Dim sProvider$ : sProvider = "com.sun.star.graphic.GraphicProvider"
Dim oGraphicProvider

   REM Create graphic provider instance to load images from files.
   oGraphicProvider = createUnoService(sProvider)
   REM Set URL property so graphic provider is able to load the image
   oMediaProperties(0).Name = "URL"
   oMediaProperties(0).Value = URL
   REM Retrieve the com.sun.star.graphic.XGraphic instance
   GetImageFromURL = oGraphicProvider.queryGraphic(oMediaProperties())
   
End Function
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