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
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"
Property Private BackDrop As Picture
Property Private mmyImage As Picture
End Class