Colored square icon for toolbar or menu item

Creating Extension - 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 forum is not for asking questions about writing your own macros.

Colored square icon for toolbar or menu item

Postby JeJe » Wed Jan 15, 2020 12:40 pm

Creates a color icon for a toolbar or menubar item in the color of your choice - like one of the squares in a color picker grid - and assigns to item.

Code: Select all   Expand viewCollapse view


'COLOR ICON MOD

sub testColoredIcon
   'use a macro or command assigned to a toolbar or menu item. This will add a colored square icon like one of the squares in a colorpicker grid.

   'choose color
   col = rgb(200,170,100)
   
   'choose macro and pass along with color to function
  'form of macrourl = "vnd.sun.star.script:YourLibrary.YourModule.YourSubOrFunction?language=Basic&location=document" 'or = application
   macrourl = "vnd.sun.star.script:Standard.AATest.ToolbarPopUPTest?language=Basic&location=document" 'my test macro
   ColoredIconForMacro  col, macrourl
end sub


sub ColoredIconForMacro(col,Macrourl)

   'createImage using a Writer document shape and save image to a file
   'note I add a 1 pixel border round color
   oPathSettings = CreateUnoService( "com.sun.star.util.PathSettings" )
   URL =oPathSettings.UserConfig & "/" & "TMPCol.png"

   createColored_16x16_PNG(col,URL)

   'load the image file and assign to the wanted macro
   img = GetImageFromURL(URL)
   oManager = thiscomponent.getUIConfigurationManager()
   omanager.imagemanager.insertimages(0,array(macrourl),array(img))
   'omanager.imagemanager.removeimages(0,array(url))
   omanager.imagemanager.store 'make change persistant

   kill convertfromurl(url) 'remove the file
end sub



Sub createColored_16x16_PNG(col,URL)
   'adapted from
   'http://www.ooowiki.de/DiagrammExport.html
   'https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=82965

   Dim oDoc, oVal(0) As New com.sun.star.beans.PropertyValue
   Dim exportFilter, aFilterData(5) As New com.sun.star.beans.PropertyValue,aProps(2) As New com.sun.star.beans.PropertyValue

   oVal(0).Name = "Hidden": oVal(0).Value = true
   oDoc = stardesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, oval)

   dz = createunostruct("com.sun.star.awt.Size")
   dp = createunostruct("com.sun.star.awt.Point")

   with odoc.currentcontroller.frame.componentwindow.info
      pixx =.PixelPerMeterX
      piyy =.PixelPerMetery
   end with

   dz.width = ((16 * 100000) / pixx)
   dz.height = ((16 * 100000) / piyy)
   dp.x= 0
   dp.y = 0

   oshape=odoc.createinstance("com.sun.star.drawing.RectangleShape") '"com.sun.star.drawing.ControlShape")
   with oshape
      '.shapetype = "com.sun.star.drawing.RectangleShape"
      .fillcolor = col
      .linecolor = col

      .setsize(dz)
      .setposition(dp)
      .visible = true

   end with

'   odoc.text.string = "  " & chr(10) & "  "
   odoc.drawpage.add(oshape)

   exportFilter = createUnoService( "com.sun.star.drawing.GraphicExportFilter" )
   exportFilter.setSourceDocument(oshape)

   ' Set the filter data
   aFilterData(0).Name = "Level" '1=PS level 1, 2=PS level 2
   aFilterData(0).Value = 2
   aFilterData(1).Name = "ColorFormat" '1=color, 2=grayscale
   aFilterData(1).Value = 1
   aFilterData(2).Name = "TextMode" '0=glyph outlines, 1=no glyph outlines, see ooo bug 7918
   aFilterData(2).Value = 1
   aFilterData(3).Name = "Preview" '0=none, 1=TIFF, 2=EPSI, 3=TIFF+EPSI
   aFilterData(3).Value = 0
   aFilterData(4).Name = "CompressionMode" '1=LZW, 2=none
   aFilterData(4).Value = 2

   aProps(0).Name = "MediaType"
   aProps(0).Value = mediaType
   aProps(1).Name = "URL"
   aProps(1).Value = url
   aProps(2).Name = "FilterData"
   aProps(2).Value = aFilterData()

   exportFilter.filter( aProps() )

   oDoc.close(true)
End Sub

   'GetImageFromURL function from "Useful Macro Information For OpenOffice" By Andrew Pitonyak
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





EDIT: NOTE COMMENT ABOUT LARGE ICONS BELOW
Last edited by JeJe on Sun Jan 19, 2020 3:21 pm, edited 1 time in total.
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1408
Joined: Wed Mar 09, 2016 2:40 pm

Re: Colored square icon for toolbar or menu item

Postby JeJe » Fri Jan 17, 2020 1:16 pm

Simplified - using shape bitmap without saving to file:


Code: Select all   Expand viewCollapse view
'COLOR ICON MOD
sub testColoredIcon
   'use a macro assigned to a toolbar or menu item. This will add an colored square icon such as you will see in a colorpicker grid.

   'choose color
   col = rgb(200,0,60)
   
   'choose macro and pass along with color to function
    'form of macrourl = "vnd.sun.star.script:YourLibrary.YourModule.YourSubOrFunction?language=Basic&location=document" 'or = application
   macrourl = "vnd.sun.star.script:Standard.AATest.ToolbarPopUPTest?language=Basic&location=document" 'my test macro
   ColoredIconForMacro  col, macrourl
end sub


sub ColoredIconForMacro(col,Macrourl)

   'createImage using a Writer document shape and save image to a file
   Dim oDoc, oVal(0) As New com.sun.star.beans.PropertyValue

   oVal(0).Name = "Hidden": oVal(0).Value = true
   oDoc = stardesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, oval)

   dz = createunostruct("com.sun.star.awt.Size")
   dp = createunostruct("com.sun.star.awt.Point")

   with odoc.currentcontroller.frame.componentwindow.info
      pixx =.PixelPerMeterX
      piyy =.PixelPerMetery
   end with

   dz.width = ((16 * 100000) / pixx):dz.height = ((16 * 100000) / piyy)
   dp.x= 0:   dp.y = 0
   oshape=odoc.createinstance("com.sun.star.drawing.RectangleShape")
   with oshape
      .fillcolor = col
      .linecolor = col
      .setsize(dz)
      .setposition(dp)
      .visible = true
   end with

   odoc.drawpage.add(oshape)

   oManager = thiscomponent.getUIConfigurationManager()
   omanager.imagemanager.insertimages(0,array(macrourl),array(oshape.bitmap))
   omanager.imagemanager.store 'make change persistant

   oDoc.close(true)
end sub
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1408
Joined: Wed Mar 09, 2016 2:40 pm

Re: Colored square icon for toolbar or menu item

Postby Evgeniy » Sun Jan 19, 2020 1:28 pm

JeJe If it not difficult, add result screenshots of work your code. It is interesting. Thanks.
OpenOffice 4.1.7 OS: Win10 x32 + Win10 x64
User avatar
Evgeniy
 
Posts: 43
Joined: Thu Jan 09, 2020 9:31 pm
Location: Russia

Re: Colored square icon for toolbar or menu item

Postby JeJe » Sun Jan 19, 2020 1:39 pm

I was using it to create the toolbar in the picture... the point is I can change the colors, not be stuck with fixed colors if the icons had to be pre-prepared.

(For what I'm doing I click on those buttons and it sets the section background color to color code parts of the document.)
Attachments
colcode.JPG
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1408
Joined: Wed Mar 09, 2016 2:40 pm

Re: Colored square icon for toolbar or menu item

Postby Evgeniy » Sun Jan 19, 2020 2:01 pm

Looks cool!
OpenOffice 4.1.7 OS: Win10 x32 + Win10 x64
User avatar
Evgeniy
 
Posts: 43
Joined: Thu Jan 09, 2020 9:31 pm
Location: Russia

Re: Colored square icon for toolbar or menu item

Postby JeJe » Sun Jan 19, 2020 3:09 pm

NOTE: the above code works for default (small) images. To save an icon if large images are displayed the line

Code: Select all   Expand viewCollapse view
   omanager.imagemanager.insertimages(0,array(macrourl),array(oshape.bitmap))


becomes

Code: Select all   Expand viewCollapse view
   omanager.imagemanager.insertimages(1,array(macrourl),array(oshape.bitmap))


https://www.openoffice.org/api/docs/com ... eType.html

Edit: the 0 become 1

EDIT2: but unfortunately when reloaded the icon doesn't display properly... just a colored square in the top left corner in both oo and lo...

Edit3: The first post version DOES work with large icons if the width and height are changed to 26 x 26 pixels. The second simpler version (which doesn't save to a file) fails.
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1408
Joined: Wed Mar 09, 2016 2:40 pm

Re: Colored square icon for toolbar or menu item

Postby JeJe » Sun Jan 19, 2020 9:02 pm

By setting the string for the shape after its been inserted into the document we can also create on-the-fly icons with text... better with large icons... the last icon in the picture is a unicode clock character.
Attachments
unicode clock.JPG
unicode clock.JPG (4.46 KiB) Viewed 2644 times
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1408
Joined: Wed Mar 09, 2016 2:40 pm


Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest