Xojo Conferences

Platforms to show: All Mac Windows Linux Cross-Platform

/MacCocoa/NSStatusItem/StatusItem with Image in menu
Required plugins for this example: MBS MacBase Plugin, MBS MacCocoa Plugin, MBS MacControls Plugin, MBS MacOSX Plugin, MBS Main Plugin, MBS Picture Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/NSStatusItem/StatusItem with Image in menu
This example is the version from Thu, 16th Nov 2016.
Project "StatusItem with Image in menu.rbp"
Class Window1 Inherits Window
EventHandler Sub Open() dim f as FolderItem e=new MyCocoaStatusItemMBS if not e.Available then MsgBox "Cocoa not loaded." end if Create AddMenu AddMenuEntryWithPicture End EventHandler
Protected Sub AddMenu() dim a,b,c,d as MyCocoaMenuItemMBS dim m as MyNSMenuMBS dim p as Picture m=new MyNSMenuMBS a=new MyCocoaMenuItemMBS a.CreateMenuItem "Top menu entry" a.Enabled=true a.Tag=1 items.Append a // so RB doesn't free the object. m.AddItem a b=new MyCocoaMenuItemMBS b.CreateSeparator items.Append b m.AddItem b c=new MyCocoaMenuItemMBS c.CreateMenuItem "Middle menu entry" c.Enabled=true c.Tag=2 c.state=1 items.Append c m.AddItem c d=new MyCocoaMenuItemMBS d.CreateMenuItem "Bottom menu entry" d.Enabled=false d.tag=3 items.Append d m.AddItem d const NSShiftKeyMask=131072 const NSControlKeyMask=262144 const NSAlternateKeyMask=524288 const NSCommandKeyMask=1048576 const NSFunctionKeyMask=8388608 const NSF1FunctionKey=&hF704 // From NSEvents.h d=new MyCocoaMenuItemMBS d.CreateMenuItem "Function key test","" d.Enabled=true d.KeyEquivalent=encodings.UTF16.chr(NSF1FunctionKey) d.KeyEquivalentModifierMask=NSCommandKeyMask+NSFunctionKeyMask d.tag=4 items.Append d m.AddItem d d=new MyCocoaMenuItemMBS d.CreateMenuItem "Last menu entry","" d.Enabled=true d.KeyEquivalent="A" d.KeyEquivalentModifierMask=NSShiftKeyMask+NSCommandKeyMask+NSAlternateKeyMask // command-option-shift d.tag=5 p = new picture(10,10,32) p.Graphics.ForeColor=rgb(0,128,0) p.Graphics.fillrect 0,0,10,10 p.Graphics.ForeColor=rgb(255,0,0) p.Graphics.filloval 0,0,10,10 d.Image=new NSImageMBS(p) items.Append d m.AddItem d e.Menu=m End Sub
Protected Sub AddMenuEntryWithPicture() view=new MyNSViewMBS(0,0,128+40,130) view.needsDisplay=True dim d as new MyCocoaMenuItemMBS d.CreateMenuItem "Last menu entry","" d.Enabled=true d.tag=6 d.view = view items.Append d e.Menu.insertItem d,3 End Sub
Protected Sub Create() dim p,m as Picture dim s as String dim img as NSImageMBS System.DebugLog "Create" call e.CreateMenu(-1) e.HighlightMode=true // clickable e.Title = "Hello" const NSLeftMouseDownMask = &h00002 const NSLeftMouseUpMask = &H00004 const NSLeftMouseDraggedMask = &h00040 const NSPeriodicMask = &h10000 e.SendActionOn NSLeftMouseDownMask+NSLeftMouseUpMask End Sub
Property Protected e As MyCocoaStatusItemMBS
Property i As integer
Property Protected items(0) As NSMenuitemMBS
Property Protected view As MyNSViewMBS
End Class
MenuBar Menu
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = "File"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu0 = "Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class MyCocoaMenuItemMBS Inherits NSMenuItemMBS
EventHandler Sub Action() MsgBox "You chose menu item number "+str(tag) End EventHandler
End Class
Class MyCocoaStatusItemMBS Inherits NSStatusItemMBS
EventHandler Sub Action() System.DebugLog "Statusitem.Action" run End EventHandler
EventHandler Sub DoubleAction() System.DebugLog "Statusitem.DoubleAction" MsgBox "Double Click!" End EventHandler
Sub run() End Sub
End Class
Class App Inherits Application
EventHandler Sub Open() if TargetMachO=false then MsgBox "This example needs a MachO target running on Mac OS X." quit end if End EventHandler
End Class
Class MyNSViewMBS Inherits CustomNSViewMBS
EventHandler Sub DrawRect(g as NSGraphicsMBS, left as double, top as double, width as double, height as double) System.DebugLog "Realbasic drawRect event" // draw icon g.drawAtPoint Image,20,2,0,0,128,128,g.NSCompositeSourceOver,1.0 End EventHandler
EventHandler Sub Open() image=GetSafariIcon End EventHandler
Protected Function GetSafariIcon() As nsimagembs dim file as FolderItem dim p,m as Picture file=LaunchServicesFindApplicationForInfoMBS("","com.apple.Safari","") if file<>nil then p=file.IconImageMBS(128) m=file.IconMaskMBS(128) if p<>Nil and m<>nil then Return new NSImageMBS(p,m) end if end if End Function
Property Protected clicked As boolean
Property Protected image As nsimagembs
Property menu As nsmenumbs
Property statusitem As mycocoastatusItemMBS
End Class
Class MyNSMenuMBS Inherits NSMenuMBS
End Class
End Project

See also:

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS MacCocoa Plugin.

MBS Xojo Plugins