Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS Util Plugin, MBS MacCocoa Plugin, MBS MacCG Plugin, MBS MacBase Plugin, MBS Picture Plugin, MBS MacCF Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/EasyStatusItem
This example is the version from Sun, 17th Mar 2012.
Project "EasyStatusItem.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action()
dim p As Picture=new Picture(MyStatusItem.Width,MenuBarHeightMBS,32)
//Update to a black line
p.mask.Graphics.PenWidth=3
p.mask.Graphics.PenHeight=3
p.Mask.Graphics.ClearRect(0,0,p.Width,p.Height)
p.mask.Graphics.DrawLine(5,5,p.Width-10,p.Height-10)
p.Graphics.ForeColor=&c000000
p.Graphics.FillRect(0,0,p.Width,p.Height)
//we can call it with any image with a mask
MyStatusItem.SetImage p
End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
EventHandler Sub Open()
dim p As picture=new picture(220,100,32)///we'll make a masked image of the wrong size
p.Mask.Graphics.ClearRect(0,0,p.Width,p.Height)
p.mask.Graphics.TextSize=60
p.mask.Graphics.TextFont="System"
p.mask.Graphics.Bold=True
p.mask.Graphics.DrawString("Status!",5,80)
p.Graphics.FillRect(0,0,p.Width,p.Height)
//we can call it with any black and white image with a mask
MyStatusItem=new EasyStatusItem(p)//or pass false in hasMask to use white=transparent style, although a masked image looks way better
//pass an NSMenuMBS if you like
//call setimage(image) to update the icon!
//be sure to store a reference somewhere.
End EventHandler
Property MyStatusItem As EasyStatusItem
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class EasyStatusItem Inherits NSStatusItemMBS
Sub Constructor(image As Picture,menu As NSMenuMBS=nil,hasMask as boolean=true)
dim p as picture=new picture(image.Width,MenuBarHeightMBS,32)
dim cgd As CGDisplayMBS=CGDisplayMBS.MainDisplay
dim scale As Double=1
if Image.Height>MenuBarHeightMBS then scale=MenuBarHeightMBS/image.Height
call me.CreateMenu(image.Width*scale)
backdrop=new picture(image.Width*scale,MenuBarHeightMBS,32)
p.Mask.Graphics.ClearRect(0,0,p.Width,p.Height)
me.Image=new NSImageMBS(p,p.mask) ///give the menu a blank image so we can capture a shot of the menubar for a backdrop
backdrop=cgd.CreateImageForRect(new CGRectMBS(me.left,me.Top,me.Width,me.Height-1)).Picture(CGColorSpaceMBS.CreateWithName(CGColorSpaceMBS.kCGColorSpaceGenericRGB))
me.HighlightMode=true
if not hasMask then
Image.Mask=image
image.Graphics.FillRect(0,0,Image.Width,Image.Height)
end if
p=image.ScaleMBS(image.Width*scale,Image.Height*scale,true)
p.mask=image.mask.ScaleMBS(image.Width*scale,Image.Height*scale,true)
SetImage p
if menu<>nil then me.Menu=menu
End Sub
Sub SetImage(value As picture)
dim p As Picture=new picture(value.Width,value.Height,32)
dim p2 As Picture=new picture(value.Width,value.Height,32)
dim nsi As NSImageMBS=new NSImageMBS
dim ok As Boolean
mmyImage = value
//make an inverted alternateImage with original mask
p=value.InvertMBS
p.mask=value.mask
ok=nsi.initWithPicture(p,p.mask)
me.alternateImage=nsi
//make the new image with menubar background
p2=BackDrop.CopyPictureWithoutMaskMBS
p2.mask=value.Mask.InvertMBS
p=value.CopyPictureWithMaskMBS
p.graphics.drawpicture(p2,0,0)
p.mask=value.Mask.ChangeBrightnessAbsoluteMBS(-20) // so it's all clickable
ok=nsi.initWithPicture(p,p.mask)
me.Image=nsi
End Sub
Note "Information"
EasyStatusMenu is a subclass to quickly create a statusmenu.
It creates a capture of the menubar where the menu will reside
and uses that to draw an almost transaperent but clickable background.
Use the normal method for updating the NSMenuMBS property or just pass a reference to the construcor.
Pass a black and white image with a mask for best results, you can also pass false in the constructor
to use the black areas of the image as a mask.
An alternateImage(clicked state) will be created for you, but you can also set your own after the constructor.
If the image is taller than MenuBarHeightMBS it will be scaled down to fit.
call it like this...
myStatusItem=new EasyStatusItem(image,NSMenuMBS,hasMask)
...and store the result in a persistant variable.
This example was created by Jim McKay of piDog Software and is free to use and modify.
http://www.pidog.com
Property Private BackDrop As Picture
Property Private mmyImage As Picture
End Class
End Project
The items on this page are in the following plugins: MBS MacCocoa Plugin.